001 /** 002 * Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of version 2 of the GNU General Public License as 005 * published by the Free Software Foundation. 006 * There are special exceptions to the terms and conditions of the GPL 007 * as it is applied to this software. See the GNU General Public License for more details. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 017 */ 018 019 package org.dbreplicator.replication.column; 020 021 import java.io.*; 022 import java.sql.*; 023 import java.util.*; 024 025 import org.dbreplicator.replication.xml.*; 026 import org.dbreplicator.replication.EncoderDecoder; 027 import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler; 028 029 /** 030 * 031 */ 032 public class LongObject extends AbstractColumnObject 033 { 034 035 int sqlType; 036 AbstractDataBaseHandler abstractDBHandler; 037 038 /** 039 * sets the SQL type fro long datatype 040 * @param sqlType0 041 */ 042 public LongObject(int sqlType0,AbstractDataBaseHandler abstractDBHandler0) 043 { 044 sqlType = sqlType0; 045 abstractDBHandler=abstractDBHandler0; 046 } 047 048 /** 049 * set the value for the corresponding datatype i.e long 050 * @param pst 051 * @param element 052 * @param index 053 * @throws SQLException 054 */ 055 public void setColumnObject(PreparedStatement pst, XMLElement element, 056 int index) throws SQLException 057 { 058 String value = element.elementValue; 059 setColumnObject(pst, value, index); 060 } 061 062 public void setColumnObject(PreparedStatement pst, String value, 063 int index) throws SQLException 064 { 065 if (value.equalsIgnoreCase("NULL")) 066 { 067 pst.setNull(index, sqlType); 068 } 069 else 070 { 071 pst.setLong(index, Long.parseLong(value)); 072 } 073 } 074 075 /** 076 * writes a long values in XML file 077 * @param os 078 * @param rs 079 * @param index 080 * @throws IOException 081 * @throws SQLException 082 */ 083 public void write(Writer os, ResultSet rs, int index,ArrayList encodedCols,String col) throws 084 SQLException, IOException 085 { 086 try 087 { 088 if(!encodedCols.contains(col.toUpperCase())) { 089 os.write(getObject(rs, index).toString()); 090 // os.write("<![CDATA[" + getObject(rs, index).toString() + "]]>"); 091 } else { 092 os.write(EncoderDecoder.escapeUnicodeString1(getObject(rs, index). 093 toString(), true) ); 094 095 // os.write("<![CDATA[" + 096 // EncoderDecoder.escapeUnicodeString1(getObject(rs, index). 097 // toString(), true) + 098 // "]]>"); 099 } 100 101 } 102 catch (NullPointerException ex) 103 { 104 os.write("NULL"); 105 } 106 } 107 108 private void write(Writer os, Object rowValue,ArrayList encodedCols,String col) throws 109 SQLException, IOException 110 { 111 try 112 { 113 if(!encodedCols.contains(col.toUpperCase())) { 114 os.write(rowValue.toString()); 115 // os.write("<![CDATA[" + rowValue.toString() + "]]>"); 116 } else { 117 // os.write("<![CDATA[" + 118 // EncoderDecoder.escapeUnicodeString1(rowValue.toString(), true) + 119 // "]]>"); 120 121 os.write(EncoderDecoder.escapeUnicodeString1(rowValue.toString(), true)); 122 } 123 } 124 catch (NullPointerException ex) 125 { 126 os.write("NULL"); 127 } 128 } 129 130 /** 131 * puts the value for the column aginst the column Name 132 * @param os 133 * @param rows 134 * @param oldResultSet 135 * @param index 136 * @param modifiedColumns 137 * @param columnName 138 * @throws SQLException 139 * @throws IOException 140 */ 141 public void writeUpdate(Writer os, ResultSet rows, 142 ResultSet oldResultSet 143 , int index, HashMap modifiedColumns, 144 String columnName,ArrayList encodedCols) throws SQLException, IOException 145 { 146 Object newObject = rows.getObject(index); 147 Object oldObject = oldResultSet.getObject(index); 148 149 if (newObject == null) 150 { 151 write(os, "NULL",encodedCols,columnName); 152 if (oldObject != null) 153 { 154 modifiedColumns.put(columnName, "NULL"); 155 } 156 } 157 else 158 { 159 write(os, newObject,encodedCols,columnName); 160 if (oldObject != null) 161 { 162 if (! (newObject.equals(oldObject))) 163 { 164 modifiedColumns.put(columnName, newObject); 165 } 166 } 167 else 168 { 169 modifiedColumns.put(columnName, newObject); 170 } 171 172 } 173 } 174 175 /** 176 * returns the parsed value of String in long 177 * @param value String 178 * @return Object 179 */ 180 public Object getObject(String value) throws SQLException 181 { 182 if (value.equalsIgnoreCase("NULL")) 183 { 184 return null; 185 } 186 return Long.valueOf(value); 187 } 188 189 /** 190 * returns the Object corresponding to the index passed from the resultSet 191 * @param row 192 * @param index 193 * @return 194 * @throws SQLException 195 */ 196 197 private Object getObject(ResultSet row, int index) throws SQLException 198 { 199 return row.getObject(index); 200 } 201 202 203 204 }

