1 package org.apache.torque.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedOutputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.OutputStream;
26 import java.io.Serializable;
27 import java.math.BigDecimal;
28 import java.util.Hashtable;
29 import java.util.Iterator;
30 import java.util.Map;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.torque.om.SimpleKey;
35
36 import com.workingdogs.village.QueryDataSet;
37 import com.workingdogs.village.Record;
38 import com.workingdogs.village.TableDataSet;
39
40 /***
41 * Some Village related code factored out of the BasePeer.
42 *
43 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 * @version $Id: VillageUtils.java 476550 2006-11-18 16:08:37Z tfischer $
45 */
46 public final class VillageUtils
47 {
48 /*** The log. */
49 private static Log log = LogFactory.getLog(VillageUtils.class);
50
51 /***
52 * Private constructor to prevent instantiation.
53 *
54 * Class contains only static method ans should therefore not be
55 * instantiated.
56 */
57 private VillageUtils()
58 {
59 }
60
61 /***
62 * Convenience Method to close a Table Data Set without
63 * Exception check.
64 *
65 * @param tds A TableDataSet
66 */
67 public static final void close(final TableDataSet tds)
68 {
69 if (tds != null)
70 {
71 try
72 {
73 tds.close();
74 }
75 catch (Exception ignored)
76 {
77 log.debug("Caught exception when closing a TableDataSet",
78 ignored);
79 }
80 }
81 }
82
83 /***
84 * Convenience Method to close a Table Data Set without
85 * Exception check.
86 *
87 * @param qds A TableDataSet
88 */
89 public static final void close(final QueryDataSet qds)
90 {
91 if (qds != null)
92 {
93 try
94 {
95 qds.close();
96 }
97 catch (Exception ignored)
98 {
99 log.debug("Caught exception when closing a QueryDataSet",
100 ignored);
101 }
102 }
103 }
104
105 /***
106 * Convenience Method to close an Output Stream without
107 * Exception check.
108 *
109 * @param os An OutputStream
110 */
111 public static final void close(final OutputStream os)
112 {
113 try
114 {
115 if (os != null)
116 {
117 os.close();
118 }
119 }
120 catch (Exception ignored)
121 {
122 log.debug("Caught exception when closing an OutputStream",
123 ignored);
124 }
125 }
126
127 /***
128 * Converts a hashtable to a byte array for storage/serialization.
129 *
130 * @param hash The Hashtable to convert.
131 * @return A byte[] with the converted Hashtable.
132 * @throws Exception If an error occurs.
133 */
134 public static final byte[] hashtableToByteArray(final Hashtable hash)
135 throws Exception
136 {
137 Hashtable saveData = new Hashtable(hash.size());
138 byte[] byteArray = null;
139
140 Iterator keys = hash.entrySet().iterator();
141 while (keys.hasNext())
142 {
143 Map.Entry entry = (Map.Entry) keys.next();
144 if (entry.getValue() instanceof Serializable)
145 {
146 saveData.put(entry.getKey(), entry.getValue());
147 }
148 }
149
150 ByteArrayOutputStream baos = null;
151 BufferedOutputStream bos = null;
152 ObjectOutputStream out = null;
153 try
154 {
155
156 baos = new ByteArrayOutputStream();
157 bos = new BufferedOutputStream(baos);
158 out = new ObjectOutputStream(bos);
159
160 out.writeObject(saveData);
161
162 out.flush();
163 bos.flush();
164 baos.flush();
165 byteArray = baos.toByteArray();
166 }
167 finally
168 {
169 close(out);
170 close(bos);
171 close(baos);
172 }
173 return byteArray;
174 }
175
176 /***
177 * Factored out setting of a Village Record column from a Criteria Key
178 *
179 * @param crit The Criteria
180 * @param key The Criterion Key
181 * @param rec The Village Record
182 * @param colName The name of the Column in the Record
183 */
184 public static final void setVillageValue(final Criteria crit,
185 final String key,
186 final Record rec,
187 final String colName)
188 throws Exception
189 {
190
191
192 Object obj = crit.getValue(key);
193 if (obj instanceof SimpleKey)
194 {
195 obj = ((SimpleKey) obj).getValue();
196 }
197 if (obj == null)
198 {
199 rec.setValueNull(colName);
200 }
201 else if (obj instanceof String)
202 {
203 rec.setValue(colName, (String) obj);
204 }
205 else if (obj instanceof Integer)
206 {
207 rec.setValue(colName,
208 crit.getInt(key));
209 }
210 else if (obj instanceof BigDecimal)
211 {
212 rec.setValue(colName, (BigDecimal) obj);
213 }
214 else if (obj instanceof Boolean)
215 {
216 rec.setValue(colName,
217 ((Boolean) obj).booleanValue());
218 }
219 else if (obj instanceof java.util.Date)
220 {
221 rec.setValue(colName,
222 (java.util.Date) obj);
223 }
224 else if (obj instanceof Float)
225 {
226 rec.setValue(colName,
227 crit.getFloat(key));
228 }
229 else if (obj instanceof Double)
230 {
231 rec.setValue(colName,
232 crit.getDouble(key));
233 }
234 else if (obj instanceof Byte)
235 {
236 rec.setValue(colName,
237 ((Byte) obj).byteValue());
238 }
239 else if (obj instanceof Long)
240 {
241 rec.setValue(colName,
242 crit.getLong(key));
243 }
244 else if (obj instanceof Short)
245 {
246 rec.setValue(colName,
247 ((Short) obj).shortValue());
248 }
249 else if (obj instanceof Hashtable)
250 {
251 rec.setValue(colName,
252 hashtableToByteArray((Hashtable) obj));
253 }
254 else if (obj instanceof byte[])
255 {
256 rec.setValue(colName, (byte[]) obj);
257 }
258 }
259 }