← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22035: various user import fixes

 

------------------------------------------------------------
revno: 22035
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-02-24 15:58:29 +0700
message:
  various user import fixes
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserRoleSchemaDescriptor.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserSchemaDescriptor.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.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
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserRoleSchemaDescriptor.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserRoleSchemaDescriptor.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserRoleSchemaDescriptor.java	2016-02-24 08:58:29 +0000
@@ -29,10 +29,10 @@
  */
 
 import com.google.common.collect.Lists;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaDescriptor;
 import org.hisp.dhis.security.Authority;
 import org.hisp.dhis.security.AuthorityType;
-import org.hisp.dhis.schema.Schema;
-import org.hisp.dhis.schema.SchemaDescriptor;
 import org.hisp.dhis.user.UserAuthorityGroup;
 import org.springframework.stereotype.Component;
 
@@ -54,7 +54,7 @@
         Schema schema = new Schema( UserAuthorityGroup.class, SINGULAR, PLURAL );
         schema.setRelativeApiEndpoint( API_ENDPOINT );
         schema.setShareable( true );
-        schema.setOrder( 1330 );
+        schema.setOrder( 100 );
 
         schema.getAuthorities().add( new Authority( AuthorityType.CREATE_PUBLIC, Lists.newArrayList( "F_USERROLE_PUBLIC_ADD" ) ) );
         schema.getAuthorities().add( new Authority( AuthorityType.CREATE_PRIVATE, Lists.newArrayList( "F_USERROLE_PRIVATE_ADD" ) ) );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserSchemaDescriptor.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserSchemaDescriptor.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/descriptors/UserSchemaDescriptor.java	2016-02-24 08:58:29 +0000
