=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueStoreTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueStoreTest.java 2010-05-06 16:05:43 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueStoreTest.java 2011-02-15 12:55:14 +0000 @@ -173,9 +173,8 @@ try { - // Should give unique constraint violation dataValueStore.addDataValue( dataValueD ); - fail(); + fail("Should give unique constraint violation"); } catch ( Exception e ) { === removed file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java 2011-02-15 06:46:27 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapper.java 1970-01-01 00:00:00 +0000 @@ -1,178 +0,0 @@ -package org.hisp.dhis.importexport.datavalueset; - -/* - * 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.util.ArrayList; -import java.util.Date; -import java.util.List; - -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementCategoryService; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.dataset.DataSet; -import org.hisp.dhis.dataset.DataSetService; -import org.hisp.dhis.datavalue.DataValue; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.organisationunit.OrganisationUnitService; -import org.hisp.dhis.period.Period; - -public class DataValueSetMapper -{ - - private OrganisationUnitService organisationUnitService; - - private DataSetService dataSetService; - - private DataElementCategoryService categoryService; - - private DataElementService dataElementService; - - public List getDataValues( DataValueSet dataValueSet ) - { - Date timestamp = new Date(); - - DataSet dataSet = dataSetService.getDataSet( dataValueSet.getDataSetUuid() ); - - if ( dataSet == null ) - { - throw new IllegalArgumentException( "Data set with UUID " + dataValueSet.getDataSetUuid() - + " does not exist" ); - } - - OrganisationUnit unit = organisationUnitService.getOrganisationUnit( dataValueSet.getOrganisationUnitUuid() ); - - if ( unit == null ) - { - throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() - + " does not exist" ); - } - - if ( !dataSet.getSources().contains( unit ) ) - { - throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() - + " does not report data set with UUID " + dataSet.getUuid() ); - } - - Period period; - - try - { - period = dataSet.getPeriodType().createPeriod( dataValueSet.getPeriodIsoDate() ); - } - catch ( Exception e ) - { - throw new IllegalArgumentException( "Period " + dataValueSet.getPeriodIsoDate() - + " is not valid period of type " + dataSet.getPeriodType().getName() ); - } - - List dxfDataValues = dataValueSet.getDataValues(); - List dataValues = new ArrayList( dxfDataValues.size() ); - - for ( org.hisp.dhis.importexport.datavalueset.DataValue dxfValue : dxfDataValues ) - { - DataValue dataValue = getDataValue( dxfValue, dataSet ); - dataValue.setSource( unit ); - dataValue.setTimestamp( timestamp ); - dataValue.setStoredBy( dataValueSet.getStoredBy() ); - dataValue.setPeriod( period ); - - dataValues.add( dataValue ); - - } - - return dataValues; - } - - public DataValue getDataValue( org.hisp.dhis.importexport.datavalueset.DataValue dxfValue, DataSet dataSet ) - { - DataElement dataElement = dataElementService.getDataElement( dxfValue.getDataElementUuid() ); - - if ( dataElement == null ) - { - throw new IllegalArgumentException( "Data value with UUID " + dxfValue.getDataElementUuid() - + " does not exist" ); - } - - if ( !dataSet.getDataElements().contains( dataElement ) ) - { - throw new IllegalArgumentException( "Data element " + dataElement.getUuid() + " isn't in data set " - + dataSet.getUuid() ); - } - - DataValue dv = new DataValue(); - - dv.setDataElement( dataElement ); - - dv.setValue( dxfValue.getValue() ); - - DataElementCategoryOptionCombo combo; - - String comboId = dxfValue.getCategoryOptionComboUuid(); - - if ( comboId != null ) - { - combo = categoryService.getDataElementCategoryOptionCombo( Integer.parseInt( comboId ) ); - - if ( combo == null ) - { - throw new IllegalArgumentException( "DataElementCategoryOptionCombo with UUID " + comboId - + " does not exist" ); - } - } - else - { - combo = categoryService.getDefaultDataElementCategoryOptionCombo(); - } - - dv.setOptionCombo( combo ); - - return dv; - } - - public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) - { - this.organisationUnitService = organisationUnitService; - } - - public void setDataSetService( DataSetService dataSetService ) - { - this.dataSetService = dataSetService; - } - - public void setCategoryService( DataElementCategoryService categoryService ) - { - this.categoryService = categoryService; - } - - public void setDataElementService( DataElementService dataElementService ) - { - this.dataElementService = dataElementService; - } - -} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetService.java' --- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/datavalueset/DataValueSetService.java 2011-02-15 12:55:14 +0000 @@ -0,0 +1,188 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * 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.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.dataset.DataSet; +import org.hisp.dhis.dataset.DataSetService; +import org.hisp.dhis.datavalue.DataValue; +import org.hisp.dhis.datavalue.DataValueService; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.period.Period; +import org.springframework.beans.factory.annotation.Required; + +public class DataValueSetService +{ + + private OrganisationUnitService organisationUnitService; + + private DataSetService dataSetService; + + private DataElementCategoryService categoryService; + + private DataElementService dataElementService; + + private DataValueService dataValueService; + + public void saveDataValueSet( DataValueSet dataValueSet ) + { + Date timestamp = new Date(); + + DataSet dataSet = dataSetService.getDataSet( dataValueSet.getDataSetUuid() ); + + if ( dataSet == null ) + { + throw new IllegalArgumentException( "Data set with UUID " + dataValueSet.getDataSetUuid() + + " does not exist" ); + } + + OrganisationUnit unit = organisationUnitService.getOrganisationUnit( dataValueSet.getOrganisationUnitUuid() ); + + if ( unit == null ) + { + throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() + + " does not exist" ); + } + + if ( !dataSet.getSources().contains( unit ) ) + { + throw new IllegalArgumentException( "Org unit with UUID " + dataValueSet.getOrganisationUnitUuid() + + " does not report data set with UUID " + dataSet.getUuid() ); + } + + Period period; + + try + { + period = dataSet.getPeriodType().createPeriod( dataValueSet.getPeriodIsoDate() ); + } + catch ( Exception e ) + { + throw new IllegalArgumentException( "Period " + dataValueSet.getPeriodIsoDate() + + " is not valid period of type " + dataSet.getPeriodType().getName() ); + } + + List dxfDataValues = dataValueSet.getDataValues(); + List dataValues = new ArrayList( dxfDataValues.size() ); + + for ( org.hisp.dhis.importexport.datavalueset.DataValue dxfValue : dxfDataValues ) + { + DataElement dataElement = dataElementService.getDataElement( dxfValue.getDataElementUuid() ); + + if ( dataElement == null ) + { + throw new IllegalArgumentException( "Data value with UUID " + dxfValue.getDataElementUuid() + + " does not exist" ); + } + + if ( !dataSet.getDataElements().contains( dataElement ) ) + { + throw new IllegalArgumentException( "Data element '" + dataElement.getUuid() + "' isn't in data set " + + dataSet.getUuid() ); + } + + DataElementCategoryOptionCombo combo = getCombo( dxfValue.getCategoryOptionComboUuid() ); + + if ( !dataElement.getCategoryCombo().getOptionCombos().contains( combo ) ) + { + throw new IllegalArgumentException( "DataElementCategoryOptionCombo with UUID '" + combo.getUuid() + + "' isn't in DataElement '" + dataElement.getUuid() + "'" ); + } + + DataValue dv = dataValueService.getDataValue( unit, dataElement, period, combo ); + + if ( dv == null ) + { + dv = new DataValue( dataElement, period, unit, dxfValue.getValue(), dataValueSet.getStoredBy(), + timestamp, null, combo ); + dataValueService.addDataValue( dv ); + } + else + { + dv.setValue( dxfValue.getValue() ); + dv.setTimestamp( timestamp ); + dv.setStoredBy( dataValueSet.getStoredBy() ); + dataValueService.updateDataValue( dv ); + } + } + } + + private DataElementCategoryOptionCombo getCombo( String comboId ) + { + if ( comboId == null ) + { + return categoryService.getDefaultDataElementCategoryOptionCombo(); + } + + DataElementCategoryOptionCombo combo = categoryService.getDataElementCategoryOptionCombo( Integer + .parseInt( comboId ) ); + + if ( combo == null ) + { + throw new IllegalArgumentException( "DataElementCategoryOptionCombo with UUID '" + comboId + + "' does not exist" ); + } + + return combo; + } + + public void setOrganisationUnitService( OrganisationUnitService organisationUnitService ) + { + this.organisationUnitService = organisationUnitService; + } + + public void setDataSetService( DataSetService dataSetService ) + { + this.dataSetService = dataSetService; + } + + public void setCategoryService( DataElementCategoryService categoryService ) + { + this.categoryService = categoryService; + } + + public void setDataElementService( DataElementService dataElementService ) + { + this.dataElementService = dataElementService; + } + + @Required + public void setDataValueService( DataValueService dataValueService ) + { + this.dataValueService = dataValueService; + } + +} === 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-15 06:46:27 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/main/resources/META-INF/dhis/beans.xml 2011-02-15 12:55:14 +0000 @@ -405,11 +405,13 @@ - + + === removed file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java 2011-02-15 09:03:06 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetMapperTest.java 1970-01-01 00:00:00 +0000 @@ -1,235 +0,0 @@ -package org.hisp.dhis.importexport.datavalueset; - -/* - * 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 static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; - -import org.hisp.dhis.DhisTest; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -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.util.ImportExportUtils; -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 into a correct list of {@link DataValues data values} and also checks that it is stored.. - */ -public class DataValueSetMapperTest - extends DhisTest -{ - - private static final String ORGANISATION_UNIT_UUID = "9C1B1B5E-3D65-48F2-8D1D-D36C60DD7344"; - - private static final String DATA_SET_UUID = "16B2299E-ECD6-46CF-A61F-817D350C180D"; - - private static final String DATA_ELEMENT_UUID = "56B2299E-ECD6-46CF-A61F-817D350C180D"; - - private static final String DATA_ELEMENT_NOT_IN_SET_UUID = "96B2299E-ECD6-46CF-A61F-817D350C180D"; - - private DataValueSetMapper mapper; - - private DataValueSet dataValueSet; - - private ImportService importService; - - private ClassLoader classLoader; - - private DataElementCategoryOptionCombo defaultCombo; - - // ------------------------------------------------------------------------- - // Fixture - // ------------------------------------------------------------------------- - - @SuppressWarnings( "serial" ) - @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 ); - - mapper = (DataValueSetMapper) getBean( "org.hisp.dhis.importexport.datavalueset.DataValueSetMapper" ); - - classLoader = Thread.currentThread().getContextClassLoader(); - - InputStream is = classLoader.getResourceAsStream( "datavalueset/base.xml" ); - ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, - false ); - importService.importData( importParams, is ); - is.close(); - - dataValueSet = new DataValueSet(); - dataValueSet.setDataSetUuid( DATA_SET_UUID ); - dataValueSet.setPeriodIsoDate( "2011W5" ); - dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); - dataValueSet.setStoredBy( "misterindia" ); - - final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); - dv.setDataElementUuid( DATA_ELEMENT_UUID ); - dv.setValue( "11" ); - - dataValueSet.setDataValues( new ArrayList() {{ add( dv ); }} ); - - defaultCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); - - } - - // ------------------------------------------------------------------------- - // Tests - // ------------------------------------------------------------------------- - - @Test - public void testJaxb() - throws JAXBException, IOException - { - JAXBContext jc = JAXBContext.newInstance( DataValueSet.class, org.hisp.dhis.importexport.datavalueset.DataValue.class ); - Unmarshaller u = jc.createUnmarshaller(); - InputStream is = classLoader.getResourceAsStream( "datavalueset/dataValueSet.xml" ); - - DataValueSet dxfDataValueSet = (DataValueSet) u.unmarshal( is ); - is.close(); - - assertEquals( dataValueSet.getDataSetUuid(), dxfDataValueSet.getDataSetUuid() ); - assertEquals( dataValueSet.getPeriodIsoDate(), dxfDataValueSet.getPeriodIsoDate() ); - assertEquals( dataValueSet.getOrganisationUnitUuid(), dxfDataValueSet.getOrganisationUnitUuid() ); - assertEquals( dataValueSet.getStoredBy(), dxfDataValueSet.getStoredBy() ); - - assertEquals( 1, dxfDataValueSet.getDataValues().size() ); - - org.hisp.dhis.importexport.datavalueset.DataValue dv = dxfDataValueSet.getDataValues().get( 0 ); - - assertEquals( dataValueSet.getDataValues().get( 0 ).getDataElementUuid(), dv.getDataElementUuid() ); - - assertNull( dv.getCategoryOptionComboUuid() ); - } - - @Test - public void simpleMapping() - throws Exception - { - long before = new Date().getTime(); - - List dataValues = mapper.getDataValues( dataValueSet ); - - long after = new Date().getTime(); - - assertEquals( 1, dataValues.size() ); - - DataValue dv = dataValues.get( 0 ); - - verifyDataValue( before, after, dv ); - - dataValueService.addDataValue( dv ); - Collection persistedDataValues = dataValueService.getAllDataValues(); - assertEquals(1, persistedDataValues.size()); - - DataValue persisted = persistedDataValues.iterator().next(); - assertEquals( dv, persisted ); - verifyDataValue( before, after, persisted ); - - } - - @Test - public void missingThingsFromInput() { - - dataValueSet.setDataSetUuid( null ); - try { - mapper.getDataValues( dataValueSet ); - fail("Should miss data set"); - - } catch (IllegalArgumentException e) { - // Expected - } - - dataValueSet.setDataSetUuid( DATA_SET_UUID ); - dataValueSet.setOrganisationUnitUuid( "ladlalad" ); - try { - mapper.getDataValues( dataValueSet ); - fail("Should miss org unit"); - - } catch (IllegalArgumentException e) { - // Expected - } - - dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); - - final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); - dv.setDataElementUuid( DATA_ELEMENT_NOT_IN_SET_UUID ); - dv.setValue( "11" ); - dataValueSet.getDataValues().add( dv ); - - try { - mapper.getDataValues( dataValueSet ); - fail("Should not accept extra data value"); - - } catch (IllegalArgumentException e) { - // Expected - } - - } - - private void verifyDataValue( long before, long after, DataValue dv ) - { - assertEquals( DATA_ELEMENT_UUID, dv.getDataElement().getUuid() ); - assertEquals( ORGANISATION_UNIT_UUID, dv.getSource().getUuid() ); - assertEquals( "misterindia", dv.getStoredBy() ); - assertEquals( "11", dv.getValue() ); - - long time = dv.getTimestamp().getTime(); - assertTrue( time >= before ); - assertTrue( time <= after ); - - assertEquals( defaultCombo, dv.getOptionCombo() ); - } - -} === added file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetServiceTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetServiceTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/datavalueset/DataValueSetServiceTest.java 2011-02-15 12:55:14 +0000 @@ -0,0 +1,287 @@ +package org.hisp.dhis.importexport.datavalueset; + +/* + * 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 static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.hisp.dhis.DhisTest; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +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.util.ImportExportUtils; +import org.hisp.dhis.period.WeeklyPeriodType; +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 and + * stored into a correct set of {@link DataValue data values}. + */ +public class DataValueSetServiceTest + extends DhisTest +{ + + private static final String ORGANISATION_UNIT_UUID = "9C1B1B5E-3D65-48F2-8D1D-D36C60DD7344"; + + private static final String DATA_SET_UUID = "16B2299E-ECD6-46CF-A61F-817D350C180D"; + + private static final String DATA_ELEMENT_UUID = "56B2299E-ECD6-46CF-A61F-817D350C180D"; + + private static final String DATA_ELEMENT_NOT_IN_SET_UUID = "96B2299E-ECD6-46CF-A61F-817D350C180D"; + + private DataValueSetService service; + + private DataValueSet dataValueSet; + + private ImportService importService; + + private ClassLoader classLoader; + + private DataElementCategoryOptionCombo defaultCombo; + + // ------------------------------------------------------------------------- + // Fixture + // ------------------------------------------------------------------------- + + @SuppressWarnings( "serial" ) + @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 ); + + service = (DataValueSetService) getBean( "org.hisp.dhis.importexport.datavalueset.DataValueSetMapper" ); + + classLoader = Thread.currentThread().getContextClassLoader(); + + InputStream is = classLoader.getResourceAsStream( "datavalueset/base.xml" ); + ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, + false ); + importService.importData( importParams, is ); + is.close(); + + dataValueSet = new DataValueSet(); + dataValueSet.setDataSetUuid( DATA_SET_UUID ); + dataValueSet.setPeriodIsoDate( "2011W5" ); + dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); + dataValueSet.setStoredBy( "misterindia" ); + + final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); + dv.setDataElementUuid( DATA_ELEMENT_UUID ); + dv.setValue( "11" ); + + dataValueSet.setDataValues( new ArrayList() + { + { + add( dv ); + } + } ); + + defaultCombo = categoryService.getDefaultDataElementCategoryOptionCombo(); + + } + + // ------------------------------------------------------------------------- + // Tests + // ------------------------------------------------------------------------- + + @Test + public void testJaxb() + throws JAXBException, IOException + { + JAXBContext jc = JAXBContext.newInstance( DataValueSet.class, + org.hisp.dhis.importexport.datavalueset.DataValue.class ); + Unmarshaller u = jc.createUnmarshaller(); + InputStream is = classLoader.getResourceAsStream( "datavalueset/dataValueSet.xml" ); + + DataValueSet dxfDataValueSet = (DataValueSet) u.unmarshal( is ); + is.close(); + + assertEquals( dataValueSet.getDataSetUuid(), dxfDataValueSet.getDataSetUuid() ); + assertEquals( dataValueSet.getPeriodIsoDate(), dxfDataValueSet.getPeriodIsoDate() ); + assertEquals( dataValueSet.getOrganisationUnitUuid(), dxfDataValueSet.getOrganisationUnitUuid() ); + assertEquals( dataValueSet.getStoredBy(), dxfDataValueSet.getStoredBy() ); + + assertEquals( 1, dxfDataValueSet.getDataValues().size() ); + + org.hisp.dhis.importexport.datavalueset.DataValue dv = dxfDataValueSet.getDataValues().get( 0 ); + + assertEquals( dataValueSet.getDataValues().get( 0 ).getDataElementUuid(), dv.getDataElementUuid() ); + + assertNull( dv.getCategoryOptionComboUuid() ); + } + + @Test + public void simpleMapping() + throws Exception + { + 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 ); + + } + + @Test + public void missingThingsFromInput() + { + + dataValueSet.setDataSetUuid( null ); + try + { + service.saveDataValueSet( dataValueSet ); + fail( "Should miss data set" ); + + } + catch ( IllegalArgumentException e ) + { + // Expected + } + + dataValueSet.setDataSetUuid( DATA_SET_UUID ); + dataValueSet.setOrganisationUnitUuid( "ladlalad" ); + try + { + service.saveDataValueSet( dataValueSet ); + fail( "Should miss org unit" ); + + } + catch ( IllegalArgumentException e ) + { + // Expected + } + + dataValueSet.setOrganisationUnitUuid( ORGANISATION_UNIT_UUID ); + + final org.hisp.dhis.importexport.datavalueset.DataValue dv = new org.hisp.dhis.importexport.datavalueset.DataValue(); + dv.setDataElementUuid( DATA_ELEMENT_NOT_IN_SET_UUID ); + dv.setValue( "11" ); + dataValueSet.getDataValues().add( dv ); + + try + { + service.saveDataValueSet( dataValueSet ); + fail( "Should not accept extra data value" ); + + } + catch ( IllegalArgumentException e ) + { + // Expected + } + + dataValueSet.getDataValues().remove( dv ); + + } + + @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 ); + + // Update + dataValueSet.getDataValues().get( 0 ).setValue( "101" ); + + before = new Date().getTime(); + + service.saveDataValueSet( dataValueSet ); + + after = new Date().getTime(); + + dataValues = dataValueService.getAllDataValues(); + assertEquals( 1, dataValues.size() ); + + dataValue = dataValues.iterator().next(); + + verifyDataValue( before, after, dataValue, "101" ); + + } + + private void verifyDataValue( long before, long after, DataValue dv) + { + verifyDataValue( before, after, dv, "11" ); + } + + private void verifyDataValue( long before, long after, DataValue dv, String value ) + { + assertEquals( DATA_ELEMENT_UUID, dv.getDataElement().getUuid() ); + assertEquals( ORGANISATION_UNIT_UUID, dv.getSource().getUuid() ); + assertEquals( new WeeklyPeriodType().createPeriod( "2011W5" ), dv.getPeriod() ); + assertEquals( "misterindia", dv.getStoredBy() ); + assertEquals( value, dv.getValue() ); + + long time = dv.getTimestamp().getTime(); + assertTrue( time >= before ); + assertTrue( time <= after ); + + assertEquals( defaultCombo, dv.getOptionCombo() ); + } + +} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/rpc/RPCResource.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/rpc/RPCResource.java 2011-02-15 09:03:06 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/rpc/RPCResource.java 2011-02-15 12:55:14 +0000 @@ -1,7 +1,6 @@ package org.hisp.dhis.web.api.rpc; import java.net.URI; -import java.util.List; import java.util.Set; import javax.ws.rs.Consumes; @@ -18,24 +17,16 @@ import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; import org.hisp.dhis.dataset.DataSet; import org.hisp.dhis.dataset.DataSetService; -import org.hisp.dhis.datavalue.DataValue; -import org.hisp.dhis.datavalue.DataValueService; import org.hisp.dhis.importexport.datavalueset.DataValueSet; -import org.hisp.dhis.importexport.datavalueset.DataValueSetMapper; +import org.hisp.dhis.importexport.datavalueset.DataValueSetService; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.springframework.beans.factory.annotation.Required; -import com.ibatis.common.logging.Log; -import com.ibatis.common.logging.LogFactory; - @Path( "/rpc" ) public class RPCResource { - private static Log log = LogFactory.getLog( RPCResource.class ); - - private DataValueSetMapper dataValueSetMapper; - - private DataValueService dataValueService; + + private DataValueSetService dataValueSetService; private DataSetService dataSetService; @@ -47,12 +38,7 @@ @Consumes( MediaType.APPLICATION_XML ) public void storeDataValueSet( DataValueSet dataValueSet ) { - List dataValues = dataValueSetMapper.getDataValues( dataValueSet ); - - for ( DataValue dataValue : dataValues ) - { - dataValueService.addDataValue( dataValue ); - } + dataValueSetService.saveDataValueSet( dataValueSet ); } @GET @@ -167,15 +153,9 @@ } @Required - public void setDataValueSetMapper( DataValueSetMapper dataValueSetMapper ) - { - this.dataValueSetMapper = dataValueSetMapper; - } - - @Required - public void setDataValueService( DataValueService dataValueService ) - { - this.dataValueService = dataValueService; + public void setDataValueSetService( DataValueSetService dataValueSetService ) + { + this.dataValueSetService = dataValueSetService; } @Required === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java 2011-02-15 06:22:24 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java 2011-02-15 12:55:14 +0000 @@ -61,8 +61,7 @@ throw new IllegalArgumentException( "Couldn't make a period of type " + periodType.getName() + " and name " + periodName, e ); } - DailyPeriodType dailyPeriodType = new DailyPeriodType(); - return dailyPeriodType.createPeriod( date ); + return periodType.createPeriod( date ); } @@ -97,8 +96,7 @@ cal.set( Calendar.YEAR, year ); cal.set( Calendar.MONTH, month ); - MonthlyPeriodType monthlyPeriodType = new MonthlyPeriodType(); - return monthlyPeriodType.createPeriod( cal.getTime() ); + return periodType.createPeriod( cal.getTime() ); } if ( periodType instanceof YearlyPeriodType ) @@ -106,9 +104,7 @@ Calendar cal = Calendar.getInstance(); cal.set( Calendar.YEAR, Integer.parseInt( periodName ) ); - YearlyPeriodType yearlyPeriodType = new YearlyPeriodType(); - - return yearlyPeriodType.createPeriod( cal.getTime() ); + return periodType.createPeriod( cal.getTime() ); } if ( periodType instanceof QuarterlyPeriodType ) @@ -138,10 +134,9 @@ cal.set( Calendar.MONTH, month ); cal.set( Calendar.YEAR, year ); - QuarterlyPeriodType quarterlyPeriodType = new QuarterlyPeriodType(); if ( month != 0 ) { - return quarterlyPeriodType.createPeriod( cal.getTime() ); + return periodType.createPeriod( cal.getTime() ); } } === 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-15 06:53:26 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml 2011-02-15 12:55:14 +0000 @@ -82,8 +82,7 @@ - - +