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 2014/12/29 19:15:47 UTC

isis git commit: ISIS-970: facet factory for @Parameter annotation.

Repository: isis
Updated Branches:
  refs/heads/ISIS-970 c0da099a5 -> 4e706794f


ISIS-970: facet factory for @Parameter annotation.


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

Branch: refs/heads/ISIS-970
Commit: 4e706794f3a6dcdef3764b079f0beac8ab056bb2
Parents: c0da099
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Mon Dec 29 18:15:42 2014 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Mon Dec 29 18:15:42 2014 +0000

----------------------------------------------------------------------
 .../param/autocomplete/MinLengthUtil.java       |  8 +-
 .../MandatoryFacetForParameterAnnotation.java   | 72 +++++++++++++++
 .../MaxLengthFacetForParameterAnnotation.java   | 40 +++++++++
 ...pecificationFacetForParameterAnnotation.java | 49 ++++++++++
 .../param/parameter/ParameterFactory.java       | 94 ++++++++++++++++++++
 .../RegExFacetForParameterAnnotation.java       | 62 +++++++++++++
 .../RegExFacetForPropertyAnnotation.java        |  2 +-
 7 files changed, 325 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/autocomplete/MinLengthUtil.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/autocomplete/MinLengthUtil.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/autocomplete/MinLengthUtil.java
index b655a1a..51f9c27 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/autocomplete/MinLengthUtil.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/autocomplete/MinLengthUtil.java
@@ -20,13 +20,15 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 
 import org.apache.isis.applib.annotation.MinLength;
