You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/06/22 08:29:33 UTC

[causeway] branch master updated: CAUSEWAY-3437: adds MandatoryFacetFromXmlElementAnnotation

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3d1342fd80 CAUSEWAY-3437: adds MandatoryFacetFromXmlElementAnnotation
3d1342fd80 is described below

commit 3d1342fd801d34f1d0dfa8494df1d239ee7f395b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Jun 22 10:29:26 2023 +0200

    CAUSEWAY-3437: adds MandatoryFacetFromXmlElementAnnotation
    
    - unrelated: ditches unused BusinessKeyFacet
---
 .../properties/businesskey/BusinessKeyFacet.java   | 35 -------------
 .../businesskey/BusinessKeyFacetAbstract.java      | 35 -------------
 .../MandatoryFacetFromXmlElementAnnotation.java    | 58 ++++++++++++++++++++++
 ...datoryFacetFromXmlElementAnnotationFactory.java | 52 +++++++++++++++++++
 .../dflt/ProgrammingModelFacetsJava11.java         |  4 +-
 .../facets/param/name/ParameterNameFacetTest.java  |  2 +-
 .../column/MandatoryFacetFromColumnAnnotation.java |  2 +-
 7 files changed, 115 insertions(+), 73 deletions(-)

diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacet.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacet.java
deleted file mode 100644
index ca51eb0e26..0000000000
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacet.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- *  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.causeway.core.metamodel.facets.properties.businesskey;
-
-import org.apache.causeway.core.metamodel.facets.SingleStringValueFacet;
-
-/**
- * Indicates that this property is part of the business key.
- *
- * <p>
- * In the standard Apache Causeway Programming Model, corresponds to annotating the
- * property with the <tt>@BusinessKey</tt> annotation.
- *
- * <p>
- * TODO: not yet implemented by the framework or any viewer.
- */
-public interface BusinessKeyFacet extends SingleStringValueFacet {
-
-}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacetAbstract.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacetAbstract.java
deleted file mode 100644
index 10e9475429..0000000000
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/businesskey/BusinessKeyFacetAbstract.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- *  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.causeway.core.metamodel.facets.properties.businesskey;
-
-import org.apache.causeway.core.metamodel.facetapi.Facet;
-import org.apache.causeway.core.metamodel.facetapi.FacetHolder;
-import org.apache.causeway.core.metamodel.facets.SingleStringValueFacetAbstract;
-
-public abstract class BusinessKeyFacetAbstract extends SingleStringValueFacetAbstract implements BusinessKeyFacet {
-
-    private static final Class<? extends Facet> type() {
-        return BusinessKeyFacet.class;
-    }
-
-    public BusinessKeyFacetAbstract(final String value, final FacetHolder holder) {
-        super(type(), holder, value);
-    }
-
-}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotation.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotation.java
new file mode 100644
index 0000000000..dd0f9a90f4
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotation.java
@@ -0,0 +1,58 @@
+/*
+ *  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.causeway.core.metamodel.facets.properties.property.mandatory.jaxb;
+
+import java.util.Optional;
+
+import javax.xml.bind.annotation.XmlElement;
+
+import org.apache.causeway.core.metamodel.facetapi.FacetHolder;
+import org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacetAbstract;
+
+/**
+ * Inferred from presence of an {@code @XmlElement(required = false/true)} annotation.
+ *
+ * @implNote There is an ambiguous use case {@code @XmlElement(nillable = true, required = true)},
+ * where at the time of writing we were not sure whether this maps onto
+ * {@link org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacet.Semantics#OPTIONAL OPTIONAL}
+ * or
+ * {@link org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacet.Semantics#REQUIRED REQUIRED}.
+ * <p>
+ * Ignoring the nillable attribute completely for now. In the future, if we want to refine things,
+ * then we would do that by figuring out what "nillable" actually means in our meta-model
+ * (perhaps not the same as mandatory/optional).
+ */
+public class MandatoryFacetFromXmlElementAnnotation
+extends MandatoryFacetAbstract {
+
+    public static Optional<MandatoryFacetAbstract> create(
+            final Optional<XmlElement> xmlElementIfAny, final FacetHolder facetHolder){
+        return xmlElementIfAny
+                .map(XmlElement::required)
+                .map(Semantics::required)
+                .map(semantics->new MandatoryFacetFromXmlElementAnnotation(semantics, facetHolder));
+    }
+
+    private MandatoryFacetFromXmlElementAnnotation(
+            final Semantics semantics, final FacetHolder holder) {
+        super(holder, semantics);
+    }
+
+}
+
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotationFactory.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotationFactory.java
new file mode 100644
index 0000000000..48c24812a3
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/properties/property/mandatory/jaxb/MandatoryFacetFromXmlElementAnnotationFactory.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.causeway.core.metamodel.facets.properties.property.mandatory.jaxb;
+
+import javax.inject.Inject;
+import javax.xml.bind.annotation.XmlElement;
+
+import org.apache.causeway.core.metamodel.context.MetaModelContext;
+import org.apache.causeway.core.metamodel.facetapi.FeatureType;
+import org.apache.causeway.core.metamodel.facets.FacetFactoryAbstract;
+
+import lombok.val;
+
+public class MandatoryFacetFromXmlElementAnnotationFactory
+extends FacetFactoryAbstract {
+
+    @Inject
+    public MandatoryFacetFromXmlElementAnnotationFactory(final MetaModelContext mmc) {
+        super(mmc, FeatureType.PROPERTIES_ONLY);
+    }
+
+    @Override
+    public void process(final ProcessMethodContext processMethodContext) {
+        if(processMethodContext.isMixinMain()) {
+            return; // shortcut just in case
+        }
+
+        val xmlElementIfAny = processMethodContext.synthesizeOnMethod(XmlElement.class);
+        val facetHolder = processMethodContext.getFacetHolder();
+
+        // search for @XmlElement(required=...)
+        addFacetIfPresent(
+                MandatoryFacetFromXmlElementAnnotation.create(xmlElementIfAny, facetHolder));
+    }
+
+}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/progmodels/dflt/ProgrammingModelFacetsJava11.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/progmodels/dflt/ProgrammingModelFacetsJava11.java
index 07b3561412..0d3722179e 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/progmodels/dflt/ProgrammingModelFacetsJava11.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/progmodels/dflt/ProgrammingModelFacetsJava11.java
@@ -77,6 +77,7 @@ import org.apache.causeway.core.metamodel.facets.properties.defaults.method.Prop
 import org.apache.causeway.core.metamodel.facets.properties.disabled.inferred.DisabledFacetOnPropertyInferredFactory;
 import org.apache.causeway.core.metamodel.facets.properties.mandatory.dflt.MandatoryFacetOnProperyDefaultFactory;
 import org.apache.causeway.core.metamodel.facets.properties.property.PropertyAnnotationFacetFactory;
