1 5 package com.tdsecurities.itracker.converter; 6 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Timestamp ; 12 import java.text.SimpleDateFormat ; 13 14 import org.apache.log4j.Logger; 15 16 import com.tdsecurities.itracker.common.DataSourceManager; 17 18 22 public abstract class BasicConverter implements Converter 23 { 24 protected static final SimpleDateFormat SQL_DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 25 protected static final int OK = 0; 26 protected static final int SKIP_ROW = 1; 27 protected DataSourceManager ds = DataSourceManager.getInstance(); 28 29 protected abstract String getSourceQuery(); 30 protected abstract String getTargetQuery(); 31 protected abstract String getIdStoreName(); 32 protected abstract String getLastIdQuery(); 33 protected abstract int prepareTargetStatement(PreparedStatement targetStmt, ResultSet rs) throws Exception ; 34 35 private Logger log = Logger.getLogger(BasicConverter.class); 36 37 public final void convert() 38 { 39 PreparedStatement stmt = null; 40 PreparedStatement targetStmt = null; 41 ResultSet rs = null; 42 43 Connection target = ds.getConnection(DataSourceManager.ITRACKER); 44 Connection source = ds.getConnection(DataSourceManager.SOURCEFORGE); 45 try 46 { 47 preConversionProcessing(); 48 stmt = source.prepareStatement(getSourceQuery()); 49 targetStmt = target.prepareStatement(getTargetQuery()); 50 log.debug( "Executing source query"); 51 rs = stmt.executeQuery(); 52 log.debug( "Processing results"); 53 while( rs.next()) 54 { 55 log.debug( "Preparing update"); 56 if( prepareTargetStatement(targetStmt, rs) == OK) 57 { 58 log.debug( "Performing update"); 59 targetStmt.executeUpdate(); 60 } 61 additionalProcessing(targetStmt, rs); 62 } 63 postConversionProcessing(); 64 updateIdStore(); 65 } 66 catch(Throwable t) 67 { 68 log.error("Processing failed: ", t); 69 throw new RuntimeException ("Error converting data", t); 70 } 71 finally 72 { 73 ds.cleanup(source, stmt, rs); 74 ds.cleanup(target, targetStmt, null); 75 } 76 } 77 78 protected void updateIdStore() 79 { 80 log.info( "Updating id store"); 81 PreparedStatement stmt = null; 82 PreparedStatement updateStmt = null; 83 ResultSet rs = null; 84 85 Connection target = ds.getConnection(DataSourceManager.ITRACKER); 86 try 87 { 88 stmt = target.prepareStatement(getLastIdQuery()); 89 rs = stmt.executeQuery(); 90 while( rs.next()) 91 { 92 int lastId = rs.getInt(1); 93 if( lastId > 0) 94 { 95 executeUpdate(DataSourceManager.ITRACKER, 96 " update idstore set last_id = " + lastId + 97 " where name = '" + getIdStoreName() + "'" 98 ); 99 } 100 } 101 } 102 catch(Throwable t) 103 { 104 log.error("Processing failed updating id store: ", t); 105 } 106 finally 107 { 108 ds.cleanup(null, updateStmt, null); 109 ds.cleanup(target, stmt, rs); 110 } 111 } 112 113 protected void preConversionProcessing() 114 { 115 } 116 117 protected void postConversionProcessing() 118 { 119 } 120 121 protected void additionalProcessing(PreparedStatement stmt, ResultSet rs) 122 throws SQLException 123 { 124 } 125 126 protected void executeUpdate(String dataSourceName, String sql) 127 { 128 PreparedStatement stmt = null; 129 130 Connection conn = ds.getConnection(dataSourceName); 131 try 132 { 133 log.debug(sql); 134 stmt = conn.prepareStatement(sql); 135 int rows = stmt.executeUpdate(); 136 log.debug(rows + " affected"); 137 } 138 catch(Throwable t) 139 { 140 log.error("Error executing '" + sql + "' on '" + dataSourceName +"'", t); 141 } 142 finally 143 { 144 ds.cleanup(conn, stmt, null); 145 } 146 } 147 148 protected Object executeQuery(String dataSourceName, String sql) 149 { 150 Object o = null; 151 PreparedStatement stmt = null; 152 ResultSet rs = null; 153 Connection conn = ds.getConnection(dataSourceName); 154 try 155 { 156 log.debug(sql); 157 stmt = conn.prepareStatement(sql); 158 rs = stmt.executeQuery(); 159 if( rs.next()) 160 { 161 o = rs.getObject(1); 162 } 163 } 164 catch(Throwable t) 165 { 166 log.error("Error executing '" + sql + "' on '" + dataSourceName +"'", t); 167 } 168 finally 169 { 170 ds.cleanup(conn, stmt, rs); 171 } 172 return o; 173 } 174 175 protected Timestamp getDate( ResultSet rs, String col) 176 throws SQLException 177 { 178 long time = rs.getLong( col); 179 time = time * 1000; 180 return new Timestamp (time); 181 } 182 183 protected String getFormattedDate(ResultSet rs, String col) 184 throws SQLException 185 { 186 Timestamp ts = getDate( rs, col); 187 return SQL_DATE_FORMAT.format( ts); 188 } 189 190 protected String cleanString(String s) 191 { 192 String tmp = s.replaceAll("\\'","""); 193 tmp = tmp.replaceAll("\\\\","\\\\\\\\"); 194 return tmp; 195 } 196 } 197 | Popular Tags |