=== added directory 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/option' === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionService.java 2012-02-03 08:17:56 +0000 @@ -0,0 +1,50 @@ +package org.hisp.dhis.option; + +/* + * Copyright (c) 2004-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.Collection; + +/** + * @author Lars Helge Overland + */ +public interface OptionService +{ + final String ID = OptionService.class.getName(); + + int saveOptionSet( OptionSet optionSet ); + + void updateOptionSet( OptionSet optionSet ); + + OptionSet getOptionSet( int id ); + + OptionSet getOptionSet( String uid ); + + void deleteOptionSet( OptionSet optionSet ); + + Collection getAllOptionSets(); +} === added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionSet.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionSet.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionSet.java 2012-02-03 08:17:56 +0000 @@ -0,0 +1,105 @@ +package org.hisp.dhis.option; + +/* + * Copyright (c) 2004-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.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.hisp.dhis.common.BaseIdentifiableObject; + +/** + * @author Lars Helge Overland + */ +public class OptionSet + extends BaseIdentifiableObject +{ + private static final Pattern OPTION_PATTERN = Pattern.compile( "\\[(.*)\\]" ); + + private Set options = new HashSet(); + + public OptionSet() + { + } + + public OptionSet( String name ) + { + this.name = name; + } + + public Set getOptions() + { + return options; + } + + public void setOptions( Set options ) + { + this.options = options; + } + + @Override + public int hashCode() + { + return name.hashCode(); + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + + if ( o == null ) + { + return false; + } + + if ( !(o instanceof OptionSet) ) + { + return false; + } + + final OptionSet other = (OptionSet) o; + + return name.equals( other.getName() ); + } + + public static String optionEncode( String option ) + { + return option != null ? ( "[" + option.replaceAll( " ", "_" ) + "]" ) : null; + } + + public static String optionDecode( String option ) + { + Matcher matcher = OPTION_PATTERN.matcher( option ); + return matcher.find() && matcher.groupCount() > 0 ? matcher.group( 1 ).replaceAll( "_", " " ) : null; + } +} === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/option' === added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/option/DefaultOptionService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/option/DefaultOptionService.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/option/DefaultOptionService.java 2012-02-03 08:17:56 +0000 @@ -0,0 +1,78 @@ +package org.hisp.dhis.option; + +/* + * Copyright (c) 2004-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.Collection; + +import org.hisp.dhis.common.GenericIdentifiableObjectStore; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author Lars Helge Overland + */ +@Transactional +public class DefaultOptionService + implements OptionService +{ + private GenericIdentifiableObjectStore optionSetStore; + + public void setOptionSetStore( GenericIdentifiableObjectStore optionSetStore ) + { + this.optionSetStore = optionSetStore; + } + + public int saveOptionSet( OptionSet optionSet ) + { + return optionSetStore.save( optionSet ); + } + + public void updateOptionSet( OptionSet optionSet ) + { + optionSetStore.update( optionSet ); + } + + public OptionSet getOptionSet( int id ) + { + return optionSetStore.get( id ); + } + + public OptionSet getOptionSet( String uid ) + { + return optionSetStore.getByUid( uid ); + } + + public void deleteOptionSet( OptionSet optionSet ) + { + optionSetStore.delete( optionSet ); + } + + public Collection getAllOptionSets() + { + return optionSetStore.getAll(); + } +} === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2012-02-02 20:01:36 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml 2012-02-03 08:17:56 +0000 @@ -258,6 +258,12 @@ + + + + + + @@ -479,6 +485,10 @@ + + + + === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option' === added directory 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option/hibernate' === added file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option/hibernate/OptionSet.hbm.xml' --- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option/hibernate/OptionSet.hbm.xml 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option/hibernate/OptionSet.hbm.xml 2012-02-03 08:17:56 +0000 @@ -0,0 +1,25 @@ + +] +> + + + + + + + + + + &identifiableProperties; + + + + + + + + + === added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/option' === added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/option/OptionServiceTest.java' --- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/option/OptionServiceTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/option/OptionServiceTest.java 2012-02-03 08:17:56 +0000 @@ -0,0 +1,98 @@ +package org.hisp.dhis.option; + +/* + * Copyright (c) 2004-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.HashSet; +import java.util.Set; + +import org.hisp.dhis.DhisSpringTest; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Lars Helge Overland + */ +public class OptionServiceTest + extends DhisSpringTest +{ + private OptionService optionService; + + private Set options = new HashSet(); + + private OptionSet optionSetA = new OptionSet( "OptionSetA" ); + private OptionSet optionSetB = new OptionSet( "OptionSetB" ); + private OptionSet optionSetC = new OptionSet( "OptionSetC" ); + + @Override + public void setUpTest() + { + optionService = (OptionService) getBean( OptionService.ID ); + + options.add( "OptionA" ); + options.add( "OptionB" ); + options.add( "OptionC" ); + + optionSetA.setOptions( options ); + optionSetB.setOptions( options ); + } + + @Test + public void testSaveGet() + { + int idA = optionService.saveOptionSet( optionSetA ); + int idB = optionService.saveOptionSet( optionSetB ); + int idC = optionService.saveOptionSet( optionSetC ); + + OptionSet actualA = optionService.getOptionSet( idA ); + OptionSet actualB = optionService.getOptionSet( idB ); + OptionSet actualC = optionService.getOptionSet( idC ); + + assertEquals( optionSetA, actualA ); + assertEquals( optionSetB, actualB ); + assertEquals( optionSetC, actualC ); + + assertEquals( 3, optionSetA.getOptions().size() ); + assertEquals( 3, optionSetB.getOptions().size() ); + assertEquals( 0, optionSetC.getOptions().size() ); + + assertTrue( optionSetA.getOptions().contains( "OptionA" ) ); + assertTrue( optionSetA.getOptions().contains( "OptionB" ) ); + assertTrue( optionSetA.getOptions().contains( "OptionC" ) ); + } + + @Test + public void testCodec() + { + String decoded = "Malaria Severe Under 5"; + String encoded = "[Malaria_Severe_Under_5]"; + + assertEquals( encoded, OptionSet.optionEncode( decoded ) ); + assertEquals( decoded, OptionSet.optionDecode( encoded ) ); + } +}