← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17775: rename nullable => required in schema/property, allow POST/PUT to /api/schemas/type to validate o...

 

------------------------------------------------------------
revno: 17775
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-12-22 14:52:51 +0100
message:
  rename nullable => required in schema/property, allow POST/PUT to /api/schemas/type to validate object, wip
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java
  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/AbstractPropertyIntrospectorService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/SchemaValidator.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/ValidationViolation.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/common/BaseIdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java	2014-12-22 10:28:42 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java	2014-12-22 13:52:51 +0000
@@ -37,6 +37,7 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.apache.commons.lang.Validate;
 import org.hisp.dhis.acl.Access;
+import org.hisp.dhis.acl.AccessStringHelper;
 import org.hisp.dhis.common.annotation.Description;
 import org.hisp.dhis.common.view.DimensionalView;
 import org.hisp.dhis.common.view.SharingBasicView;
@@ -106,7 +107,7 @@
     /**
      * Access string for public access.
      */
-    protected String publicAccess;
+    protected String publicAccess = AccessStringHelper.DEFAULT;
 
     /**
      * Owner of this object.

=== 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-19 13:43:08 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2014-12-22 13:52:51 +0000
@@ -173,7 +173,7 @@
     /**
      * Nullability of this property.
      */
-    private boolean nullable;
+    private boolean required;
 
     /**
      * Maximum length/size/value of this property.
@@ -470,14 +470,14 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
-    public boolean isNullable()
+    public boolean isRequired()
     {
-        return nullable;
+        return required;
     }
 
-    public void setNullable( boolean nullable )
+    public void setRequired( boolean required )
     {
-        this.nullable = nullable;
+        this.required = required;
     }
 
     @JsonProperty

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/AbstractPropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/AbstractPropertyIntrospectorService.java	2014-12-22 11:16:13 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/AbstractPropertyIntrospectorService.java	2014-12-22 13:52:51 +0000
@@ -147,7 +147,7 @@
         while ( propertyIterator.hasNext() )
         {
             Property property = new Property( klass );
-            property.setNullable( true );
+            property.setRequired( false );
             property.setPersisted( true );
             property.setOwner( true );
 
@@ -186,7 +186,7 @@
                 Column column = (Column) hibernateProperty.getColumnIterator().next();
 
                 property.setUnique( column.isUnique() );
-                property.setNullable( column.isNullable() );
+                property.setRequired( !column.isNullable() );
 
                 property.setMax( column.getLength() );
                 property.setMin( 0 );

=== 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-19 13:27:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2014-12-22 13:52:51 +0000
@@ -107,7 +107,7 @@
                 property.setPersisted( true );
                 property.setWritable( true );
                 property.setUnique( hibernateProperty.isUnique() );
-                property.setNullable( hibernateProperty.isNullable() );
+                property.setRequired( hibernateProperty.isRequired() );
                 property.setMax( hibernateProperty.getMax() );
                 property.setMin( hibernateProperty.getMin() );
                 property.setCollection( hibernateProperty.isCollection() );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java	2014-12-22 09:52:13 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java	2014-12-22 13:52:51 +0000
@@ -29,7 +29,6 @@
  */
 
 import org.apache.commons.validator.GenericValidator;
-import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.PropertyType;
 import org.hisp.dhis.schema.Schema;
@@ -51,7 +50,7 @@
     private SchemaService schemaService;
 
     @Override
-    public <T extends IdentifiableObject> List<ValidationViolation> validate( T object )
+    public List<ValidationViolation> validate( Object object )
     {
         if ( object == null || schemaService.getSchema( object.getClass() ) == null )
         {
@@ -68,7 +67,7 @@
 
             if ( value == null )
             {
-                if ( !property.isNullable() )
+                if ( !property.isRequired() )
                 {
                     validationViolations.add( new ValidationViolation( "Property '" + property.getName() + "' can not be null." ) );
                 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/SchemaValidator.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/SchemaValidator.java	2014-12-22 08:04:25 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/SchemaValidator.java	2014-12-22 13:52:51 +0000
@@ -28,9 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.dxf2.webmessage.WebMessage;
-
 import java.util.List;
 
 /**
@@ -44,5 +41,5 @@
      * @param object Object to validate
      * @return WebMessage containing validation response
      */
-    <T extends IdentifiableObject> List<ValidationViolation> validate( T object );
+    List<ValidationViolation> validate( Object object );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/ValidationViolation.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/ValidationViolation.java	2014-12-22 08:04:25 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/ValidationViolation.java	2014-12-22 13:52:51 +0000
@@ -59,4 +59,13 @@
     {
         this.message = message;
     }
+
+    @Override
+    public String toString()
+    {
+        final StringBuilder sb = new StringBuilder( "ValidationViolation{" );
+        sb.append( "message='" ).append( message ).append( '\'' );
+        sb.append( '}' );
+        return sb.toString();
+    }
 }

=== 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-12-19 11:37:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SchemaController.java	2014-12-22 13:52:51 +0000
@@ -28,12 +28,16 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.dxf2.render.RenderService;
+import org.hisp.dhis.dxf2.schema.SchemaValidator;
+import org.hisp.dhis.dxf2.schema.ValidationViolation;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.schema.Schemas;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -41,6 +45,11 @@
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.client.HttpClientErrorException;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -51,13 +60,19 @@
     @Autowired
     private SchemaService schemaService;
 
+    @Autowired
+    private SchemaValidator schemaValidator;
+
+    @Autowired
+    private RenderService renderService;
+
     @RequestMapping
     public @ResponseBody Schemas getSchemas()
     {
         return new Schemas( schemaService.getSortedSchemas() );
     }
 
-    @RequestMapping( value = "/{type}" )
+    @RequestMapping( value = "/{type}", method = RequestMethod.GET )
     public @ResponseBody Schema getSchema( @PathVariable String type )
     {
         Schema schema = getSchemaFromType( type );
@@ -70,7 +85,23 @@
         throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
     }
 
-    @RequestMapping( value = "/{type}/{property}" )
+    @RequestMapping( value = "/{type}", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE )
+    public void validateSchema( @PathVariable String type, HttpServletRequest request, HttpServletResponse response ) throws IOException
+    {
+        Schema schema = getSchemaFromType( type );
+
+        if ( schema == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.NOT_FOUND, "Type " + type + " does not exist." );
+        }
+
+        Object object = renderService.fromJson( request.getInputStream(), schema.getKlass() );
+        List<ValidationViolation> validationViolations = schemaValidator.validate( object );
+
+        renderService.toJson( response.getOutputStream(), validationViolations );
+    }
+
+    @RequestMapping( value = "/{type}/{property}", method = RequestMethod.GET )
     public @ResponseBody Property getSchemaProperty( @PathVariable String type, @PathVariable String property )
     {
         Schema schema = getSchemaFromType( type );