=== added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataElement.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataElement.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataElement.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,37 @@ +package org.hisp.dhis.importexport.dxf2.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; + +@XmlAccessorType(XmlAccessType.FIELD) +public class DataElement +{ + @XmlAttribute(name="id") + private String uuid; + + @XmlAttribute + private String name; + + public String getUuid() + { + return uuid; + } + + public void setUuid( String uuid ) + { + this.uuid = uuid; + } + + public String getName() + { + return name; + } + + public void setName( String name ) + { + this.name = name; + } + + +} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataSet.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataSet.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataSet.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,96 @@ +package org.hisp.dhis.importexport.dxf2.model; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType( XmlAccessType.FIELD ) +public class DataSet +{ + @XmlAttribute(name="id") + private String uuid; + + @XmlAttribute + private String name; + + @XmlAttribute + private String shortName; + + @XmlAttribute + private String code; + + @XmlAttribute + private String periodType; + + @XmlElementWrapper(name="members") + @XmlElement(name="dataElement") + private List members; + + public String getUuid() + { + return uuid; + } + + public void setUuid( String uuid ) + { + this.uuid = uuid; + } + + public String getName() + { + return name; + } + + public void setName( String name ) + { + this.name = name; + } + + public String getShortName() + { + return shortName; + } + + public void setShortName( String shortName ) + { + this.shortName = shortName; + } + + public String getCode() + { + return code; + } + + public void setCode( String code ) + { + this.code = code; + } + + public String getPeriodType() + { + return periodType; + } + + public void setPeriodType( String periodType ) + { + this.periodType = periodType; + } + + public List getMembers() + { + return members; + } + + public void setMembers( List members ) + { + this.members = members; + } + + +} === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataValueSet.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataValueSet.java 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/DataValueSet.java 2011-02-21 10:32:35 +0000 @@ -27,7 +27,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import java.util.Date; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; @@ -36,12 +35,12 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +@XmlRootElement @XmlAccessorType( XmlAccessType.FIELD ) -@XmlRootElement public class DataValueSet { - @XmlAttribute( name = "dataSet", required = true ) + @XmlAttribute( name = "dataSet" ) private String dataSetUuid; @XmlAttribute( name = "orgUnit", required = true ) === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/Dxf.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/Dxf.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/Dxf.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,46 @@ +package org.hisp.dhis.importexport.dxf2.model; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType( XmlAccessType.FIELD ) +public class Dxf +{ + + + @XmlElementWrapper( name="dataSets" ) + @XmlElement( name="dataSet" ) + private List dataSets; + + @XmlElementWrapper( name="dataValueSets" ) + @XmlElement( name="dataValueSet" ) + private List dataValueSets; + + public void setDataValueSets( List dataValueSets ) + { + this.dataValueSets = dataValueSets; + } + + public List getDataValueSets() + { + return dataValueSets; + } + + public List getDataSets() + { + return dataSets; + } + + public void setDataSets( List dataSets ) + { + this.dataSets = dataSets; + } + + +} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/OrgUnit.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/OrgUnit.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/OrgUnit.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,37 @@ +package org.hisp.dhis.importexport.dxf2.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; + +@XmlAccessorType(XmlAccessType.FIELD) +public class OrgUnit +{ + + @XmlAttribute(name="id") + private String uuid; + + @XmlAttribute + private String name; + + public String getUuid() + { + return uuid; + } + + public void setUuid( String uuid ) + { + this.uuid = uuid; + } + + public void setName( String name ) + { + this.name = name; + } + + public String getName() + { + return name; + } + +} === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/package-info.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/package-info.java 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/model/package-info.java 2011-02-21 10:32:35 +0000 @@ -1,6 +1,5 @@ @javax.xml.bind.annotation.XmlSchema( - namespace = "http://dhis2.org/schema/dxf/2.0-SNAPSHOT", - xmlns = { @javax.xml.bind.annotation.XmlNs( prefix = "xs", namespaceURI = "http://www.w3.org/2001/XMLSchema" ) }, + namespace = "http://dhis2.org/schema/dxf/2.0-SNAPSHOT", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED ) package org.hisp.dhis.importexport.dxf2.model; === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataSetMapper.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataSetMapper.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataSetMapper.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,40 @@ +package org.hisp.dhis.importexport.dxf2.service; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.hisp.dhis.importexport.dxf2.model.DataElement; +import org.hisp.dhis.importexport.dxf2.model.DataSet; + +public class DataSetMapper +{ + + public static DataSet convert(org.hisp.dhis.dataset.DataSet dataSet) { + DataSet dxfDataSet = new DataSet(); + + dxfDataSet.setCode( dataSet.getCode() ); + dxfDataSet.setMembers( getDataElements(dataSet.getDataElements()) ); + dxfDataSet.setName( dataSet.getName() ); + dxfDataSet.setPeriodType( dataSet.getPeriodType().getName() ); + dxfDataSet.setShortName( dataSet.getShortName() ); + dxfDataSet.setUuid( dataSet.getUuid() ); + + return dxfDataSet; + } + + public static List getDataElements( Collection dataElements ) + { + List dxfDataElements = new ArrayList(); + + for ( org.hisp.dhis.dataelement.DataElement dataElement : dataElements ) + { + DataElement dxfDataElement = new DataElement(); + dxfDataElement.setName( dataElement.getName() ); + dxfDataElement.setUuid( dataElement.getUuid() ); + + dxfDataElements.add( dxfDataElement ); + } + return dxfDataElements; + } +} === 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-02-18 20:17:34 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetService.java 2011-02-21 10:32:35 +0000 @@ -29,9 +29,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; @@ -49,6 +49,7 @@ import org.hisp.dhis.period.DailyPeriodType; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodType; +import org.hisp.dhis.user.CurrentUserService; import org.springframework.beans.factory.annotation.Required; import org.springframework.transaction.annotation.Transactional; @@ -67,44 +68,58 @@ private CompleteDataSetRegistrationService registrationService; + private CurrentUserService currentUserService; + /** - * Save a dataValueSet if all of the following is valid + * Save a dataValueSet. + *

+ * Handles the content in the following way + *

    + *
  • if data set is not specified, will resolve it through data elements if not ambigous. + *
  • optionCombo defaults to 'default' if not specified + *
  • storedBy defaults to currently logged in user's name + *
+ *
    + * Validates the following: *

    * First checks that: *

      - *
    • dataSet exists + *
    • dataSet exists (tries to resolve it through the data elements if not specified) *
    • 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 - *
    • a present complete date is valid + *
    • if complete date present checks validity *
    - * For all dataValues it checks that: + * For all dataValues check that: *
      *
    • dataElement exists and is in dataSet - *
    • optionCombo exists (defaults to 'default' if not specified) and is in dataElement + *
    • optionCombo exists and is in dataElement *
    * What isn't checked yet: *
      - *
    • That there isn't duplicated value entries (will throw Constraint exception) - *
    • That the value is valid! + *
    • 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) *
    * Concerns: *
      *
    • deletion through sending "empty string" value dependant on semantics of add/update in data value store - *
    • completed semantics, can't uncomplete but can complete and "recomplete" - *
    • what is 'comment' really? + *
    • completed semantics: can't uncomplete but can complete and "recomplete" + *
    • what is 'comment' good for really? * * @param dataValueSet - * @throws IllegalArgumentException if + * @throws IllegalArgumentException if there are any inconsistencies */ + @Transactional public void saveDataValueSet( DataValueSet dataValueSet ) throws IllegalArgumentException { + Date timestamp = new Date(); - DataSet dataSet = getDataSet( dataValueSet.getDataSetUuid() ); + DataSet dataSet = getDataSet( dataValueSet ); OrganisationUnit unit = getOrgUnit( dataValueSet.getOrganisationUnitUuid() ); @@ -116,6 +131,131 @@ Period period = getPeriod( dataValueSet.getPeriodIsoDate(), dataSet.getPeriodType() ); + handleComplete( dataValueSet, dataSet, unit, period ); + + for ( org.hisp.dhis.importexport.dxf2.model.DataValue dxfValue : dataValueSet.getDataValues() ) + { + saveDataValue( timestamp, dataSet, unit, period, dxfValue ); + } + + } + + private DataSet getDataSet( DataValueSet dataValueSet ) + { + DataSet dataSet = null; + + String uuid = dataValueSet.getDataSetUuid(); + if ( uuid != null ) + { + dataSet = dataSetService.getDataSet( uuid ); + + if ( dataSet == null ) + { + throw new IllegalArgumentException( "Data set with UUID " + uuid + " does not exist" ); + } + } + else + { + dataSet = resolveDataSet( dataValueSet ); + } + return dataSet; + } + + private DataSet resolveDataSet( DataValueSet dataValueSet ) + { + if ( dataValueSet.getDataValues() == null ) + { + throw new IllegalArgumentException( + "Data value set doesn't specify data set and does not contain data values." ); + } + + Set potential = null; + + for ( org.hisp.dhis.importexport.dxf2.model.DataValue value : dataValueSet.getDataValues() ) + { + DataElement dataElement = getDataElement( value.getDataElementUuid() ); + Set dataSets = dataElement.getDataSets(); + + if ( dataSets == null || dataSets.isEmpty() ) + { + throw new IllegalArgumentException( "Data element '" + dataElement.getUuid() + + "' isn't in a data set." ); + } + else if ( dataSets.size() == 1 ) + { + return dataSets.iterator().next(); + } + else + { + if ( potential == null ) + { + potential = new HashSet( dataSets ); + } + else + { + for ( DataSet set : dataSets ) + { + if ( !potential.contains( set ) ) + { + potential.remove( set ); + } + } + if ( potential.size() == 1 ) + { + return potential.iterator().next(); + } + } + } + } + + // TODO: Check if potential data sets are compatible + + String message = "Ambiguous which of these data set the data values belong to: "; + for ( DataSet p : potential ) + { + message += p.getUuid() + ", "; + } + message.substring( 0, message.length() - 2 ); + throw new IllegalArgumentException( message ); + } + + private void saveDataValue( Date timestamp, DataSet dataSet, OrganisationUnit unit, Period period, + org.hisp.dhis.importexport.dxf2.model.DataValue dxfValue ) + { + DataElement dataElement = getDataElement( dxfValue.getDataElementUuid() ); + + if ( !dataSet.getDataElements().contains( dataElement ) ) + { + throw new IllegalArgumentException( "Data element '" + dataElement.getUuid() + "' isn't in data set " + + dataSet.getUuid() ); + } + + DataElementCategoryOptionCombo combo = getOptionCombo( dxfValue.getCategoryOptionComboUuid(), dataElement ); + + DataValue dv = dataValueService.getDataValue( unit, dataElement, period, combo ); + + String value = dxfValue.getValue(); + + // dataElement.isValidValue(value); + + String storedBy = getStoredBy( dxfValue ); + + if ( dv == null ) + { + dv = new DataValue( dataElement, period, unit, value, storedBy, timestamp, null, combo ); + dataValueService.addDataValue( dv ); + } + else + { + dv.setValue( value ); + dv.setTimestamp( timestamp ); + dv.setStoredBy( storedBy ); + dataValueService.updateDataValue( dv ); + } + } + + private void handleComplete( DataValueSet dataValueSet, DataSet dataSet, OrganisationUnit unit, Period period ) + { CompleteDataSetRegistration alreadyComplete = registrationService.getCompleteDataSetRegistration( dataSet, period, unit ); String completeDateString = dataValueSet.getCompleteDate(); @@ -126,6 +266,11 @@ "DataValueSet is complete, include a new complete date if you want to recomplete" ); } + if ( alreadyComplete != null ) + { + registrationService.deleteCompleteDataSetRegistration( alreadyComplete ); + } + CompleteDataSetRegistration complete = null; if ( completeDateString != null ) @@ -133,39 +278,21 @@ complete = getComplete( dataSet, unit, period, completeDateString, complete ); } - List newDataValues = new ArrayList( dataValueSet.getDataValues().size() ); - List updatedDataValues = new ArrayList( dataValueSet.getDataValues().size() ); - - for ( org.hisp.dhis.importexport.dxf2.model.DataValue dxfValue : dataValueSet.getDataValues() ) - { - DataElement dataElement = getDataElement( dxfValue.getDataElementUuid(), dataSet ); - - DataElementCategoryOptionCombo combo = getOptionCombo( dxfValue.getCategoryOptionComboUuid(), dataElement ); - - - - DataValue dv = dataValueService.getDataValue( unit, dataElement, period, combo ); - - String value = dxfValue.getValue(); - - // dataElement.isValidValue(value); - - if ( dv == null ) - { - dv = new DataValue( dataElement, period, unit, value, dxfValue.getStoredBy(), timestamp, - null, combo ); - newDataValues.add( dv ); - } - else - { - dv.setValue( value ); - dv.setTimestamp( timestamp ); - dv.setStoredBy( dxfValue.getStoredBy() ); - updatedDataValues.add( dv ); - } - } - - save( alreadyComplete, complete, newDataValues, updatedDataValues ); + if ( complete != null ) + { + registrationService.saveCompleteDataSetRegistration( complete ); + } + } + + private String getStoredBy( org.hisp.dhis.importexport.dxf2.model.DataValue dxfValue ) + { + String storedBy = dxfValue.getStoredBy(); + + if ( storedBy == null || storedBy.trim().equals( "" ) ) + { + storedBy = currentUserService.getCurrentUsername(); + } + return storedBy; } private CompleteDataSetRegistration getComplete( DataSet dataSet, OrganisationUnit unit, Period period, @@ -211,18 +338,7 @@ return unit; } - private DataSet getDataSet( String uuid ) - { - DataSet dataSet = dataSetService.getDataSet( uuid ); - - if ( dataSet == null ) - { - throw new IllegalArgumentException( "Data set with UUID " + uuid + " does not exist" ); - } - return dataSet; - } - - private DataElement getDataElement( String uuid, DataSet dataSet ) + private DataElement getDataElement( String uuid ) { DataElement dataElement = dataElementService.getDataElement( uuid ); @@ -231,11 +347,6 @@ throw new IllegalArgumentException( "Data element with UUID " + uuid + " does not exist" ); } - if ( !dataSet.getDataElements().contains( dataElement ) ) - { - throw new IllegalArgumentException( "Data element '" + dataElement.getUuid() + "' isn't in data set " - + dataSet.getUuid() ); - } return dataElement; } @@ -266,31 +377,6 @@ return combo; } - @Transactional - private void save( CompleteDataSetRegistration alreadyComplete, CompleteDataSetRegistration complete, - List newDataValues, List updatedDataValues ) - { - if ( alreadyComplete != null ) - { - registrationService.deleteCompleteDataSetRegistration( alreadyComplete ); - } - - for ( DataValue dataValue : newDataValues ) - { - dataValueService.addDataValue( dataValue ); - } - - for ( DataValue dataValue : updatedDataValues ) - { - dataValueService.updateDataValue( dataValue ); - } - - if ( complete != null ) - { - registrationService.saveCompleteDataSetRegistration( complete ); - } - } - public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) { this.organisationUnitService = organisationUnitService; @@ -323,4 +409,10 @@ this.registrationService = registrationService; } + @Required + public void setCurrentUserService( CurrentUserService currentUserService ) + { + this.currentUserService = currentUserService; + } + } === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml 2011-02-21 10:32:35 +0000 @@ -416,6 +416,7 @@ + === added file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataSetMappingTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataSetMappingTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataSetMappingTest.java 2011-02-21 10:32:35 +0000 @@ -0,0 +1,113 @@ +package org.hisp.dhis.importexport.dxf2.service; + +/* + * Copyright (c) 2011, 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.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +import org.hisp.dhis.DhisTest; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.datavalue.DataValue; +import org.hisp.dhis.datavalue.DataValueService; +import org.hisp.dhis.importexport.ImportException; +import org.hisp.dhis.importexport.ImportParams; +import org.hisp.dhis.importexport.ImportService; +import org.hisp.dhis.importexport.ImportStrategy; +import org.hisp.dhis.importexport.dxf2.model.DataSet; +import org.hisp.dhis.importexport.util.ImportExportUtils; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Stub test class... + */ +public class DataSetMappingTest + extends DhisTest +{ + + private static final String DATA_SET_UUID = "16B2299E-ECD6-46CF-A61F-817D350C180D"; + + private ImportService importService; + + // ------------------------------------------------------------------------- + // Fixture + // ------------------------------------------------------------------------- + + @Override + public void setUpTest() + throws JAXBException, IOException, ImportException + { + importService = (ImportService) getBean( "org.hisp.dhis.importexport.ImportService" ); + categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID ); + dataElementService = (DataElementService) getBean( DataElementService.ID ); + dataSetService = (DataSetService) getBean( DataSetService.ID ); + dataValueService = (DataValueService) getBean( DataValueService.ID ); + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + InputStream is = classLoader.getResourceAsStream( "dxf2/base.xml" ); + ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, + false ); + importService.importData( importParams, is ); + is.close(); + + } + + // ------------------------------------------------------------------------- + // Tests + // ------------------------------------------------------------------------- + + @Test @Ignore + public void testJaxb() + throws JAXBException, IOException + { + JAXBContext jc = JAXBContext.newInstance( DataSet.class ); + Marshaller u = jc.createMarshaller(); + + StringWriter writer = new StringWriter(); + org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet( DATA_SET_UUID ); + DataSet dxfDataSet = DataSetMapper.convert( dataSet ); + u.marshal( dxfDataSet, writer ); + + System.out.println( writer.toString() ); + } + + @Override + protected boolean emptyDatabaseAfterTest() + { + return true; + } + +} === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java 2011-02-21 10:32:35 +0000 @@ -58,10 +58,11 @@ import org.junit.Test; /** - * Messy test class checking that jaxb produces the expected java - * @link{DataValueSet data value set} structure, that the set is converted, validated and - * stored into a correct set of {@link DataValue data values} - * . + * Messy test class checking that jaxb produces the expected java + * + * @link{DataValueSet data value set} structure, that the set is converted, + * validated and stored into a correct set of + * {@link DataValue data values} . */ public class DataValueSetServiceTest extends DhisTest @@ -186,8 +187,10 @@ } - @Test @Ignore - public void testValidvalue() { + @Test + @Ignore + public void testValidvalue() + { setValue( "" ); try @@ -198,21 +201,26 @@ { // Expected } - - } - - @Test @Ignore - public void testDuplicatedDataValues() { - - } - - @Test @Ignore - public void testExistingComboButNotInDataElement() { - - } - - @Test - public void deleteDataValue() { + + } + + @Test + @Ignore + public void testDuplicatedDataValues() + { + + } + + @Test + @Ignore + public void testExistingComboButNotInDataElement() + { + + } + + @Test + public void deleteDataValue() + { service.saveDataValueSet( dataValueSet ); Collection dataValues = dataValueService.getAllDataValues(); @@ -227,23 +235,17 @@ dataValues = dataValueService.getAllDataValues(); assertEquals( 0, dataValues.size() ); - + } - + @Test public void dataSetMissing() { dataValueSet.setDataSetUuid( null ); - try - { - service.saveDataValueSet( dataValueSet ); - fail( "Should miss data set" ); + setValue( "999" ); + + testSave( "999" ); - } - catch ( IllegalArgumentException e ) - { - // Expected - } } @Test @@ -262,7 +264,7 @@ } dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_NOT_IN_SET_UUID ); - + try { service.saveDataValueSet( dataValueSet ); @@ -276,7 +278,8 @@ } @Test - public void illegalPeriod() { + public void illegalPeriod() + { dataValueSet.setPeriodIsoDate( "2011" ); @@ -291,9 +294,10 @@ // Expected } } - + @Test - public void completeness() { + public void completeness() + { service.saveDataValueSet( dataValueSet ); @@ -305,13 +309,13 @@ try { service.saveDataValueSet( dataValueSet ); - fail("Shouldn't allow saving to a completed set"); + fail( "Shouldn't allow saving to a completed set" ); } catch ( IllegalArgumentException e ) { // TODO: Expected } - + dataValueSet.setCompleteDate( "201lala" ); try @@ -325,11 +329,12 @@ dataValueSet.setCompleteDate( "20101010" ); service.saveDataValueSet( dataValueSet ); - + } @Test - public void elementExistsAndNotInSet() { + public void elementExistsAndNotInSet() + { org.hisp.dhis.importexport.dxf2.model.DataValue dv = new org.hisp.dhis.importexport.dxf2.model.DataValue(); dv.setDataElementUuid( "ladida" ); @@ -360,7 +365,8 @@ } @Test - public void optionComboExistsAndInDataElement() { + public void optionComboExistsAndInDataElement() + { dataValueSet.getDataValues().get( 0 ).setCategoryOptionComboUuid( DEFAULT_COMBO_UUID ); @@ -379,44 +385,39 @@ } } - - @Test public void testUpdate() { - long before = new Date().getTime(); - - service.saveDataValueSet( dataValueSet ); - - long after = new Date().getTime(); - - Collection dataValues = dataValueService.getAllDataValues(); - assertEquals( 1, dataValues.size() ); - - DataValue dataValue = dataValues.iterator().next(); - - verifyDataValue( before, after, dataValue ); + testSave("11"); // Update - setValue("101"); - - before = new Date().getTime(); + setValue( "101" ); + + testSave( "101" ); + + } + + private void testSave( String value ) + { + Collection dataValues; + DataValue dataValue; + + long before = new Date().getTime(); service.saveDataValueSet( dataValueSet ); - after = new Date().getTime(); + long after = new Date().getTime(); dataValues = dataValueService.getAllDataValues(); assertEquals( 1, dataValues.size() ); dataValue = dataValues.iterator().next(); - verifyDataValue( before, after, dataValue, "101" ); - + verifyDataValue( before, after, dataValue, value ); } - private void setValue(String value) + private void setValue( String value ) { dataValueSet.getDataValues().get( 0 ).setValue( value ); } @@ -447,5 +448,4 @@ return true; } - } === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ApiResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ApiResource.java 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/ApiResource.java 2011-02-21 10:32:35 +0000 @@ -29,7 +29,7 @@ sb.append( "

      The api currently supports two specific pilot use cases, posting of data value sets and a mobile GPRS client." ); - sb.append( "

      Posting data value sets (experimental)

      " ); + sb.append( "

      Posting data value sets

      " ); sb.append( "

      To find the needed information about the data sets you want to post data about, go to " ); URI uri = uriInfo.getBaseUriBuilder().path( DataSetResource.class ).build(); sb.append( "the data set list.

      " ); === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DataSetResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DataSetResource.java 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DataSetResource.java 2011-02-21 10:32:35 +0000 @@ -15,6 +15,7 @@ import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.importexport.dxf2.service.DataSetMapper; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.springframework.beans.factory.annotation.Required; @@ -26,7 +27,7 @@ @Context UriInfo uriInfo; - + @GET @Produces( MediaType.TEXT_HTML ) public String getDataSetList() @@ -49,6 +50,20 @@ } @GET + @Produces( { MediaType.APPLICATION_XML, DhisMediaType.DXF } ) + @Path( "{uuid}/dxf" ) + public org.hisp.dhis.importexport.dxf2.model.DataSet getDataSetXml( @PathParam( "uuid" ) String uuid ) + { + DataSet dataSet = dataSetService.getDataSet( uuid ); + + if ( dataSet == null ) + { + throw new IllegalArgumentException( "No dataset with uuid " + uuid ); + } + return DataSetMapper.convert( dataSet ); + } + + @GET @Path( "{uuid}" ) @Produces( MediaType.TEXT_HTML ) public String getDataSet( @PathParam( "uuid" ) String uuid ) @@ -64,6 +79,9 @@ StringBuilder t = new StringBuilder(); t.append( head( "Data set " + dataSet.getName() ) ); + URI url = uriInfo.getAbsolutePathBuilder().path( "/dxf" ).build(); + t.append( "

      (There is also a prototype xml version of this data set available)

      \n" ); t.append( "

      Uuid: " ).append( dataSet.getUuid() ).append( "
      \n" ); t.append( "Period type: " ).append( dataSet.getPeriodType().getName() ).append( " - " ) .append( dataSet.getPeriodType().getIsoFormat() ); @@ -116,14 +134,10 @@ return "\n\n"; } - - @Required public void setDataSetService( DataSetService dataSetService ) { this.dataSetService = dataSetService; } - - } === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java 2011-01-12 09:13:36 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java 2011-02-21 10:32:35 +0000 @@ -29,6 +29,9 @@ public class DhisMediaType { + public static final String DXF = "application/vnd.org.dhis2.dxf"; + + public static final String ACTIVITYVALUELIST_SERIALIZED = "application/vnd.org.dhis2.activityvaluelist+serialized"; public static final String DATASETVALUE_SERIALIZED = "application/vnd.org.dhis2.datasetvalue+serialized"; === 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 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2011-02-21 10:32:35 +0000 @@ -96,7 +96,8 @@ - + === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/web.xml' --- dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/web.xml 2011-02-18 20:17:34 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/web.xml 2011-02-21 10:32:35 +0000 @@ -95,5 +95,9 @@ web-api /api/* + + web-api + /api + \ No newline at end of file === removed directory 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/dhis-web-case-api' === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/dhis-web-case-api/main.vm' === modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml 2011-02-15 13:45:11 +0000 +++ dhis-2/dhis-web/dhis-web-commons/src/main/resources/META-INF/dhis/beans.xml 2011-02-21 10:32:35 +0000 @@ -297,7 +297,9 @@ - + + +