------------------------------------------------------------ revno: 5855 committer: Lars Helge Overland <larshe...@gmail.com> branch nick: dhis2 timestamp: Fri 2012-02-03 09:17:56 +0100 message: Work in progress on options (predefined values) for data elements. To be used in tracker module. added: dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/ dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionService.java dhis-2/dhis-api/src/main/java/org/hisp/dhis/option/OptionSet.java dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/option/ 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/resources/org/hisp/dhis/option/ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/option/hibernate/ 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/test/java/org/hisp/dhis/option/ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/option/OptionServiceTest.java modified: dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
-- lp:dhis2 https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk Your team DHIS 2 developers is subscribed to branch lp:dhis2. To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== 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<OptionSet> 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<String> options = new HashSet<String>(); + + public OptionSet() + { + } + + public OptionSet( String name ) + { + this.name = name; + } + + public Set<String> getOptions() + { + return options; + } + + public void setOptions( Set<String> 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<OptionSet> optionSetStore; + + public void setOptionSetStore( GenericIdentifiableObjectStore<OptionSet> 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<OptionSet> 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 @@ <property name="cacheable" value="true" /> </bean> + <bean id="org.hisp.dhis.option.OptionSetStore" class="org.hisp.dhis.hibernate.HibernateGenericStore"> + <property name="clazz" value="org.hisp.dhis.option.OptionSet" /> + <property name="sessionFactory" ref="sessionFactory" /> + <property name="cacheable" value="true" /> + </bean> + <!-- Service definitions --> <bean id="org.hisp.dhis.dataelement.DataElementOperandService" class="org.hisp.dhis.dataelement.DefaultDataElementOperandService"> @@ -479,6 +485,10 @@ <property name="attributeStore" ref="org.hisp.dhis.attribute.AttributeStore" /> <property name="attributeValueStore" ref="org.hisp.dhis.attribute.AttributeValueStore" /> </bean> + + <bean id="org.hisp.dhis.option.OptionService" class="org.hisp.dhis.option.DefaultOptionService"> + <property name="optionSetStore" ref="org.hisp.dhis.option.OptionSetStore"/> + </bean> <bean id="org.hisp.dhis.setting.SystemSettingManager" class="org.hisp.dhis.setting.DefaultSystemSettingManager"> <property name="systemSettingStore" ref="org.hisp.dhis.setting.SystemSettingStore" /> === 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 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" + [<!ENTITY identifiableProperties SYSTEM "classpath://org/hisp/dhis/common/identifiableProperties.hbm">] +> + +<hibernate-mapping> + <class name="org.hisp.dhis.option.OptionSet" table="optionset"> + + <cache usage="read-write" /> + + <id name="id" column="optionsetid"> + <generator class="native" /> + </id> + &identifiableProperties; + + <set name="options" table="optionsetmembers"> + <cache usage="read-write" /> + <key column="optionsetid" foreign-key="fk_optionsetmembers_optionsetid" /> + <element type="string" column="option" /> + </set> + + </class> +</hibernate-mapping> === 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<String> options = new HashSet<String>(); + + 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 ) ); + } +}
_______________________________________________ Mailing list: https://launchpad.net/~dhis2-devs Post to : dhis2-devs@lists.launchpad.net Unsubscribe : https://launchpad.net/~dhis2-devs More help : https://help.launchpad.net/ListHelp