=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java 2015-09-02 16:15:15 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java 2015-09-13 15:47:21 +0000 @@ -64,6 +64,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; +import com.google.common.collect.Sets; /** * This class is used for defining the standardized DataSets. A DataSet consists @@ -289,18 +290,21 @@ public void updateOrganisationUnits( Set updates ) { - for ( OrganisationUnit unit : new HashSet<>( sources ) ) - { - if ( !updates.contains( unit ) ) - { - removeOrganisationUnit( unit ); - } - } + Set toRemove = Sets.difference( sources, updates ); + Set toAdd = Sets.difference( updates, sources ); - for ( OrganisationUnit unit : updates ) - { - addOrganisationUnit( unit ); - } + for ( OrganisationUnit unit : toRemove ) + { + unit.getDataSets().remove( this ); + } + + for ( OrganisationUnit unit : toAdd ) + { + unit.getDataSets().add( this ); + } + + sources.clear(); + sources.addAll( updates ); } public void addDataElement( DataElement dataElement ) @@ -317,18 +321,21 @@ public void updateDataElements( Set updates ) { - for ( DataElement dataElement : new HashSet<>( dataElements ) ) - { - if ( !updates.contains( dataElement ) ) - { - removeDataElement( dataElement ); - } - } - - for ( DataElement dataElement : updates ) - { - addDataElement( dataElement ); - } + Set toRemove = Sets.difference( dataElements, updates ); + Set toAdd = Sets.difference( updates, dataElements ); + + for ( DataElement element : toRemove ) + { + element.getDataSets().remove( this ); + } + + for ( DataElement element : toAdd ) + { + element.getDataSets().add( this ); + } + + dataElements.clear(); + dataElements.addAll( updates ); } public void addIndicator( Indicator indicator ) === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java 2015-09-03 03:11:47 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java 2015-09-13 15:47:21 +0000 @@ -72,6 +72,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import com.google.common.base.Joiner; +import com.google.common.collect.Sets; /** * @author Kristian Nordal @@ -274,18 +275,21 @@ public void updateDataSets( Set updates ) { - for ( DataSet dataSet : new HashSet<>( dataSets ) ) - { - if ( !updates.contains( dataSet ) ) - { - removeDataSet( dataSet ); - } - } - - for ( DataSet dataSet : updates ) - { - addDataSet( dataSet ); - } + Set toRemove = Sets.difference( dataSets, updates ); + Set toAdd = Sets.difference( updates, dataSets ); + + for ( DataSet dataSet : toRemove ) + { + dataSet.getSources().remove( this ); + } + + for ( DataSet dataSet : toAdd ) + { + dataSet.getSources().add( this ); + } + + dataSets.clear(); + dataSets.addAll( updates ); } public void addUser( User user ) === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java 2015-09-13 14:17:27 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Program.java 2015-09-13 15:47:21 +0000 @@ -60,6 +60,7 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; +import com.google.common.collect.Sets; /** * @author Abyot Asalefew @@ -167,18 +168,21 @@ public void updateOrganisationUnits( Set updates ) { - for ( OrganisationUnit unit : new HashSet<>( organisationUnits ) ) - { - if ( !updates.contains( unit ) ) - { - removeOrganisationUnit( unit ); - } - } - - for ( OrganisationUnit unit : updates ) - { - addOrganisationUnit( unit ); - } + Set toRemove = Sets.difference( organisationUnits, updates ); + Set toAdd = Sets.difference( updates, organisationUnits ); + + for ( OrganisationUnit unit : toRemove ) + { + unit.getPrograms().remove( this ); + } + + for ( OrganisationUnit unit : toAdd ) + { + unit.getPrograms().add( this ); + } + + organisationUnits.clear(); + organisationUnits.addAll( updates ); } /** === added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataset/DataSetTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataset/DataSetTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataset/DataSetTest.java 2015-09-13 15:47:21 +0000 @@ -0,0 +1,104 @@ +package org.hisp.dhis.dataset; + +/* + * Copyright (c) 2004-2015, 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 org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.dataelement.DataElement; +import org.junit.Test; + +import com.google.common.collect.Sets; + +/** + * @author Lars Helge Overland + */ +public class DataSetTest +{ + @Test + public void testUpdateOrganisationUnits() + { + DataSet dsA = new DataSet( "dsA" ); + + OrganisationUnit ouA = new OrganisationUnit( "ouA" ); + OrganisationUnit ouB = new OrganisationUnit( "ouB" ); + OrganisationUnit ouC = new OrganisationUnit( "ouC" ); + OrganisationUnit ouD = new OrganisationUnit( "ouD" ); + + dsA.addOrganisationUnit( ouA ); + dsA.addOrganisationUnit( ouB ); + + assertEquals( 2, dsA.getSources().size() ); + assertTrue( dsA.getSources().containsAll( Sets.newHashSet( ouA, ouB ) ) ); + assertTrue( ouA.getDataSets().contains( dsA ) ); + assertTrue( ouB.getDataSets().contains( dsA ) ); + assertTrue( ouC.getDataSets().isEmpty() ); + assertTrue( ouD.getDataSets().isEmpty() ); + + dsA.updateOrganisationUnits( Sets.newHashSet( ouB, ouC ) ); + + assertEquals( 2, dsA.getSources().size() ); + assertTrue( dsA.getSources().containsAll( Sets.newHashSet( ouB, ouC ) ) ); + assertTrue( ouA.getDataSets().isEmpty() ); + assertTrue( ouB.getDataSets().contains( dsA ) ); + assertTrue( ouC.getDataSets().contains( dsA ) ); + assertTrue( ouD.getDataSets().isEmpty() ); + } + + @Test + public void testUpdateDataElements() + { + DataSet dsA = new DataSet( "dsA" ); + + DataElement deA = new DataElement( "deA" ); + DataElement deB = new DataElement( "deB" ); + DataElement deC = new DataElement( "deC" ); + DataElement deD = new DataElement( "deD" ); + + dsA.addDataElement( deA ); + dsA.addDataElement( deB ); + + assertEquals( 2, dsA.getDataElements().size() ); + assertTrue( dsA.getDataElements().containsAll( Sets.newHashSet( deA, deB ) ) ); + assertTrue( deA.getDataSets().contains( dsA ) ); + assertTrue( deB.getDataSets().contains( dsA ) ); + assertTrue( deC.getDataSets().isEmpty() ); + assertTrue( deD.getDataSets().isEmpty() ); + + dsA.updateDataElements( Sets.newHashSet( deB, deC ) ); + + assertEquals( 2, dsA.getDataElements().size() ); + assertTrue( dsA.getDataElements().containsAll( Sets.newHashSet( deB, deC ) ) ); + assertTrue( deA.getDataSets().isEmpty() ); + assertTrue( deB.getDataSets().contains( dsA ) ); + assertTrue( deC.getDataSets().contains( dsA ) ); + assertTrue( deD.getDataSets().isEmpty() ); + } +} === added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java' --- dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/program/ProgramTest.java 2015-09-13 15:47:21 +0000 @@ -0,0 +1,73 @@ +package org.hisp.dhis.program; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.junit.Test; + +import com.google.common.collect.Sets; + +/* + * Copyright (c) 2004-2015, 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. + */ + +/** + * @author Lars Helge Overland + */ +public class ProgramTest +{ + @Test + public void testUpdateOrganisationUnits() + { + Program prA = new Program(); + + OrganisationUnit ouA = new OrganisationUnit( "ouA" ); + OrganisationUnit ouB = new OrganisationUnit( "ouB" ); + OrganisationUnit ouC = new OrganisationUnit( "ouC" ); + OrganisationUnit ouD = new OrganisationUnit( "ouD" ); + + prA.addOrganisationUnit( ouA ); + prA.addOrganisationUnit( ouB ); + + assertEquals( 2, prA.getOrganisationUnits().size() ); + assertTrue( prA.getOrganisationUnits().containsAll( Sets.newHashSet( ouA, ouB ) ) ); + assertTrue( ouA.getPrograms().contains( prA ) ); + assertTrue( ouB.getPrograms().contains( prA ) ); + assertTrue( ouC.getPrograms().isEmpty() ); + assertTrue( ouD.getPrograms().isEmpty() ); + + prA.updateOrganisationUnits( Sets.newHashSet( ouB, ouC ) ); + + assertEquals( 2, prA.getOrganisationUnits().size() ); + assertTrue( prA.getOrganisationUnits().containsAll( Sets.newHashSet( ouB, ouC ) ) ); + assertTrue( ouA.getPrograms().isEmpty() ); + assertTrue( ouB.getPrograms().contains( prA ) ); + assertTrue( ouC.getPrograms().contains( prA ) ); + assertTrue( ouD.getPrograms().isEmpty() ); + } +} === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramService.java 2015-09-13 14:54:57 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramService.java 2015-09-13 15:47:21 +0000 @@ -328,7 +328,7 @@ Set userOrganisationUnits = Sets.newHashSet( organisationUnitService.getOrganisationUnitsByQuery( params ) ); - selectedOrgUnits.removeAll( userOrganisationUnits ); + selectedOrgUnits.removeAll( userOrganisationUnits ); selectedOrgUnits.addAll( mergeOrganisationUnits ); program.updateOrganisationUnits( selectedOrgUnits );