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

cziegeler pushed a commit to branch SLING-9867
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-apiregions.git


The following commit(s) were added to refs/heads/SLING-9867 by this push:
     new 3c6e7a2  Add required properties
3c6e7a2 is described below

commit 3c6e7a28dd056b30accca4a7dcaa9ef9608299f2
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Nov 3 13:47:43 2020 +0100

    Add required properties
---
 .../apiregions/api/config/AttributeableEntity.java | 22 +++++++++++++++++-
 .../extension/apiregions/api/config/Constants.java |  2 ++
 .../extension/apiregions/api/config/Property.java  | 26 +++++++++++++++++++++-
 3 files changed, 48 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
index 3d421dd..0a81f0d 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
@@ -25,6 +25,7 @@ import javax.json.JsonException;
 import javax.json.JsonObject;
 import javax.json.JsonObjectBuilder;
 import javax.json.JsonValue;
+import javax.json.JsonValue.ValueType;
 
 import org.apache.felix.cm.json.Configurations;
 
@@ -133,11 +134,12 @@ public abstract class AttributeableEntity {
                        builder.add(attributeName, value);
                }
        }
+
        /**
         * Helper method to get a integer value from an attribute
         * @param attributeName The attribute name
         * @param defaultValue default value
-        * @return The integer value or
+        * @return The integer value or the default value
         */
        int getInteger(final String attributeName, final int defaultValue) {
                final String val = this.getString(attributeName);
@@ -146,4 +148,22 @@ public abstract class AttributeableEntity {
                }
                return defaultValue;
        }
+
+       /**
+        * Helper method to get a boolean value from an attribute
+        * @param attributeName The attribute name
+        * @param defaultValue default value
+        * @return The boolean value or the default value
+        */
+       boolean getBoolean(final String attributeName, final boolean 
defaultValue) throws IOException {
+               final JsonValue val = 
this.getAttributes().remove(attributeName);
+               if ( val != null ) {
+                       final Object obj = Configurations.convertToObject(val);
+                       if ( obj instanceof Boolean ) {
+                return ((Boolean)obj).booleanValue();
+                       }
+                       throw new IOException("Invalid type for boolean value " 
+ attributeName + " : " + val.getValueType().name());
+               }
+               return defaultValue;
+       }
 }
diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Constants.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Constants.java
index 88f5788..d253419 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Constants.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Constants.java
@@ -62,4 +62,6 @@ public abstract class Constants {
     static final String KEY_REGEX = "regex";
 
     static final String KEY_VALUE = "value";
+
+    static final String KEY_REQUIRED = "required";
 }
diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Property.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Property.java
index b06ed54..3579029 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Property.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Property.java
@@ -56,6 +56,9 @@ public class Property extends DescribableEntity {
        /** The optional regex */
        private String regex;
 
+       /** Required? */
+       private boolean required;
+       
     /**
      * Clear the object and remove all metadata
      */
@@ -63,6 +66,7 @@ public class Property extends DescribableEntity {
         super.clear();
                this.setType(PropertyType.STRING);
                this.setCardinality(1);
+               this.setRequired(false);
                this.setVariable(null);
                this.setRange(null);
                this.setIncludes(null);
@@ -82,7 +86,8 @@ public class Property extends DescribableEntity {
         try {
                        
this.setVariable(this.getString(Constants.KEY_VARIABLE));
                        
this.setCardinality(this.getInteger(Constants.KEY_CARDINALITY, 
this.getCardinality()));
-
+                       
this.setRequired(this.getBoolean(Constants.KEY_REQUIRED, this.isRequired()));
+                       
                        final String typeVal = 
this.getString(Constants.KEY_TYPE);
                        if ( typeVal != null ) {
                 this.setType(PropertyType.valueOf(typeVal.toUpperCase()));     
                        
@@ -140,6 +145,9 @@ public class Property extends DescribableEntity {
                if ( this.getCardinality() != 1 ) {
                        objectBuilder.add(Constants.KEY_CARDINALITY, 
this.getCardinality());
                }
+               if ( this.isRequired() ) {
+                       objectBuilder.add(Constants.KEY_REQUIRED, 
this.isRequired());
+               }
            this.setString(objectBuilder, Constants.KEY_VARIABLE, 
this.getVariable());
                
                if ( this.range != null ) {
@@ -298,4 +306,20 @@ public class Property extends DescribableEntity {
        public void setRegex(final String regex) {
                this.regex = regex;
        }
+
+       /**
+        * Is this property required?
+        * @return {@code true} if it is required
+        */
+       public boolean isRequired() {
+               return this.required;
+       }
+
+       /**
+        * Set whether this property is required
+        * @param flag The new value
+        */
+       public void setRequired(final boolean flag) {
+               this.required = flag;
+       }
 }

Reply via email to