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 2013/10/08 20:15:28 UTC

git commit: ISIS-557: MaxLengthFacet inferred from JDO @Column(length=...) annotation

Updated Branches:
  refs/heads/master 3df42bf45 -> 595c8d51f


ISIS-557: MaxLengthFacet inferred from JDO @Column(length=...) annotation


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/595c8d51
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/595c8d51
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/595c8d51

Branch: refs/heads/master
Commit: 595c8d51fe74bc0766231ab2d7c81239851eb342
Parents: 3df42bf
Author: Dan Haywood <da...@apache.org>
Authored: Tue Oct 8 19:14:57 2013 +0100
Committer: Dan Haywood <da...@apache.org>
Committed: Tue Oct 8 19:14:57 2013 +0100

----------------------------------------------------------------------
 ...ataNucleusPersistenceMechanismInstaller.java |   2 +
 ...ivedFromJdoColumnAnnotationFacetFactory.java | 134 +++++++++++++++++++
 .../MaxLengthFacetDerivedFromJdoColumn.java     |  31 +++++
 .../dom/src/main/java/dom/todo/ToDoItem.java    |   5 +-
 .../tests/props/ToDoItemTest_description.java   |  15 +++
 .../integration/SimpleAppSystemInitializer.java |   6 +-
 6 files changed, 188 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/DataNucleusPersistenceMechanismInstaller.java
----------------------------------------------------------------------
diff --git a/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/DataNucleusPersistenceMechanismInstaller.java b/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/DataNucleusPersistenceMechanismInstaller.java
index bd14bd7..b20effb 100644
--- a/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/DataNucleusPersistenceMechanismInstaller.java
+++ b/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/DataNucleusPersistenceMechanismInstaller.java
@@ -51,6 +51,7 @@ import org.apache.isis.objectstore.jdo.metamodel.facets.object.persistencecapabl
 import org.apache.isis.objectstore.jdo.metamodel.facets.object.query.JdoQueryAnnotationFacetFactory;
 import org.apache.isis.objectstore.jdo.metamodel.facets.prop.column.BigDecimalDerivedFromJdoColumnAnnotationFacetFactory;
 import org.apache.isis.objectstore.jdo.metamodel.facets.prop.column.MandatoryFromJdoColumnAnnotationFacetFactory;
+import org.apache.isis.objectstore.jdo.metamodel.facets.prop.column.MaxLengthDerivedFromJdoColumnAnnotationFacetFactory;
 import org.apache.isis.objectstore.jdo.metamodel.facets.prop.notpersistent.JdoNotPersistentAnnotationFacetFactory;
 import org.apache.isis.objectstore.jdo.metamodel.facets.prop.primarykey.JdoPrimaryKeyAnnotationFacetFactory;
 import org.apache.isis.objectstore.jdo.metamodel.specloader.validator.JdoMetaModelValidator;
@@ -193,6 +194,7 @@ public class DataNucleusPersistenceMechanismInstaller extends PersistenceMechani
         programmingModel.addFactory(JdoQueryAnnotationFacetFactory.class);
         
         programmingModel.addFactory(BigDecimalDerivedFromJdoColumnAnnotationFacetFactory.class);
+        programmingModel.addFactory(MaxLengthDerivedFromJdoColumnAnnotationFacetFactory.class);
         // must appear after JdoPrimaryKeyAnnotationFacetFactory (above)
         programmingModel.addFactory(MandatoryFromJdoColumnAnnotationFacetFactory.class);
         

