← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5269: Ported data value set posting to the new api

 

------------------------------------------------------------
revno: 5269
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-12-02 15:53:08 +0100
message:
  Ported data value set posting to the new api
  - added option to specify org unit through phoneNumber
  - added some logging when storing values
  Fixed data set to use uid in api 
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java
  dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetService.java
  dhis-2/dhis-web/dhis-web-api/pom.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.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/dataset/DataSetService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java	2011-11-22 15:48:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetService.java	2011-12-02 14:53:08 +0000
@@ -83,7 +83,7 @@
      * @return the DataSet with the given UID, or null if no match.
      */
     DataSet getDataSet( String uid );
-    
+
     /**
      * Returns a DataSets with the given name.
      * 
@@ -196,22 +196,24 @@
      * @return a collection of data elements.
      */
     Collection<DataElement> getDataElements( DataSet dataSet );
-    
+
     /**
-     * Returns all DataSets that can be collected through mobile (one organisation unit).
+     * Returns all DataSets that can be collected through mobile (one
+     * organisation unit).
      */
     Collection<DataSet> getDataSetsForMobile( OrganisationUnit source );
 
     /**
-     * Returns all DataSets that can be collected through mobile (all organisation unit).
+     * Returns all DataSets that can be collected through mobile (all
+     * organisation unit).
      */
     Collection<DataSet> getDataSetsForMobile();
-    
+
     int getDataSetCountByName( String name );
-    
+
     Collection<DataSet> getDataSetsBetweenByName( String name, int first, int max );
-    
+
     int getDataSetCount();
-    
-    Collection<DataSet> getDataSetsBetween(int first, int max );
+
+    Collection<DataSet> getDataSetsBetween( int first, int max );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java	2011-11-22 15:48:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSetStore.java	2011-12-02 14:53:08 +0000
@@ -29,6 +29,7 @@
 
 import java.util.Collection;
 
+import org.hisp.dhis.common.GenericNameableObjectStore;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.PeriodType;
 
@@ -37,6 +38,7 @@
  * @version $Id: DataSetStore.java 6255 2008-11-10 16:01:24Z larshelg $
  */
 public interface DataSetStore
