← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1287: Work in progress on eliminating data element duplicates

 

------------------------------------------------------------
revno: 1287
committer: Lars Helge Oeverland <larshelge@xxxxxxxxx>
branch nick: trunk
timestamp: Fri 2009-12-25 18:33:46 +0100
message:
  Work in progress on eliminating data element duplicates
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/DefaultDataMergeService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc/
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc/JdbcDataMergeStore.java
modified:
  dhis-2/dhis-services/dhis-service-administration/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/datamerge'
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeService.java	2009-12-25 17:33:46 +0000
@@ -0,0 +1,43 @@
+package org.hisp.dhis.datamerge;
+
+/*
+ * 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 org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+/**
+ * @author Lars Helge Overland
+ */
+public interface DataMergeService
+{
+    void mergeDataElements( DataElement destDataElement, DataElementCategoryOptionCombo destCategoryOptionCombo,
+        DataElement sourceDataElemenet, DataElementCategoryOptionCombo sourceCategoryOptionCombo );
+    
+    void mergeOrganisationUnits( OrganisationUnit dest, OrganisationUnit source );
+}

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/datamerge/DataMergeStore.java	2009-12-25 17:33:46 +0000
@@ -0,0 +1,43 @@
+package org.hisp.dhis.datamerge;
+
+/*
+ * 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 org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+/**
+ * @author Lars Helge Overland
+ */
+public interface DataMergeStore
+{
+    void mergeDataElements( DataElement destDataElement, DataElementCategoryOptionCombo destCategoryOptionCombo,
+        DataElement sourceDataElemenet, DataElementCategoryOptionCombo sourceCategoryOptionCombo );
+    
+    void mergeOrganisationUnits( OrganisationUnit dest, OrganisationUnit source );
+}