http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthDerivedFromJdoColumnAnnotationFacetFactory.java
----------------------------------------------------------------------
diff --git a/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthDerivedFromJdoColumnAnnotationFacetFactory.java b/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthDerivedFromJdoColumnAnnotationFacetFactory.java
new file mode 100644
index 0000000..ec3e6cd
--- /dev/null
+++ b/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthDerivedFromJdoColumnAnnotationFacetFactory.java
@@ -0,0 +1,134 @@
+/*
+ *  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.objectstore.jdo.metamodel.facets.prop.column;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+import javax.jdo.annotations.Column;
+import javax.jdo.annotations.IdentityType;
+
+import org.apache.isis.core.commons.config.IsisConfiguration;
+import org.apache.isis.core.metamodel.facetapi.FacetUtil;
+import org.apache.isis.core.metamodel.facetapi.FeatureType;
+import org.apache.isis.core.metamodel.facetapi.MetaModelRefiner;
+import org.apache.isis.core.metamodel.facetapi.MetaModelValidatorRefiner;
+import org.apache.isis.core.metamodel.facets.Annotations;
+import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
+import org.apache.isis.core.metamodel.facets.FacetedMethod;
+import org.apache.isis.core.metamodel.facets.mandatory.MandatoryFacet;
+import org.apache.isis.core.metamodel.facets.maxlen.MaxLengthFacet;
+import org.apache.isis.core.metamodel.spec.ObjectSpecification;
+import org.apache.isis.core.metamodel.spec.feature.Contributed;
+import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
+import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorComposite;
+import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorVisiting;
+import org.apache.isis.core.metamodel.specloader.validator.ValidationFailures;
+import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorVisiting.Visitor;
+import org.apache.isis.core.progmodel.facets.properties.validate.maxlenannot.MaxLengthFacetAnnotationForProperty;
+import org.apache.isis.core.progmodel.facets.value.bigdecimal.BigDecimalValueFacet;
+import org.apache.isis.objectstore.jdo.metamodel.facets.object.persistencecapable.JdoPersistenceCapableFacet;
+import org.apache.isis.objectstore.jdo.metamodel.facets.prop.notpersistent.JdoNotPersistentFacet;
+
+
+public class MaxLengthDerivedFromJdoColumnAnnotationFacetFactory extends FacetFactoryAbstract implements MetaModelValidatorRefiner {
+
+    public MaxLengthDerivedFromJdoColumnAnnotationFacetFactory() {
+        super(FeatureType.PROPERTIES_ONLY);
+    }
+
+    @Override
+    public void process(final ProcessMethodContext processMethodContext) {
+
+        final Column annotation = Annotations.getAnnotation(processMethodContext.getMethod(), Column.class);
+
+        if(String.class != processMethodContext.getMethod().getReturnType()) {
+            return;
+        } 
+
+        if (annotation == null || annotation.length() == -1) {
+            return;
+        } 
+        
+        final FacetedMethod holder = processMethodContext.getFacetHolder();
+        
+        MaxLengthFacet existingFacet = holder.getFacet(MaxLengthFacet.class);
+        
+        final MaxLengthFacet facet = new MaxLengthFacetDerivedFromJdoColumn(annotation.length(), holder);
+        
+        if(!existingFacet.isNoop()) {
+            // will raise violation later
+            facet.setUnderlyingFacet(existingFacet);
+        }
+        
+        FacetUtil.addFacet(facet);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.isis.core.metamodel.facetapi.MetaModelValidatorRefiner#refineMetaModelValidator(org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorComposite, org.apache.isis.core.commons.config.IsisConfiguration)
+     */
+    @Override
+    public void refineMetaModelValidator(MetaModelValidatorComposite metaModelValidator, IsisConfiguration configuration) {
+        metaModelValidator.add(new MetaModelValidatorVisiting(newValidatorVisitor()));
+    }
+
+    private Visitor newValidatorVisitor() {
+        return new MetaModelValidatorVisiting.Visitor() {
+
+            @Override
+            public boolean visit(ObjectSpecification objectSpec, ValidationFailures validationFailures) {
+                validate(objectSpec, validationFailures);
+                return true;
+            }
+
+            private void validate(ObjectSpecification objectSpec, ValidationFailures validationFailures) {
+                
+                final JdoPersistenceCapableFacet pcFacet = objectSpec.getFacet(JdoPersistenceCapableFacet.class);
+                if(pcFacet==null || pcFacet.getIdentityType() == IdentityType.NONDURABLE) {
+                    return;
+                }
+                
+                final List<ObjectAssociation> associations = objectSpec.getAssociations(Contributed.EXCLUDED, ObjectAssociation.Filters.PROPERTIES);
+                for (ObjectAssociation association : associations) {
+                    
+                    // skip checks if annotated with JDO @NotPersistent
+                    if(association.containsDoOpFacet(JdoNotPersistentFacet.class)) {
+                        return;
+                    }
+                    
+                    MaxLengthFacet facet = association.getFacet(MaxLengthFacet.class);
+
+                    MaxLengthFacet underlying = (MaxLengthFacet) facet.getUnderlyingFacet();
+                    if(underlying == null) {
+                        continue;
+                    }
+
+                    if(facet instanceof MaxLengthFacetDerivedFromJdoColumn && underlying instanceof MaxLengthFacetAnnotationForProperty) {
+                        if(facet.value() != underlying.value()) {
+                            validationFailures.add("%s: incompatible usage of Isis' @MaxLength annotation and @javax.jdo.annotations.Column with inconsistent lengths; use just @javax.jdo.annotations.Column(length=...)", association.getIdentifier().toClassAndNameIdentityString());
+                        }
+                    }
+                }
+            }
+        };
+    }
+
+
+    
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthFacetDerivedFromJdoColumn.java
----------------------------------------------------------------------
diff --git a/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthFacetDerivedFromJdoColumn.java b/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthFacetDerivedFromJdoColumn.java
new file mode 100644
index 0000000..ab487d5
--- /dev/null
+++ b/component/objectstore/jdo/jdo-metamodel/src/main/java/org/apache/isis/objectstore/jdo/metamodel/facets/prop/column/MaxLengthFacetDerivedFromJdoColumn.java
@@ -0,0 +1,31 @@
+/*
+ *  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.objectstore.jdo.metamodel.facets.prop.column;
+
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.maxlen.MaxLengthFacetAbstract;
+
+
+public class MaxLengthFacetDerivedFromJdoColumn extends MaxLengthFacetAbstract {
+
+    public MaxLengthFacetDerivedFromJdoColumn(final Integer length, final FacetHolder holder) {
+        super(length, holder);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/example/application/quickstart_wicket_restful_jdo/dom/src/main/java/dom/todo/ToDoItem.java
----------------------------------------------------------------------
diff --git a/example/application/quickstart_wicket_restful_jdo/dom/src/main/java/dom/todo/ToDoItem.java b/example/application/quickstart_wicket_restful_jdo/dom/src/main/java/dom/todo/ToDoItem.java
index 26819cc..a73b604 100644
--- a/example/application/quickstart_wicket_restful_jdo/dom/src/main/java/dom/todo/ToDoItem.java
+++ b/example/application/quickstart_wicket_restful_jdo/dom/src/main/java/dom/todo/ToDoItem.java
@@ -46,6 +46,7 @@ import org.apache.isis.applib.annotation.Bulk;
 import org.apache.isis.applib.annotation.CssClass;
 import org.apache.isis.applib.annotation.Disabled;
 import org.apache.isis.applib.annotation.Hidden;
+import org.apache.isis.applib.annotation.MaxLength;
 import org.apache.isis.applib.annotation.MinLength;
 import org.apache.isis.applib.annotation.MultiLine;
 import org.apache.isis.applib.annotation.Named;
@@ -148,7 +149,7 @@ public class ToDoItem implements Comparable<ToDoItem> /*, Locatable*/ { // GMAP3
     
     private String description;
 
-    @javax.jdo.annotations.Column(allowsNull="false")
+    @javax.jdo.annotations.Column(allowsNull="false", length=30)
     @RegEx(validation = "\\w[@&:\\-\\,\\.\\+ \\w]*") 
     public String getDescription() {
         return description;
@@ -369,7 +370,7 @@ public class ToDoItem implements Comparable<ToDoItem> /*, Locatable*/ { // GMAP3
 
     private String notes;
 
-    @javax.jdo.annotations.Column(allowsNull="true")
+    @javax.jdo.annotations.Column(allowsNull="true", length=400)
     @Hidden(where=Where.ALL_TABLES)
     @MultiLine(numberOfLines=5)
     public String getNotes() {

http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests/props/ToDoItemTest_description.java
----------------------------------------------------------------------
diff --git a/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests/props/ToDoItemTest_description.java b/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests/props/ToDoItemTest_description.java
index 73e9645..0411a90 100644
--- a/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests/props/ToDoItemTest_description.java
+++ b/example/application/quickstart_wicket_restful_jdo/integtests/src/test/java/integration/tests/props/ToDoItemTest_description.java
@@ -73,4 +73,19 @@ public class ToDoItemTest_description extends ToDoIntegTest {
         toDoItem.setDescription(null);
     }
 
+    @Test
+    public void onlyJustShortEnough() throws Exception {
+        
+        // when, then
+        toDoItem.setDescription("123456789012345678901234567890");
+    }
+
+    @Test
+    public void tooLong() throws Exception {
+        
+        // when, then
+        expectedExceptions.expectMessage("The value proposed exceeds the maximum length of 30");
+        toDoItem.setDescription("1234567890123456789012345678901");
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/isis/blob/595c8d51/example/application/simple_wicket_restful_jdo/integtests/src/test/java/integration/SimpleAppSystemInitializer.java
----------------------------------------------------------------------
diff --git a/example/application/simple_wicket_restful_jdo/integtests/src/test/java/integration/SimpleAppSystemInitializer.java b/example/application/simple_wicket_restful_jdo/integtests/src/test/java/integration/SimpleAppSystemInitializer.java
index 126682a..d0c9ce9 100644
--- a/example/application/simple_wicket_restful_jdo/integtests/src/test/java/integration/SimpleAppSystemInitializer.java
+++ b/example/application/simple_wicket_restful_jdo/integtests/src/test/java/integration/SimpleAppSystemInitializer.java
@@ -38,15 +38,15 @@ public class SimpleAppSystemInitializer {
     public static IsisSystemForTest initIsft() {
         IsisSystemForTest isft = IsisSystemForTest.getElseNull();
         if(isft == null) {
-            isft = new ToDoSystemBuilder().build().setUpSystem();
+            isft = new SimpleAppSystemBuilder().build().setUpSystem();
             IsisSystemForTest.set(isft);
         }
         return isft;
     }
 
-    private static class ToDoSystemBuilder extends IsisSystemForTest.Builder {
+    private static class SimpleAppSystemBuilder extends IsisSystemForTest.Builder {
 
-        public ToDoSystemBuilder() {
+        public SimpleAppSystemBuilder() {
             //withFixtures( ... reference data fixtures ...); // if we had any...
             withLoggingAt(org.apache.log4j.Level.INFO);
             with(testConfiguration());