+    extends GenericNameableObjectStore<DataSet>
 {
     String ID = DataSetStore.class.getName();
 
@@ -82,7 +84,6 @@
      */
     DataSet getDataSet( String uid );
 
-    
     /**
      * Returns the DataSet with the given name.
      * 
@@ -98,7 +99,7 @@
      * @return The DataSet with the given short name.
      */
     DataSet getDataSetByShortName( String shortName );
-    
+
     /**
      * Returns the DataSet with the given code.
      * 
@@ -106,14 +107,14 @@
      * @return The DataSet with the given code.
      */
     DataSet getDataSetByCode( String code );
-    
+
     /**
      * Gets all DataSets.
      * 
      * @return A collection containing all DataSets.
      */
     Collection<DataSet> getAllDataSets();
-    
+
     /**
      * Gets all DataSets associated with the given PeriodType.
      * 
@@ -126,17 +127,17 @@
      * Returns all DataSets that can be collected through mobile.
      */
     Collection<DataSet> getDataSetsForMobile( OrganisationUnit source );
-    
+
     Collection<DataSet> getDataSetsForMobile();
 
     Collection<DataSet> getDataSetsBySources( Collection<OrganisationUnit> sources );
-    
+
     int getDataSetCountByName( String name );
-    
+
     Collection<DataSet> getDataSetsBetweenByName( String name, int first, int max );
-    
+
     int getDataSetCount();
-    
-    Collection<DataSet> getDataSetsBetween(int first, int max );
+
+    Collection<DataSet> getDataSetsBetween( int first, int max );
 
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2011-11-22 15:48:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DefaultDataSetService.java	2011-12-02 14:53:08 +0000
@@ -128,7 +128,7 @@
     @Override
     public DataSet getDataSet( String uid )
     {
-        return i18n( i18nService, dataSetStore.getDataSet( uid ) );
+        return i18n( i18nService, dataSetStore.getByUid( uid ) );
     }
 
     public DataSet getDataSetByName( String name )

=== modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetService.java	2011-11-22 15:48:30 +0000
+++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetService.java	2011-12-02 14:53:08 +0000
@@ -33,6 +33,8 @@
 import java.util.HashSet;
 import java.util.Set;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
@@ -56,6 +58,7 @@
 
 public class DataValueSetService
 {
+    private static final Log log = LogFactory.getLog( DataValueSetService.class );
 
     private OrganisationUnitService organisationUnitService;
 
@@ -76,7 +79,8 @@
      * <p>
      * Handles the content in the following way
      * <ul>
-     * <li>if data set is not specified, will resolve it through data elements if not ambiguous.
+     * <li>if data set is not specified, will resolve it through data elements
+     * if not ambiguous.
      * <li>optionCombo defaults to 'default' if not specified
      * <li>storedBy defaults to currently logged in user's name
      * <li>if value is empty not present -> delete value
@@ -91,7 +95,8 @@
      * <li>orgUnit exists
      * <li>orgunit reports dataSet
      * <li>period is a valid period
-     * <li>the dataValueSet is not registered as complete or that if it is a complete date is present
+     * <li>the dataValueSet is not registered as complete or that if it is a
+     * complete date is present
      * <li>if complete date is empty string - delete completion
      * <li>if complete date present checks validity
      * </ul>
@@ -103,12 +108,15 @@
      * What isn't checked yet:
      * <ul>
      * <li>The value is valid!
-     * <li>There isn't duplicated value entries (will throw Constraint exception)
-     * <li>If multiple data sets are possible, evaluate if they are incompatible (complete, locking and possibly period)
+     * <li>There isn't duplicated value entries (will throw Constraint
+     * exception)
+     * <li>If multiple data sets are possible, evaluate if they are incompatible
+     * (complete, locking and possibly period)
      * </ul>
      * Concerns:
      * <ul>
-     * <li>deletion through sending "empty string" value dependent on semantics of add/update in data value store
+     * <li>deletion through sending "empty string" value dependent on semantics
+     * of add/update in data value store
      * <li>completed semantics: can't uncomplete but can complete and
      * "recomplete"
      * <li>what is 'comment' good for really?
@@ -124,13 +132,14 @@
         Date timestamp = new Date();
 
         IdentificationStrategy idStrategy = dataValueSet.getIdScheme();
-        if (idStrategy != DataValueSet.DEFAULT_STRATEGY) {
+        if ( idStrategy != DataValueSet.DEFAULT_STRATEGY )
+        {
             throw new IllegalArgumentException( "Onlu UUID id strategy supported currently." );
         }
 
         DataSet dataSet = getDataSet( dataValueSet );
 
-        OrganisationUnit unit = getOrgUnit( dataValueSet.getOrganisationUnitIdentifier());
+        OrganisationUnit unit = getOrgUnit( dataValueSet.getOrganisationUnitIdentifier() );
 
         if ( !dataSet.getSources().contains( unit ) )
         {
@@ -147,6 +156,20 @@
             saveDataValue( timestamp, dataSet, unit, period, dxfValue );
         }
 
+        log( dataValueSet, unit, dataSet );
+    }
+
+    private void log( DataValueSet dataValueSet, OrganisationUnit unit, DataSet dataSet )
+    {
+        String message = "Saved data value set for " + dataSet.getName() + ", " + unit.getName() + ", "
+            + dataValueSet.getPeriodIsoDate() + " - Data values received: ";
+
+        for ( org.hisp.dhis.importexport.dxf2.model.DataValue value : dataValueSet.getDataValues() )
+        {
+            message += value.getDataElementIdentifier() + " = " + value.getValue() + ", ";
+        }
+
+        log.info( message.substring( 0, message.length() - 3 ) );
     }
 
     private DataSet getDataSet( DataValueSet dataValueSet )
@@ -248,7 +271,6 @@
 
         String storedBy = currentUserService.getCurrentUsername();
 
-
         if ( dv == null )
         {
             dv = new DataValue( dataElement, period, unit, value, storedBy, timestamp, null, combo );
@@ -282,7 +304,7 @@
 
         CompleteDataSetRegistration complete = null;
 
-        if ( completeDateString != null && !completeDateString.trim().isEmpty())
+        if ( completeDateString != null && !completeDateString.trim().isEmpty() )
         {
             complete = getComplete( dataSet, unit, period, completeDateString, complete );
         }
@@ -299,7 +321,8 @@
         try
         {
             Date completeDate = format.parse( completeDateString );
-            complete = new CompleteDataSetRegistration( dataSet, period, unit, completeDate, currentUserService.getCurrentUsername() );
+            complete = new CompleteDataSetRegistration( dataSet, period, unit, completeDate,
+                currentUserService.getCurrentUsername() );
         }
         catch ( ParseException e )
         {
@@ -324,7 +347,7 @@
         return period;
     }
 
-    private OrganisationUnit getOrgUnit( String id)
+    private OrganisationUnit getOrgUnit( String id )
     {
         OrganisationUnit unit = organisationUnitService.getOrganisationUnit( id );
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/pom.xml'
--- dhis-2/dhis-web/dhis-web-api/pom.xml	2011-11-29 12:06:35 +0000
+++ dhis-2/dhis-web/dhis-web-api/pom.xml	2011-12-02 14:53:08 +0000
@@ -35,6 +35,10 @@
       <artifactId>dhis-service-core</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.hisp.dhis</groupId>
+      <artifactId>dhis-service-importexport</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
     </dependency>

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2011-11-24 17:03:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2011-12-02 14:53:08 +0000
@@ -37,7 +37,7 @@
     }
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
-    public DataSet getDataSet( @PathVariable( "uid" ) Integer uid, HttpServletRequest request )
+    public DataSet getDataSet( @PathVariable( "uid" ) String uid, HttpServletRequest request )
     {
         DataSet dataSet = dataSetService.getDataSet( uid );
 

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2011-12-02 14:53:08 +0000
@@ -0,0 +1,112 @@
+package org.hisp.dhis.api.controller;
+
+import java.io.IOException;
+import java.util.Collection;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.importexport.dxf2.model.DataValueSet;
+import org.hisp.dhis.importexport.dxf2.service.DataValueSetService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@Controller
+@RequestMapping( value = "/dataValueSets" )
+public class DataValueSetController
+{
+
+    private static final Log log = LogFactory.getLog( DataValueSetController.class );
+
+    @Autowired
+    private DataValueSetService dataValueSetService;
+
+    @Autowired
+    private UserService userService;
+
+    @RequestMapping( method = RequestMethod.POST )
+    public void storeDataValueSet( @RequestBody
+    DataValueSet dataValueSet, @RequestParam( required = false )
+    String phoneNumber )
+    {
+        if ( phoneNumber != null && !phoneNumber.trim().isEmpty() )
+        {
+            String unitId = findOrgUnit( phoneNumber );
+            dataValueSet.setOrganisationUnitIdentifier( unitId );
+        }
+
+        dataValueSetService.saveDataValueSet( dataValueSet );
+
+        if ( log.isDebugEnabled() )
+        {
+            String message = "Saved data value set for " + dataValueSet.getDataSetIdentifier() + ", "
+                + dataValueSet.getOrganisationUnitIdentifier() + ", " + dataValueSet.getPeriodIsoDate();
+
+            log.debug( message );
+        }
+    }
+
+    @ExceptionHandler
+    public void mapException( IllegalArgumentException exception, HttpServletResponse response )
+        throws IOException
+    {
+        response.setStatus( HttpServletResponse.SC_CONFLICT );
+        response.setContentType( "text/plain" );
+        response.getWriter().write( "Problem with input: " + exception.getMessage() );
+    }
+
+    /**
+     * Find orgunit corresponding to the registered phone number.
+     * 
+     * @param phoneNumber The phone number to look up
+     * @return the organisation unit uid
+     * @throws IllegalArgumentException if
+     *         <ul>
+     *         <li>No user has phone number
+     *         <li>More than one user has phone number
+     *         <li>User not associated with org unit
+     *         <li>User associated with multiple org units
+     */
+    private String findOrgUnit( String phoneNumber )
+        throws IllegalArgumentException
+    {
+        Collection<User> users = userService.getUsersByPhoneNumber( phoneNumber );
+
+        if ( users == null || users.isEmpty() )
+        {
+            throw new IllegalArgumentException( "Phone number '" + phoneNumber + "' not associated with any user" );
+        }
+        else if ( users.size() > 1 )
+        {
+            throw new IllegalArgumentException( "Phone number '" + phoneNumber + "' associated with multiple users" );
+        }
+
+        User user = users.iterator().next();
+
+        Collection<OrganisationUnit> organisationUnits = user.getOrganisationUnits();
+
+        if ( organisationUnits == null || organisationUnits.isEmpty() )
+        {
+            throw new IllegalArgumentException( "User '" + user.getName()
+                + "' not associated with any organisation unit" );
+        }
+        else if ( organisationUnits.size() > 1 )
+        {
+            throw new IllegalArgumentException( "User '" + user.getName()
+                + "' associated with multiple organisation units" );
+        }
+
+        return organisationUnits.iterator().next().getUid();
+
+    }
+
+}