← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21995: added user var to save, update, delete methods in hibgenstor, allows to bypass expensive getCurrent...

 

------------------------------------------------------------
revno: 21995
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-02-18 14:59:49 +0700
message:
  added user var to save,update,delete methods in hibgenstor, allows to bypass expensive getCurrentUser/getUsername methods
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.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/common/GenericStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2016-02-18 07:59:49 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.user.User;
 
 import java.util.List;
 
@@ -52,6 +53,15 @@
     int save( T object );
 
     /**
+     * Saves the given object instance, with clear sharing set to true.
+     *
+     * @param object the object instance.
+     * @param user   User
+     * @return the generated identifier.
+     */
+    int save( T object, User user );
+
+    /**
      * Saves the given object instance.
      *
      * @param object       the object instance.
@@ -61,6 +71,16 @@
     int save( T object, boolean clearSharing );
 
     /**
+     * Saves the given object instance.
+     *
+     * @param object       the object instance.
+     * @param user         User
+     * @param clearSharing Should we clear all sharing related properties?
+     * @return the generated identifier.
+     */
+    int save( T object, User user, boolean clearSharing );
+
+    /**
      * Updates the given object instance.
      *
      * @param object the object instance.
@@ -68,6 +88,29 @@
     void update( T object );
 
     /**
+     * Updates the given object instance.
+     *
+     * @param object the object instance.
+     * @param user   User
+     */
+    void update( T object, User user );
+
+    /**
+     * Removes the given object instance.
+     *
+     * @param object the object instance to delete.
+     */
+    void delete( T object );
+
+    /**
+     * Removes the given object instance.
+     *
+     * @param object the object instance to delete.
+     * @param user   User
+     */
+    void delete( T object, User user );
+
+    /**
      * Retrieves the object with the given identifier. This method will first
      * look in the current Session, then hit the database if not existing.
      *
@@ -108,13 +151,6 @@
     List<T> getAll( int first, int max );
 
     /**
-     * Removes the given object instance.
-     *
-     * @param object the object instance to delete.
-     */
-    void delete( T object );
-
-    /**
      * Gets the count of objects.
      *
      * @return the count of objects.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2016-02-17 09:17:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2016-02-18 07:59:49 +0000
@@ -31,6 +31,7 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.NameableObject.NameableProperty;
+import org.hisp.dhis.user.User;
 
 import java.util.Collection;
 import java.util.Date;
@@ -47,11 +48,23 @@
 
     void save( IdentifiableObject object );
 
+    void save( IdentifiableObject object, User user );
+
     void save( IdentifiableObject object, boolean clearSharing );
 
+    void save( IdentifiableObject object, User user, boolean clearSharing );
+
     void update( IdentifiableObject object );
 
-    void update( List<IdentifiableObject> object );
+    void update( IdentifiableObject object, User user );
+
+    void update( List<IdentifiableObject> objects );
+
+    void update( List<IdentifiableObject> objects, User user );
+
+    void delete( IdentifiableObject object );
+
+    void delete( IdentifiableObject object, User user );
 
     <T extends IdentifiableObject> T get( String uid );
 
@@ -111,8 +124,6 @@
 
     <T extends IdentifiableObject> Date getLastUpdated( Class<T> clazz );
 
-    void delete( IdentifiableObject object );
-
     <T extends IdentifiableObject> Set<Integer> convertToId( Class<T> clazz, Collection<String> uids );
 
     <T extends IdentifiableObject> Map<String, T> getIdMap( Class<T> clazz, IdentifiableProperty property );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2016-02-18 03:43:11 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2016-02-18 07:59:49 +0000
@@ -42,6 +42,8 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserCredentials;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
@@ -85,6 +87,9 @@
     @Autowired
     private OrganisationUnitService organisationUnitService;
 
+    @Autowired
+    private CurrentUserService currentUserService;
+
     private Map<Class<? extends IdentifiableObject>, GenericIdentifiableObjectStore<? extends IdentifiableObject>> identifiableObjectStoreMap;
 
     private Map<Class<? extends NameableObject>, GenericNameableObjectStore<? extends NameableObject>> nameableObjectStoreMap;
@@ -98,34 +103,58 @@
     @Override
     public void save( IdentifiableObject object )
     {
-        save( object, true );
+        save( object, currentUserService.getCurrentUser(), true );
+    }
+
+    @Override
+    public void save( IdentifiableObject object, User user )
+    {
+        save( object, user, true );
     }
 
     @Override
     public void save( IdentifiableObject object, boolean clearSharing )
     {
+        save( object, currentUserService.getCurrentUser(), clearSharing );
+    }
+
+    @Override
+    public void save( IdentifiableObject object, User user, boolean clearSharing )
+    {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( object.getClass() );
 
         if ( store != null )
         {
-            store.save( object, clearSharing );
+            store.save( object, user, clearSharing );
         }
     }
 
     @Override
     public void update( IdentifiableObject object )
     {
+        update( object, currentUserService.getCurrentUser() );
+    }
+
+    @Override
+    public void update( IdentifiableObject object, User user )
+    {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( object.getClass() );
 
         if ( store != null )
         {
-            store.update( object );
+            store.update( object, user );
         }
     }
 
     @Override
     public void update( List<IdentifiableObject> objects )
     {
+        update( objects, currentUserService.getCurrentUser() );
+    }
+
+    @Override
+    public void update( List<IdentifiableObject> objects, User user )
+    {
         if ( objects == null || objects.isEmpty() )
         {
             return;
@@ -133,18 +162,24 @@
 
         for ( IdentifiableObject object : objects )
         {
-            update( object );
+            update( object, user );
         }
     }
 
     @Override
     public void delete( IdentifiableObject object )
     {
+        delete( object, currentUserService.getCurrentUser() );
+    }
+
+    @Override
+    public void delete( IdentifiableObject object, User user )
+    {
         GenericIdentifiableObjectStore<IdentifiableObject> store = getIdentifiableObjectStore( object.getClass() );
 
         if ( store != null )
         {
-            store.delete( object );
+            store.delete( object, user );
         }
     }
 

=== 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-17 09:17:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-18 07:59:49 +0000
@@ -33,10 +33,8 @@
 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.dataelement.DataElementCategory;
-import org.hisp.dhis.dataelement.DataElementCategoryCombo;
-import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.preheat.InvalidObject;
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.preheat.PreheatParams;
@@ -71,6 +69,9 @@
     @Autowired
     private SessionFactory sessionFactory;
 
+    @Autowired
+    private IdentifiableObjectManager manager;
+
     @Override
     public ObjectBundle create( ObjectBundleParams params )
     {
@@ -223,7 +224,7 @@
 
         for ( IdentifiableObject object : persistedObjects )
         {
-            session.delete( object );
+            manager.delete( object, bundle.getUser() );
 
             if ( log.isDebugEnabled() )
             {

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2016-01-27 07:34:02 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2016-02-18 07:59:49 +0000
@@ -334,13 +334,25 @@
     @Override
     public int save( T object )
     {
-        return save( object, true );
+        return save( object, currentUserService.getCurrentUser(), true );
+    }
+
+    @Override
+    public int save( T object, User user )
+    {
+        return save( object, user, true );
     }
 
     @Override
     public int save( T object, boolean clearSharing )
     {
-        User currentUser = currentUserService.getCurrentUser();
+        return save( object, currentUserService.getCurrentUser(), clearSharing );
+    }
+
+    @Override
+    public int save( T object, User user, boolean clearSharing )
+    {
+        String username = user != null ? user.getUsername() : "system-process";
 
         if ( IdentifiableObject.class.isAssignableFrom( object.getClass() ) )
         {
@@ -359,37 +371,37 @@
 
             if ( identifiableObject.getUser() == null )
             {
-                identifiableObject.setUser( currentUser );
+                identifiableObject.setUser( user );
             }
         }
 
-        if ( !Interpretation.class.isAssignableFrom( clazz ) && currentUser != null && aclService.isShareable( clazz ) )
+        if ( !Interpretation.class.isAssignableFrom( clazz ) && user != null && aclService.isShareable( clazz ) )
         {
             BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
 
             if ( clearSharing )
             {
-                if ( aclService.canCreatePublic( currentUser, identifiableObject.getClass() ) )
+                if ( aclService.canCreatePublic( user, identifiableObject.getClass() ) )
                 {
                     if ( aclService.defaultPublic( identifiableObject.getClass() ) )
                     {
                         identifiableObject.setPublicAccess( AccessStringHelper.READ_WRITE );
                     }
                 }
-                else if ( aclService.canCreatePrivate( currentUser, identifiableObject.getClass() ) )
+                else if ( aclService.canCreatePrivate( user, identifiableObject.getClass() ) )
                 {
                     identifiableObject.setPublicAccess( AccessStringHelper.newInstance().build() );
                 }
             }
 
-            if ( !checkPublicAccess( currentUser, identifiableObject ) )
+            if ( !checkPublicAccess( user, identifiableObject ) )
             {
-                AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_CREATE_DENIED );
+                AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_CREATE_DENIED );
                 throw new CreateAccessDeniedException( object.toString() );
             }
         }
 
-        AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_CREATE );
+        AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_CREATE );
         return (Integer) sessionFactory.getCurrentSession().save( object );
     }
 
@@ -403,6 +415,14 @@
     @Override
     public void update( T object )
     {
+        update( object, currentUserService.getCurrentUser() );
+    }
+
+    @Override
+    public void update( T object, User user )
+    {
+        String username = user != null ? user.getUsername() : "system-process";
+
         if ( IdentifiableObject.class.isInstance( object ) )
         {
             BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
@@ -410,17 +430,17 @@
 
             if ( identifiableObject.getUser() == null )
             {
-                identifiableObject.setUser( currentUserService.getCurrentUser() );
+                identifiableObject.setUser( user );
             }
         }
 
         if ( !Interpretation.class.isAssignableFrom( clazz ) && !isUpdateAllowed( object ) )
         {
-            AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_UPDATE_DENIED );
+            AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_UPDATE_DENIED );
             throw new UpdateAccessDeniedException( object.toString() );
         }
 
-        AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_UPDATE );
+        AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_UPDATE );
 
         if ( object != null )
         {
@@ -429,6 +449,31 @@
     }
 
     @Override
+    public void delete( T object )
+    {
+        delete( object, currentUserService.getCurrentUser() );
+    }
+
+    @Override
+    public final void delete( T object, User user )
+    {
+        String username = user != null ? user.getUsername() : "system-process";
+
+        if ( !isDeleteAllowed( object ) )
+        {
+            AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_DELETE_DENIED );
+            throw new DeleteAccessDeniedException( object.toString() );
+        }
+
+        AuditLogUtil.infoWrapper( log, username, object, AuditLogUtil.ACTION_DELETE );
+
+        if ( object != null )
+        {
+            sessionFactory.getCurrentSession().delete( object );
+        }
+    }
+
+    @Override
     @SuppressWarnings( "unchecked" )
     public final T get( int id )
     {
@@ -466,23 +511,6 @@
     }
 
     @Override
-    public final void delete( T object )
-    {
-        if ( !isDeleteAllowed( object ) )
-        {
-            AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_DELETE_DENIED );
-            throw new DeleteAccessDeniedException( object.toString() );
-        }
-
-        AuditLogUtil.infoWrapper( log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_DELETE );
-
-        if ( object != null )
-        {
-            sessionFactory.getCurrentSession().delete( object );
-        }
-    }
-
-    @Override
     @SuppressWarnings( "unchecked" )
     public final List<T> getAll()
     {