View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.tools.database;
19  
20  import java.sql.*;
21  import java.util.*;
22  import java.lang.reflect.*;
23  
24  /***
25   * Implementation of the rowmapper for Oracle. This class has been tested against
26   * Oracle 8.1.7 and 8.2.
27   *
28   * @author  P.S.D.Reitsma, Finalist IT Group
29   * @version 1.0
30   */
31  public class DBFRowMapper extends RowMapper {
32  
33     public PreparedStatement bind(PreparedStatement pstmt, Object bindVar, String bindVarClassType, int pos) throws SQLException, NotMappableException {
34        return null;
35     }
36  
37  
38     public HashMap unBindIntoHashMap(ResultSet r, HashMap target) throws SQLException, NotMappableException {
39        return null;
40     }
41  
42  
43     public Object unBindIntoBean(ResultSet rset, Object target) throws SQLException, NotMappableException {
44        //System.out.println("unbinding into bean");
45  
46        ResultSetMetaData rsmd = rset.getMetaData();
47        String sourceType = null;
48        String sourceName = null;
49        String targetType = null;
50        for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {
51           try {
52              //determine source data type
53              sourceType = rsmd.getColumnTypeName(i);
54              //int sourcePrecision = rsmd.getPrecision(i);
55              sourceName = rsmd.getColumnName(i);
56              //determine target datatype
57              targetType = MethodInvoker.getReturnType(target, rsmd.getColumnName(i));
58  
59              //            System.out.println("sourceType | sourceName");
60              //            System.out.println(sourceType + " | " + sourceName);
61              //
62              //            System.out.println("targetType : " + targetType);
63  
64              if (sourceType.indexOf("C") > -1 && targetType.endsWith("String")) {
65                 String targetValue = byteArrayExtendedToString(rset.getBytes(i));
66                 MethodInvoker.setProperty(target, sourceName, targetValue);
67                 continue;
68              }
69              if (sourceType.indexOf("M") > -1 && targetType.endsWith("String")) {
70                 String targetValue = byteArrayExtendedToString(rset.getBytes(i));
71                 MethodInvoker.setProperty(target, sourceName, targetValue);
72                 continue;
73              }
74              if (sourceType.indexOf("C") > -1 && targetType.toLowerCase().endsWith("long")) {
75                 String targetStringValue = rset.getString(i);
76                 Long targetValue = new Long(0);
77                 try {
78                    targetValue = new Long(targetStringValue.trim());
79                 }
80                 catch (NumberFormatException e) {
81                    System.out.println("value is not a number");
82                 }
83                 finally {
84                    MethodInvoker.setProperty(target, sourceName, targetValue);
85                    continue;
86                 }
87              }
88  
89              if (sourceType.startsWith("D") && targetType.endsWith("util.Date")) {
90                 java.util.Date targetValue = null;
91                 //check if the Date is not null
92                 if (rset.getDate(i) != null) {
93                    targetValue = new java.util.Date(rset.getDate(i).getTime());
94                 }
95                 MethodInvoker.setProperty(target, sourceName, targetValue);
96                 continue;
97              }
98              if (sourceType.startsWith("D") && targetType.endsWith("Calendar")) {
99                 java.util.Calendar targetValue = Calendar.getInstance();
100                //check if the Date is not null
101                if (rset.getDate(i) != null) {
102                   targetValue.setTime(rset.getTimestamp(i));
103                   MethodInvoker.setProperty(target, sourceName, targetValue);
104                }
105                continue;
106             }
107             if (sourceType.startsWith("N") && targetType.toLowerCase().indexOf("int") > -1) {
108                if (rset.getObject(i) != null) {
109                   Integer targetValue = new Integer(rset.getInt(i));
110                   MethodInvoker.setProperty(target, sourceName, targetValue);
111                }
112                continue;
113             }
114             if (sourceType.startsWith("N") && targetType.toLowerCase().endsWith("float")) {
115                if (rset.getObject(i) != null) {
116                   Float targetValue = new Float(rset.getFloat(i));
117                   MethodInvoker.setProperty(target, sourceName, targetValue);
118                }
119                continue;
120             }
121             if (sourceType.startsWith("N") && targetType.toLowerCase().endsWith("byte")) {
122                if (rset.getObject(i) != null) {
123                   Byte targetValue = new Byte(rset.getByte(i));
124                   MethodInvoker.setProperty(target, sourceName, targetValue);
125                }
126                continue;
127             }
128             if (sourceType.startsWith("N") && targetType.toLowerCase().endsWith("short")) {
129                if (rset.getObject(i) != null) {
130                   Short targetValue = new Short(rset.getShort(i));
131                   MethodInvoker.setProperty(target, sourceName, targetValue);
132                }
133                continue;
134             }
135             if (sourceType.startsWith("N") && targetType.toLowerCase().endsWith("double")) {
136                if (rset.getObject(i) != null) {
137                   Double targetValue = new Double(rset.getDouble(i));
138                   MethodInvoker.setProperty(target, sourceName, targetValue);
139                }
140                continue;
141             }
142             if (sourceType.startsWith("N") && targetType.toLowerCase().endsWith("long")) {
143                if (rset.getObject(i) != null) {
144                   Long targetValue = new Long(rset.getLong(i));
145                   MethodInvoker.setProperty(target, sourceName, targetValue);
146                }
147                continue;
148             }
149 
150             //if no mapping has been found, throw the NotMappableException
151             NotMappableException nme = new NotMappableException();
152             nme.setDuringBind(false);
153             nme.setSourceName(sourceName);
154             nme.setSourceType(sourceType);
155             nme.setTargetType(targetType);
156             throw nme;
157          }
158          catch (IllegalAccessException iae) {
159             System.out.println("No public getter/setter found in templateBean for column " + sourceName);
160             //throw new NotMappableException("No public getter/setter found in templateBean for column "+sourceName);
161          }
162          catch (InvocationTargetException ite) {
163             throw new NotMappableException("An InvocationTargetException ocurred");
164          }
165          catch (NoSuchMethodException nsme) {
166             System.out.println("No getter/setter found in templateBean for column " + sourceName);
167             //throw new NotMappableException("No getter/setter found in templateBean for column "+sourceName);
168          }
169          catch (Exception e) {
170             e.printStackTrace();
171          }
172 
173       }
174       return target;
175    }
176 
177 
178    public static String byteArrayExtendedToString(byte[] ba) {
179       StringBuffer sb = new StringBuffer();
180       if (ba != null) {
181          for (int i = 0; i < ba.length; i++) {
182             if (ba[i] < 0) {
183                switch (ba[i]) {
184                   case -119:
185                      { // trema e
186                         sb.append((char) 235);
187                         break;
188                      }
189                   case -108:
190                      { // trema o
191                         sb.append((char) 246);
192                         break;
193                      }
194                   case -127:
195                      { // trema u
196                         sb.append((char) 252);
197                         break;
198                      }
199                   case -126:
200                      { // e accent grave
201                         sb.append((char) 233);
202                         break;
203                      }
204                   default :
205                      {
206                         sb.append('|');
207                         break;
208                      }
209                }
210             }
211             else {
212                sb.append((char) ba[i]);
213             }
214          }
215       }
216       return new String(sb).trim();
217    }
218 }