← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22205: support compulsory data elements in data sets for new importer

 

------------------------------------------------------------
revno: 22205
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-03-09 17:26:29 +0700
message:
  support compulsory data elements in data sets for new importer
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/DataSetObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_compulsory.json
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.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
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/DataSetObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/DataSetObjectBundleHook.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/DataSetObjectBundleHook.java	2016-03-09 10:26:29 +0000
@@ -0,0 +1,90 @@
+package org.hisp.dhis.dxf2.metadata2.objectbundle.hooks;
+
+/*
+ * Copyright (c) 2004-2016, 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.IdentifiableObject;
+import org.hisp.dhis.dataelement.DataElementOperand;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.dataset.Section;
+import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@Component
+public class DataSetObjectBundleHook extends AbstractObjectBundleHook
+{
+    @Override
+    public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+        if ( !DataSet.class.isInstance( identifiableObject ) ) return;
+        DataSet dataSet = (DataSet) identifiableObject;
+
+        for ( DataElementOperand dataElementOperand : dataSet.getCompulsoryDataElementOperands() )
+        {
+            preheatService.connectReferences( dataElementOperand, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+            sessionFactory.getCurrentSession().save( dataElementOperand );
+        }
+    }
+
+    @Override
+    public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+        if ( !DataSet.class.isInstance( identifiableObject ) ) return;
+        DataSet dataSet = (DataSet) identifiableObject;
+        dataSet.getCompulsoryDataElementOperands().clear();
+    }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+        if ( !DataSet.class.isInstance( identifiableObject ) ) return;
+        DataSet dataSet = (DataSet) identifiableObject;
+
+        Map<String, Object> references = objectBundle.getObjectReferences( Section.class ).get( dataSet.getUid() );
+        if ( references == null ) return;
+
+        Set<DataElementOperand> dataElementOperands = (Set<DataElementOperand>) references.get( "compulsoryDataElementOperands" );
+        if ( dataElementOperands == null || dataElementOperands.isEmpty() ) return;
+
+        for ( DataElementOperand dataElementOperand : dataElementOperands )
+        {
+            preheatService.connectReferences( dataElementOperand, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
+            sessionFactory.getCurrentSession().save( dataElementOperand );
+            dataSet.getCompulsoryDataElementOperands().add( dataElementOperand );
+        }
+
+        sessionFactory.getCurrentSession().update( dataSet );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java	2016-03-09 07:55:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/SectionObjectBundleHook.java	2016-03-09 10:26:29 +0000
@@ -47,7 +47,6 @@
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
         if ( !Section.class.isInstance( identifiableObject ) ) return;
-
         Section section = (Section) identifiableObject;
 
         for ( DataElementOperand dataElementOperand : section.getGreyedFields() )
@@ -61,7 +60,6 @@
     public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
         if ( !Section.class.isInstance( identifiableObject ) ) return;
-
         Section section = (Section) identifiableObject;
         section.getGreyedFields().clear();
     }
@@ -71,10 +69,13 @@
     public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
         if ( !Section.class.isInstance( identifiableObject ) ) return;
+        Section section = (Section) identifiableObject;
 
-        Section section = (Section) identifiableObject;
         Map<String, Object> references = objectBundle.getObjectReferences( Section.class ).get( section.getUid() );
+        if ( references == null ) return;
+
         Set<DataElementOperand> dataElementOperands = (Set<DataElementOperand>) references.get( "greyedFields" );
+        if ( dataElementOperands == null || dataElementOperands.isEmpty() ) return;
 
         for ( DataElementOperand dataElementOperand : dataElementOperands )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-03-09 08:02:01 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-03-09 10:26:29 +0000
@@ -927,6 +927,45 @@
         assertNotNull( section2.getDataSet() );
     }
 
+    @Test
+    public void testCreateDataSetWithCompulsoryDataElements() throws IOException
+    {
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/dataset_with_compulsory.json" ).getInputStream(), RenderFormat.JSON );
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.CREATE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        ObjectBundleValidation validate = objectBundleService.validate( bundle );
+        assertTrue( validate.getObjectErrorReportsMap().isEmpty() );
+
+        objectBundleService.commit( bundle );
+
+        List<DataSet> dataSets = manager.getAll( DataSet.class );
+        List<OrganisationUnit> organisationUnits = manager.getAll( OrganisationUnit.class );
+        List<DataElement> dataElements = manager.getAll( DataElement.class );
+        List<UserAuthorityGroup> userRoles = manager.getAll( UserAuthorityGroup.class );
+        List<User> users = manager.getAll( User.class );
+        List<DataElementOperand> dataElementOperands = manager.getAll( DataElementOperand.class );
+
+        assertFalse( organisationUnits.isEmpty() );
+        assertFalse( dataElements.isEmpty() );
+        assertFalse( users.isEmpty() );
+        assertFalse( userRoles.isEmpty() );
+
+        assertEquals( 1, dataSets.size() );
+        assertEquals( 1, dataElementOperands.size() );
+
+        DataSet dataSet = dataSets.get( 0 );
+        assertEquals( "DataSetA", dataSet.getName() );
+        assertTrue( dataSet.getSections().isEmpty() );
+        assertNotNull( dataSet.getUser() );
+        assertEquals( 1, dataSet.getCompulsoryDataElementOperands().size() );
+    }
+
     private void defaultSetup()
     {
         DataElement de1 = createDataElement( 'A' );

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_compulsory.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_compulsory.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/dataset_with_compulsory.json	2016-03-09 10:26:29 +0000
@@ -0,0 +1,264 @@
+{
+  "categories": [
+    {
+      "lastUpdated": "2016-03-09T09:29:28.448+0000",
+      "dataDimensionType": "DISAGGREGATION",
+      "categoryOptions": [
+        {
+          "id": "SOhvMNg1Vos"
+        }
+      ],
+      "publicAccess": "--------",
+      "id": "uj2BOmh1Yv1",
+      "dataDimension": true,
+      "userGroupAccesses": [ ],
+      "created": "2016-03-09T09:29:28.288+0000",
+      "name": "default"
+    }
+  ],
+  "userRoles": [
+    {
+      "name": "Superuser",
+      "dataSets": [ ],
+      "userGroupAccesses": [ ],
+      "created": "2016-03-09T09:41:06.966+0000",
+      "authorities": [
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS",
+        "ALL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_ADD",
+        "F_REPORTTABLE_PUBLIC_ADD",
+        "F_TRACKED_ENTITY_INSTANCE_DELETE",
+        "F_USER_GROUPS_READ_ONLY_ADD_MEMBERS",
+        "F_MAP_PUBLIC_ADD",
+        "F_USER_ADD_WITHIN_MANAGED_GROUP",
+        "F_TRACKED_ENTITY_INSTANCE_SEARCH",
+        "F_PROGRAM_ENROLLMENT",
+        "F_REPORTTABLE_EXTERNAL",
+        "F_SQLVIEW_EXTERNAL",
+        "F_GIS_ADMIN",
+        "F_REPLICATE_USER",
+        "F_INSERT_CUSTOM_JS_CSS",
+        "F_DASHBOARD_PUBLIC_ADD",
+        "F_METADATA_IMPORT",
+        "F_CHART_PUBLIC_ADD",
+        "F_VIEW_UNAPPROVED_DATA",
+        "F_CHART_EXTERNAL",
+        "F_USERGROUP_MANAGING_RELATIONSHIPS_VIEW",
+        "F_METADATA_EXPORT",
+        "F_PROGRAM_UNENROLLMENT",
+        "F_APPROVE_DATA",
+        "F_ACCEPT_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_INSTANCE_ADD",
+        "F_USERGROUP_PUBLIC_ADD",
+        "F_OAUTH2_CLIENT_MANAGE",
+        "F_TRACKED_ENTITY_DATAVALUE_ADD",
+        "F_PROGRAM_DASHBOARD_CONFIG_ADMIN",
+        "F_MAP_EXTERNAL",
+        "F_APPROVE_DATA_LOWER_LEVELS",
+        "F_TRACKED_ENTITY_DATAVALUE_DELETE"
+      ],
+      "id": "WpMYCIt4tHF",
+      "publicAccess": "--------",
+      "programs": [ ],
+      "lastUpdated": "2016-03-09T09:41:06.966+0000"
+    }
+  ],
+  "dataElements": [
+    {
+      "lastUpdated": "2016-03-09T09:58:46.015+0000",
+      "code": "DataElementCodeA",
+      "domainType": "AGGREGATE",
+      "valueType": "TEXT",
+      "id": "lQBOv8uoqn0",
+      "created": "2016-03-09T09:58:46.012+0000",
+      "shortName": "DataElementShortA",
+      "zeroIsSignificant": false,
+      "categoryCombo": {
+        "id": "WoOynCnjOQs"
+      },
+      "attributeValues": [ ],
+      "name": "DataElementA",
+      "publicAccess": "rw------",
+      "aggregationLevels": [ ],
+      "aggregationType": "SUM",
+      "userGroupAccesses": [ ],
+      "user": {
+        "id": "HEh4ijSTjeV"
+      }
+    }
+  ],
+  "categoryOptions": [
+    {
+      "lastUpdated": "2016-03-09T09:29:28.295+0000",
+      "id": "SOhvMNg1Vos",
+      "publicAccess": "--------",
+      "userGroupAccesses": [ ],
+      "organisationUnits": [ ],
+      "shortName": "default",
+      "created": "2016-03-09T09:29:28.278+0000",
+      "name": "default",
+      "attributeValues": [ ]
+    }
+  ],
+  "categoryCombos": [
+    {
+      "created": "2016-03-09T09:29:28.290+0000",
+      "userGroupAccesses": [ ],
+      "name": "default",
+      "lastUpdated": "2016-03-09T09:29:28.293+0000",
+      "skipTotal": false,
+      "categories": [
+        {
+          "id": "uj2BOmh1Yv1"
+        }
+      ],
+      "publicAccess": "--------",
+      "id": "WoOynCnjOQs",
+      "dataDimensionType": "DISAGGREGATION"
+    }
+  ],
+  "date": "2016-03-09T10:02:22.665+0000",
+  "users": [
+    {
+      "surname": "admin",
+      "id": "HEh4ijSTjeV",
+      "teiSearchOrganisationUnits": [ ],
+      "lastUpdated": "2016-03-09T09:41:06.953+0000",
+      "userCredentials": {
+        "externalAuth": false,
+        "created": "2016-03-09T09:41:07.157+0000",
+        "passwordLastUpdated": "2016-03-09T09:41:07.022+0000",
+        "selfRegistered": false,
+        "disabled": false,
+        "username": "admin",
+        "userInfo": {
+          "id": "HEh4ijSTjeV"
+        },
+        "cogsDimensionConstraints": [ ],
+        "lastLogin": "2016-03-09T09:41:07.011+0000",
+        "userRoles": [
+          {
+            "id": "WpMYCIt4tHF"
+          }
+        ],
+        "catDimensionConstraints": [ ],
+        "invitation": false
+      },
+      "attributeValues": [ ],
+      "dataViewOrganisationUnits": [ ],
+      "organisationUnits": [
+        {
+          "id": "bpPIb53g1yR"
+        }
+      ],
+      "firstName": "admin",
+      "created": "2016-03-09T09:41:06.953+0000"
+    }
+  ],
+  "trackedEntities": [
+    {
+      "id": "GVSfhRyCaiZ",
+      "name": "Person",
+      "description": "Person",
+      "attributeValues": [ ]
+    }
+  ],
+  "organisationUnitLevels": [
+    {
+      "level": 1,
+      "name": "Level 1",
+      "id": "dESafvpKeu7",
+      "created": "2016-03-09T09:56:10.596+0000",
+      "lastUpdated": "2016-03-09T09:56:10.597+0000"
+    }
+  ],
+  "organisationUnits": [
+    {
+      "shortName": "Country",
+      "created": "2016-03-09T09:56:07.050+0000",
+      "openingDate": "2016-03-09",
+      "attributeValues": [ ],
+      "name": "Country",
+      "user": {
+        "id": "HEh4ijSTjeV"
+      },
+      "lastUpdated": "2016-03-09T09:56:07.064+0000",
+      "uuid": "36686c76-6fe4-4fda-9cce-d588acf0aa0a",
+      "path": "/bpPIb53g1yR",
+      "featureType": "NONE",
+      "description": "",
+      "id": "bpPIb53g1yR"
+    }
+  ],
+  "categoryOptionCombos": [
+    {
+      "categoryCombo": {
+        "id": "WoOynCnjOQs"
+      },
+      "ignoreApproval": false,
+      "lastUpdated": "2016-03-09T09:29:28.291+0000",
+      "created": "2016-03-09T09:29:28.291+0000",
+      "id": "vdJtj6OXOTQ",
+      "name": "default",
+      "categoryOptions": [
+        {
+          "id": "SOhvMNg1Vos"
+        }
+      ]
+    }
+  ],
+  "dataSets": [
+    {
+      "user": {
+        "id": "HEh4ijSTjeV"
+      },
+      "indicators": [ ],
+      "userGroupAccesses": [ ],
+      "periodType": "Monthly",
+      "version": 1,
+      "noValueRequiresComment": false,
+      "validCompleteOnly": false,
+      "publicAccess": "rw------",
+      "mobile": false,
+      "dataElementDecoration": false,
+      "openFuturePeriods": 0,
+      "timelyDays": 15,
+      "name": "DataSetA",
+      "attributeValues": [ ],
+      "renderAsTabs": false,
+      "notifyCompletingUser": false,
+      "categoryCombo": {
+        "id": "WoOynCnjOQs"
+      },
+      "skipOffline": false,
+      "expiryDays": 0,
+      "organisationUnits": [
+        {
+          "id": "bpPIb53g1yR"
+        }
+      ],
+      "created": "2016-03-09T09:59:13.107+0000",
+      "shortName": "DataSetShortA",
+      "id": "couZzkKqTjV",
+      "compulsoryDataElementOperands": [
+        {
+          "categoryOptionCombo": {
+            "id": "vdJtj6OXOTQ"
+          },
+          "dataElement": {
+            "id": "lQBOv8uoqn0"
+          }
+        }
+      ],
+      "dataElements": [
+        {
+          "id": "lQBOv8uoqn0"
+        }
+      ],
+      "code": "DataSetCodeA",
+      "fieldCombinationRequired": false,
+      "renderHorizontally": false,
+      "lastUpdated": "2016-03-09T09:59:25.699+0000"
+    }
+  ]
+}