← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8861: Impl startup routine for upgrading MapViews to belong to a Map instance

 

------------------------------------------------------------
revno: 8861
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-11-04 23:45:20 +0300
message:
  Impl startup routine for upgrading MapViews to belong to a Map instance
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/MapViewUpgrader.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/MapBatchHandler.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 file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/MapViewUpgrader.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/MapViewUpgrader.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/MapViewUpgrader.java	2012-11-04 20:45:20 +0000
@@ -0,0 +1,113 @@
+package org.hisp.dhis.startup;
+
+import java.sql.ResultSet;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.BatchHandlerFactory;
+import org.amplecode.quick.StatementManager;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.jdbc.batchhandler.MapBatchHandler;
+import org.hisp.dhis.mapping.Map;
+import org.hisp.dhis.system.startup.AbstractStartupRoutine;
+import org.hisp.dhis.user.User;
+import org.springframework.transaction.annotation.Transactional;
+
+public class MapViewUpgrader
+    extends AbstractStartupRoutine
+{
+    private static final Log log = LogFactory.getLog( MapViewUpgrader.class );
+    
+    private StatementManager statementManager;
+    
+    public void setStatementManager( StatementManager statementManager )
+    {
+        this.statementManager = statementManager;
+    }
+    
+    private BatchHandlerFactory batchHandlerFactory;
+
+    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
+    {
+        this.batchHandlerFactory = batchHandlerFactory;
+    }
+    
+    @Override
+    @Transactional
+    public void execute()
+    {
+        executeSql( "update mapview set valuetype=mapvaluetype where valuetype is null" );
+        executeSql( "update mapview set legendtype=maplegendtype where legendtype is null" );
+        executeSql( "update mapview set legendsetid=maplegendsetid where legendsetid is null" );
+        
+        executeSql( "alter table mapview drop column mapvaluetype" );
+        executeSql( "alter table mapview drop column maplegendtype" );
+        executeSql( "alter table mapview drop column maplegendsetid" );
+        
+        executeSql( "alter table mapview drop column bounds" );
+        executeSql( "alter table mapview drop column code" );
+        executeSql( "alter table mapview drop column periodtypeid" );
+        
+        executeSql( "update mapview set layer = 'thematic1' where layer is null" );
+        executeSql( "alter table mapview alter column opacity type double precision" );
+        
+        String sql = "select mapviewid, name, userid, longitude, latitude, zoom from mapview where mapviewid not in (" +
+            "select mapviewid from mapmapviews)";
+
+        BatchHandler<Map> batchHandler = batchHandlerFactory.createBatchHandler( MapBatchHandler.class ).init();
+        
+        try
+        {
+            ResultSet rs = statementManager.getHolder().getStatement().executeQuery( sql );
+            
+            while ( rs.next() )
+            {
+                User user = null;
+                int userId = rs.getInt( "userid" );
+                
+                if ( userId != 0 )
+                {
+                    user = new User();
+                    user.setId( userId );
+                    
+                    log.info( "Creating user " + userId );
+                }
+
+                Map map = new Map( rs.getString( "name" ), user, 
+                    rs.getDouble( "longitude" ), rs.getDouble( "latitude" ), rs.getInt( "zoom" ) );
+
+                batchHandler.addObject( map );
+                
+                log.info( "Upgraded map view: " + map );
+            }
+        }
+        catch ( Exception ex )
+        {
+            log.warn( ex );
+            return;
+        }
+        finally
+        {
+            batchHandler.flush();
+        }
+
+        executeSql( "alter table mapview drop column name" );
+        executeSql( "alter table mapview drop column userid" );
+        executeSql( "alter table mapview drop column longitude" );
+        executeSql( "alter table mapview drop column latitude" );
+        executeSql( "alter table mapview drop column zoom" );
+    }
+
+    private int executeSql( String sql )
+    {
+        try
+        {
+            return statementManager.getHolder().executeUpdate( sql );
+        }
+        catch ( Exception ex )
+        {
+            log.warn( ex );
+            return -1;
+        }
+    }
+}

