← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9244: FRED-API: made Identifier into its own class. Added validator for properties (checks parent, data...

 

------------------------------------------------------------
revno: 9244
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-12-09 19:50:22 +0300
message:
  FRED-API: made Identifier into its own class. Added validator for properties (checks parent, dataSets for now)
added:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Identifier.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/PropertiesValidator.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/ValidProperties.java
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.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-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2012-12-09 12:01:09 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Facility.java	2012-12-09 16:50:22 +0000
@@ -30,6 +30,7 @@
 import org.hibernate.validator.constraints.Length;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.IdentifiableObjectExists;
+import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.ValidProperties;
 import org.hisp.dhis.web.webapi.v1.validation.group.Standard;
 import org.hisp.dhis.web.webapi.v1.validation.group.Update;
 
@@ -74,9 +75,10 @@
     private List<Double> coordinates = new ArrayList<Double>();
 
     // External Facility Identifiers
-    private List<Map<String, String>> identifiers = new ArrayList<Map<String, String>>();
+    private List<Identifier> identifiers = new ArrayList<Identifier>();
 
     // Implementation specific custom properties
+    @ValidProperties
     private Map<String, Object> properties = new HashMap<String, Object>();
 
     public Facility()
@@ -153,12 +155,12 @@
         this.coordinates = coordinates;
     }
 
-    public List<Map<String, String>> getIdentifiers()
+    public List<Identifier> getIdentifiers()
     {
         return identifiers;
     }
 
