← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14276: support programAttributes in enrollments (web-api)

 

------------------------------------------------------------
revno: 14276
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-03-18 14:20:03 +0100
message:
  support programAttributes in enrollments (web-api)
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/Enrollment.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.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/program/ProgramInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2014-03-18 13:20:03 +0000
@@ -28,12 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.view.DetailedView;
@@ -43,11 +42,11 @@
 import org.hisp.dhis.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.trackedentitycomment.TrackedEntityComment;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 /**
  * @author Abyot Asalefew
@@ -116,9 +115,9 @@
     /**
      * Updated the bi-directional associations between this program instance and
      * the given entity instance and program.
-     * 
+     *
      * @param entityInstance the entity instance to enroll.
-     * @param program the program to enroll the entity instance to.
+     * @param program        the program to enroll the entity instance to.
      */
     public void enrollTrackedEntityInstance( TrackedEntityInstance entityInstance, Program program )
     {
@@ -325,10 +324,10 @@
         this.status = status;
     }
 
-    @JsonProperty
+    @JsonProperty( "trackedEntityInstance" )
     @JsonSerialize( as = BaseIdentifiableObject.class )
     @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "trackedEntityInstance", namespace = DxfNamespaces.DXF_2_0 )
     public TrackedEntityInstance getEntityInstance()
     {
         return entityInstance;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2014-03-18 13:20:03 +0000
@@ -28,12 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
+import com.google.common.collect.Maps;
+import org.hisp.dhis.dxf2.events.trackedentity.Attribute;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService;
+import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.i18n.I18nManager;
@@ -42,9 +41,19 @@
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramService;
+import org.hisp.dhis.program.ProgramTrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.Assert;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -64,6 +73,12 @@
     private org.hisp.dhis.trackedentity.TrackedEntityInstanceService entityInstanceService;
 
     @Autowired
+    private TrackedEntityAttributeService trackedEntityAttributeService;
+
+    @Autowired
+    private TrackedEntityAttributeValueService trackedEntityAttributeValueService;
+
+    @Autowired
     private I18nManager i18nManager;
 
     // -------------------------------------------------------------------------
@@ -229,6 +244,9 @@
     @Override
     public ImportSummary addEnrollment( Enrollment enrollment )
     {
+        ImportSummary importSummary = new ImportSummary();
+        importSummary.setDataValueCount( null );
+
         org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance( enrollment.getTrackedEntityInstance() );
         TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance( entityInstance );
         Program program = getProgram( enrollment.getProgram() );
@@ -237,13 +255,26 @@
 
         if ( !enrollments.getEnrollments().isEmpty() )
         {
-            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance()
+            importSummary = new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance()
                 + " already have an active enrollment in program " + program.getUid() );
             importSummary.getImportCount().incrementIgnored();
 
             return importSummary;
         }
 
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        importConflicts.addAll( checkAttributes( enrollment ) );
+
+        importSummary.setConflicts( importConflicts );
+
+        if ( !importConflicts.isEmpty() )
+        {
+            importSummary.setStatus( ImportStatus.ERROR );
+            importSummary.getImportCount().incrementIgnored();
+
+            return importSummary;
+        }
+
         ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, program,
             enrollment.getDateOfEnrollment(), enrollment.getDateOfIncident(), entityInstance.getOrganisationUnit(),
             i18nManager.getI18nFormat() );
@@ -254,9 +285,10 @@
                 + " into program " + enrollment.getProgram() );
         }
 
-        ImportSummary importSummary = new ImportSummary( ImportStatus.SUCCESS );
+        updateAttributeValues( enrollment );
+        programInstanceService.updateProgramInstance( programInstance );
+
         importSummary.setReference( programInstance.getUid() );
-        importSummary.setDataValueCount( null );
         importSummary.getImportCount().incrementImported();
 
         return importSummary;
@@ -269,10 +301,12 @@
     @Override
     public ImportSummary updateEnrollment( Enrollment enrollment )
     {
+        ImportSummary importSummary = new ImportSummary();
+        importSummary.setDataValueCount( null );
+
         if ( enrollment == null || enrollment.getEnrollment() == null )
         {
-            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR,
-                "No enrollment or enrollment ID was supplied" );
+            importSummary = new ImportSummary( ImportStatus.ERROR, "No enrollment or enrollment ID was supplied" );
             importSummary.getImportCount().incrementIgnored();
 
             return importSummary;
@@ -282,7 +316,20 @@
 
         if ( programInstance == null )
         {
-            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, "Enrollment ID was not valid." );
+            importSummary = new ImportSummary( ImportStatus.ERROR, "Enrollment ID was not valid." );
+            importSummary.getImportCount().incrementIgnored();
+
+            return importSummary;
+        }
+
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+        importConflicts.addAll( checkAttributes( enrollment ) );
+
+        importSummary.setConflicts( importConflicts );
+
+        if ( !importConflicts.isEmpty() )
+        {
+            importSummary.setStatus( ImportStatus.ERROR );
             importSummary.getImportCount().incrementIgnored();
 
             return importSummary;
@@ -308,7 +355,7 @@
             }
             else
             {
-                ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR,
+                importSummary = new ImportSummary( ImportStatus.ERROR,
                     "Re-enrollment is not allowed, please create a new enrollment." );
                 importSummary.getImportCount().incrementIgnored();
 
@@ -316,11 +363,10 @@
             }
         }
 