+import org.apache.causeway.core.metamodel.facets.properties.property.mandatory.jaxb.MandatoryFacetFromXmlElementAnnotationFactory;
 import org.apache.causeway.core.metamodel.facets.properties.propertylayout.PropertyLayoutFacetFactory;
 import org.apache.causeway.core.metamodel.facets.properties.update.PropertySetterFacetFactory;
 import org.apache.causeway.core.metamodel.facets.properties.validating.dflt.PropertyValidateFacetDefaultFactory;
@@ -85,8 +86,8 @@ import org.apache.causeway.core.metamodel.facets.value.semantics.ValueSemanticsA
 import org.apache.causeway.core.metamodel.methods.DomainIncludeAnnotationEnforcesMetamodelContributionValidator;
 import org.apache.causeway.core.metamodel.postprocessors.all.CssOnActionFromConfiguredRegexPostProcessor;
 import org.apache.causeway.core.metamodel.postprocessors.all.DescribedAsFromTypePostProcessor;
-import org.apache.causeway.core.metamodel.postprocessors.all.SanityChecksValidator;
 import org.apache.causeway.core.metamodel.postprocessors.all.MixinSanityChecksValidator;
+import org.apache.causeway.core.metamodel.postprocessors.all.SanityChecksValidator;
 import org.apache.causeway.core.metamodel.postprocessors.all.i18n.SynthesizeObjectNamingPostProcessor;
 import org.apache.causeway.core.metamodel.postprocessors.all.i18n.TranslationPostProcessor;
 import org.apache.causeway.core.metamodel.postprocessors.allbutparam.authorization.AuthorizationPostProcessor;
@@ -194,6 +195,7 @@ extends ProgrammingModelAbstract {
         addFactory(FacetProcessingOrder.E1_MEMBER_MODELLING, new ActionAnnotationFacetFactory(mmc));
         // after the ActionAnnotationFacetFactory so that takes precedent for contributed associations
         addFactory(FacetProcessingOrder.E1_MEMBER_MODELLING, new PropertyAnnotationFacetFactory(mmc));
+        addFactory(FacetProcessingOrder.E1_MEMBER_MODELLING, new MandatoryFacetFromXmlElementAnnotationFactory(mmc));
         // after the ActionAnnotationFacetFactory so that takes precedent for contributed associations
         addFactory(FacetProcessingOrder.E1_MEMBER_MODELLING, new CollectionAnnotationFacetFactory(mmc));
 
diff --git a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/name/ParameterNameFacetTest.java b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/name/ParameterNameFacetTest.java
index 8a755a0667..caffee75b2 100644
--- a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/name/ParameterNameFacetTest.java
+++ b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/name/ParameterNameFacetTest.java
@@ -54,7 +54,7 @@ extends FacetFactoryTestAbstract {
 
     @Test
     public void verifyProgrammingModelNumberOfFactories() {
-        assertEquals(62, programmingModel.streamFactories().count());
+        assertEquals(63, programmingModel.streamFactories().count());
     }
 
     @Test //verify we have the javac -parameter flag set when compiling this class
diff --git a/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/facets/prop/column/MandatoryFacetFromColumnAnnotation.java b/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/facets/prop/column/MandatoryFacetFromColumnAnnotation.java
index 0e57fa8657..28a8af4c5f 100644
--- a/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/facets/prop/column/MandatoryFacetFromColumnAnnotation.java
+++ b/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/facets/prop/column/MandatoryFacetFromColumnAnnotation.java
@@ -22,7 +22,7 @@ import org.apache.causeway.core.metamodel.facetapi.FacetHolder;
 import org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacetAbstract;
 
 /**
- * Inferred from presence of an <tt>@Column</tt> method.
+ * Inferred from presence of an <tt>@Column</tt> annotation.
  */
 public class MandatoryFacetFromColumnAnnotation
 extends MandatoryFacetAbstract {