+import org.apache.isis.applib.annotation.Parameter;
 
 public final class MinLengthUtil {
 
     private MinLengthUtil(){}
 
     /**
-     * Finds the value of the {@link MinLength} annotation on the first parameter of the
+     * Finds the value of the minimum length, from either the {@link MinLength} annotation or the
+     * {@link org.apache.isis.applib.annotation.Parameter#minLength()} annotation, on the first parameter of the
      * supplied method.
      */
     public static int determineMinLength(final Method method) {
@@ -38,6 +40,10 @@ public final class MinLengthUtil {
                     MinLength minLength = (MinLength) annotation;
                     return minLength.value();
                 }
+                if(annotation instanceof Parameter) {
+                    Parameter parameter = (Parameter) annotation;
+                    return parameter.minLength();
+                }
             }
         }
         return MinLengthUtil.MIN_LENGTH_DEFAULT;

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MandatoryFacetForParameterAnnotation.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MandatoryFacetForParameterAnnotation.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MandatoryFacetForParameterAnnotation.java
new file mode 100644
index 0000000..733e472
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MandatoryFacetForParameterAnnotation.java
@@ -0,0 +1,72 @@
+/*
+ *  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.param.parameter;
+
+import java.lang.reflect.Method;
+import org.apache.isis.applib.annotation.Optionality;
+import org.apache.isis.applib.annotation.Parameter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.propparam.mandatory.MandatoryFacet;
+import org.apache.isis.core.metamodel.facets.propparam.mandatory.MandatoryFacetAbstract;
+
+public abstract class MandatoryFacetForParameterAnnotation extends MandatoryFacetAbstract {
+
+    public MandatoryFacetForParameterAnnotation(final FacetHolder holder, final Semantics semantics) {
+        super(holder, semantics);
+    }
+
+    static MandatoryFacet create(final Parameter parameter, Method method, final FacetHolder holder) {
+        final Class<?> returnType = method.getReturnType();
+        if (returnType.isPrimitive()) {
+            return new MandatoryFacetForParameterAnnotation.Primitive(holder);
+        }
+        final Optionality optionality = parameter.optional();
+        switch (optionality) {
+            case DEFAULT:
+                // do nothing here; instead will rely on MandatoryFromJdoColumnAnnotationFacetFactory to perform
+                // the remaining processing
+                return null;
+            case FALSE:
+                return new MandatoryFacetForParameterAnnotation.Required(holder);
+            case TRUE:
+                return new MandatoryFacetForParameterAnnotation.Optional(holder);
+        }
+        return null;
+    }
+
+    public static class Primitive extends MandatoryFacetForParameterAnnotation {
+        public Primitive(final FacetHolder holder) {
+            super(holder, Semantics.REQUIRED);
+        }
+    }
+
+    public static class Required extends MandatoryFacetForParameterAnnotation {
+        public Required(final FacetHolder holder) {
+            super(holder, Semantics.REQUIRED);
+        }
+    }
+
+    public static class Optional extends MandatoryFacetForParameterAnnotation {
+        public Optional(final FacetHolder holder) {
+            super(holder, Semantics.OPTIONAL);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MaxLengthFacetForParameterAnnotation.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MaxLengthFacetForParameterAnnotation.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MaxLengthFacetForParameterAnnotation.java
new file mode 100644
index 0000000..74c17c3
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MaxLengthFacetForParameterAnnotation.java
@@ -0,0 +1,40 @@
+/*
+ *  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.param.parameter;
+
+import org.apache.isis.applib.annotation.Parameter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.propparam.maxlen.MaxLengthFacet;
+import org.apache.isis.core.metamodel.facets.propparam.maxlen.MaxLengthFacetAbstract;
+
+public class MaxLengthFacetForParameterAnnotation extends MaxLengthFacetAbstract {
+
+    public static MaxLengthFacet create(Parameter parameter, FacetHolder holder) {
+        final int maxLength = parameter.maxLength();
+        return maxLength != -1
+                ? new MaxLengthFacetForParameterAnnotation(maxLength, holder)
+                : null;
+    }
+
+    private MaxLengthFacetForParameterAnnotation(final int value, final FacetHolder holder) {
+        super(value, holder);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MustSatisfySpecificationFacetForParameterAnnotation.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MustSatisfySpecificationFacetForParameterAnnotation.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MustSatisfySpecificationFacetForParameterAnnotation.java
new file mode 100644
index 0000000..43d9624
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/MustSatisfySpecificationFacetForParameterAnnotation.java
@@ -0,0 +1,49 @@
+/*
+ *  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.param.parameter;
+
+import java.util.List;
+import com.google.common.collect.Lists;
+import org.apache.isis.applib.annotation.Parameter;
+import org.apache.isis.applib.spec.Specification;
+import org.apache.isis.core.metamodel.facetapi.Facet;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.propparam.mustsatisfyspec.MustSatisfySpecificationFacetAbstract;
+
+public class MustSatisfySpecificationFacetForParameterAnnotation extends MustSatisfySpecificationFacetAbstract {
+
+    static Facet create(final Parameter parameter, final FacetHolder holder) {
+        final Class<?>[] values = parameter.mustSatisfy();
+        final List<Specification> specifications = Lists.newArrayList();
+        for (final Class<?> value : values) {
+            final Specification specification = newSpecificationElseNull(value);
+            if (specification != null) {
+                specifications.add(specification);
+            }
+        }
+        return specifications.size() > 0 ? new MustSatisfySpecificationFacetForParameterAnnotation(specifications, holder) : null;
+    }
+
+
+    private MustSatisfySpecificationFacetForParameterAnnotation(final List<Specification> specifications, final FacetHolder holder) {
+        super(specifications, holder);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/ParameterFactory.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/ParameterFactory.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/ParameterFactory.java
new file mode 100644
index 0000000..bfc6bfa
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/ParameterFactory.java
@@ -0,0 +1,94 @@
+/*
+ *  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.param.parameter;
+
+import java.lang.reflect.Method;
+import org.apache.isis.applib.annotation.Parameter;
+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.Annotations;
+import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
+import org.apache.isis.core.metamodel.runtimecontext.ServicesInjector;
+import org.apache.isis.core.metamodel.runtimecontext.ServicesInjectorAware;
+
+public class ParameterFactory extends FacetFactoryAbstract implements ServicesInjectorAware {
+
+    private ServicesInjector servicesInjector;
+
+    public ParameterFactory() {
+        super(FeatureType.PARAMETERS_ONLY);
+    }
+
+    @Override
+    public void process(final ProcessMethodContext processMethodContext) {
+
+        final Method method = processMethodContext.getMethod();
+        final Parameter parameter = Annotations.getAnnotation(method, Parameter.class);
+        if (parameter == null) {
+            return;
+        }
+
+        processMaxLength(processMethodContext);
+        processMustSatisfy(processMethodContext);
+        processOptional(processMethodContext);
+        processRegEx(processMethodContext);
+    }
+
+    private void processMaxLength(final ProcessMethodContext processMethodContext) {
+        final Method method = processMethodContext.getMethod();
+        final Parameter parameter = Annotations.getAnnotation(method, Parameter.class);
+        final FacetHolder holder = processMethodContext.getFacetHolder();
+
+        FacetUtil.addFacet(
+                MaxLengthFacetForParameterAnnotation.create(parameter, holder));
+    }
+
+    private void processMustSatisfy(final ProcessMethodContext processMethodContext) {
+        final Method method = processMethodContext.getMethod();
+        final Parameter parameter = Annotations.getAnnotation(method, Parameter.class);
+        final FacetHolder holder = processMethodContext.getFacetHolder();
+
+        FacetUtil.addFacet(
+                MustSatisfySpecificationFacetForParameterAnnotation.create(parameter, holder));
+    }
+
+    private void processOptional(final ProcessMethodContext processMethodContext) {
+        final Method method = processMethodContext.getMethod();
+        final Parameter parameter = Annotations.getAnnotation(method, Parameter.class);
+        final FacetHolder holder = processMethodContext.getFacetHolder();
+
+        FacetUtil.addFacet(MandatoryFacetForParameterAnnotation.create(parameter, method, holder));
+    }
+
+    private void processRegEx(final ProcessMethodContext processMethodContext) {
+        final Method method = processMethodContext.getMethod();
+        final Parameter parameter = Annotations.getAnnotation(method, Parameter.class);
+        final FacetHolder holder = processMethodContext.getFacetHolder();
+
+        FacetUtil.addFacet(RegExFacetForParameterAnnotation.create(parameter, holder));
+    }
+
+
+    @Override
+    public void setServicesInjector(final ServicesInjector servicesInjector) {
+        this.servicesInjector = servicesInjector;
+    }
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/RegExFacetForParameterAnnotation.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/RegExFacetForParameterAnnotation.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/RegExFacetForParameterAnnotation.java
new file mode 100644
index 0000000..d28ed17
--- /dev/null
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/param/parameter/RegExFacetForParameterAnnotation.java
@@ -0,0 +1,62 @@
+/*
+ *  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.param.parameter;
+
+import java.util.regex.Pattern;
+import org.apache.isis.applib.annotation.Parameter;
+import org.apache.isis.core.metamodel.facetapi.FacetHolder;
+import org.apache.isis.core.metamodel.facets.object.regex.RegExFacet;
+import org.apache.isis.core.metamodel.facets.object.regex.RegExFacetAbstract;
+
+public class RegExFacetForParameterAnnotation extends RegExFacetAbstract {
+
+    private final Pattern pattern;
+
+    static RegExFacet create(final Parameter parameter, final FacetHolder holder) {
+
+        final String pattern = parameter.regexPattern();
+        final int patternFlags = parameter.regexPatternFlags();
+
+        return new RegExFacetForParameterAnnotation(pattern, patternFlags, holder);
+    }
+
+    private RegExFacetForParameterAnnotation(final String pattern, final int patternFlags, final FacetHolder holder) {
+        super(pattern, "", false, holder);
+        this.pattern = Pattern.compile(pattern, patternFlags);
+    }
+
+    /**
+     * Unused (for the TitledFacet)
+     */
+    @Override
+    public String format(String text) {
+        return text;
+    }
+
+    @Override
+    public boolean doesNotMatch(final String text) {
+        if (text == null) {
+            return true;
+        }
+        return !pattern.matcher(text).matches();
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/isis/blob/4e706794/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/RegExFacetForPropertyAnnotation.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/RegExFacetForPropertyAnnotation.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/RegExFacetForPropertyAnnotation.java
index 553983a..14b3db2 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/RegExFacetForPropertyAnnotation.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/RegExFacetForPropertyAnnotation.java
@@ -43,7 +43,7 @@ public class RegExFacetForPropertyAnnotation extends RegExFacetAbstract {
     }
 
     /**
-     * Unused
+     * Unused (for the TitledFacet)
      */
     @Override
     public String format(String text) {