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.math.*;
023    import java.sql.*;
024    import java.util.*;
025    
026    import org.dbreplicator.replication.xml.*;
027    import org.dbreplicator.replication.EncoderDecoder;
028    import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler;
029    
030    /**
031     * Class for handling long decimal numbers.
032     */
033    public class BigDecimalObject extends AbstractColumnObject
034    {
035    
036        int sqlType;
037        AbstractDataBaseHandler abstractDBHandler;
038        /**
039         * sets the SQL type fro BigDecimal datatype
040         * @param sqlType0
041         */
042        public BigDecimalObject(int sqlType0,AbstractDataBaseHandler abstractDBHandler0 )
043        {
044            sqlType = sqlType0;
045             abstractDBHandler =abstractDBHandler0;
046        }
047    
048        /**
049         * set the value for the corresponding datatype i.e bigDecimal
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.setBigDecimal(index, BigDecimal.valueOf(Long.parseLong(value)));
072            }
073        }
074    
075        /**
076         * writes a bigdecimal values in XML file
077         * @param bw          Writer
078         * @param rs          ResultSet
079         * @param index       int
080             * @param encodedCols ArrayList
081             * @param col         String
082         * @throws IOException
083         * @throws SQLException
084         */
085        public void write(Writer bw, ResultSet rs, int index,ArrayList encodedCols,String col) throws
086            IOException, SQLException
087        {
088            try
089            {
090              if (!encodedCols.contains(col.toUpperCase())) {
091                  bw.write(getObject(rs, index).toString() );
092    //            bw.write("<![CDATA[" + getObject(rs, index).toString() + "]]>");
093              } else {
094                bw.write(EncoderDecoder.escapeUnicodeString1(getObject(rs, index).
095                                toString(), true));
096    
097    //            bw.write("<![CDATA[" +
098    //                     EncoderDecoder.escapeUnicodeString1(getObject(rs, index).
099    //                toString(), true) + "]]>");
100              }
101    
102            }
103            catch (NullPointerException ex)
104            {
105                bw.write("NULL");
106            }
107        }
108    
109        private void write(Writer bw, Object rowValue,ArrayList encodedCols,String col) throws IOException
110        {
111            try
112            {
113                if(!encodedCols.contains(col.toUpperCase())) {
114                bw.write("<![CDATA[" + rowValue.toString() + "]]>");
115                } else {
116                  bw.write("<![CDATA[" +
117                           EncoderDecoder.escapeUnicodeString1(rowValue.toString(), true) +
118                           "]]>");
119                }
120    
121            }
122            catch (NullPointerException ex)
123            {
124                bw.write("NULL");
125            }
126        }
127    
128        /**
129         * puts the value for the column aginst the column Name
130         * @param bw                Writer
131         * @param rows              ResultSet
132         * @param oldResultSet      ResultSet
133         * @param index             int
134         * @param modifiedColumns   HashMap
135         * @param columnName        String
136         * @param encodedCols       ArrayList
137         * @throws SQLException  
138         * @throws IOException
139         */
140        public void writeUpdate(Writer bw, ResultSet rows,
141                                ResultSet oldResultSet
142                                , int index, HashMap modifiedColumns,
143                                String columnName,ArrayList encodedCols) throws SQLException, IOException
144        {
145            Object newObject = rows.getObject(index);
146            Object oldObject = oldResultSet.getObject(index);
147    
148            if (newObject == null)
149            {
150                write(bw, "NULL",encodedCols,columnName);
151                if (oldObject != null)
152                {
153                    modifiedColumns.put(columnName, "NULL");
154                }
155            }
156            else
157            {
158                write(bw, newObject,encodedCols,columnName);
159                if (oldObject != null)
160                {
161                    if (! (newObject.equals(oldObject)))
162                    {
163                        modifiedColumns.put(columnName, newObject);
164                    }
165                }
166                else
167                {
168                    modifiedColumns.put(columnName, newObject);
169                }
170            }
171        }
172    
173        /**
174         * returns the parsed value of String in BigDecimal
175         * @param value
176         * @return Object
177         */
178        public Object getObject(String value)
179        {
180            if (value.equalsIgnoreCase("NULL"))
181            {
182                return null;
183            }
184            return BigDecimal.valueOf(Long.parseLong(value));
185        }
186    
187        /**
188         * returns the Object corresponding to the index passed from the resultSet
189         * @param row
190         * @param index
191         * @return
192         * @throws SQLException
193         */
194        private Object getObject(ResultSet row, int index) throws SQLException
195        {
196            return row.getObject(index);
197        }
198    
199    
200    
201    }





























































Powered by Drupal - Theme by Danger4k