=== 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 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 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 getDataSetsForMobile(); - + int getDataSetCountByName( String name ); - + Collection getDataSetsBetweenByName( String name, int first, int max ); - + int getDataSetCount(); - - Collection getDataSetsBetween(int first, int max ); + + Collection 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 { 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 getAllDataSets(); - + /** * Gets all DataSets associated with the given PeriodType. * @@ -126,17 +127,17 @@ * Returns all DataSets that can be collected through mobile. */ Collection getDataSetsForMobile( OrganisationUnit source ); - + Collection getDataSetsForMobile(); Collection getDataSetsBySources( Collection sources ); - + int getDataSetCountByName( String name ); - + Collection getDataSetsBetweenByName( String name, int first, int max ); - + int getDataSetCount(); - - Collection getDataSetsBetween(int first, int max ); + + Collection 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 @@ *

* Handles the content in the following way *

    - *
  • if data set is not specified, will resolve it through data elements if not ambiguous. + *
  • if data set is not specified, will resolve it through data elements + * if not ambiguous. *
  • optionCombo defaults to 'default' if not specified *
  • storedBy defaults to currently logged in user's name *
  • if value is empty not present -> delete value @@ -91,7 +95,8 @@ *
  • orgUnit exists *
  • orgunit reports dataSet *
  • period is a valid period - *
  • the dataValueSet is not registered as complete or that if it is a complete date is present + *
  • the dataValueSet is not registered as complete or that if it is a + * complete date is present *
  • if complete date is empty string - delete completion *
  • if complete date present checks validity *
@@ -103,12 +108,15 @@ * What isn't checked yet: *
    *
  • The value is valid! - *
  • There isn't duplicated value entries (will throw Constraint exception) - *
  • If multiple data sets are possible, evaluate if they are incompatible (complete, locking and possibly period) + *
  • There isn't duplicated value entries (will throw Constraint + * exception) + *
  • If multiple data sets are possible, evaluate if they are incompatible + * (complete, locking and possibly period) *
* Concerns: *
    - *
  • deletion through sending "empty string" value dependent on semantics of add/update in data value store + *
  • deletion through sending "empty string" value dependent on semantics + * of add/update in data value store *
  • completed semantics: can't uncomplete but can complete and * "recomplete" *
  • 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 @@ dhis-service-core + org.hisp.dhis + dhis-service-importexport + + org.springframework spring-webmvc === 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 + *
      + *
    • No user has phone number + *
    • More than one user has phone number + *
    • User not associated with org unit + *
    • User associated with multiple org units + */ + private String findOrgUnit( String phoneNumber ) + throws IllegalArgumentException + { + Collection 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 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(); + + } + +}