@@ -53,7 +53,7 @@
     {
         Schema schema = new Schema( User.class, SINGULAR, PLURAL );
         schema.setRelativeApiEndpoint( API_ENDPOINT );
-        schema.setOrder( 1290 );
+        schema.setOrder( 101 );
 
         schema.getAuthorities().add( new Authority( AuthorityType.CREATE, Lists.newArrayList( "F_USER_ADD", "F_USER_ADD_WITHIN_MANAGED_GROUP" ) ) );
         schema.getAuthorities().add( new Authority( AuthorityType.DELETE, Lists.newArrayList( "F_USER_DELETE", "F_USER_DELETE_WITHIN_MANAGED_GROUP" ) ) );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-24 06:10:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-02-24 08:58:29 +0000
@@ -31,6 +31,7 @@
 import com.google.common.collect.Lists;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.query.Query;
 import org.hisp.dhis.query.QueryService;
 import org.hisp.dhis.query.Restrictions;
@@ -307,6 +308,11 @@
         schema.getProperties().stream()
             .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
             .forEach( p -> {
+                if ( skipCheckAndConnect( p.getKlass() ) || skipCheckAndConnect( p.getItemKlass() ) )
+                {
+                    return;
+                }
+
                 if ( !p.isCollection() )
                 {
                     IdentifiableObject refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
@@ -360,6 +366,11 @@
         schema.getProperties().stream()
             .filter( p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType()) )
             .forEach( p -> {
+                if ( skipCheckAndConnect( p.getKlass() ) || skipCheckAndConnect( p.getItemKlass() ) )
+                {
+                    return;
+                }
+
                 if ( !p.isCollection() )
                 {
                     T refObject = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
@@ -370,7 +381,14 @@
                         ref = (T) defaults.get( refObject.getClass() );
                     }
 
-                    ReflectionUtils.invokeMethod( object, p.getSetterMethod(), ref );
+                    if ( ref != null && ref.getId() == 0 )
+                    {
+                        ReflectionUtils.invokeMethod( object, p.getSetterMethod(), (Object) null );
+                    }
+                    else
+                    {
+                        ReflectionUtils.invokeMethod( object, p.getSetterMethod(), ref );
+                    }
                 }
                 else
                 {
@@ -386,7 +404,7 @@
                             ref = (T) defaults.get( refObject.getClass() );
                         }
 
-                        if ( ref != null ) objects.add( ref );
+                        if ( ref != null && ref.getId() != 0 ) objects.add( ref );
                     }
 
                     ReflectionUtils.invokeMethod( object, p.getSetterMethod(), objects );
@@ -408,4 +426,9 @@
 
         return userCredentialsMap;
     }
+
+    private boolean skipCheckAndConnect( Class<?> klass )
+    {
+        return klass != null && (UserCredentials.class.isAssignableFrom( klass ) || DataElementOperand.class.isAssignableFrom( klass ));
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-19 02:27:16 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-24 08:58:29 +0000
@@ -30,11 +30,11 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableObjectUtils;
+import org.hisp.dhis.dxf2.metadata2.objectbundle.hooks.ObjectBundleHook;
 import org.hisp.dhis.preheat.InvalidObject;
 import org.hisp.dhis.preheat.Preheat;
 import org.hisp.dhis.preheat.PreheatIdentifier;
@@ -81,6 +81,9 @@
     @Autowired
     private IdentifiableObjectManager manager;
 
+    @Autowired( required = false )
+    private List<ObjectBundleHook> objectBundleHooks = new ArrayList<>();
+
     @Override
     public ObjectBundle create( ObjectBundleParams params )
     {
@@ -208,8 +211,6 @@
             return; // skip if validate only
         }
 
-        Session session = sessionFactory.getCurrentSession();
-
         List<Class<? extends IdentifiableObject>> klasses = getSortedClasses( bundle );
 
         for ( Class<? extends IdentifiableObject> klass : klasses )
@@ -226,49 +227,55 @@
                 case CREATE_AND_UPDATE:
                 case NEW_AND_UPDATES:
                 {
-                    handleCreatesAndUpdates( session, objects, bundle );
+                    handleCreatesAndUpdates( objects, bundle );
                     break;
                 }
                 case CREATE:
                 case NEW:
                 {
-                    handleCreates( session, objects, bundle );
+                    handleCreates( objects, bundle );
                     break;
                 }
                 case UPDATE:
                 case UPDATES:
                 {
-                    handleUpdates( session, objects, bundle );
+                    handleUpdates( objects, bundle );
                     break;
                 }
                 case DELETE:
                 case DELETES:
                 {
-                    handleDeletes( session, objects, bundle );
+                    handleDeletes( objects, bundle );
                     break;
                 }
             }
+
+            sessionFactory.getCurrentSession().flush();
         }
 
         bundle.setObjectBundleStatus( ObjectBundleStatus.COMMITTED );
     }
 
-    private void handleCreatesAndUpdates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    private void handleCreatesAndUpdates( List<IdentifiableObject> objects, ObjectBundle bundle )
     {
 
     }
 