+        updateAttributeValues( enrollment );
         programInstanceService.updateProgramInstance( programInstance );
 
-        ImportSummary importSummary = new ImportSummary( ImportStatus.SUCCESS );
         importSummary.setReference( enrollment.getEnrollment() );
-        importSummary.setDataValueCount( null );
         importSummary.getImportCount().incrementImported();
 
         return importSummary;
@@ -361,6 +407,100 @@
     // HELPERS
     // -------------------------------------------------------------------------
 
+    private List<ImportConflict> checkAttributes( Enrollment enrollment )
+    {
+        List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
+
+        Program program = getProgram( enrollment.getProgram() );
+        org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(
+            enrollment.getTrackedEntityInstance() );
+
+        Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
+        Map<String, String> attributeValueMap = Maps.newHashMap();
+
+        for ( ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getAttributes() )
+        {
+            // TODO this will be replaced with programTrackedEntityAttribute.isMandatory when added to the model
+            mandatoryMap.put( programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.getAttribute().isMandatory() );
+        }
+
+        for ( TrackedEntityAttributeValue value : trackedEntityInstance.getAttributeValues() )
+        {
+            // ignore attributes which do not belong to this program
+            if ( mandatoryMap.containsKey( value.getAttribute() ) )
+            {
+                attributeValueMap.put( value.getAttribute().getUid(), value.getValue() );
+            }
+        }
+
+        for ( Attribute attribute : enrollment.getAttributes() )
+        {
+            attributeValueMap.put( attribute.getAttribute(), attribute.getValue() );
+        }
+
+        for ( TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet() )
+        {
+            Boolean mandatory = mandatoryMap.get( trackedEntityAttribute );
+
+            if ( mandatory && !attributeValueMap.containsKey( trackedEntityAttribute.getUid() ) )
+            {
+                importConflicts.add( new ImportConflict( "Attribute.attribute", "Missing mandatory attribute " +
+                    trackedEntityAttribute.getUid() ) );
+                continue;
+            }
+
+            attributeValueMap.remove( trackedEntityAttribute.getUid() );
+        }
+
+        if ( !attributeValueMap.isEmpty() )
+        {
+            importConflicts.add( new ImportConflict( "Attribute.attribute", "Only program attributes is allowed for enrollment " +
+                attributeValueMap ) );
+        }
+
+        return importConflicts;
+    }
+
+    private void updateAttributeValues( Enrollment enrollment )
+    {
+        org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(
+            enrollment.getTrackedEntityInstance() );
+        Map<String, String> attributeValueMap = Maps.newHashMap();
+
+        for ( Attribute attribute : enrollment.getAttributes() )
+        {
+            attributeValueMap.put( attribute.getAttribute(), attribute.getValue() );
+        }
+
+        for ( TrackedEntityAttributeValue value : trackedEntityInstance.getAttributeValues() )
+        {
+            if ( attributeValueMap.containsKey( value.getAttribute().getUid() ) )
+            {
+                String newValue = attributeValueMap.get( value.getAttribute().getUid() );
+                value.setValue( newValue );
+
+                trackedEntityAttributeValueService.updateTrackedEntityAttributeValue( value );
+
+                attributeValueMap.remove( value.getAttribute().getUid() );
+            }
+        }
+
+        for ( String key : attributeValueMap.keySet() )
+        {
+            TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute( key );
+
+            if ( attribute != null )
+            {
+                TrackedEntityAttributeValue value = new TrackedEntityAttributeValue();
+                value.setValue( attributeValueMap.get( key ) );
+                value.setAttribute( attribute );
+
+                trackedEntityAttributeValueService.addTrackedEntityAttributeValue( value );
+                trackedEntityInstance.addAttributeValue( value );
+            }
+        }
+    }
+
     private List<Program> getProgramsWithRegistration()
     {
         List<Program> programs = new ArrayList<Program>();

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/Enrollment.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/Enrollment.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/Enrollment.java	2014-03-18 13:20:03 +0000
@@ -29,9 +29,12 @@
  */
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.dxf2.events.trackedentity.Attribute;
 
 import java.util.ArrayList;
@@ -135,6 +138,7 @@
     }
 
     @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public List<Attribute> getAttributes()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-18 10:05:39 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java	2014-03-18 13:20:03 +0000
@@ -174,7 +174,6 @@
         updateAttributeValues( trackedEntityInstance, entityInstance );
         trackedEntityInstanceService.updateTrackedEntityInstance( entityInstance );
 
-        importSummary.setStatus( ImportStatus.SUCCESS );
         importSummary.setReference( entityInstance.getUid() );
         importSummary.getImportCount().incrementImported();
 
@@ -217,6 +216,7 @@
         {
             importSummary.setStatus( ImportStatus.ERROR );
             importSummary.getImportCount().incrementIgnored();
+
             return importSummary;
         }
 
@@ -290,7 +290,7 @@
 
             if ( entityAttribute == null )
             {
-                importConflicts.add( new ImportConflict( "Attribute.type", "Invalid type "
+                importConflicts.add( new ImportConflict( "Attribute.attribute", "Invalid attribute "
                     + attribute.getAttribute() ) );
             }
         }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	2014-03-18 13:20:03 +0000
@@ -40,7 +40,7 @@
 @JacksonXmlRootElement( localName = "importSummary", namespace = DxfNamespaces.DXF_2_0 )
 public class ImportSummary
 {
-    private ImportStatus status;
+    private ImportStatus status = ImportStatus.SUCCESS;
 
     private String description;