=== 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-11-02 11:03:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2012-11-04 20:45:20 +0000
@@ -692,6 +692,14 @@
     <property name="skipInTests" value="true" />
   </bean>
 
+  <bean id="org.hisp.dhis.startup.MapViewUpgrader" class="org.hisp.dhis.startup.MapViewUpgrader">
+    <property name="statementManager" ref="statementManager" />
+    <property name="batchHandlerFactory" ref="batchHandlerFactory" />
+    <property name="name" value="MapViewUpgrader" />
+    <property name="runlevel" value="1" />
+    <property name="skipInTests" value="true" />
+  </bean>
+  
   <bean id="org.hisp.dhis.common.IdentityPopulator" class="org.hisp.dhis.common.IdentityPopulator">
     <property name="statementManager" ref="statementManager" />
     <property name="name" value="IdentityPopulator" />
@@ -739,6 +747,7 @@
       <list>
         <list>
           <ref local="org.hisp.dhis.startup.TableAlteror" />
+          <ref local="org.hisp.dhis.startup.MapViewUpgrader" />
           <ref local="org.hisp.dhis.common.IdentityPopulator" />
           <ref local="org.hisp.dhis.period.PeriodTypePopulator" />
           <ref local="org.hisp.dhis.startup.TableCreator" />

=== added file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/MapBatchHandler.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/MapBatchHandler.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/MapBatchHandler.java	2012-11-04 20:45:20 +0000
@@ -0,0 +1,107 @@
+package org.hisp.dhis.jdbc.batchhandler;
+
+/*
+ * Copyright (c) 2004-2012, 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.amplecode.quick.JdbcConfiguration;
+import org.amplecode.quick.batchhandler.AbstractBatchHandler;
+import org.hisp.dhis.mapping.Map;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class MapBatchHandler
+    extends AbstractBatchHandler<Map>
+{
+    // -------------------------------------------------------------------------
+    // Constructor
+    // -------------------------------------------------------------------------
+
+    public MapBatchHandler( JdbcConfiguration config )
+    {
+        super( config, false, false );
+    }
+
+    // -------------------------------------------------------------------------
+    // AbstractBatchHandler implementation
+    // -------------------------------------------------------------------------
+
+    protected void setTableName()
+    {
+        statementBuilder.setTableName( "map" );
+    }
+
+    @Override
+    protected void setAutoIncrementColumn()
+    {
+        statementBuilder.setAutoIncrementColumn( "mapid" );
+    }
+    
+    @Override
+    protected void setIdentifierColumns()
+    {
+        statementBuilder.setIdentifierColumn( "mapid" );
+    }
+    
+    @Override
+    protected void setIdentifierValues( Map map )
+    {        
+        statementBuilder.setIdentifierValue( map.getId() );
+    }
+
+    @Override
+    protected void setUniqueColumns()
+    {        
+    }
+
+    @Override
+    protected void setUniqueValues( Map object )
+    {        
+    }
+
+    @Override
+    protected void setColumns()
+    {
+        statementBuilder.setColumn( "uid" );
+        statementBuilder.setColumn( "name" );
+        statementBuilder.setColumn( "userid" );
+        statementBuilder.setColumn( "longitude" );
+        statementBuilder.setColumn( "latitude" );
+        statementBuilder.setColumn( "zoom" );
+    }
+
+    @Override
+    protected void setValues( Map map )
+    {
+        statementBuilder.setValue( map.getUid() );        
+        statementBuilder.setValue( map.getName() );        
+        statementBuilder.setValue( map.getUser() != null ? map.getUser().getId() : null );
+        statementBuilder.setValue( map.getLongitude() );
+        statementBuilder.setValue( map.getLatitude() );
+        statementBuilder.setValue( map.getZoom() );
+    }
+}