← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17272: Approval. Added resource for batch approval.

 

------------------------------------------------------------
revno: 17272
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-10-25 12:06:33 +0200
message:
  Approval. Added resource for batch approval.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalController.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/BaseMetaDataCollectionObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java	2014-10-25 08:28:23 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java	2014-10-25 10:06:33 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.dataset.DataSet;
@@ -52,10 +53,12 @@
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 
 /**
 * @author Lars Helge Overland
 */
+@JacksonXmlRootElement( localName = "metaDataCollectionObject", namespace = DxfNamespaces.DXF_2_0 )
 public class BaseMetaDataCollectionObject
     extends BaseIdentifiableObject
 {
@@ -87,7 +90,44 @@
 
     @Scanned
     protected List<Integer> organisationUnitLevels = new ArrayList<>();
-
+    
+    @Scanned
+    protected List<DataElementCategoryOptionCombo> categoryOptionCombos = new ArrayList<>();
+
+    // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+    
+    public boolean hasIndicators()
+    {
+        return indicators != null && !indicators.isEmpty();
+    }
+    
+    public boolean hasDataElements()
+    {
+        return dataElements != null && !dataElements.isEmpty();
+    }
+
+    public boolean hasDataSets()
+    {
+        return dataSets != null && !dataSets.isEmpty();
+    }
+
+    public boolean hasOrganisationUnits()
+    {
+        return organisationUnits != null && !organisationUnits.isEmpty();
+    }
+
+    public boolean hasPeriods()
+    {
+        return periods != null && !periods.isEmpty();
+    }
+    
+    public boolean hasCategoryOptionCombos()
+    {
+        return categoryOptionCombos != null && !categoryOptionCombos.isEmpty();
+    }
+    
     // -------------------------------------------------------------------------
     // Getters and setters
     // -------------------------------------------------------------------------
@@ -237,4 +277,18 @@
     {
         this.organisationUnitLevels = organisationUnitLevels;
     }
+
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlElementWrapper( localName = "categoryOptionCombos", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "categoryOptionCombo", namespace = DxfNamespaces.DXF_2_0 )
+    public List<DataElementCategoryOptionCombo> getCategoryOptionCombos()
+    {
+        return categoryOptionCombos;
+    }
+
+    public void setCategoryOptionCombos( List<DataElementCategoryOptionCombo> categoryOptionCombos )
+    {
+        this.categoryOptionCombos = categoryOptionCombos;
+    }    
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java	2014-10-24 12:25:34 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalLevelService.java	2014-10-25 10:06:33 +0000
@@ -216,7 +216,16 @@
      * @return approval level
      */
     DataApprovalLevel getUserApprovalLevel( OrganisationUnit orgUnit, boolean includeDataViewOrgUnits );
-    
+
+    /**
+     * Gets the approval level at which this user may make approval actions
+     * (if the user is authorized for any) on this organisation unit.
+     *
+     * @param orgUnit org unit to test
+     * @param includeDataViewOrgUnits include data view org units in test?
+     * @param user user to get approval level from.
+     * @return approval level
+     */
     DataApprovalLevel getUserApprovalLevel( User user, OrganisationUnit orgUnit, boolean includeDataViewOrgUnits );
 
     /**

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2014-10-24 12:15:42 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/PeriodType.java	2014-10-25 10:06:33 +0000
@@ -418,8 +418,8 @@
     }
 
     /**
-     * Returns a period type based on the given date string in ISO format. Returns
-     * null if the date string cannot be parsed to a date.
+     * Returns a period based on the given date string in ISO format. Returns
+     * null if the date string cannot be parsed to a period.
      *
      * @param isoPeriod the date string in ISO format.
      * @return a period.
@@ -444,6 +444,29 @@
     }
 
     /**
+     * Returns a list of periods based on the given date string in ISO format.
+     *
+     * @param isoPeriod the date string in ISO format.
+     * @return a period.
+     */
+    public static List<Period> getPeriodsFromIsoStrings( List<String> isoPeriods )
+    {
+        List<Period> periods = new ArrayList<>();
+        
+        for ( String isoPeriod : isoPeriods )
+        {
+            Period period = getPeriodFromIsoString( isoPeriod );
+            
+            if ( period != null )
+            {
+                periods.add( period );
+            }
+        }
+        
+        return periods;
+    }
+
+    /**
      * Return the potential number of periods of the given period type which is
      * spanned by this period.
      *

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2014-10-25 07:40:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2014-10-25 10:06:33 +0000
@@ -171,6 +171,8 @@
 
             dataApprovalStore.addDataApproval( da );
         }
+        
+        log.info( "Approvals saved: " + dataApprovalList.size() );
     }
 
     @Override
@@ -212,6 +214,8 @@
 
             dataApprovalStore.deleteDataApproval( d );
         }
+        
+        log.info( "Unapprovals saved: " + dataApprovalList.size() );
     }
 
     @Override
@@ -261,6 +265,8 @@
 
             dataApprovalStore.updateDataApproval( d );
         }
+        
+        log.info( "Accepts saved: " + dataApprovalList.size() );
     }
 
     @Override
@@ -306,6 +312,8 @@
 
             dataApprovalStore.updateDataApproval( da );
         }
+        
+        log.info( "Unaccepts saved: " + dataApprovalList.size() );
     }
 
     @Override

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalController.java	2014-10-25 08:07:52 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataApprovalController.java	2014-10-25 10:06:33 +0000
@@ -29,6 +29,7 @@
  */
 
 import static com.google.common.collect.Lists.newArrayList;
+import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -39,10 +40,12 @@
 import java.util.Map;
 import java.util.Set;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.BaseMetaDataCollectionObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.view.BasicView;
 import org.hisp.dhis.dataapproval.DataApproval;
@@ -106,7 +109,7 @@
     private DataSetService dataSetService;
 
     @Autowired
-    private IdentifiableObjectManager manager;
+    private IdentifiableObjectManager objectManager;
 
     @Autowired
     private OrganisationUnitService organisationUnitService;
@@ -185,7 +188,7 @@
     {
         Set<DataSet> dataSets = new HashSet<>();
 
-        dataSets.addAll( manager.getByUid( DataSet.class, ds ) );
+        dataSets.addAll( objectManager.getByUid( DataSet.class, ds ) );
 
         Set<Period> periods = new HashSet<>();
 
@@ -253,7 +256,7 @@
         @RequestParam String pe, 
         HttpServletResponse response ) throws IOException
     {
-        Set<DataSet> dataSets = new HashSet<>( manager.getByUid( DataSet.class, ds ) );
+        Set<DataSet> dataSets = new HashSet<>( objectManager.getByUid( DataSet.class, ds ) );
         
         Period period = PeriodType.getPeriodFromIsoString( pe );
 
@@ -348,6 +351,50 @@
         }
     }
 
