=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Dimension.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Dimension.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/Dimension.java 2009-10-16 15:31:26 +0000 @@ -0,0 +1,33 @@ +package org.hisp.dhis.common; + +/* + * Copyright (c) 2004-2007, 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. + */ + +public interface Dimension +{ + String getName(); +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionOption.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionOption.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DimensionOption.java 2009-10-16 15:31:26 +0000 @@ -0,0 +1,33 @@ +package org.hisp.dhis.common; + +/* + * Copyright (c) 2004-2007, 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. + */ + +public interface DimensionOption +{ + String getName(); +} === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java 2009-10-15 17:28:51 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java 2009-10-16 15:31:26 +0000 @@ -34,6 +34,8 @@ import java.util.List; import java.util.Set; +import org.hisp.dhis.common.Dimension; +import org.hisp.dhis.common.DimensionOption; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.datadictionary.ExtendedDataElement; import org.hisp.dhis.dataset.DataSet; @@ -53,7 +55,7 @@ * @version $Id: DataElement.java 5540 2008-08-19 10:47:07Z larshelg $ */ public class DataElement - extends IdentifiableObject + extends IdentifiableObject implements DimensionOption { public static final String TYPE_STRING = "string"; public static final String TYPE_INT = "int"; @@ -128,7 +130,19 @@ public DataElement() { } + + // ------------------------------------------------------------------------- + // Dimension + // ------------------------------------------------------------------------- + public static final Dimension DIMENSION = new Dimension() + { + public String getName() + { + return "DataElement"; + } + }; + // ------------------------------------------------------------------------- // hashCode, equals and toString // ------------------------------------------------------------------------- === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategory.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategory.java 2009-10-15 17:28:51 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategory.java 2009-10-16 15:31:26 +0000 @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.List; +import org.hisp.dhis.common.Dimension; import org.hisp.dhis.common.IdentifiableObject; @@ -43,7 +44,7 @@ * @version $Id$ */ public class DataElementCategory - extends IdentifiableObject + extends IdentifiableObject implements Dimension { public static final String DEFAULT_NAME = "default"; === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOption.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOption.java 2009-10-16 13:09:18 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOption.java 2009-10-16 15:31:26 +0000 @@ -27,6 +27,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.hisp.dhis.common.DimensionOption; import org.hisp.dhis.common.IdentifiableObject; /** @@ -34,7 +35,7 @@ * @version $Id$ */ public class DataElementCategoryOption - extends IdentifiableObject + extends IdentifiableObject implements DimensionOption { public static final String DEFAULT_NAME = "default"; === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOptionComboService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOptionComboService.java 2009-10-15 18:07:59 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryOptionComboService.java 2009-10-16 15:31:26 +0000 @@ -73,20 +73,28 @@ /** * Retrieves the DataElementCategoryOptionCombos with the given identifiers. * - * @param identifiers the identifiers of the - * DataElementCategoryOptionCombos. + * @param identifiers the identifiers of the DataElementCategoryOptionCombos. * @return a Collection of DataElementCategoryOptionCombos. */ Collection getDataElementCategoryOptionCombos( Collection identifiers ); /** + * Retrieves the DataElementCategoryOptionCombo with the given Collection + * of DataElementCategoryOptions. + * + * @param categoryOptions + * @return + */ + DataElementCategoryOptionCombo getDataElementCategoryOptionCombo( Collection categoryOptions ); + + /** * Retrieves a DataElementCategoryOptionCombo. * * @param categoryOptionCombo the DataElementCategoryOptionCombo to * retrieve. * @return a DataElementCategoryOptionCombo. */ - DataElementCategoryOptionCombo getDataElementCategoryOptionCombo( DataElementCategoryOptionCombo categoryOptionCombo ); // TODO WTF? + DataElementCategoryOptionCombo getDataElementCategoryOptionCombo( DataElementCategoryOptionCombo categoryOptionCombo ); /** * Retrieves all DataElementCategoryOptionCombos. === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/datavalue/DataValue.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/datavalue/DataValue.java 2009-09-27 22:30:31 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/datavalue/DataValue.java 2009-10-16 15:31:26 +0000 @@ -29,7 +29,12 @@ import java.io.Serializable; import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import org.hisp.dhis.common.Dimension; +import org.hisp.dhis.common.DimensionOption; +import org.hisp.dhis.dataelement.DataElementCategoryOption; import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.period.Period; @@ -123,18 +128,38 @@ } public DataValue( DataElement dataElement, Period period, Source source, String value, String storedBy, - Date timestamp, String comment, DataElementCategoryOptionCombo optionCombo ) - { - this.dataElement = dataElement; - this.period = period; - this.source = source; - this.value = value; - this.storedBy = storedBy; - this.timestamp = timestamp; - this.comment = comment; - this.optionCombo = optionCombo; - } - + Date timestamp, String comment, DataElementCategoryOptionCombo optionCombo ) + { + this.dataElement = dataElement; + this.period = period; + this.source = source; + this.value = value; + this.storedBy = storedBy; + this.timestamp = timestamp; + this.comment = comment; + this.optionCombo = optionCombo; + } + + // ------------------------------------------------------------------------- + // Logic + // ------------------------------------------------------------------------- + + public Map getDimensions() + { + Map dimensions = new HashMap(); + + dimensions.put( DataElement.DIMENSION, dataElement ); + dimensions.put( Period.DIMENSION, period ); + dimensions.put( Source.DIMENSION, source ); + + for ( DataElementCategoryOption categoryOption : optionCombo.getCategoryOptions() ) + { + dimensions.put( categoryOption.getCategory(), categoryOption ); + } + + return dimensions; + } + // ------------------------------------------------------------------------- // hashCode and equals // ------------------------------------------------------------------------- @@ -260,5 +285,4 @@ { this.optionCombo = optionCombo; } - } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java 2009-05-23 15:20:46 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java 2009-10-16 15:31:26 +0000 @@ -30,12 +30,15 @@ import java.io.Serializable; import java.util.Date; +import org.hisp.dhis.common.Dimension; +import org.hisp.dhis.common.DimensionOption; + /** * @author Kristian Nordal * @version $Id: Period.java 5277 2008-05-27 15:48:42Z larshelg $ */ public class Period - implements Serializable + implements Serializable, DimensionOption { private int id; @@ -75,6 +78,18 @@ } // ------------------------------------------------------------------------- + // Dimension + // ------------------------------------------------------------------------- + + public static final Dimension DIMENSION = new Dimension() + { + public String getName() + { + return "Period"; + } + }; + + // ------------------------------------------------------------------------- // hashCode, equals and toString // ------------------------------------------------------------------------- === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/source/Source.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/source/Source.java 2009-10-15 17:28:51 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/source/Source.java 2009-10-16 15:31:26 +0000 @@ -30,6 +30,8 @@ import java.util.HashSet; import java.util.Set; +import org.hisp.dhis.common.Dimension; +import org.hisp.dhis.common.DimensionOption; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.dataset.DataSet; @@ -38,9 +40,21 @@ * @version $Id: Source.java 5277 2008-05-27 15:48:42Z larshelg $ */ public abstract class Source - extends IdentifiableObject + extends IdentifiableObject implements DimensionOption { protected Set dataSets = new HashSet(); + + // ------------------------------------------------------------------------- + // Dimension + // ------------------------------------------------------------------------- + + public static final Dimension DIMENSION = new Dimension() + { + public String getName() + { + return "Source"; + } + }; // ------------------------------------------------------------------------- // hashCode, equals and toString === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryOptionComboService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryOptionComboService.java 2009-10-15 18:07:59 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementCategoryOptionComboService.java 2009-10-16 15:31:26 +0000 @@ -38,6 +38,7 @@ import org.hisp.dhis.common.GenericStore; import org.springframework.transaction.annotation.Transactional; +import org.apache.commons.collections.CollectionUtils; /** * @author Abyot Asalefew @@ -129,6 +130,21 @@ return categoryOptionCombos; } + public DataElementCategoryOptionCombo getDataElementCategoryOptionCombo( Collection categoryOptions ) + { + // TODO Re-implement with a Hibernate Criteria + + for ( DataElementCategoryOptionCombo categoryOptionCombo : getAllDataElementCategoryOptionCombos() ) + { + if ( CollectionUtils.isEqualCollection( categoryOptions, categoryOptionCombo.getCategoryOptions() ) ) + { + return categoryOptionCombo; + } + } + + return null; + } + public DataElementCategoryOptionCombo getDataElementCategoryOptionCombo( DataElementCategoryOptionCombo categoryOptionCombo ) { for ( DataElementCategoryOptionCombo dcoc : getAllDataElementCategoryOptionCombos() ) @@ -501,6 +517,5 @@ } return optionsMap; - } - + } } === added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueDimensionTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueDimensionTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/DataValueDimensionTest.java 2009-10-16 15:31:26 +0000 @@ -0,0 +1,193 @@ +package org.hisp.dhis.datavalue; + +/* + * Copyright (c) 2004-2007, 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.Collection; +import java.util.Map; + +import org.hisp.dhis.DhisSpringTest; +import org.hisp.dhis.common.Dimension; +import org.hisp.dhis.common.DimensionOption; +import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategory; +import org.hisp.dhis.dataelement.DataElementCategoryCombo; +import org.hisp.dhis.dataelement.DataElementCategoryComboService; +import org.hisp.dhis.dataelement.DataElementCategoryOption; +import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService; +import org.hisp.dhis.dataelement.DataElementCategoryOptionService; +import org.hisp.dhis.dataelement.DataElementCategoryService; +import org.hisp.dhis.dataelement.DataElementService; +import org.hisp.dhis.mock.MockSource; +import org.hisp.dhis.period.Period; +import org.hisp.dhis.period.PeriodService; +import org.hisp.dhis.source.Source; +import org.hisp.dhis.source.SourceStore; + +import org.junit.Test; + +import static org.junit.Assert.*; + +import org.apache.commons.collections.CollectionUtils; + +/** + * @author Lars Helge Overland + * @version $Id$ + */ +public class DataValueDimensionTest + extends DhisSpringTest +{ + private DataElementCategoryOption male; + private DataElementCategoryOption female; + private DataElementCategoryOption under15; + private DataElementCategoryOption over15; + + private DataElementCategory gender; + private DataElementCategory ageGroup; + + private DataElementCategoryCombo genderAndAgeGroup; + + private DataElement dataElementA; + + private Period periodA; + + private Source sourceA; + + @Override + public void setUpTest() + { + categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID ); + categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID ); + categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID ); + categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID ); + dataElementService = (DataElementService) getBean( DataElementService.ID ); + dataValueService = (DataValueService) getBean( DataValueService.ID ); + periodService = (PeriodService) getBean( PeriodService.ID ); + sourceStore = (SourceStore) getBean( SourceStore.ID ); + + male = new DataElementCategoryOption( "Male" ); + female = new DataElementCategoryOption( "Female" ); + under15 = new DataElementCategoryOption( "<15" ); + over15 = new DataElementCategoryOption( ">15" ); + + categoryOptionService.addDataElementCategoryOption( male ); + categoryOptionService.addDataElementCategoryOption( female ); + categoryOptionService.addDataElementCategoryOption( under15 ); + categoryOptionService.addDataElementCategoryOption( over15 ); + + gender = new DataElementCategory( "Gender" ); + gender.getCategoryOptions().add( male ); + gender.getCategoryOptions().add( female ); + + ageGroup = new DataElementCategory( "Agegroup" ); + ageGroup.getCategoryOptions().add( under15 ); + ageGroup.getCategoryOptions().add( over15 ); + + categoryService.addDataElementCategory( gender ); + categoryService.addDataElementCategory( ageGroup ); + + genderAndAgeGroup = new DataElementCategoryCombo( "Gender and Agegroup" ); + genderAndAgeGroup.getCategories().add( gender ); + genderAndAgeGroup.getCategories().add( ageGroup ); + + categoryComboService.addDataElementCategoryCombo( genderAndAgeGroup ); + + categoryOptionComboService.generateOptionCombos( genderAndAgeGroup ); + + dataElementA = createDataElement( 'A', genderAndAgeGroup ); + + dataElementService.addDataElement( dataElementA ); + + periodA = createPeriod( getDate( 2000, 1, 1 ), getDate( 2000, 2, 1 ) ); + + periodService.addPeriod( periodA ); + + sourceA = new MockSource( "Bobs Clinic" ); + + sourceStore.addSource( sourceA ); + + for ( DataElementCategoryOptionCombo categoryOptionCombo : genderAndAgeGroup.getOptionCombos() ) + { + dataValueService.addDataValue( createDataValue( dataElementA, periodA, sourceA, "10", categoryOptionCombo ) ); + } + } + + @Test + public void testGetDimensions() + { + Collection categoryOptions = new ArrayList(); + categoryOptions.add( male ); + categoryOptions.add( under15 ); + + DataElementCategoryOptionCombo categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( categoryOptions ); + + DataValue dataValue = dataValueService.getDataValue( sourceA, dataElementA, periodA, categoryOptionCombo ); + + assertNotNull( dataValue ); + + Map dimensions = dataValue.getDimensions(); + + assertEquals( dimensions.keySet().size(), 4 ); + assertEquals( dataElementA, dimensions.get( DataElement.DIMENSION ) ); + assertEquals( periodA, dimensions.get( Period.DIMENSION ) ); + assertEquals( sourceA, dimensions.get( Source.DIMENSION ) ); + } + + @Test + public void testGetByCategoryOptionCombos() + { + Collection categoryOptions = new ArrayList(); + categoryOptions.add( male ); + categoryOptions.add( under15 ); + + DataElementCategoryOptionCombo categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( categoryOptions ); + + assertNotNull( categoryOptionCombo ); + assertEquals( genderAndAgeGroup, categoryOptionCombo.getCategoryCombo() ); + assertTrue( CollectionUtils.isEqualCollection( categoryOptions, categoryOptionCombo.getCategoryOptions() ) ); + + categoryOptions.clear(); + categoryOptions.add( female ); + categoryOptions.add( over15 ); + + categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( categoryOptions ); + + assertNotNull( categoryOptionCombo ); + assertEquals( genderAndAgeGroup, categoryOptionCombo.getCategoryCombo() ); + assertTrue( CollectionUtils.isEqualCollection( categoryOptions, categoryOptionCombo.getCategoryOptions() ) ); + + categoryOptions.clear(); + categoryOptions.add( male ); + categoryOptions.add( female ); + + categoryOptionCombo = categoryOptionComboService.getDataElementCategoryOptionCombo( categoryOptions ); + + assertNull( categoryOptionCombo ); + } +}