=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataExportParams.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataExportParams.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataExportParams.java 2015-06-01 22:07:39 +0000 @@ -0,0 +1,159 @@ +package org.hisp.dhis.dxf2.datavalueset; + +/* + * Copyright (c) 2004-2015, 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.Date; +import java.util.HashSet; +import java.util.Set; + +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dxf2.common.IdSchemes; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.period.Period; + +/** + * @author Lars Helge Overland + */ +public class DataExportParams +{ + private Set dataSets = new HashSet<>(); + + private Set periods = new HashSet<>(); + + private Date startDate; + + private Date endDate; + + private Set organisationUnits = new HashSet<>(); + + private boolean includeChildren; + + private IdSchemes idSchemes; + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + + public DataExportParams() + { + } + + // ------------------------------------------------------------------------- + // Logic + // ------------------------------------------------------------------------- + + public DataSet getFirstDataSet() + { + return dataSets != null && !dataSets.isEmpty() ? dataSets.iterator().next() : null; + } + + public Period getFirstPeriod() + { + return periods != null && !periods.isEmpty() ? periods.iterator().next() : null; + } + + public OrganisationUnit getFirstOrganisationUnit() + { + return organisationUnits != null && !organisationUnits.isEmpty() ? organisationUnits.iterator().next() : null; + } + + // ------------------------------------------------------------------------- + // Get and set methods + // ------------------------------------------------------------------------- + + public Set getDataSets() + { + return dataSets; + } + + public void setDataSets( Set dataSets ) + { + this.dataSets = dataSets; + } + + public Set getPeriods() + { + return periods; + } + + public void setPeriods( Set periods ) + { + this.periods = periods; + } + + public Date getStartDate() + { + return startDate; + } + + public void setStartDate( Date startDate ) + { + this.startDate = startDate; + } + + public Date getEndDate() + { + return endDate; + } + + public void setEndDate( Date endDate ) + { + this.endDate = endDate; + } + + public Set getOrganisationUnits() + { + return organisationUnits; + } + + public void setOrganisationUnits( Set organisationUnits ) + { + this.organisationUnits = organisationUnits; + } + + public boolean isIncludeChildren() + { + return includeChildren; + } + + public void setIncludeChildren( boolean includeChildren ) + { + this.includeChildren = includeChildren; + } + + public IdSchemes getIdSchemes() + { + return idSchemes; + } + + public void setIdSchemes( IdSchemes idSchemes ) + { + this.idSchemes = idSchemes; + } +} === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java 2015-02-17 06:00:52 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java 2015-06-01 22:07:39 +0000 @@ -48,17 +48,22 @@ */ public interface DataValueSetService { - void writeDataValueSetXml( String dataSet, String period, String orgUnit, OutputStream out, IdSchemes idSchemes ); + DataExportParams getFromUrl( Set dataSets, Set periods, Date startDate, Date endDate, + Set organisationUnits, boolean includeChildren, IdSchemes idSchemes ); + + void validate( DataExportParams params ); + + void writeDataValueSetXml( DataExportParams params, OutputStream out ); void writeDataValueSetXml( Set dataSets, Date startDate, Date endDate, Set orgUnits, boolean includeChildren, OutputStream out, IdSchemes idSchemes ); - void writeDataValueSetJson( String ds, String period, String ou, OutputStream outputStream, IdSchemes idSchemes ); + void writeDataValueSetJson( DataExportParams params, OutputStream out ); void writeDataValueSetJson( Set dataSet, Date startDate, Date endDate, Set ous, boolean includeChildren, OutputStream outputStream, IdSchemes idSchemes ); void writeDataValueSetJson( Date lastUpdated, OutputStream outputStream, IdSchemes idSchemes ); - void writeDataValueSetCsv( String dataSet, String period, String orgUnit, Writer writer, IdSchemes idSchemes ); + void writeDataValueSetCsv( DataExportParams params, Writer writer ); void writeDataValueSetCsv( Set dataSets, Date startDate, Date endDate, Set orgUnits, boolean includeChildren, Writer writer, IdSchemes idSchemes ); === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java 2015-06-01 20:48:55 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java 2015-06-01 22:07:39 +0000 @@ -28,8 +28,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static com.google.common.collect.Sets.newHashSet; import static org.apache.commons.lang3.StringUtils.trimToNull; +import static org.hisp.dhis.common.IdentifiableObjectUtils.getUids; import static org.hisp.dhis.common.IdentifiableProperty.UUID; import static org.hisp.dhis.system.notification.NotificationLevel.ERROR; import static org.hisp.dhis.system.notification.NotificationLevel.INFO; @@ -40,6 +40,7 @@ import java.io.OutputStream; import java.io.Writer; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; @@ -84,7 +85,6 @@ import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.PeriodType; import org.hisp.dhis.scheduling.TaskId; import org.hisp.dhis.system.callable.CategoryOptionComboAclCallable; import org.hisp.dhis.system.callable.IdentifiableObjectCallable; @@ -107,9 +107,6 @@ { private static final Log log = LogFactory.getLog( DefaultDataValueSetService.class ); - private static final String ERROR_INVALID_DATA_SET = "Invalid data set: "; - private static final String ERROR_INVALID_PERIOD = "Invalid period: "; - private static final String ERROR_INVALID_ORG_UNIT = "Invalid org unit: "; private static final String ERROR_OBJECT_NEEDED_TO_COMPLETE = "Must be provided to complete data set"; @Autowired @@ -161,43 +158,100 @@ // DataValueSet implementation //-------------------------------------------------------------------------- + @Override + public DataExportParams getFromUrl( Set dataSets, Set periods, Date startDate, Date endDate, + Set organisationUnits, boolean includeChildren, IdSchemes idSchemes ) + { + DataExportParams params = new DataExportParams(); + + if ( dataSets != null ) + { + params.getDataSets().addAll( identifiableObjectManager.getByUid( DataSet.class, dataSets ) ); + } + + if ( periods != null ) + { + params.getPeriods().addAll( periodService.reloadIsoPeriods( new ArrayList( periods ) ) ); + } + + if ( organisationUnits != null ) + { + params.getOrganisationUnits().addAll( identifiableObjectManager.getByUid( OrganisationUnit.class, organisationUnits ) ); + + if ( includeChildren ) + { + params.setOrganisationUnits( new HashSet( + organisationUnitService.getOrganisationUnitsWithChildren( getUids( params.getOrganisationUnits() ) ) ) ); + } + } + + params.setStartDate( startDate ); + params.setEndDate( endDate ); + params.setIncludeChildren( includeChildren ); + params.setIdSchemes( idSchemes ); + + return params; + } + + @Override + public void validate( DataExportParams params ) + { + String violation = null; + + if ( params == null ) + { + throw new IllegalArgumentException( "Params cannot be null" ); + } + + if ( params.getDataSets().isEmpty() ) + { + violation = "At least one valid data set must be specified"; + } + + if ( params.getPeriods().isEmpty() && ( params.getStartDate() == null || params.getEndDate() == null ) ) + { + violation = "At least one valid period or start/end dates must be specified"; + } + + if ( params.getOrganisationUnits().isEmpty() ) + { + violation = "At least one valid organisation unit must be specified"; + } + + for ( OrganisationUnit unit : params.getOrganisationUnits() ) + { + if ( !organisationUnitService.isInUserHierarchy( unit ) ) + { + violation = "Organisation unit is not inside hierarchy of current user: " + unit.getUid(); + } + } + + if ( violation != null ) + { + log.warn( "Validation failed: " + violation ); + + throw new IllegalArgumentException( violation ); + } + } + //-------------------------------------------------------------------------- // Write //-------------------------------------------------------------------------- @Override - public void writeDataValueSetXml( String dataSet, String period, String orgUnit, OutputStream out, IdSchemes idSchemes ) + public void writeDataValueSetXml( DataExportParams params, OutputStream out ) { - DataSet dataSet_ = dataSetService.getDataSet( dataSet ); - Period period_ = PeriodType.getPeriodFromIsoString( period ); - OrganisationUnit orgUnit_ = organisationUnitService.getOrganisationUnit( orgUnit ); - - if ( dataSet_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_DATA_SET + dataSet ); - } - - if ( period_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_PERIOD + period ); - } - - if ( orgUnit_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + orgUnit ); - } + validate( params ); DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); //TODO CompleteDataSetRegistration registration = registrationService - .getCompleteDataSetRegistration( dataSet_, period_, orgUnit_, optionCombo ); + .getCompleteDataSetRegistration( params.getFirstDataSet(), params.getFirstPeriod(), params.getFirstOrganisationUnit(), optionCombo ); Date completeDate = registration != null ? registration.getDate() : null; - period_ = periodService.reloadPeriod( period_ ); - - dataValueSetStore.writeDataValueSetXml( newHashSet( dataSet_ ), completeDate, period_, orgUnit_, newHashSet( period_ ), - newHashSet( orgUnit_ ), out, idSchemes ); + dataValueSetStore.writeDataValueSetXml( params.getDataSets(), completeDate, params.getFirstPeriod(), params.getFirstOrganisationUnit(), + params.getPeriods(), params.getOrganisationUnits(), out, params.getIdSchemes() ); } @Override @@ -232,38 +286,19 @@ } @Override - public void writeDataValueSetJson( String dataSet, String period, String orgUnit, OutputStream outputStream, IdSchemes idSchemes ) + public void writeDataValueSetJson( DataExportParams params, OutputStream out ) { - DataSet dataSet_ = dataSetService.getDataSet( dataSet ); - Period period_ = PeriodType.getPeriodFromIsoString( period ); - OrganisationUnit orgUnit_ = organisationUnitService.getOrganisationUnit( orgUnit ); - - if ( dataSet_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_DATA_SET + dataSet ); - } - - if ( period_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_PERIOD + period ); - } - - if ( orgUnit_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + orgUnit ); - } + validate( params ); DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); //TODO CompleteDataSetRegistration registration = registrationService - .getCompleteDataSetRegistration( dataSet_, period_, orgUnit_, optionCombo ); + .getCompleteDataSetRegistration( params.getFirstDataSet(), params.getFirstPeriod(), params.getFirstOrganisationUnit(), optionCombo ); Date completeDate = registration != null ? registration.getDate() : null; - period_ = periodService.reloadPeriod( period_ ); - - dataValueSetStore.writeDataValueSetJson( newHashSet( dataSet_ ), completeDate, period_, orgUnit_, newHashSet( period_ ), - newHashSet( orgUnit_ ), outputStream, idSchemes ); + dataValueSetStore.writeDataValueSetJson( params.getDataSets(), completeDate, params.getFirstPeriod(), params.getFirstOrganisationUnit(), + params.getPeriods(), params.getOrganisationUnits(), out, params.getIdSchemes() ); } @Override @@ -304,38 +339,19 @@ } @Override - public void writeDataValueSetCsv( String dataSet, String period, String orgUnit, Writer writer, IdSchemes idSchemes ) + public void writeDataValueSetCsv( DataExportParams params, Writer writer ) { - DataSet dataSet_ = dataSetService.getDataSet( dataSet ); - Period period_ = PeriodType.getPeriodFromIsoString( period ); - OrganisationUnit orgUnit_ = organisationUnitService.getOrganisationUnit( orgUnit ); - - if ( dataSet_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_DATA_SET + dataSet ); - } - - if ( period_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_PERIOD + period ); - } - - if ( orgUnit_ == null ) - { - throw new IllegalArgumentException( ERROR_INVALID_ORG_UNIT + orgUnit ); - } - + validate( params ); + DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); //TODO CompleteDataSetRegistration registration = registrationService - .getCompleteDataSetRegistration( dataSet_, period_, orgUnit_, optionCombo ); + .getCompleteDataSetRegistration( params.getFirstDataSet(), params.getFirstPeriod(), params.getFirstOrganisationUnit(), optionCombo ); Date completeDate = registration != null ? registration.getDate() : null; - period_ = periodService.reloadPeriod( period_ ); - - dataValueSetStore.writeDataValueSetCsv( newHashSet( dataSet_ ), completeDate, period_, orgUnit_, newHashSet( period_ ), - newHashSet( orgUnit_ ), writer, idSchemes ); + dataValueSetStore.writeDataValueSetCsv( params.getDataSets(), completeDate, params.getFirstPeriod(), params.getFirstOrganisationUnit(), + params.getPeriods(), params.getOrganisationUnits(), writer, params.getIdSchemes() ); } @Override @@ -369,6 +385,10 @@ dataValueSetStore.writeDataValueSetCsv( ds, null, null, null, pe, ou, writer, idSchemes ); } + //-------------------------------------------------------------------------- + // Template + //-------------------------------------------------------------------------- + @Override public RootNode getDataValueSetTemplate( DataSet dataSet, Period period, List orgUnits, boolean writeComments, String ouScheme, String deScheme ) === modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java' --- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java 2015-05-28 15:04:54 +0000 +++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/SpringDataValueSetStore.java 2015-06-01 22:07:39 +0000 @@ -133,7 +133,7 @@ private void writeDataValueSet( String sql, Set dataSets, Date completeDate, Period period, OrganisationUnit orgUnit, final DataValueSet dataValueSet ) { - dataValueSet.setDataSet( (dataSets != null && dataSets.size() == 1) ? dataSets.iterator().next().getUid() : null ); + dataValueSet.setDataSet( ( dataSets != null && dataSets.size() == 1 ) ? dataSets.iterator().next().getUid() : null ); dataValueSet.setCompleteDate( getLongGmtDateString( completeDate ) ); dataValueSet.setPeriod( period != null ? period.getIsoDate() : null ); dataValueSet.setOrgUnit( orgUnit != null ? orgUnit.getUid() : null ); === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java 2015-05-11 06:02:29 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataValueSetController.java 2015-06-01 22:07:39 +0000 @@ -33,6 +33,7 @@ import org.hisp.dhis.dxf2.common.IdSchemes; import org.hisp.dhis.dxf2.common.ImportOptions; import org.hisp.dhis.dxf2.common.JacksonUtils; +import org.hisp.dhis.dxf2.datavalueset.DataExportParams; import org.hisp.dhis.dxf2.datavalueset.DataValueSetService; import org.hisp.dhis.dxf2.importsummary.ImportSummary; import org.hisp.dhis.webapi.utils.ContextUtils; @@ -45,7 +46,10 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import com.google.common.collect.Sets; + import javax.servlet.http.HttpServletResponse; + import java.io.IOException; import java.io.InputStream; import java.util.Date; @@ -53,6 +57,9 @@ import static org.hisp.dhis.webapi.utils.ContextUtils.*; +/** + * @author Lars Helge Overland + */ @Controller @RequestMapping( value = DataValueSetController.RESOURCE_PATH ) public class DataValueSetController @@ -80,6 +87,9 @@ { response.setContentType( CONTENT_TYPE_XML ); + DataExportParams params = dataValueSetService.getFromUrl( dataSet, Sets.newHashSet( period ), + startDate, endDate, orgUnit, children, idSchemes ); + boolean isSingleDataValueSet = dataSet.size() == 1 && period != null && orgUnit.size() == 1; if ( isSingleDataValueSet ) @@ -89,7 +99,7 @@ log.debug( "Get XML data value set for data set: " + ds + ", period: " + period + ", org unit: " + ou ); - dataValueSetService.writeDataValueSetXml( ds, period, ou, response.getOutputStream(), idSchemes ); + dataValueSetService.writeDataValueSetXml( params, response.getOutputStream() ); } else { @@ -111,6 +121,9 @@ { response.setContentType( CONTENT_TYPE_JSON ); + DataExportParams params = dataValueSetService.getFromUrl( dataSet, Sets.newHashSet( period ), + startDate, endDate, orgUnit, children, idSchemes ); + boolean isSingleDataValueSet = dataSet.size() == 1 && period != null && orgUnit.size() == 1; if ( isSingleDataValueSet ) @@ -120,7 +133,7 @@ log.debug( "Get JSON data value set for data set: " + ds + ", period: " + period + ", org unit: " + ou ); - dataValueSetService.writeDataValueSetJson( ds, period, ou, response.getOutputStream(), idSchemes ); + dataValueSetService.writeDataValueSetJson( params, response.getOutputStream() ); } else { @@ -143,6 +156,9 @@ { response.setContentType( CONTENT_TYPE_CSV ); + DataExportParams params = dataValueSetService.getFromUrl( dataSet, Sets.newHashSet( period ), + startDate, endDate, orgUnit, children, idSchemes ); + boolean isSingleDataValueSet = dataSet.size() == 1 && period != null && orgUnit.size() == 1; if ( isSingleDataValueSet ) @@ -152,7 +168,7 @@ log.debug( "Get CSV data value set for data set: " + ds + ", period: " + period + ", org unit: " + ou ); - dataValueSetService.writeDataValueSetCsv( ds, period, ou, response.getWriter(), idSchemes ); + dataValueSetService.writeDataValueSetCsv( params, response.getWriter() ); } else {