+    /**
+     * The data set, period and category option combo associations will be used.
+     */
+    @RequestMapping( method = RequestMethod.POST, value = "/batch" )
+    public void saveApprovalBatch( @RequestBody BaseMetaDataCollectionObject dataApproval,
+        HttpServletRequest request, HttpServletResponse response )
+    {
+        if ( !dataApproval.hasDataSets() || !dataApproval.hasPeriods() || !dataApproval.hasCategoryOptionCombos() )
+        {
+            ContextUtils.conflictResponse( response, "Approval must have data sets, periods and category option combos" );
+        }
+        
+        List<DataSet> dataSets = objectManager.getByUid( DataSet.class, getUids( dataApproval.getDataSets() ) );
+        List<Period> periods = PeriodType.getPeriodsFromIsoStrings( getUids( dataApproval.getPeriods() ) );
+        List<DataElementCategoryOptionCombo> optionCombos = objectManager.getByUid( DataElementCategoryOptionCombo.class, getUids( dataApproval.getCategoryOptionCombos() ) );
+
+        User user = currentUserService.getCurrentUser();
+        OrganisationUnit unit = user.getOrganisationUnit(); //TODO
+        DataApprovalLevel approvalLevel = dataApprovalLevelService.getHighestDataApprovalLevel( unit, null );
+        
+        Date date = new Date();
+
+        List<DataApproval> approvals = new ArrayList<>();
+        
+        for ( DataSet dataSet : dataSets )
+        {
+            Set<DataElementCategoryOptionCombo> dataSetOptionCombos = dataSet.hasCategoryCombo() ? dataSet.getCategoryCombo().getOptionCombos() : null;
+            
+            for ( Period period : periods )
+            {
+                for ( DataElementCategoryOptionCombo optionCombo : optionCombos )
+                {
+                    if ( dataSetOptionCombos != null && dataSetOptionCombos.contains( optionCombo ) )
+                    {
+                        DataApproval approval = new DataApproval( approvalLevel, dataSet, period, unit, optionCombo, false, date, user );
+                        approvals.add( approval );
+                    }
+                }
+            }
+        }
+        
+        dataApprovalService.approveData( approvals );
+    }
+    
     @PreAuthorize( "hasRole('ALL') or hasRole('F_APPROVE_DATA') or hasRole('F_APPROVE_DATA_LOWER_LEVELS')" )
     @RequestMapping( method = RequestMethod.POST, value = MULTIPLE_SAVE_RESOURCE_PATH )
     public void saveApprovalMultiple( @RequestBody DataApprovalStateRequests dataApprovalStateRequests,
@@ -540,7 +587,7 @@
     {
         log.info( "DELETE " + RESOURCE_PATH + "?ds=" + ds + "&pe=" + pe + "&ou=" + ou );
 
-        Set<DataSet> dataSets = new HashSet<>( manager.getByUid( DataSet.class, ds ) );
+        Set<DataSet> dataSets = new HashSet<>( objectManager.getByUid( DataSet.class, ds ) );
 
         if ( dataSets.size() != ds.size() )
         {