-    public void setIdentifiers( List<Map<String, String>> identifiers )
+    public void setIdentifiers( List<Identifier> identifiers )
     {
         this.identifiers = identifiers;
     }

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Identifier.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Identifier.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/domain/Identifier.java	2012-12-09 16:50:22 +0000
@@ -0,0 +1,77 @@
+package org.hisp.dhis.web.webapi.v1.domain;
+
+/*
+ * Copyright (c) 2004-2012, 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.
+ */
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class Identifier
+{
+    public static final String DHIS2_AGENCY = "DHIS2";
+    public static final String DHIS2_CODE_CONTEXT = "DHIS2_CODE";
+
+    private String id;
+
+    private String context;
+
+    private String agency;
+
+    public Identifier()
+    {
+    }
+
+    public String getId()
+    {
+        return id;
+    }
+
+    public void setId( String id )
+    {
+        this.id = id;
+    }
+
+    public String getContext()
+    {
+        return context;
+    }
+
+    public void setContext( String context )
+    {
+        this.context = context;
+    }
+
+    public String getAgency()
+    {
+        return agency;
+    }
+
+    public void setAgency( String agency )
+    {
+        this.agency = agency;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java	2012-12-09 13:36:18 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToFacilityConverter.java	2012-12-09 16:50:22 +0000
@@ -31,13 +31,12 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.web.webapi.v1.controller.FacilityController;
 import org.hisp.dhis.web.webapi.v1.domain.Facility;
+import org.hisp.dhis.web.webapi.v1.domain.Identifier;
 import org.springframework.core.convert.converter.Converter;
 import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
 
@@ -80,23 +79,23 @@
 
         if ( organisationUnit.getCode() != null )
         {
-            Map<String, String> codeMap = new HashMap<String, String>();
-            codeMap.put( "agency", "DHIS2" );
-            codeMap.put( "context", "DHIS2_CODE" );
-            codeMap.put( "id", organisationUnit.getCode() );
-
-            facility.getIdentifiers().add( codeMap );
-        }
-
-        if ( !organisationUnit.getDataSets().isEmpty() )
-        {
-            List<String> dataSets = new ArrayList<String>();
-
-            for ( DataSet dataSet : organisationUnit.getDataSets() )
-            {
-                dataSets.add( dataSet.getUid() );
-            }
-
+            Identifier identifier = new Identifier();
+            identifier.setAgency( "DHIS2" );
+            identifier.setContext( "DHIS2_CODE" );
+            identifier.setId( organisationUnit.getCode() );
+
+            facility.getIdentifiers().add( identifier );
+        }
+
+        List<String> dataSets = new ArrayList<String>();
+
+        for ( DataSet dataSet : organisationUnit.getDataSets() )
+        {
+            dataSets.add( dataSet.getUid() );
+        }
+
+        if ( !dataSets.isEmpty() )
+        {
             facility.getProperties().put( "dataSets", dataSets );
         }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.java	2012-12-09 13:36:18 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.java	2012-12-09 16:50:22 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.web.webapi.v1.domain.Facility;
+import org.hisp.dhis.web.webapi.v1.domain.Identifier;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.core.convert.converter.Converter;
@@ -46,11 +47,11 @@
 public class ToOrganisationUnitConverter implements Converter<Facility, OrganisationUnit>
 {
     @Autowired
-    @Qualifier("org.hisp.dhis.organisationunit.OrganisationUnitService")
+    @Qualifier( "org.hisp.dhis.organisationunit.OrganisationUnitService" )
     private OrganisationUnitService organisationUnitService;
 
     @Autowired
-    @Qualifier("org.hisp.dhis.dataset.DataSetService")
+    @Qualifier( "org.hisp.dhis.dataset.DataSetService" )
     private DataSetService dataSetService;
 
     @Override
@@ -84,6 +85,19 @@
             }
         }
 
+        if ( facility.getIdentifiers() != null )
+        {
+            for ( Identifier identifier : facility.getIdentifiers() )
+            {
+                // for now, this is the only known identifier
+                if ( identifier.getAgency().equalsIgnoreCase( Identifier.DHIS2_AGENCY )
+                    && identifier.getContext().equalsIgnoreCase( Identifier.DHIS2_CODE_CONTEXT ) )
+                {
+                    organisationUnit.setCode( identifier.getId() );
+                }
+            }
+        }
+
         organisationUnit.setFeatureType( OrganisationUnit.FEATURETYPE_POINT );
 
         try

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/PropertiesValidator.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/PropertiesValidator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/PropertiesValidator.java	2012-12-09 16:50:22 +0000
@@ -0,0 +1,122 @@
+package org.hisp.dhis.web.webapi.v1.validation.constraint;
+
+/*
+ * Copyright (c) 2004-2012, 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 org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.web.webapi.v1.validation.constraint.annotation.ValidProperties;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class PropertiesValidator implements ConstraintValidator<ValidProperties, Map<String, Object>>
+{
+    @Autowired
+    private IdentifiableObjectManager identifiableObjectManager;
+
+    @Override
+    public void initialize( ValidProperties constraintAnnotation )
+    {
+    }
+
+    @Override
+    public boolean isValid( Map<String, Object> values, ConstraintValidatorContext context )
+    {
+        boolean returnValue = true;
+
+        boolean validParent = validateParent( values );
+
+        if ( !validParent )
+        {
+            context.disableDefaultConstraintViolation();
+            context.buildConstraintViolationWithTemplate( String.format( "parent does not point to a valid facility." ) )
+                .addConstraintViolation();
+
+            returnValue = false;
+        }
+
+        boolean validDataSets = validateDataSets( values );
+
+        if ( !validDataSets )
+        {
+            context.disableDefaultConstraintViolation();
+            context.buildConstraintViolationWithTemplate( String.format( "dataSets has one or more non-matching IDs." ) )
+                .addConstraintViolation();
+
+            returnValue = false;
+        }
+
+        // TODO should check for code also, but we don't know if its an update, or a creation.. split validator into create/update?
+
+        return returnValue;
+    }
+
+    private boolean validateParent( Map<String, Object> values )
+    {
+        String parentId = (String) values.get( "parent" );
+
+        if ( parentId != null )
+        {
+            OrganisationUnit organisationUnit = identifiableObjectManager.get( OrganisationUnit.class, parentId );
+
+            if ( organisationUnit == null )
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    private boolean validateDataSets( Map<String, Object> values )
+    {
+        Collection<String> dataSetIds = (Collection<String>) values.get( "dataSets" );
+
+        if ( dataSetIds != null )
+        {
+            for ( String dataSetId : dataSetIds )
+            {
+                DataSet dataSet = identifiableObjectManager.get( DataSet.class, dataSetId );
+
+                if ( dataSet == null )
+                {
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+}

=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/ValidProperties.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/ValidProperties.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/validation/constraint/annotation/ValidProperties.java	2012-12-09 16:50:22 +0000
@@ -0,0 +1,52 @@
+package org.hisp.dhis.web.webapi.v1.validation.constraint.annotation;
+
+/*
+ * Copyright (c) 2004-2012, 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 org.hisp.dhis.web.webapi.v1.validation.constraint.PropertiesValidator;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE } )
+@Retention( RetentionPolicy.RUNTIME )
+@Constraint( validatedBy = PropertiesValidator.class )
+public @interface ValidProperties
+{
+    String message() default "Properties did not validate.";
+
+    Class<?>[] groups() default { };
+
+    Class<? extends Payload>[] payload() default { };
+}