← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21339: Data value import. Binding Hibernate session to background task ImportDataValueTask. Allows for u...

 

------------------------------------------------------------
revno: 21339
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-12-04 17:01:39 +0100
message:
  Data value import. Binding Hibernate session to background task ImportDataValueTask. Allows for using model asociations.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/SecurityContextRunnable.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/tasks/ImportDataValueTask.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.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/dataelement/DataElementService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementService.java	2015-11-30 17:16:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementService.java	2015-12-04 16:01:39 +0000
@@ -38,7 +38,6 @@
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
 
 /**
@@ -113,15 +112,6 @@
     DataElement getDataElementByName( String name );
 
     /**
-     * Indicates whether this data element allows for data entry for future periods
-     * through its data set assignments.
-     * 
-     * @param id the data element id.
-     * @return true or false.
-     */
-    boolean isOpenFuturePeriods( int id );
-
-    /**
      * Returns List of DataElements with a given key.
      *
      * @param key the name of the DataElement to return.
@@ -311,16 +301,6 @@
 
     Map<String, Integer> getDataElementUidIdMap();
     
-    /**
-     * Returns all option codes of the option set associated with the data element
-     * with the given identifier. Returns empty if the data element does not exist
-     * or if the data element does not have an option set.
-     * 
-     * @param id the data element identifier.
-     * @return a set of option codes.
-     */
-    Optional<Set<String>> getOptionCodesAsSet( int id );
-
     // -------------------------------------------------------------------------
     // DataElementGroup
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java	2015-11-30 17:16:26 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java	2015-12-04 16:01:39 +0000
@@ -46,7 +46,6 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -132,14 +131,6 @@
     }
 
     @Override
-    public boolean isOpenFuturePeriods( int id )
-    {
-        DataElement dataElement = getDataElement( id );
-        
-        return dataElement != null && dataElement.getOpenFuturePeriods() >= 0;
-    }
-    
-    @Override
     public List<DataElement> getAllDataElements()
     {
         return i18n( i18nService, dataElementStore.getAll() );
@@ -387,15 +378,6 @@
         return i18n( i18nService, dataElementStore.get( dataSet, key, max ) );
     }
 
-    @Override
-    public Optional<Set<String>> getOptionCodesAsSet( int id )
-    {
-        DataElement dataElement = getDataElement( id );
-        
-        return dataElement != null && dataElement.hasOptionSet() ? 
-            Optional.of( dataElement.getOptionSet().getOptionCodesAsSet() ) : Optional.empty();
-    }
-    
     // -------------------------------------------------------------------------
     // DataElementGroup
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/SecurityContextRunnable.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/SecurityContextRunnable.java	2015-09-10 10:22:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/SecurityContextRunnable.java	2015-12-04 16:01:39 +0000
@@ -53,13 +53,29 @@
         try
         {
             SecurityContextHolder.setContext( securityContext );
+            before();
             call();
         }
         finally
         {
+            after();
             SecurityContextHolder.clearContext();
         }
     }
 
     public abstract void call();
+    
+    /**
+     * Hook invoked before {@link call()}.
+     */
+    public void before()
+    {
+    }
+
+    /**
+     * Hook invoked after {@link call()}.
+     */
+    public void after()
+    {   
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-12-04 05:41:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-12-04 16:01:39 +0000
@@ -46,7 +46,6 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
-import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
 import org.hisp.dhis.dataset.DataSet;
@@ -116,9 +115,6 @@
     private IdentifiableObjectManager identifiableObjectManager;
 
     @Autowired
-    private DataElementService dataElementService;
-
-    @Autowired
     private DataElementCategoryService categoryService;
 
     @Autowired
@@ -735,8 +731,7 @@
                 continue;
             }
 
-            boolean inUserHierarchy = orgUnitInHierarchyMap.get( orgUnit.getUid(),
-                () -> organisationUnitService.isInUserHierarchy( orgUnit.getUid(), currentOrgUnits ) );
+            boolean inUserHierarchy = orgUnitInHierarchyMap.get( orgUnit.getUid(), () -> orgUnit.isDescendant( currentOrgUnits ) );
 
             if ( !inUserHierarchy )
             {
@@ -744,8 +739,7 @@
                 continue;
             }
 
-            boolean invalidFuturePeriod = period.isFuture() && !dataElementOpenFuturePeriodsMap.get( dataElement.getUid(),
-                () -> dataElementService.isOpenFuturePeriods( dataElement.getId() ) );
+            boolean invalidFuturePeriod = period.isFuture() && !dataElementOpenFuturePeriodsMap.get( dataElement.getUid(), () -> dataElement.getOpenFuturePeriods() > 0 );
 
             if ( invalidFuturePeriod )
             {
@@ -774,9 +768,9 @@
                 continue;
             }
 
