JavaDoc


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.sql.Date;
024    import java.util.*;
025    
026    import org.dbreplicator.replication.xml.*;
027    import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler;
028    
029    /**
030     * Handles the Date Ojbect Type
031     */
032    public class DateObject extends AbstractColumnObject
033    {
034    
035        int sqlType;
036        AbstractDataBaseHandler abstractDBHandler;
037    
038        /**
039         * sets the SQL type fro date datatype
040         * @param sqlType0
041         */
042        public DateObject(int sqlType0,AbstractDataBaseHandler abstractDBHandler0)
043        {
044            sqlType = sqlType0;
045            abstractDBHandler=abstractDBHandler0;
046        }
047    
048        /**
049         * set the value for the corresponding datatype i.e date
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                boolean isTime = value.indexOf(":") != -1;
072                boolean isDate = value.indexOf("-") != -1;
073                if (isTime && isDate)
074                {
075                    pst.setTimestamp(index, Timestamp.valueOf(value));
076                }
077                else if (isTime)
078                {
079                    pst.setTime(index, Time.valueOf(value));
080                }
081                else if (isDate)
082                {
083                    pst.setDate(index, Date.valueOf(value));
084                }
085                else
086                {
087                    pst.setObject(index, value);
088                }
089            }
090        }
091    
092        //            else if(value.indexOf(" ")==-1){
093    //            pst.setObject(index, value);
094    //            } else {
095    //            String subvalue=value.substring(0, value.indexOf(" "));
096        //System.out.println(" Inside DateObject Class In Sset Object Method ="+subvalue.trim());
097    //            pst.setObject(index, subvalue.trim());
098    //            }
099    
100    
101        /**
102         * writes a date values in XML file
103         * @param os
104         * @param rs
105         * @param index
106         * @throws IOException
107         * @throws SQLException
108         */
109        public void write(Writer os, ResultSet rs, int index,ArrayList encodedCols,String col) throws
110            SQLException, IOException
111        {
112            try
113            {
114              os.write( getObject(rs, index).toString() );
115    //            os.write("<![CDATA[" + getObject(rs, index).toString() + "]]>");
116            }
117            catch (NullPointerException ex)
118            {
119                os.write("NULL");
120            }
121        }
122    
123        private void write(Writer os, Object rowValue,ArrayList encodedCols,String col) throws
124            SQLException, IOException
125        {
126            try
127            {
128                os.write("<![CDATA[" + rowValue.toString() + "]]>");
129            }
130            catch (NullPointerException ex)
131            {
132                os.write("NULL");
133            }
134        }
135    
136        /**
137         * puts the value for the column aginst the column Name
138         * @param os
139         * @param rows
140         * @param oldResultSet
141         * @param index
142         * @param modifiedColumns
143         * @param columnName
144         * @throws SQLException
145         * @throws IOException
146         */
147        public void writeUpdate(Writer os, ResultSet rows,
148                                ResultSet oldResultSet
149                                , int index, HashMap modifiedColumns,
150                                String columnName,ArrayList encodedCols) throws SQLException, IOException
151        {
152            Object newObject = null;
153            try {
154              newObject = rows.getObject(index);
155            }
156            catch (SQLException ex) {
157            newObject =  new Date(00,00,00);
158            }
159            Object oldObject = null;
160            try {
161               oldObject = oldResultSet.getObject(index);
162            }
163            catch (SQLException ex1) {
164              oldObject =  new Date(00,00,00);
165            }
166    
167            if (newObject == null)
168            {
169                write(os, "NULL",encodedCols,columnName);
170                if (oldObject != null)
171                {
172                    modifiedColumns.put(columnName, "NULL");
173                }
174            }
175            else
176            {
177                write(os, newObject,encodedCols,columnName);
178                if (oldObject != null)
179                {
180                    if (! (newObject.equals(oldObject)))
181                    {
182                        modifiedColumns.put(columnName, newObject);
183                    }
184                }
185                else
186                {
187                    modifiedColumns.put(columnName, newObject);
188                }
189    
190            }
191        }
192    
193        /**
194         * returns the parsed value of String in date
195         * @param value String
196         * @return Object
197         */
198        public Object getObject(String value) throws SQLException
199        {
200            if (value.equalsIgnoreCase("NULL"))
201            {
202                return null;
203            }
204            boolean isTime = value.indexOf(":") != -1;
205            boolean isDate = value.indexOf("-") != -1;
206            if (isTime && isDate)
207            {
208                return Timestamp.valueOf(value);
209            }
210            if (isTime)
211            {
212                return Time.valueOf(value);
213            }
214            if (isDate)
215            {
216                return Date.valueOf(value);
217            }
218            return value;
219        }
220    
221    //      else if(value.indexOf(" ")==-1)
222    //      {
223    //     return Date.valueOf(value);
224    //      }else {
225    //System.out.println("DateObject.getObject(value) ==="+value);
226    //        String subvalue=value.substring(0,value.indexOf(" "));
227    // System.out.println(" Inside DateObject Class In Sset Object Method ="+subvalue.trim());
228    //       return Date.valueOf(subvalue.trim());
229    //      }
230    
231    //    }
232    
233        /**
234         * returns the Object corresponding to the index passed from the resultSet
235         * @param row
236         * @param index
237         * @return
238         * @throws SQLException
239         */
240    
241        private Object getObject(ResultSet row, int index) throws SQLException
242        {
243            try {
244              return row.getObject(index);
245            }
246            catch (SQLException ex) {
247              return new Date(00,00,00);
248            }
249        }
250    }





























































Powered by Drupal - Theme by Danger4k