-    private void handleCreates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    private void handleCreates( List<IdentifiableObject> objects, ObjectBundle bundle )
     {
         for ( IdentifiableObject object : objects )
         {
             if ( Preheat.isDefault( object ) ) continue;
 
+            objectBundleHooks.forEach( hook -> hook.preCreate( object, bundle ) );
+
             preheatService.connectReferences( object, bundle.getPreheat(), bundle.getPreheatIdentifier() );
             manager.save( object, bundle.getUser() );
 
             bundle.getPreheat().put( bundle.getPreheatIdentifier(), object );
 
+            objectBundleHooks.forEach( hook -> hook.postCreate( object, bundle ) );
+
             if ( log.isDebugEnabled() )
             {
                 String msg = "Created object '" + IdentifiableObjectUtils.getDisplayName( object ) + "'";
@@ -277,17 +284,18 @@
         }
     }
 
-    private void handleUpdates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    private void handleUpdates( List<IdentifiableObject> objects, ObjectBundle bundle )
     {
 
     }
 
-    private void handleDeletes( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    private void handleDeletes( List<IdentifiableObject> objects, ObjectBundle bundle )
     {
         List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll( bundle.getPreheatIdentifier(), objects );
 
         for ( IdentifiableObject object : persistedObjects )
         {
+            objectBundleHooks.forEach( hook -> hook.preDelete( object, bundle ) );
             manager.delete( object, bundle.getUser() );
 
             bundle.getPreheat().remove( bundle.getPreheatIdentifier(), object );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java	2016-02-24 06:10:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/AbstractObjectBundleHook.java	2016-02-24 08:58:29 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hibernate.SessionFactory;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
@@ -49,33 +50,31 @@
     @Autowired
     protected SchemaValidator validator;
 
+    @Autowired
+    protected SessionFactory sessionFactory;
+
     @Override
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
     }
 
     @Override
+    public void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+    }
+
+    @Override
     public void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
     }
 
     @Override
+    public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
+    {
+    }
+
+    @Override
     public void preDelete( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
     }
-
-    @Override
-    public void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
-    {
-    }
-
-    @Override
-    public void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
-    {
-    }
-
-    @Override
-    public void postDelete( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
-    {
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java	2016-02-23 10:32:50 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/ObjectBundleHook.java	2016-02-24 08:58:29 +0000
@@ -38,13 +38,11 @@
 {
     void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
 
+    void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
+
     void preUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
 
+    void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
+
     void preDelete( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
-
-    void postCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
-
-    void postUpdate( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
-
-    void postDelete( IdentifiableObject identifiableObject, ObjectBundle objectBundle );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-24 06:10:49 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/hooks/UserObjectBundleHook.java	2016-02-24 08:58:29 +0000
@@ -32,7 +32,10 @@
 import org.hisp.dhis.dxf2.metadata2.objectbundle.ObjectBundle;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserCredentials;
+import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -40,6 +43,9 @@
 @Component
 public class UserObjectBundleHook extends AbstractObjectBundleHook
 {
+    @Autowired
+    private UserService userService;
+
     @Override
     public void preCreate( IdentifiableObject identifiableObject, ObjectBundle objectBundle )
     {
@@ -51,6 +57,16 @@
         User user = (User) identifiableObject;
         UserCredentials userCredentials = user.getUserCredentials();
 
+        if ( objectBundle.getPreheat().getUsernames().containsKey( userCredentials.getUsername() ) )
+        {
+            // Username exists, throw validation error
+        }
+
+        if ( !StringUtils.isEmpty( userCredentials.getPassword() ) )
+        {
+            userService.encodeAndSetPassword( userCredentials, userCredentials.getPassword() );
+        }
+
         preheatService.connectReferences( userCredentials, objectBundle.getPreheat(), objectBundle.getPreheatIdentifier() );
         manager.save( userCredentials );
 

=== 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-02-23 10:38:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-24 08:58:29 +0000
@@ -47,6 +47,7 @@
 import org.hisp.dhis.render.RenderFormat;
 import org.hisp.dhis.render.RenderService;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserAuthorityGroup;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
@@ -442,12 +443,14 @@
         List<OrganisationUnit> organisationUnits = manager.getAll( OrganisationUnit.class );
         List<DataElement> dataElements = manager.getAll( DataElement.class );
         List<DataSet> dataSets = manager.getAll( DataSet.class );
+        List<UserAuthorityGroup> userRoles = manager.getAll( UserAuthorityGroup.class );
         List<User> users = manager.getAll( User.class );
 
         assertFalse( organisationUnits.isEmpty() );
         assertFalse( dataElements.isEmpty() );
         assertFalse( dataSets.isEmpty() );
         assertFalse( users.isEmpty() );
+        assertFalse( userRoles.isEmpty() );
 
         Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaults = manager.getDefaults();
 
@@ -465,7 +468,10 @@
         assertEquals( 1, dataSet.getSources().size() );
         assertEquals( 2, dataSet.getDataElements().size() );
         assertEquals( PeriodType.getPeriodTypeByName( "Monthly" ), dataSet.getPeriodType() );
-        // assertNotNull( user.getUserCredentials() );
+
+        assertNotNull( user.getUserCredentials() );
+        assertEquals( "admin", user.getUserCredentials().getUsername() );
+        assertFalse( user.getUserCredentials().getUserAuthorityGroups().isEmpty() );
     }
 
     private void defaultSetup()