You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2019/01/08 07:25:56 UTC

[isis] 06/10: ISIS-2080: adds LayoutFacet with Method and Fallback implementations

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 115c700d8a2ef463339233546178f6184e488cfe
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Jan 7 09:56:25 2019 +0000

    ISIS-2080: adds LayoutFacet with Method and Fallback implementations
---
 .../facets/object/layout/LayoutFacet.java          | 37 ++++++++++
 .../facets/object/layout/LayoutFacetAbstract.java  | 41 +++++++++++
 .../facets/object/layout/LayoutFacetFactory.java   | 57 +++++++++++++++
 .../facets/object/layout/LayoutFacetFallback.java  | 43 +++++++++++
 .../facets/object/layout/LayoutFacetMethod.java    | 52 ++++++++++++++
 .../dflt/ProgrammingModelFacetsJava5.java          |  2 +
 .../ident/layout/LayoutFacetFactoryTest.java       | 71 ++++++++++++++++++
 .../object/ident/layout/LayoutFacetMethodTest.java | 84 ++++++++++++++++++++++
 8 files changed, 387 insertions(+)

diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacet.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacet.java
new file mode 100644
index 0000000..8a072de
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacet.java
@@ -0,0 +1,37 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.layout;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.facetapi.Facet;
+
+/*
+ * Layout for an object.
+ * 
+ * <p>
+ * In the standard Apache Isis Programming Model, typically corresponds to a method named <tt>layout</tt>.
+ * 
+ * @see TitleFacet
+ * @see IconFacet
+ */
+public interface LayoutFacet extends Facet {
+
+    public String layout(final ObjectAdapter object);
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetAbstract.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetAbstract.java
new file mode 100644
index 0000000..12e0bee
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetAbstract.java
@@ -0,0 +1,41 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.layout;
+
+import org.apache.isis.core.metamodel.facetapi.Facet;
+import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+
+public abstract class LayoutFacetAbstract extends FacetAbstract implements LayoutFacet {
+
+    public static Class<? extends Facet> type() {
+        return LayoutFacet.class;
+    }
+
+    protected LayoutFacetAbstract(final FacetHolder holder) {
+        this(holder, Derivation.NOT_DERIVED);
+    }
+    protected LayoutFacetAbstract(
+            final FacetHolder holder,
+            final Derivation derivation) {
+        super(type(), holder, derivation);
+    }
+
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFactory.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFactory.java
new file mode 100644
index 0000000..61b549b
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFactory.java
@@ -0,0 +1,57 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.layout;
+
+import java.lang.reflect.Method;
+
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facetapi.FacetUtil;
+import org.apache.isis.core.metamodel.facetapi.FeatureType;
+import org.apache.isis.core.metamodel.facets.MethodFinderUtils;
+import org.apache.isis.core.metamodel.facets.MethodPrefixBasedFacetFactoryAbstract;
+import org.apache.isis.core.metamodel.methodutils.MethodScope;
+
+public class LayoutFacetFactory extends MethodPrefixBasedFacetFactoryAbstract {
+
+    private static final String ICON_NAME_PREFIX = "iconName";
+
+    private static final String[] PREFIXES = { ICON_NAME_PREFIX, };
+
+    public LayoutFacetFactory() {
+        super(FeatureType.OBJECTS_ONLY, OrphanValidation.VALIDATE, PREFIXES);
+    }
+
+    @Override
+    public void process(final ProcessClassContext processClassContext) {
+        final Class<?> cls = processClassContext.getCls();
+        final FacetHolder facetHolder = processClassContext.getFacetHolder();
+
+        final Method method = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, ICON_NAME_PREFIX, String.class, NO_PARAMETERS_TYPES);
+
+        final LayoutFacet facet;
+        if (method == null) {
+            facet = new LayoutFacetFallback(facetHolder);
+        } else {
+            processClassContext.removeMethod(method);
+            facet = new LayoutFacetMethod(method, facetHolder);
+        }
+        FacetUtil.addFacet(facet);
+    }
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFallback.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFallback.java
new file mode 100644
index 0000000..4f47c37
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetFallback.java
@@ -0,0 +1,43 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.layout;
+
+import java.util.Map;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+
+public class LayoutFacetFallback extends LayoutFacetAbstract {
+
+    public LayoutFacetFallback(final FacetHolder holder) {
+        super(holder, Derivation.DERIVED);
+    }
+
+    @Override
+    public String layout(final ObjectAdapter owningAdapter) {
+        return null;
+    }
+
+    @Override
+    public void appendAttributesTo(final Map<String, Object> attributeMap) {
+        super.appendAttributesTo(attributeMap);
+    }
+
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetMethod.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetMethod.java
new file mode 100644
index 0000000..27f72c3
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/object/layout/LayoutFacetMethod.java
@@ -0,0 +1,52 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.layout;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacetAbstract;
+
+public class LayoutFacetMethod extends LayoutFacetAbstract {
+
+    private final Method method;
+
+    public LayoutFacetMethod(final Method method, final FacetHolder holder) {
+        super(holder);
+        this.method = method;
+    }
+
+    @Override
+    public String layout(final ObjectAdapter owningAdapter) {
+        try {
+            return (String) ObjectAdapter.InvokeUtils.invoke(method, owningAdapter);
+        } catch (final RuntimeException ex) {
+            return null;
+        }
+    }
+
+    @Override public void appendAttributesTo(final Map<String, Object> attributeMap) {
+        super.appendAttributesTo(attributeMap);
+        attributeMap.put("method", method);
+    }
+
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/progmodels/dflt/ProgrammingModelFacetsJava5.java b/core/metamodel/src/main/java/org/apache/isis/progmodels/dflt/ProgrammingModelFacetsJava5.java
index d097e30..8fa2683 100644
--- a/core/metamodel/src/main/java/org/apache/isis/progmodels/dflt/ProgrammingModelFacetsJava5.java
+++ b/core/metamodel/src/main/java/org/apache/isis/progmodels/dflt/ProgrammingModelFacetsJava5.java
@@ -103,6 +103,7 @@ import org.apache.isis.core.metamodel.facets.object.ignore.javalang.RemoveMethod
 import org.apache.isis.core.metamodel.facets.object.ignore.jdo.RemoveJdoEnhancementTypesFacetFactory;
 import org.apache.isis.core.metamodel.facets.object.ignore.jdo.RemoveJdoPrefixedMethodsFacetFactory;
 import org.apache.isis.core.metamodel.facets.object.immutable.immutablemarkerifc.ImmutableFacetMarkerInterfaceFactory;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacetFactory;
 import org.apache.isis.core.metamodel.facets.object.mask.annotation.MaskFacetOnTypeAnnotationFactory;
 import org.apache.isis.core.metamodel.facets.object.maxlen.annotation.MaxLengthFacetOnTypeAnnotationFactory;
 import org.apache.isis.core.metamodel.facets.object.membergroups.annotprop.MemberGroupLayoutFacetFactory;
@@ -409,6 +410,7 @@ public final class ProgrammingModelFacetsJava5 extends ProgrammingModelAbstract
         addFactory(new TitleFacetViaMethodsFactory());
         addFactory(new IconFacetMethodFactory());
         addFactory(new CssClassFacetMethodFactory());
+        addFactory(new LayoutFacetFactory());
 
 
         addFactory(new DomainServiceLayoutFacetFactory());
diff --git a/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetFactoryTest.java b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetFactoryTest.java
new file mode 100644
index 0000000..7eea575
--- /dev/null
+++ b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetFactoryTest.java
@@ -0,0 +1,71 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.ident.layout;
+
+import java.lang.reflect.Method;
+
+import org.apache.isis.core.metamodel.facetapi.Facet;
+import org.apache.isis.core.metamodel.facets.AbstractFacetFactoryTest;
+import org.apache.isis.core.metamodel.facets.FacetFactory.ProcessClassContext;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacet;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacetMethod;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacetFactory;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public class LayoutFacetFactoryTest extends AbstractFacetFactoryTest {
+
+    private LayoutFacetFactory facetFactory;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        facetFactory = new LayoutFacetFactory();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        facetFactory = null;
+        super.tearDown();
+    }
+
+    public void testLayoutMethodPickedUpOnClassAndMethodRemoved() {
+        class Customer {
+            @SuppressWarnings("unused")
+            public String layout() {
+                return null;
+            }
+        }
+        final Method method = findMethod(Customer.class, "layout");
+
+        facetFactory.process(new ProcessClassContext(Customer.class, methodRemover, facetedMethod));
+
+        final Facet facet = facetedMethod.getFacet(LayoutFacet.class);
+        assertThat(facet, is(notNullValue()));
+        assertThat(facet, is(instanceOf(LayoutFacetMethod.class)));
+
+        assertTrue(methodRemover.getRemovedMethodMethodCalls().contains(method));
+    }
+
+}
diff --git a/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetMethodTest.java b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetMethodTest.java
new file mode 100644
index 0000000..e54da19
--- /dev/null
+++ b/core/metamodel/src/test/java/org/apache/isis/core/metamodel/facets/object/ident/layout/LayoutFacetMethodTest.java
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.isis.core.metamodel.facets.object.ident.layout;
+
+import java.lang.reflect.Method;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.object.layout.LayoutFacetMethod;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class LayoutFacetMethodTest {
+
+    private final Mockery mockery = new JUnit4Mockery();
+
+    private LayoutFacetMethod facet;
+    private FacetHolder mockFacetHolder;
+
+    private ObjectAdapter mockOwningAdapter;
+
+    private DomainObjectWithProblemInLayoutMethod pojo;
+
+    public static class DomainObjectWithProblemInLayoutMethod {
+        public String layout() {
+            throw new NullPointerException();
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+
+        pojo = new DomainObjectWithProblemInLayoutMethod();
+        mockFacetHolder = mockery.mock(FacetHolder.class);
+        mockOwningAdapter = mockery.mock(ObjectAdapter.class);
+        final Method iconNameMethod = DomainObjectWithProblemInLayoutMethod.class.getMethod("layout");
+        facet = new LayoutFacetMethod(iconNameMethod, mockFacetHolder);
+
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockOwningAdapter).getObject();
+                will(returnValue(pojo));
+            }
+        });
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        facet = null;
+    }
+
+    @Test
+    public void when_layout_throws_exception() {
+        final String layout = facet.layout(mockOwningAdapter);
+        assertThat(layout, is(nullValue()));
+    }
+
+}