-            Optional<Set<String>> optionCodes = dataElementOptionsMap.get( dataElement.getUid(),
-                () -> dataElementService.getOptionCodesAsSet( dataElement.getId() ) );
-
+            Optional<Set<String>> optionCodes = dataElementOptionsMap.get( dataElement.getUid(), () -> dataElement.hasOptionSet() ? 
+                Optional.of( dataElement.getOptionSet().getOptionCodesAsSet() ) : Optional.empty() );
+            
             if ( optionCodes.isPresent() && !optionCodes.get().contains( dataValue.getValue() ) )
             {
                 summary.getConflicts().add( new ImportConflict( dataValue.getValue(), "Data value is not a valid option of the data element option set: " + dataElement.getUid() ) );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/tasks/ImportDataValueTask.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/tasks/ImportDataValueTask.java	2015-11-02 18:56:53 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/tasks/ImportDataValueTask.java	2015-12-04 16:01:39 +0000
@@ -29,6 +29,8 @@
  */
 
 import org.hisp.dhis.security.SecurityContextRunnable;
+import org.hibernate.SessionFactory;
+import org.hisp.dhis.dbms.DbmsUtils;
 import org.hisp.dhis.dxf2.adx.AdxDataService;
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
@@ -52,6 +54,8 @@
 
     private AdxDataService adxDataService;
     
+    private SessionFactory sessionFactory;
+    
     private InputStream inputStream;
 
     private final ImportOptions importOptions;
@@ -60,11 +64,14 @@
 
     private final String format;
 
-    public ImportDataValueTask( DataValueSetService dataValueSetService, AdxDataService adxDataService,
+    // TODO: Re-factor as bean to avoid injecting session factory / dependencies
+    
+    public ImportDataValueTask( DataValueSetService dataValueSetService, AdxDataService adxDataService, SessionFactory sessionFactory,
         InputStream inputStream, ImportOptions importOptions, TaskId taskId, String format )
     {
         this.dataValueSetService = dataValueSetService;
         this.adxDataService = adxDataService;
+        this.sessionFactory = sessionFactory;
         this.inputStream = inputStream;
         this.importOptions = importOptions;
         this.taskId = taskId;
@@ -95,4 +102,16 @@
             dataValueSetService.saveDataValueSet( inputStream, importOptions, taskId );
         }
     }
+    
+    @Override
+    public void before()
+    {
+        DbmsUtils.bindSessionToThread( sessionFactory );
+    }
+    
+    @Override
+    public void after()
+    {
+        DbmsUtils.unbindSessionFromThread( sessionFactory );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-12-04 05:41:43 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java	2015-12-04 16:01:39 +0000
@@ -63,6 +63,7 @@
 import java.util.Set;
 
 import org.apache.commons.io.IOUtils;
+import org.hibernate.SessionFactory;
 
 import static org.hisp.dhis.webapi.utils.ContextUtils.*;
 
@@ -89,6 +90,9 @@
     
     @Autowired
     private Scheduler scheduler;
+    
+    @Autowired
+    private SessionFactory sessionFactory;
 
     // -------------------------------------------------------------------------
     // Get
@@ -250,7 +254,7 @@
         InputStream inputStream = saveTmp( request.getInputStream() );
 
         TaskId taskId = new TaskId( TaskCategory.DATAVALUE_IMPORT, currentUserService.getCurrentUser() );
-        scheduler.executeTask( new ImportDataValueTask( dataValueSetService, adxDataService, inputStream, importOptions, taskId, format ) );
+        scheduler.executeTask( new ImportDataValueTask( dataValueSetService, adxDataService, sessionFactory, inputStream, importOptions, taskId, format ) );
 
         response.setHeader( "Location", ContextUtils.getRootPath( request ) + "/system/tasks/" + TaskCategory.DATAVALUE_IMPORT );
         response.setStatus( HttpServletResponse.SC_ACCEPTED );

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java	2015-12-04 10:44:33 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/datavalue/ImportDataValueAction.java	2015-12-04 16:01:39 +0000
@@ -33,6 +33,7 @@
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hibernate.SessionFactory;
 import org.hisp.dhis.commons.util.StreamUtils;
 import org.hisp.dhis.dxf2.adx.AdxDataService;
 import org.hisp.dhis.dxf2.common.ImportOptions;
@@ -66,6 +67,9 @@
 
     @Autowired
     private CurrentUserService currentUserService;
+    
+    @Autowired
+    private SessionFactory sessionFactory;
 
     @Autowired
     private Scheduler scheduler;
@@ -166,7 +170,8 @@
 
         log.info( options );
 
-        scheduler.executeTask( new ImportDataValueTask( dataValueSetService, adxDataService, in, options, taskId, importFormat ) );
+        scheduler.executeTask( new ImportDataValueTask( dataValueSetService, 
+            adxDataService, sessionFactory, in, options, taskId, importFormat ) );
 
         return SUCCESS;
     }