← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17736: introduce new properties on Schema: propertyType, itemPropertyType, minValue, maxValue (only intr...

 

------------------------------------------------------------
revno: 17736
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-12-19 12:18:40 +0100
message:
  introduce new properties on Schema: propertyType, itemPropertyType, minValue, maxValue (only introspected property types are set for now), also make it possible to fetch schema using /api/schemas/klass-name (not just schema name)
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaUtils.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2014-12-18 17:08:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2014-12-19 11:18:40 +0000
@@ -28,18 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.lang.reflect.Method;
-
-import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.NameableObject;
-import org.springframework.core.Ordered;
-
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.NameableObject;
+import org.springframework.core.Ordered;
+
+import java.lang.reflect.Method;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -53,11 +52,21 @@
     private Class<?> klass;
 
     /**
+     * Normalized type of this property
+     */
+    private PropertyType propertyType;
+
+    /**
      * If this property is a collection, this is the class of the items inside the collection.
      */
     private Class<?> itemKlass;
 
     /**
+     * If this property is a collection, this is the normalized type of the items inside the collection.
+     */
+    private PropertyType itemPropertyType;
+
+    /**
      * Direct link to getter for this property.
      */
     private Method getterMethod;
@@ -177,6 +186,16 @@
     private Integer minLength;
 
     /**
+     * If type is numeric (or collection), this is the maximum value (or size)
+     */
+    private Integer maxValue;
+
+    /**
+     * If type is numeric (or collection), this is the minimum value (or size)
+     */
+    private Integer minValue;
+
+    /**
      * Cascading used when doing CRUD operations.
      */
     private String cascade;
@@ -213,6 +232,18 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public PropertyType getPropertyType()
+    {
+        return propertyType;
+    }
+
+    public void setPropertyType( PropertyType propertyType )
+    {
+        this.propertyType = propertyType;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Class<?> getItemKlass()
     {
         return itemKlass;
@@ -223,6 +254,18 @@
         this.itemKlass = itemKlass;
     }
 
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public PropertyType getItemPropertyType()
+    {
+        return itemPropertyType;
+    }
+
+    public void setItemPropertyType( PropertyType itemPropertyType )
+    {
+        this.itemPropertyType = itemPropertyType;
+    }
+
     public Method getGetterMethod()
     {
         return getterMethod;
@@ -473,6 +516,30 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public Integer getMaxValue()
+    {
+        return maxValue;
+    }
+
+    public void setMaxValue( Integer maxValue )
+    {
+        this.maxValue = maxValue;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public Integer getMinValue()
+    {
+        return minValue;
+    }
+
+    public void setMinValue( Integer minValue )
+    {
+        this.minValue = minValue;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getCascade()
     {
         return cascade;
@@ -516,6 +583,7 @@
         final Property other = (Property) obj;
 
         return Objects.equal( this.klass, other.klass ) && Objects.equal( this.itemKlass, other.itemKlass )
+            && Objects.equal( this.propertyType, other.propertyType ) && Objects.equal( this.itemPropertyType, other.itemPropertyType )
             && Objects.equal( this.getterMethod, other.getterMethod ) && Objects.equal( this.setterMethod, other.setterMethod )
             && Objects.equal( this.name, other.name ) && Objects.equal( this.fieldName, other.fieldName )
             && Objects.equal( this.persisted, other.persisted ) && Objects.equal( this.collectionName, other.collectionName )
@@ -530,7 +598,9 @@
     {
         return MoreObjects.toStringHelper( this )
             .add( "klass", klass )
+            .add( "propertyType", propertyType )
             .add( "itemKlass", itemKlass )
+            .add( "itemPropertyType", itemPropertyType )
             .add( "getterMethod", getterMethod )
             .add( "name", name )
             .add( "fieldName", fieldName )

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyType.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyType.java	2014-12-19 11:18:40 +0000
@@ -0,0 +1,54 @@
+package org.hisp.dhis.schema;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import org.hisp.dhis.common.DxfNamespaces;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@JacksonXmlRootElement( localName = "propertyType", namespace = DxfNamespaces.DXF_2_0 )
+public enum PropertyType
+{
+    TEXT,
+    NUMBER,
+    INTEGER,
+    BOOLEAN,
+    EMAIL,
+    PASSWORD,
+    URL,
+    DATE,
+    PHONENUMBER,
+    GEOLOCATION,
+    COLOR,
+    COMPLEX,
+    COLLECTION,
+    REFERENCE
+}

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/SchemaUtils.java	2014-12-19 11:18:40 +0000
@@ -0,0 +1,107 @@
+package org.hisp.dhis.schema;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import com.google.common.primitives.Primitives;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.springframework.util.Assert;
+
+import java.util.Collection;
+import java.util.Date;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public final class SchemaUtils
+{
+    public static void updatePropertyTypes( Property property )
+    {
+        Assert.notNull( property );
+        Assert.notNull( property.getKlass() );
+
+        property.setPropertyType( getPropertyType( property.getKlass() ) );
+
+        if ( property.isCollection() )
+        {
+            property.setItemPropertyType( getPropertyType( property.getItemKlass() ) );
+        }
+    }
+
+    private static PropertyType getPropertyType( Class<?> klass )
+    {
+        if ( isAssignableFrom( klass, String.class )
+            || isAssignableFrom( klass, Character.class )
+            || isAssignableFrom( klass, Byte.class ) )
+        {
+            return PropertyType.TEXT;
+        }
+        else if ( isAssignableFrom( klass, Integer.class ) )
+        {
+            return PropertyType.INTEGER;
+        }
+        else if ( isAssignableFrom( klass, Boolean.class ) )
+        {
+            return PropertyType.BOOLEAN;
+        }
+        else if ( isAssignableFrom( klass, Float.class )
+            || isAssignableFrom( klass, Double.class ) )
+        {
+            return PropertyType.NUMBER;
+        }
+        else if ( isAssignableFrom( klass, Date.class )
+            || isAssignableFrom( klass, java.sql.Date.class ) )
+        {
+            return PropertyType.DATE;
+        }
+        else if ( isAssignableFrom( klass, Enum.class ) )
+        {
+            return PropertyType.TEXT; // TODO enum payloads are text, but should we expose a different type?
+        }
+        else if ( isAssignableFrom( klass, IdentifiableObject.class ) )
+        {
+            return PropertyType.REFERENCE;
+        }
+        else if ( isAssignableFrom( klass, Collection.class ) )
+        {
+            return PropertyType.COLLECTION;
+        }
+
+        // if klass is primitive (but unknown), fall back to text, if its not then assume reference
+        return Primitives.isWrapperType( klass ) ? PropertyType.TEXT : PropertyType.COMPLEX;
+    }
+
+    private static boolean isAssignableFrom( Class<?> propertyKlass, Class<?> klass )
+    {
+        return klass.isAssignableFrom( propertyKlass );
+    }
+
+    private SchemaUtils()
+    {
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2014-12-08 00:33:34 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2014-12-19 11:18:40 +0000
@@ -29,16 +29,15 @@
  */
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import com.google.common.primitives.Primitives;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.annotation.Description;
-import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -147,7 +146,7 @@
             }
 
             Class<?> returnType = method.getReturnType();
-            property.setKlass( returnType );
+            property.setKlass( Primitives.wrap( returnType ) );
 
             if ( Collection.class.isAssignableFrom( returnType ) )
             {
@@ -160,7 +159,7 @@
                 {
                     ParameterizedType parameterizedType = (ParameterizedType) type;
                     Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
-                    property.setItemKlass( klass );
+                    property.setItemKlass( Primitives.wrap( klass ) );
 
                     if ( collectProperties( klass ).isEmpty() )
                     {
@@ -206,6 +205,8 @@
             {
                 propertyMap.put( property.getName(), property );
             }
+
+            SchemaUtils.updatePropertyTypes( property );
         }
 
         return propertyMap;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java	2014-10-12 08:32:32 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java	2014-12-19 11:18:40 +0000
@@ -45,7 +45,7 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Controller
-@RequestMapping(value = "/schemas", method = RequestMethod.GET)
+@RequestMapping( value = "/schemas", method = RequestMethod.GET )
 public class SchemaController
 {
     @Autowired
@@ -57,7 +57,7 @@
         return new Schemas( schemaService.getSortedSchemas() );
     }
 
-    @RequestMapping(value = "/{type}")
+    @RequestMapping( value = "/{type:.*}" )
     public @ResponseBody Schema getSchema( @PathVariable String type )
     {
         Schema schema = schemaService.getSchemaBySingularName( type );
@@ -67,14 +67,43 @@
             return schema;
         }
 
+        try
+        {
+            schema = schemaService.getSchema( Class.forName( type ) );
+
+            if ( schema != null )
+            {
+                return schema;
+            }
+        }
+        catch ( ClassNotFoundException ignored )
+        {
+        }
+
         throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
     }
 
-    @RequestMapping( value = "/{type}/{property}" )
+    @RequestMapping( value = "/{type:.*}/{property}" )
     public @ResponseBody Property getSchemaProperty( @PathVariable String type, @PathVariable String property )
     {
         Schema schema = schemaService.getSchemaBySingularName( type );
 
+        if ( schema == null )
+        {
+            try
+            {
+                schema = schemaService.getSchema( Class.forName( type ) );
+            }
+            catch ( ClassNotFoundException ignored )
+            {
+            }
+        }
+
+        if ( schema == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
+        }
+
         if ( schema.getPropertyMap().containsKey( property ) )
         {
             return schema.getPropertyMap().get( property );