=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnit.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnit.java 2010-11-16 16:28:41 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnit.java 2010-11-16 18:26:11 +0000 @@ -38,10 +38,6 @@ private String name; - private String programFormsLink; - - private String activitiesLink; - public int getId() { return id; @@ -62,34 +58,12 @@ this.name = name; } - public String getProgramFormsLink() - { - return programFormsLink; - } - - public void setProgramFormsLink( String programFormsLink ) - { - this.programFormsLink = programFormsLink; - } - - public String getActivitiesLink() - { - return activitiesLink; - } - - public void setActivitiesLink( String activitiesLink ) - { - this.activitiesLink = activitiesLink; - } - @Override public void serialize( DataOutputStream dataOutputStream ) throws IOException { dataOutputStream.write( this.id ); dataOutputStream.writeUTF( this.name ); - dataOutputStream.writeUTF( this.activitiesLink ); - dataOutputStream.writeUTF( this.programFormsLink ); } @Override @@ -98,8 +72,6 @@ { this.id = dataInputStream.readInt(); this.name = dataInputStream.readUTF(); - this.activitiesLink = dataInputStream.readUTF(); - this.programFormsLink = dataInputStream.readUTF(); } } === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java 2010-11-16 18:26:11 +0000 @@ -0,0 +1,13 @@ +package org.hisp.dhis.web.api.resources; + +public class MediaType +{ + public static final String ACTIVITYVALUELIST_SERIALIZED = "application/vnd.org.dhis2.activityvaluelist+serialized"; + + public static final String DATASETVALUE_SERIALIZED = "application/vnd.org.dhis2.datasetvalue+serialized"; + + public static final String ACTIVITYPLAN_SERIALIZED = "application/vnd.org.dhis2.activityplan+serialized"; + + public static final String MOBILE_SERIALIZED = "application/vnd.org.dhis2.mobile+serialized"; + +} === 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 2010-11-16 16:28:41 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java 2010-11-16 18:26:11 +0000 @@ -35,11 +35,8 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import javax.ws.rs.core.UriInfo; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.user.CurrentUserService; @@ -49,10 +46,9 @@ import org.hisp.dhis.web.api.model.DataSetValue; import org.hisp.dhis.web.api.model.MobileModel; import org.hisp.dhis.web.api.model.OrgUnit; +import org.hisp.dhis.web.api.service.FacilityReportingService; import org.hisp.dhis.web.api.service.IActivityPlanService; import org.hisp.dhis.web.api.service.IActivityValueService; -import org.hisp.dhis.web.api.service.IDataSetService; -import org.hisp.dhis.web.api.service.IDataValueService; import org.hisp.dhis.web.api.service.IProgramService; import org.springframework.beans.factory.annotation.Required; @@ -60,29 +56,19 @@ public class MobileResource { - private static final String MEDIA_TYPE_MOBILE_SERIALIZED = "application/vnd.org.dhis2.mobile+serialized"; - // Dependencies - private IDataValueService idataValueService; - private IActivityValueService iactivityValueService; private IProgramService programService; private IActivityPlanService activityPlanService; - private IDataSetService idataSetService; + private FacilityReportingService facilityReportingService; private CurrentUserService currentUserService; @Required - public void setIdataValueService( IDataValueService idataValueService ) - { - this.idataValueService = idataValueService; - } - - @Required public void setProgramService( IProgramService programService ) { this.programService = programService; @@ -101,9 +87,9 @@ } @Required - public void setIdataSetService( IDataSetService idataSetService ) + public void setFacilityReportingService( FacilityReportingService facilityReportingService ) { - this.idataSetService = idataSetService; + this.facilityReportingService = facilityReportingService; } @Required @@ -114,11 +100,8 @@ // Resource methods - @Context - private UriInfo uriInfo; - @GET - @Produces( MEDIA_TYPE_MOBILE_SERIALIZED ) + @Produces( MediaType.MOBILE_SERIALIZED ) public Response getOrgUnitForUser() { User user = currentUserService.getCurrentUser(); @@ -150,41 +133,63 @@ @GET @Path( "all" ) - @Produces( MEDIA_TYPE_MOBILE_SERIALIZED ) + @Produces( MediaType.MOBILE_SERIALIZED ) public MobileModel getAllDataForUser( @HeaderParam( "accept-language" ) String locale ) { + + MobileModel mobileWrapper = new MobileModel(); mobileWrapper.setActivityPlan( activityPlanService.getCurrentActivityPlan( locale ) ); mobileWrapper.setPrograms( programService.getAllProgramsForLocale( locale ) ); - mobileWrapper.setDatasets( idataSetService.getAllMobileDataSetsForLocale( locale ) ); + Collection units = currentUserService.getCurrentUser().getOrganisationUnits(); + + if ( units.size() == 1 ) + { + OrganisationUnit unit = units.iterator().next(); + mobileWrapper.setDatasets( facilityReportingService.getMobileDataSetsForUnit( unit, locale ) ); + } + else + { + // FIXME: Should handle multiple explicitly; + } + return mobileWrapper; } - @POST - @Path( "dataSets" ) - @Consumes( "application/vnd.org.dhis2.datasetvalue+serialized" ) - @Produces( "application/xml" ) - public String getValues( DataSetValue dataSetValue ) - { - return idataValueService.saveValues( dataSetValue ); - } - @GET @Path( "activities/currentplan" ) - @Produces( "application/vnd.org.dhis2.activityplan+serialized" ) + @Produces( MediaType.ACTIVITYPLAN_SERIALIZED ) public ActivityPlan getCurrentActivityPlan( @HeaderParam( "accept-language" ) String locale ) { return activityPlanService.getCurrentActivityPlan( locale ); } @POST + @Path( "dataSets" ) + @Consumes( MediaType.DATASETVALUE_SERIALIZED ) + @Produces( "application/xml" ) + public String saveDataSetValues( DataSetValue dataSetValue ) + { + Collection units = currentUserService.getCurrentUser().getOrganisationUnits(); + + if ( units.size() != 1 ) + { + return "INVALID_REPORTING_UNIT"; + } + + OrganisationUnit unit = units.iterator().next(); + + return facilityReportingService.saveDataSetValues( unit, dataSetValue ); + } + + @POST @Path( "activities" ) - @Consumes( "application/vnd.org.dhis2.activityvaluelist+serialized" ) + @Consumes( MediaType.ACTIVITYVALUELIST_SERIALIZED ) @Produces( "application/xml" ) - public String getValues( ActivityValue activityValue ) + public String saveActivityReport( ActivityValue activityValue ) { return iactivityValueService.saveValues( activityValue ); } @@ -195,8 +200,6 @@ m.setId( unit.getId() ); m.setName( unit.getShortName() ); - m.setProgramFormsLink( uriInfo.getRequestUriBuilder().path( "programforms" ).build().toString() ); - m.setActivitiesLink( uriInfo.getRequestUriBuilder().path( "activityplan/current" ).build().toString() ); return m; } === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataSetService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataSetService.java 2010-11-16 16:28:41 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataSetService.java 1970-01-01 00:00:00 +0000 @@ -1,223 +0,0 @@ -package org.hisp.dhis.web.api.service; - -import static org.hisp.dhis.i18n.I18nUtils.i18n; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.comparator.DataElementSortOrderComparator; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.user.CurrentUserService; -import org.hisp.dhis.web.api.model.DataElement; -import org.hisp.dhis.web.api.model.DataSet; -import org.hisp.dhis.web.api.model.Model; -import org.hisp.dhis.web.api.model.ModelList; -import org.hisp.dhis.web.api.model.Section; -import org.hisp.dhis.web.api.utils.LocaleUtil; - -public class DefaultDataSetService - implements IDataSetService -{ - - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - private DataElementSortOrderComparator dataElementComparator = new DataElementSortOrderComparator(); - - - private org.hisp.dhis.dataset.DataSetService dataSetService; - - public org.hisp.dhis.dataset.DataSetService getDataSetService() - { - return dataSetService; - } - - public void setDataSetService( org.hisp.dhis.dataset.DataSetService dataSetService ) - { - this.dataSetService = dataSetService; - } - - private org.hisp.dhis.i18n.I18nService i18nService; - - public org.hisp.dhis.i18n.I18nService getI18nService() - { - return i18nService; - } - - public void setI18nService( org.hisp.dhis.i18n.I18nService i18nService ) - { - this.i18nService = i18nService; - } - - private CurrentUserService currentUserService; - public CurrentUserService getCurrentUserService() - { - return currentUserService; - } - - public void setCurrentUserService( CurrentUserService currentUserService ) - { - this.currentUserService = currentUserService; - } - - // ------------------------------------------------------------------------- - // MobileDataSetService - // ------------------------------------------------------------------------- - - - public List getAllMobileDataSetsForLocale( String localeString ) - { - Collection units = currentUserService.getCurrentUser().getOrganisationUnits(); - - OrganisationUnit unit = null; - - if ( units.size() > 0 ) - { - unit = units.iterator().next(); - } - else - { - return null; - } - - List datasets = new ArrayList(); - Locale locale = LocaleUtil.getLocale( localeString ); - // AbstractModelList abstractModelList = new AbstractModelList(); - // - // List abstractModels = new ArrayList(); - // - for ( org.hisp.dhis.dataset.DataSet dataSet : dataSetService.getDataSetsForMobile( unit ) ) - { - if ( dataSet.getPeriodType().getName().equals( "Daily" ) - || dataSet.getPeriodType().getName().equals( "Weekly" ) - || dataSet.getPeriodType().getName().equals( "Monthly" ) - || dataSet.getPeriodType().getName().equals( "Yearly" ) - || dataSet.getPeriodType().getName().equals( "Quarterly" ) ) - { - datasets.add( getDataSetForLocale( dataSet.getId(), locale ) ); - } - } - // - // abstractModelList.setAbstractModels(abstractModels); - - return datasets; - } - - public DataSet getDataSetForLocale( int dataSetId, Locale locale ) - { - org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetId ); - dataSet = i18n( i18nService, locale, dataSet ); - Set sections = dataSet.getSections(); - - // Collection dataElements = - // dataSet.getDataElements(); - - // Mobile - DataSet ds = new DataSet(); - - ds.setId( dataSet.getId() ); - ds.setName( dataSet.getName() ); - ds.setPeriodType( dataSet.getPeriodType().getName() ); - - // Mobile - List
sectionList = new ArrayList
(); - ds.setSections( sectionList ); - - if ( sections.size() == 0 || sections == null ) - { - // Collection dataElements = - // new ArrayList(); - List dataElements = new ArrayList( - dataSet.getDataElements() ); - - Collections.sort( dataElements, dataElementComparator ); - - // Fake Section to store Data Elements - Section section = new Section(); - - sectionList.add( section ); - section.setId( 0 ); - section.setName( "" ); - - List dataElementList = new ArrayList(); - section.setDataElements( dataElementList ); - - for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) - { - // Server DataElement - dataElement = i18n( i18nService, locale, dataElement ); - Set deCatOptCombs = dataElement.getCategoryCombo().getOptionCombos(); - // Client DataElement - ModelList deCateOptCombo = new ModelList(); - List listCateOptCombo = new ArrayList(); - deCateOptCombo.setModels( listCateOptCombo ); - - for ( DataElementCategoryOptionCombo oneCatOptCombo : deCatOptCombs ) - { - Model oneCateOptCombo = new Model(); - oneCateOptCombo.setId( oneCatOptCombo.getId() ); - oneCateOptCombo.setName( oneCatOptCombo.getName() ); - listCateOptCombo.add( oneCateOptCombo ); - } - - DataElement de = new DataElement(); - de.setId( dataElement.getId() ); - de.setName( dataElement.getName() ); - de.setType( dataElement.getType() ); - de.setCategoryOptionCombos( deCateOptCombo ); - dataElementList.add( de ); - } - } - else - { - for ( org.hisp.dhis.dataset.Section each : sections ) - { - List dataElements = each.getDataElements(); - - Section section = new Section(); - section.setId( each.getId() ); - section.setName( each.getName() ); - // Mobile - List dataElementList = new ArrayList(); - section.setDataElements( dataElementList ); - - for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) - { - // Server DataElement - dataElement = i18n( i18nService, locale, dataElement ); - Set deCatOptCombs = dataElement.getCategoryCombo() - .getOptionCombos(); - - // Client DataElement - ModelList deCateOptCombo = new ModelList(); - List listCateOptCombo = new ArrayList(); - deCateOptCombo.setModels( listCateOptCombo ); - - for ( DataElementCategoryOptionCombo oneCatOptCombo : deCatOptCombs ) - { - Model oneCateOptCombo = new Model(); - oneCateOptCombo.setId( oneCatOptCombo.getId() ); - oneCateOptCombo.setName( oneCatOptCombo.getName() ); - listCateOptCombo.add( oneCateOptCombo ); - } - - DataElement de = new DataElement(); - de.setId( dataElement.getId() ); - de.setName( dataElement.getName() ); - de.setType( dataElement.getType() ); - de.setCategoryOptionCombos( deCateOptCombo ); - dataElementList.add( de ); - } - sectionList.add( section ); - } - } - - return ds; - } - -} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataValueService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataValueService.java 2010-11-15 08:35:53 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/DefaultDataValueService.java 1970-01-01 00:00:00 +0000 @@ -1,199 +0,0 @@ -package org.hisp.dhis.web.api.service; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.user.CurrentUserService; -import org.hisp.dhis.web.api.model.DataSetValue; -import org.hisp.dhis.web.api.model.DataValue; -import org.springframework.beans.factory.annotation.Autowired; - -public class DefaultDataValueService - implements IDataValueService -{ - - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private org.hisp.dhis.dataset.DataSetService dataSetService; - - public org.hisp.dhis.dataset.DataSetService getDataSetService() - { - return dataSetService; - } - - public void setDataSetService( org.hisp.dhis.dataset.DataSetService dataSetService ) - { - this.dataSetService = dataSetService; - } - - private IPeriodService periodService; - - public IPeriodService getPeriodService() - { - return periodService; - } - - public void setPeriodService( IPeriodService periodService ) - { - this.periodService = periodService; - } - - private org.hisp.dhis.dataelement.DataElementCategoryService categoryService; - - public org.hisp.dhis.dataelement.DataElementCategoryService getCategoryService() - { - return categoryService; - } - - public void setCategoryService( org.hisp.dhis.dataelement.DataElementCategoryService categoryService ) - { - this.categoryService = categoryService; - } - - private org.hisp.dhis.datavalue.DataValueService dataValueService; - - public org.hisp.dhis.datavalue.DataValueService getDataValueService() - { - return dataValueService; - } - - public void setDataValueService( org.hisp.dhis.datavalue.DataValueService dataValueService ) - { - this.dataValueService = dataValueService; - } - - private CurrentUserService currentUserService; - - public CurrentUserService getCurrentUserService() - { - return currentUserService; - } - - public void setCurrentUserService( CurrentUserService currentUserService ) - { - this.currentUserService = currentUserService; - } - - // ------------------------------------------------------------------------- - // DataValueService - // ------------------------------------------------------------------------- - - @Override - public String saveValues( DataSetValue dataSetValue ) - { - - Collection units = currentUserService.getCurrentUser().getOrganisationUnits(); - OrganisationUnit unit = null; - - if ( units.size() > 0 ) - { - unit = units.iterator().next(); - } - else - { - return "INVALID_REPORTING_UNIT"; - } - - org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetValue.getId() ); - - if ( !dataSetService.getDataSetsBySource( unit ).contains( dataSet ) ) - { - return "INVALID_DATASET_ASSOCIATION"; - } - - org.hisp.dhis.period.Period selectedPeriod = periodService.getPeriod( dataSetValue.getpName(), dataSet ); - - if ( selectedPeriod == null ) - { - return "INVALID_PERIOD"; - } - - Collection dataElements = dataSet.getDataElements(); - Collection dataElementIds = new ArrayList( dataSetValue.getDataValues().size() ); - - for ( DataValue dv : dataSetValue.getDataValues() ) - { - dataElementIds.add( dv.getId() ); - } - - // if ( dataElements.size() != dataElementIds.size() ) - // { - // return "INVALID_DATASET"; - // } - - Map dataElementMap = new HashMap(); - for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) - { - if ( !dataElementIds.contains( dataElement.getId() ) ) - { - return "INVALID_DATASET"; - } - dataElementMap.put( dataElement.getId(), dataElement ); - } - - // Everything is fine, hence save - saveDataValues( dataSetValue, dataElementMap, selectedPeriod, unit, - categoryService.getDefaultDataElementCategoryOptionCombo() ); - - return "SUCCESS"; - - } - - // ------------------------------------------------------------------------- - // Supportive method - // ------------------------------------------------------------------------- - - private void saveDataValues( DataSetValue dataSetValue, Map dataElementMap, Period period, - OrganisationUnit orgUnit, DataElementCategoryOptionCombo optionCombo ) - { - - org.hisp.dhis.dataelement.DataElement dataElement; - String value; - - for ( DataValue dv : dataSetValue.getDataValues() ) - { - value = dv.getVal(); - DataElementCategoryOptionCombo cateOptCombo = categoryService.getDataElementCategoryOptionCombo( dv - .getCategoryOptComboID() ); - if ( value != null && value.trim().length() == 0 ) - { - value = null; - } - - if ( value != null ) - { - value = value.trim(); - } - - dataElement = dataElementMap.get( dv.getId() ); - org.hisp.dhis.datavalue.DataValue dataValue = dataValueService.getDataValue( orgUnit, dataElement, period, - cateOptCombo ); - - if ( dataValue == null ) - { - if ( value != null ) - { - dataValue = new org.hisp.dhis.datavalue.DataValue( dataElement, period, orgUnit, value, "", - new Date(), "", cateOptCombo ); - dataValueService.addDataValue( dataValue ); - } - } - else - { - dataValue.setValue( value ); - dataValue.setTimestamp( new Date() ); - dataValueService.updateDataValue( dataValue ); - } - - } - } -} === added 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 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingService.java 2010-11-16 18:26:11 +0000 @@ -0,0 +1,46 @@ +package org.hisp.dhis.web.api.service; + +/* + * Copyright (c) 2004-2010, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.util.List; +import java.util.Locale; + +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.web.api.model.DataSet; +import org.hisp.dhis.web.api.model.DataSetValue; + +public interface FacilityReportingService +{ + + public List getMobileDataSetsForUnit( OrganisationUnit unit, String localeString ); + + public DataSet getDataSetForLocale( int dataSetId, Locale locale ); + + public String saveDataSetValues( OrganisationUnit unit, DataSetValue dataSetValue ); + +} === added 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 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java 2010-11-16 18:26:11 +0000 @@ -0,0 +1,346 @@ +package org.hisp.dhis.web.api.service; + +/* + * Copyright (c) 2004-2010, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import static org.hisp.dhis.i18n.I18nUtils.i18n; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.comparator.DataElementSortOrderComparator; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.period.Period; +import org.hisp.dhis.web.api.model.DataElement; +import org.hisp.dhis.web.api.model.DataSet; +import org.hisp.dhis.web.api.model.DataSetValue; +import org.hisp.dhis.web.api.model.DataValue; +import org.hisp.dhis.web.api.model.Model; +import org.hisp.dhis.web.api.model.ModelList; +import org.hisp.dhis.web.api.model.Section; +import org.hisp.dhis.web.api.utils.LocaleUtil; +import org.springframework.beans.factory.annotation.Required; + +public class FacilityReportingServiceImpl + implements FacilityReportingService +{ + + private DataElementSortOrderComparator dataElementComparator = new DataElementSortOrderComparator(); + + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private IPeriodService periodService; + + private org.hisp.dhis.dataelement.DataElementCategoryService categoryService; + + private org.hisp.dhis.datavalue.DataValueService dataValueService; + + private org.hisp.dhis.dataset.DataSetService dataSetService; + + private org.hisp.dhis.i18n.I18nService i18nService; + + // ------------------------------------------------------------------------- + // Service methods + // ------------------------------------------------------------------------- + + public List getMobileDataSetsForUnit( OrganisationUnit unit, String localeString ) + { + + List datasets = new ArrayList(); + Locale locale = LocaleUtil.getLocale( localeString ); + + for ( org.hisp.dhis.dataset.DataSet dataSet : dataSetService.getDataSetsForMobile( unit ) ) + { + if ( dataSet.getPeriodType().getName().equals( "Daily" ) + || dataSet.getPeriodType().getName().equals( "Weekly" ) + || dataSet.getPeriodType().getName().equals( "Monthly" ) + || dataSet.getPeriodType().getName().equals( "Yearly" ) + || dataSet.getPeriodType().getName().equals( "Quarterly" ) ) + { + datasets.add( getDataSetForLocale( dataSet.getId(), locale ) ); + } + } + + return datasets; + } + + public DataSet getDataSetForLocale( int dataSetId, Locale locale ) + { + org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetId ); + dataSet = i18n( i18nService, locale, dataSet ); + Set sections = dataSet.getSections(); + + // Collection dataElements = + // dataSet.getDataElements(); + + // Mobile + DataSet ds = new DataSet(); + + ds.setId( dataSet.getId() ); + ds.setName( dataSet.getName() ); + ds.setPeriodType( dataSet.getPeriodType().getName() ); + + // Mobile + List
sectionList = new ArrayList
(); + ds.setSections( sectionList ); + + if ( sections.size() == 0 || sections == null ) + { + // Collection dataElements = + // new ArrayList(); + List dataElements = new ArrayList( + dataSet.getDataElements() ); + + Collections.sort( dataElements, dataElementComparator ); + + // Fake Section to store Data Elements + Section section = new Section(); + + sectionList.add( section ); + section.setId( 0 ); + section.setName( "" ); + + List dataElementList = new ArrayList(); + section.setDataElements( dataElementList ); + + for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) + { + // Server DataElement + dataElement = i18n( i18nService, locale, dataElement ); + Set deCatOptCombs = dataElement.getCategoryCombo().getOptionCombos(); + // Client DataElement + ModelList deCateOptCombo = new ModelList(); + List listCateOptCombo = new ArrayList(); + deCateOptCombo.setModels( listCateOptCombo ); + + for ( DataElementCategoryOptionCombo oneCatOptCombo : deCatOptCombs ) + { + Model oneCateOptCombo = new Model(); + oneCateOptCombo.setId( oneCatOptCombo.getId() ); + oneCateOptCombo.setName( oneCatOptCombo.getName() ); + listCateOptCombo.add( oneCateOptCombo ); + } + + DataElement de = new DataElement(); + de.setId( dataElement.getId() ); + de.setName( dataElement.getName() ); + de.setType( dataElement.getType() ); + de.setCategoryOptionCombos( deCateOptCombo ); + dataElementList.add( de ); + } + } + else + { + for ( org.hisp.dhis.dataset.Section each : sections ) + { + List dataElements = each.getDataElements(); + + Section section = new Section(); + section.setId( each.getId() ); + section.setName( each.getName() ); + // Mobile + List dataElementList = new ArrayList(); + section.setDataElements( dataElementList ); + + for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) + { + // Server DataElement + dataElement = i18n( i18nService, locale, dataElement ); + Set deCatOptCombs = dataElement.getCategoryCombo() + .getOptionCombos(); + + // Client DataElement + ModelList deCateOptCombo = new ModelList(); + List listCateOptCombo = new ArrayList(); + deCateOptCombo.setModels( listCateOptCombo ); + + for ( DataElementCategoryOptionCombo oneCatOptCombo : deCatOptCombs ) + { + Model oneCateOptCombo = new Model(); + oneCateOptCombo.setId( oneCatOptCombo.getId() ); + oneCateOptCombo.setName( oneCatOptCombo.getName() ); + listCateOptCombo.add( oneCateOptCombo ); + } + + DataElement de = new DataElement(); + de.setId( dataElement.getId() ); + de.setName( dataElement.getName() ); + de.setType( dataElement.getType() ); + de.setCategoryOptionCombos( deCateOptCombo ); + dataElementList.add( de ); + } + sectionList.add( section ); + } + } + + return ds; + } + + @Override + public String saveDataSetValues( OrganisationUnit unit, DataSetValue dataSetValue ) + { + + org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( dataSetValue.getId() ); + + if ( !dataSetService.getDataSetsBySource( unit ).contains( dataSet ) ) + { + return "INVALID_DATASET_ASSOCIATION"; + } + + org.hisp.dhis.period.Period selectedPeriod = periodService.getPeriod( dataSetValue.getpName(), dataSet ); + + if ( selectedPeriod == null ) + { + return "INVALID_PERIOD"; + } + + Collection dataElements = dataSet.getDataElements(); + Collection dataElementIds = new ArrayList( dataSetValue.getDataValues().size() ); + + for ( DataValue dv : dataSetValue.getDataValues() ) + { + dataElementIds.add( dv.getId() ); + } + + // if ( dataElements.size() != dataElementIds.size() ) + // { + // return "INVALID_DATASET"; + // } + + Map dataElementMap = new HashMap(); + for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) + { + if ( !dataElementIds.contains( dataElement.getId() ) ) + { + return "INVALID_DATASET"; + } + dataElementMap.put( dataElement.getId(), dataElement ); + } + + // Everything is fine, hence save + saveDataValues( dataSetValue, dataElementMap, selectedPeriod, unit, + categoryService.getDefaultDataElementCategoryOptionCombo() ); + + return "SUCCESS"; + + } + + // ------------------------------------------------------------------------- + // Supportive method + // ------------------------------------------------------------------------- + + private void saveDataValues( DataSetValue dataSetValue, + Map dataElementMap, Period period, OrganisationUnit orgUnit, + DataElementCategoryOptionCombo optionCombo ) + { + + org.hisp.dhis.dataelement.DataElement dataElement; + String value; + + for ( DataValue dv : dataSetValue.getDataValues() ) + { + value = dv.getVal(); + DataElementCategoryOptionCombo cateOptCombo = categoryService.getDataElementCategoryOptionCombo( dv + .getCategoryOptComboID() ); + if ( value != null && value.trim().length() == 0 ) + { + value = null; + } + + if ( value != null ) + { + value = value.trim(); + } + + dataElement = dataElementMap.get( dv.getId() ); + org.hisp.dhis.datavalue.DataValue dataValue = dataValueService.getDataValue( orgUnit, dataElement, period, + cateOptCombo ); + + if ( dataValue == null ) + { + if ( value != null ) + { + dataValue = new org.hisp.dhis.datavalue.DataValue( dataElement, period, orgUnit, value, "", + new Date(), "", cateOptCombo ); + dataValueService.addDataValue( dataValue ); + } + } + else + { + dataValue.setValue( value ); + dataValue.setTimestamp( new Date() ); + dataValueService.updateDataValue( dataValue ); + } + + } + } + + // ------------------------------------------------------------------------- + // Dependency setters + // ------------------------------------------------------------------------- + + @Required + public void setPeriodService( IPeriodService periodService ) + { + this.periodService = periodService; + } + + @Required + public void setCategoryService( org.hisp.dhis.dataelement.DataElementCategoryService categoryService ) + { + this.categoryService = categoryService; + } + + @Required + public void setDataValueService( org.hisp.dhis.datavalue.DataValueService dataValueService ) + { + this.dataValueService = dataValueService; + } + + @Required + public void setDataSetService( org.hisp.dhis.dataset.DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + @Required + public void setI18nService( org.hisp.dhis.i18n.I18nService i18nService ) + { + this.i18nService = i18nService; + } + +} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataSetService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataSetService.java 2010-11-16 16:28:41 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataSetService.java 1970-01-01 00:00:00 +0000 @@ -1,14 +0,0 @@ -package org.hisp.dhis.web.api.service; - -import java.util.List; -import java.util.Locale; - -import org.hisp.dhis.web.api.model.ModelList; -import org.hisp.dhis.web.api.model.DataSet; - -public interface IDataSetService { - - List getAllMobileDataSetsForLocale( String localeString ); - - DataSet getDataSetForLocale( int dataSetId,Locale locale); -} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataValueService.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataValueService.java 2010-09-24 11:12:49 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/IDataValueService.java 1970-01-01 00:00:00 +0000 @@ -1,9 +0,0 @@ -package org.hisp.dhis.web.api.service; - -import org.hisp.dhis.web.api.model.DataSetValue; - -public interface IDataValueService { - - public String saveValues( DataSetValue dataSetValue ); - -} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2010-11-16 16:28:41 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2010-11-16 18:26:11 +0000 @@ -7,11 +7,10 @@ - + - @@ -34,28 +33,12 @@ - + - - - - - - - - - - - - - - - - - - - + + +