=== added directory 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge'
=== added file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/DefaultDataMergeService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/DefaultDataMergeService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/DefaultDataMergeService.java	2009-12-25 17:33:46 +0000
@@ -0,0 +1,59 @@
+package org.hisp.dhis.datamerge;
+
+/*
+ * 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 org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.datamerge.DataMergeService;
+import org.hisp.dhis.datamerge.DataMergeStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class DefaultDataMergeService
+    implements DataMergeService
+{
+    private DataMergeStore dataMergeStore;
+    
+    public void setDataMergeStore( DataMergeStore dataMergeStore )
+    {
+        this.dataMergeStore = dataMergeStore;
+    }
+
+    public void mergeDataElements( DataElement destDataElement, DataElementCategoryOptionCombo destCategoryOptionCombo,
+        DataElement sourceDataElemenet, DataElementCategoryOptionCombo sourceCategoryOptionCombo )
+    {
+        dataMergeStore.mergeDataElements( destDataElement, destCategoryOptionCombo, sourceDataElemenet, sourceCategoryOptionCombo );        
+    }
+
+    public void mergeOrganisationUnits( OrganisationUnit dest, OrganisationUnit source )
+    {
+        dataMergeStore.mergeOrganisationUnits( dest, source );        
+    }
+}

=== added directory 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc'
=== added file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc/JdbcDataMergeStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc/JdbcDataMergeStore.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/datamerge/jdbc/JdbcDataMergeStore.java	2009-12-25 17:33:46 +0000
@@ -0,0 +1,118 @@
+package org.hisp.dhis.datamerge.jdbc;
+
+/*
+ * 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 org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.datamerge.DataMergeStore;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class JdbcDataMergeStore
+    implements DataMergeStore
+{
+    private static final Log log = LogFactory.getLog( JdbcDataMergeStore.class );
+    
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private JdbcTemplate jdbcTemplate;
+
+    public void setJdbcTemplate( JdbcTemplate jdbcTemplate )
+    {
+        this.jdbcTemplate = jdbcTemplate;
+    }
+    
+    // -------------------------------------------------------------------------
+    // DataMergeStore implementation
+    // -------------------------------------------------------------------------
+
+    public void mergeDataElements( DataElement destDataElement, DataElementCategoryOptionCombo destCategoryOptionCombo,
+        DataElement sourceDataElemenet, DataElementCategoryOptionCombo sourceCategoryOptionCombo )
+    {
+        final int destDataElementId = destDataElement.getId();
+        final int destCategoryOptionComboId = destCategoryOptionCombo.getId();
+        final int sourceDataElementId = sourceDataElemenet.getId();
+        final int sourceCategoryOptionComboId = sourceCategoryOptionCombo.getId();
+        
+        final String sql =         
+            "UPDATE datavalue AS d1 SET dataelementid=" + destDataElementId + ", categoryoptioncomboid=" + destCategoryOptionComboId + " " +
+            "WHERE dataelementid=" + sourceDataElementId + " and categoryoptioncomboid=" + sourceCategoryOptionComboId + " " +
+            "AND NOT EXISTS ( " +
+                "SELECT * FROM datavalue AS d2 " +
+                "WHERE d2.dataelementid=" + destDataElementId + " " +
+                "AND d2.categoryoptioncomboid=" + destCategoryOptionComboId + " " +
+                "AND d1.periodid=d2.periodid " +
+                "AND d1.sourceid=d2.sourceid ); " +
+    
+            "UPDATE datavalue SET value=d2.value,storedby=d2.storedby,lastupdated=d2.lastupdated,comment=d2.comment,followup=d2.followup " +
+            "FROM datavalue AS d2 " +
+            "WHERE datavalue.periodid=d2.periodid " +
+            "AND datavalue.sourceid=d2.sourceid " +
+            "AND datavalue.lastupdated<d2.lastupdated " +
+            "AND datavalue.dataelementid=" + destDataElementId + " AND datavalue.categoryoptioncomboid=" + destCategoryOptionComboId + " " +
+            "AND d2.dataelementid=" + sourceDataElementId + " AND d2.categoryoptioncomboid=" + sourceCategoryOptionComboId + "; " +
+    
+            "DELETE FROM datavalue WHERE dataelementid=" + sourceDataElementId + " AND categoryoptioncomboid=" + sourceCategoryOptionComboId + ";";
+
+        log.info( sql );
+        
+        jdbcTemplate.execute( sql );
+
+    }
+    
+    public void mergeOrganisationUnits( OrganisationUnit dest, OrganisationUnit source )
+    {
+        final int destId = dest.getId();
+        final int sourceId = source.getId();
+        
+        final String sql = 
+            "UPDATE datavalue AS d1 SET sourceid=" + destId + " " +
+            "WHERE sourceid=" + sourceId + " " +
+            "AND NOT EXISTS ( " +
+                "SELECT * from datavalue AS d2 " +
+                "WHERE d2.sourceid=" + destId + " " +
+                "AND d1.dataelementid=d2.dataelementid " +
+                "AND d1.periodid=d2.periodid " +
+                "AND d1.categoryoptioncomboid=d2.categoryoptioncomboid ); " +
+                
+            // TODO summarize when matching values exist
+            
+            "DELETE FROM datavalue WHERE sourceid=" + sourceId + ";";
+        
+        log.info( sql );
+        
+        jdbcTemplate.execute( sql );
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2009-12-23 12:54:35 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2009-12-25 17:33:46 +0000
@@ -98,4 +98,16 @@
       ref="org.hisp.dhis.databrowser.DataBrowserStore" />
   </bean>
        
+  <!-- Data merge -->
+  
+  <bean id="org.hisp.dhis.datamerge.DataMergeStore"
+    class="org.hisp.dhis.datamerge.jdbc.JdbcDataMergeStore">
+    <property name="jdbcTemplate" ref="jdbcTemplate"/>
+  </bean>
+  
+  <bean id="org.hisp.dhis.datamerge.DataMergeService"
+    class="org.hisp.dhis.datamerge.DefaultDataMergeService">
+    <property name="dataMergeStore" ref="org.hisp.dhis.datamerge.DataMergeStore"/>
+  </bean>
+  
 </beans>