=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java 2011-01-12 09:13:36 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java 2011-01-17 06:28:00 +0000 @@ -81,7 +81,7 @@ if ( user == null ) { - throw new NotAllowedException( "NO_USER", "No user is logged in." ); + throw NotAllowedException.NO_USER; } Collection units = user.getOrganisationUnits(); === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java 2010-12-27 03:17:34 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/ActivityReportingServiceImpl.java 2011-01-17 06:28:00 +0000 @@ -300,7 +300,7 @@ if ( programStageInstance == null ) { - throw new NotAllowedException( "INVALID_PROGRAM_STAGE" ); + throw NotAllowedException.INVALID_PROGRAM_STAGE; } programStageInstance.getProgramStage(); @@ -321,7 +321,7 @@ if ( dataElements.size() != dataElementIds.size() ) { - throw new NotAllowedException( "INVALID_PROGRAM_STAGE" ); + throw NotAllowedException.INVALID_PROGRAM_STAGE; } Map dataElementMap = new HashMap(); @@ -329,7 +329,7 @@ { if ( !dataElementIds.contains( dataElement.getId() ) ) { - throw new NotAllowedException( "INVALID_PROGRAM_STAGE" ); + throw NotAllowedException.INVALID_PROGRAM_STAGE; } dataElementMap.put( dataElement.getId(), dataElement ); } === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java 2011-01-12 09:13:36 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java 2011-01-17 06:28:00 +0000 @@ -41,6 +41,11 @@ public DataSet getDataSetForLocale( int dataSetId, Locale locale ); + /** Save {@link DataSetValue} to given {@link OrganisationUnit} + * @param unit - the Organisation unit to save to + * @param dataSetValue - the data set value to save + * @throws NotAllowedException if saving is not allowed + */ public void saveDataSetValues( OrganisationUnit unit, DataSetValue dataSetValue ) throws NotAllowedException; === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java 2011-01-14 16:18:27 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java 2011-01-17 06:28:00 +0000 @@ -204,63 +204,61 @@ org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetValue.getId() ); - if ( !dataSetService.getDataSetsBySource( unit ).contains( dataSet ) ) - { - throw new NotAllowedException( "INVALID_DATASET_ASSOCIATION" ); - } - - Period selectedPeriod = getPeriod( dataSetValue.getPeriodName(), dataSet.getPeriodType() ); - - if ( selectedPeriod == null ) - { - throw new NotAllowedException( "INVALID_PERIOD" ); - } - - if ( isDataSetLocked( unit, dataSet, selectedPeriod ) ) - { - throw new NotAllowedException( "DATASET_LOCKED" ); - } - + if ( !dataSetAssociatedWithOrgUnit( unit, dataSet ) ) + { + throw NotAllowedException.INVALID_DATASET_ASSOCIATION; + } + + Period period = getPeriod( dataSetValue.getPeriodName(), dataSet.getPeriodType() ); + + if ( period == null ) + { + throw NotAllowedException.INVALID_PERIOD; + } + + if ( dataSetLocked( unit, dataSet, period ) ) + { + throw NotAllowedException.DATASET_LOCKED; + } + + Map dataElementMap = getDataElementIdMapping( dataSet ); + + for ( DataValue dataValue : dataSetValue.getDataValues() ) + { + org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get( dataValue.getId() ); + + if ( dataElement == null ) + { + log.info( "Data value submitted for data element " + dataValue.getId() + ", that is not in data set '" + + dataSet.getName() + "'" ); + continue; + } + + if ( emptyString( dataValue.getValue() ) ) + { + log.info( "Empty data value for data element " + dataValue.getId() + " not saved" ); + continue; + } + + saveValue(unit, period, dataElement, dataValue); + + } + } + + private Map getDataElementIdMapping( + org.hisp.dhis.dataset.DataSet dataSet ) + { Map dataElementMap = new HashMap(); for ( org.hisp.dhis.dataelement.DataElement dataElement : dataSet.getDataElements() ) { dataElementMap.put( dataElement.getId(), dataElement ); } - - Set handled = new HashSet(); - - for ( DataValue dv : dataSetValue.getDataValues() ) - { - int elementId = dv.getId(); - - if ( handled.contains( elementId ) ) - { - log.info( "Multiple values for element " + elementId + " submitted. Not handling this value." ); - continue; - } - - org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get( elementId ); - - if ( dataElement == null ) - { - log.info( "Data element value submitted for data element " + elementId + ", that is not in data set '" - + dataSet.getName() + "'" ); - handled.add( elementId ); - continue; - } - - if ( emptyString( dv.getValue() ) ) - { - log.info( "Empty data value for data element " + elementId + " not saved" ); - handled.add( elementId ); - continue; - } - - saveValue(unit, selectedPeriod, dataElement, dv); - - handled.add( elementId ); - } - reportMissingValues( dataSet, handled ); + return dataElementMap; + } + + private boolean dataSetAssociatedWithOrgUnit( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet ) + { + return dataSetService.getDataSetsBySource( unit ).contains( dataSet ); } private void saveValue(OrganisationUnit unit, Period period, org.hisp.dhis.dataelement.DataElement dataElement, DataValue dv) { @@ -288,24 +286,11 @@ } - private void reportMissingValues( org.hisp.dhis.dataset.DataSet dataSet, Set handled ) - { - Collection dataElements = dataSet.getDataElements(); - for ( org.hisp.dhis.dataelement.DataElement element : dataElements ) - { - if ( !handled.contains( element.getId() ) ) - { - log.info( "Submitted values for dataset '" + dataSet.getName() + "' missing data element '" + element.getName() + "'" ); - - } - } - } - // ------------------------------------------------------------------------- // Supportive method // ------------------------------------------------------------------------- - private boolean isDataSetLocked( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet, Period selectedPeriod ) + private boolean dataSetLocked( OrganisationUnit unit, org.hisp.dhis.dataset.DataSet dataSet, Period selectedPeriod ) { if ( dataSetLockService.getDataSetLockByDataSetPeriodAndSource( dataSet, selectedPeriod, unit ) != null ) return true; === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java 2011-01-12 09:13:36 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/NotAllowedException.java 2011-01-17 06:28:00 +0000 @@ -31,14 +31,25 @@ extends Exception { + public static final NotAllowedException INVALID_PROGRAM_STAGE = new NotAllowedException( "INVALID_PROGRAM_STAGE" ); + + public static final NotAllowedException INVALID_DATASET_ASSOCIATION = new NotAllowedException( "INVALID_DATASET_ASSOCIATION" ); + + public static final NotAllowedException INVALID_PERIOD = new NotAllowedException( "INVALID_PERIOD" ); + + public static final NotAllowedException DATASET_LOCKED = new NotAllowedException( "DATASET_LOCKED" ); + + public static final NotAllowedException NO_USER = new NotAllowedException( "NO_USER", "No user is logged in." ); + + private String reason; - public NotAllowedException( String reason ) + private NotAllowedException( String reason ) { this.reason = reason; } - public NotAllowedException( String reason, String message ) + private NotAllowedException( String reason, String message ) { super( message ); this.reason = reason;