← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18460: more optimisations for event import, also sets scope of EventController to prototype, to make sur...

 

------------------------------------------------------------
revno: 18460
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-03-03 12:06:59 +0700
message:
  more optimisations for event import, also sets scope of EventController to prototype, to make sure it gets a fresh EventService impl for use
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.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/ProgramService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java	2015-02-13 08:50:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java	2015-03-03 05:06:59 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.trackedentity.TrackedEntity;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.validation.ValidationCriteria;
 
 import java.util.Collection;
@@ -106,6 +107,13 @@
     Collection<Program> getProgramsByCurrentUser();
 
     /**
+     * Get {@link Program} by user.
+     *
+     * @return The program list the current user
+     */
+    Collection<Program> getProgramsByUser( User user );
+
+    /**
      * Get {@link Program} by the current user and a certain type
      *
      * @param type The type of program. There are three types, include Multi

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-03-03 03:40:50 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-03-03 05:06:59 +0000
@@ -70,6 +70,7 @@
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueService;
 import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
@@ -158,6 +159,8 @@
 
     private Map<String, DataElement> dataElementCache = new HashMap<>();
 
+    private List<Program> accessiblePrograms = new ArrayList<>();
+
     // -------------------------------------------------------------------------
     // CREATE
     // -------------------------------------------------------------------------
@@ -168,9 +171,11 @@
         ImportSummaries importSummaries = new ImportSummaries();
         int counter = 0;
 
+        User user = currentUserService.getCurrentUser();
+
         for ( Event event : events )
         {
-            importSummaries.addImportSummary( addEvent( event, importOptions ) );
+            importSummaries.addImportSummary( addEvent( event, user, importOptions ) );
 
             if ( counter % FLUSH_FREQUENCY == 0 )
             {
@@ -218,6 +223,11 @@
     @Override
     public ImportSummary addEvent( Event event, ImportOptions importOptions )
     {
+        return addEvent( event, currentUserService.getCurrentUser(), importOptions );
+    }
+
+    protected ImportSummary addEvent( Event event, User user, ImportOptions importOptions )
+    {
         Program program = getProgram( event.getProgram() );
         ProgramStage programStage = getProgramStage( event.getProgramStage() );
 
@@ -247,7 +257,7 @@
         Assert.notNull( program );
         Assert.notNull( programStage );
 
-        if ( verifyProgramAccess( program ) )
+        if ( verifyProgramAccess( program, user ) )
         {
             return new ImportSummary( ImportStatus.ERROR,
                 "Current user does not have permission to access this program" );
@@ -372,7 +382,8 @@
             return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit" );
         }
 
-        return saveEvent( program, programInstance, programStage, programStageInstance, organisationUnit, event, importOptions );
+        return saveEvent( program, programInstance, programStage, programStageInstance, organisationUnit, event,
+            user, importOptions );
     }
 
     // -------------------------------------------------------------------------
@@ -475,7 +486,7 @@
             dueDate = DateUtils.parseDate( event.getDueDate() );
         }
 
-        String storedBy = getStoredBy( event, null );
+        String storedBy = getStoredBy( event, null, currentUserService.getCurrentUsername() );
 
         if ( event.getStatus() == EventStatus.ACTIVE )
         {
@@ -572,7 +583,7 @@
             return;
         }
 
-        saveTrackedEntityComment( programStageInstance, event, getStoredBy( event, null ) );
+        saveTrackedEntityComment( programStageInstance, event, getStoredBy( event, null, currentUserService.getCurrentUsername() ) );
     }
 
     @Override
@@ -743,10 +754,14 @@
         return !assignedToOrganisationUnit;
     }
 
-    private boolean verifyProgramAccess( Program program )
+    private boolean verifyProgramAccess( Program program, User user )
     {
-        Collection<Program> programsByCurrentUser = programService.getProgramsByCurrentUser();
-        return !programsByCurrentUser.contains( program );
+        if ( accessiblePrograms.isEmpty() )
+        {
+            accessiblePrograms = new ArrayList<>( programService.getProgramsByUser( user ) );
+        }
+
+        return !accessiblePrograms.contains( program );
     }
 
     private boolean validateDataValue( DataElement dataElement, String value, ImportSummary importSummary )
@@ -763,13 +778,13 @@
         return true;
     }
 
-    private String getStoredBy( Event event, ImportSummary importSummary )
+    private String getStoredBy( Event event, ImportSummary importSummary, String defaultUsername )
     {
         String storedBy = event.getStoredBy();
 
         if ( storedBy == null )
         {
-            storedBy = currentUserService.getCurrentUsername();
+            storedBy = defaultUsername;
         }
         else if ( storedBy.length() >= 31 )
         {
@@ -780,7 +795,7 @@
                         + " is more than 31 characters, using current username instead" ) );
             }
 
-            storedBy = currentUserService.getCurrentUsername();
+            storedBy = defaultUsername;
         }
         return storedBy;
     }
@@ -870,7 +885,8 @@
 
         if ( programStageInstance.getId() == 0 )
         {
-            programStageInstanceService.addProgramStageInstance( programStageInstance );
+            programStageInstance.setAutoFields();
+            sessionFactory.getCurrentSession().save( programStageInstance );
         }
 
         if ( programStageInstance.isCompleted() )
@@ -883,7 +899,7 @@
     }
 
     private ImportSummary saveEvent( Program program, ProgramInstance programInstance, ProgramStage programStage,
-        ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event,
+        ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user,
         ImportOptions importOptions )
     {
         Assert.notNull( program );
@@ -898,7 +914,7 @@
 
         Date dueDate = DateUtils.parseDate( event.getDueDate() );
 
-        String storedBy = getStoredBy( event, importSummary );
+        String storedBy = getStoredBy( event, importSummary, user.getUsername() );
 
         if ( !dryRun )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramService.java	2015-02-13 08:50:17 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/program/DefaultProgramService.java	2015-03-03 05:06:59 +0000
@@ -28,23 +28,24 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.i18n.I18nUtils.i18n;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.system.util.CollectionUtils;
 import org.hisp.dhis.trackedentity.TrackedEntity;
 import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserAuthorityGroup;
 import org.hisp.dhis.user.UserService;
 import org.hisp.dhis.validation.ValidationCriteria;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.hisp.dhis.i18n.I18nUtils.i18n;
+
 /**
  * @author Abyot Asalefew
  * @version $Id$
@@ -166,6 +167,12 @@
     }
 
     @Override
+    public Collection<Program> getProgramsByUser( User user )
+    {
+        return i18n( i18nService, getByUser( user ) );
+    }
+
+    @Override
     public Collection<Program> getProgramsByCurrentUser( int type )
     {
         return i18n( i18nService, getByCurrentUser( type ) );
@@ -195,13 +202,13 @@
     @Override
     public Integer getProgramCountByName( String name )
     {
-        return i18n( i18nService, programStore.getCountLikeName( name ));
+        return i18n( i18nService, programStore.getCountLikeName( name ) );
     }
 
     @Override
     public Collection<Program> getProgramBetweenByName( String name, int min, int max )
     {
-        return i18n( i18nService, programStore.getAllLikeName( name, min, max ));
+        return i18n( i18nService, programStore.getAllLikeName( name, min, max ) );
     }
 
     @Override
@@ -215,13 +222,18 @@
     {
         return i18n( i18nService, programStore.getAllOrderedName( min, max ) );
     }
-    
+
     @Override
     public Collection<Program> getByCurrentUser()
     {
+        return getByUser( currentUserService.getCurrentUser() );
+    }
+
+    public Collection<Program> getByUser( User user )
+    {
         Collection<Program> programs = new HashSet<>();
 
-        if ( currentUserService.getCurrentUser() != null && !currentUserService.currentUserIsSuper() )
+        if ( user != null && !user.isSuper() )
         {
             Set<UserAuthorityGroup> userRoles = userService.getUserCredentials( currentUserService.getCurrentUser() )
                 .getUserAuthorityGroups();

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-02-17 06:00:52 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-03-03 05:06:59 +0000
@@ -34,6 +34,9 @@
 import org.hisp.dhis.common.PagerUtils;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dxf2.common.IdSchemes;
+import org.hisp.dhis.dxf2.common.ImportOptions;
+import org.hisp.dhis.dxf2.common.JacksonUtils;
 import org.hisp.dhis.dxf2.events.event.Event;
 import org.hisp.dhis.dxf2.events.event.EventService;
 import org.hisp.dhis.dxf2.events.event.Events;
@@ -47,9 +50,6 @@
 import org.hisp.dhis.dxf2.importsummary.ImportStatus;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
-import org.hisp.dhis.dxf2.common.ImportOptions;
-import org.hisp.dhis.dxf2.common.IdSchemes;
-import org.hisp.dhis.dxf2.common.JacksonUtils;
 import org.hisp.dhis.event.EventStatus;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -65,6 +65,7 @@
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.webapi.webdomain.WebOptions;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Scope;
 import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
@@ -93,6 +94,7 @@
  */
 @Controller
 @RequestMapping( value = EventController.RESOURCE_PATH )
+@Scope( value = "prototype" )
 public class EventController
 {
     public static final String RESOURCE_PATH = "/events";