← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22082: fixes to UGA imports for dxf2 importer

 

------------------------------------------------------------
revno: 22082
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2016-03-01 16:01:25 +0700
message:
  fixes to UGA imports for dxf2 importer
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupAccess.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.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/user/UserGroupAccess.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupAccess.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupAccess.java	2016-03-01 09:01:25 +0000
@@ -42,10 +42,12 @@
 {
     private int id;
 
-    private String access;
-
     private UserGroup userGroup;
 
+    private transient String uid;
+
+    private transient String access;
+
     public UserGroupAccess()
     {
     }
@@ -75,7 +77,7 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
-    public String userGroupUid()
+    public String getUserGroupUid()
     {
         return userGroup != null ? userGroup.getUid() : null;
     }
@@ -84,7 +86,12 @@
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getUid()
     {
-        return userGroup != null ? userGroup.getUid() : null;
+        return uid != null ? uid : (userGroup != null ? userGroup.getUid() : null);
+    }
+
+    public void setUid( String uid )
+    {
+        this.uid = uid;
     }
 
     @JsonProperty
@@ -96,6 +103,13 @@
 
     public UserGroup getUserGroup()
     {
+        if ( userGroup == null )
+        {
+            UserGroup userGroup = new UserGroup();
+            userGroup.setUid( uid );
+            return userGroup;
+        }
+
         return userGroup;
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2016-03-01 09:01:25 +0000
@@ -387,7 +387,7 @@
         {
             return objects.iterator().next();
         }
-        else
+        else if ( !objects.isEmpty() )
         {
             String objectName;
 

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2016-02-25 20:15:42 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2016-03-01 09:01:25 +0000
@@ -57,7 +57,6 @@
 import org.hisp.dhis.dxf2.metadata.ObjectBridge;
 import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandler;
 import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandlerUtils;
-import org.hisp.dhis.schema.validation.SchemaValidator;
 import org.hisp.dhis.eventchart.EventChart;
 import org.hisp.dhis.eventreport.EventReport;
 import org.hisp.dhis.expression.Expression;
@@ -70,6 +69,8 @@
 import org.hisp.dhis.program.ProgramValidation;
 import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
+import org.hisp.dhis.schema.validation.SchemaValidator;
+import org.hisp.dhis.schema.validation.ValidationViolation;
 import org.hisp.dhis.security.acl.AclService;
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.hisp.dhis.trackedentity.TrackedEntity;
@@ -77,9 +78,11 @@
 import org.hisp.dhis.translation.Translation;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserCredentials;
+import org.hisp.dhis.user.UserGroup;
+import org.hisp.dhis.user.UserGroupAccess;
+import org.hisp.dhis.user.UserGroupAccessService;
 import org.hisp.dhis.user.UserService;
 import org.hisp.dhis.validation.ValidationRule;
-import org.hisp.dhis.schema.validation.ValidationViolation;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.lang.reflect.Field;
@@ -138,6 +141,9 @@
     @Autowired
     private UserService userService;
 
+    @Autowired
+    private UserGroupAccessService userGroupAccessService;
+
     @Autowired( required = false )
     private List<ObjectHandler<T>> objectHandlers;
 
@@ -939,7 +945,8 @@
     // keeping this internal for now, might be split into several classes
     private class NonIdentifiableObjects
     {
-        private Set<AttributeValue> attributeValues = Sets.newHashSet();
+        private Set<AttributeValue> attributeValues = new HashSet<>();
+        private Set<UserGroupAccess> userGroupAccesses = new HashSet<>();
 
         private Expression leftSide;
         private Expression rightSide;
@@ -989,6 +996,7 @@
         {
             schema = schemaService.getDynamicSchema( object.getClass() );
             attributeValues = extractAttributeValues( object );
+            userGroupAccesses = extractUserGroupAccesses( object );
             leftSide = extractExpression( object, "leftSide" );
             rightSide = extractExpression( object, "rightSide" );
             compulsoryDataElementOperands = Sets.newHashSet( extractDataElementOperands( object, "compulsoryDataElementOperands" ) );
@@ -1021,6 +1029,7 @@
             schema = schemaService.getDynamicSchema( object.getClass() );
 
             saveAttributeValues( object, attributeValues );
+            saveUserGroupAccess( object, userGroupAccesses );
             saveExpression( object, "leftSide", leftSide );
             saveExpression( object, "rightSide", rightSide );
             saveDataElementOperands( object, "compulsoryDataElementOperands", compulsoryDataElementOperands );
@@ -1163,6 +1172,19 @@
             return attributeValues;
         }
 
+        private Set<UserGroupAccess> extractUserGroupAccesses( T object )
+        {
+            Set<UserGroupAccess> userGroupAccesses = new HashSet<>();
+
+            if ( schema.havePersistedProperty( "userGroupAccesses" ) )
+            {
+                userGroupAccesses = ReflectionUtils.invokeGetterMethod( "userGroupAccesses", object );
+                ReflectionUtils.invokeSetterMethod( "userGroupAccesses", object, new HashSet<>() );
+            }
+
+            return userGroupAccesses;
+        }
+
         private void saveAttributeValues( T object, Collection<AttributeValue> attributeValues )
         {
             for ( AttributeValue attributeValue : attributeValues )
@@ -1188,6 +1210,24 @@
             }
         }
 
+        private void saveUserGroupAccess( T object, Set<UserGroupAccess> userGroupAccesses )
+        {
+            for ( UserGroupAccess uga : userGroupAccesses )
+            {
+                UserGroup userGroup = objectBridge.getObject( uga.getUserGroup() );
+
+                if ( userGroup == null )
+                {
+                    log.info( "Unknown reference to " + uga.getUserGroup() + " on object " + uga );
+                    return;
+                }
+
+                uga.setUserGroup( userGroup );
+                userGroupAccessService.addUserGroupAccess( uga );
+                object.getUserGroupAccesses().add( uga );
+            }
+        }
+
         private void deleteExpression( T object, String fieldName )
         {
             Expression expression = extractExpression( object, fieldName );