%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.util.VillageUtils |
|
|
1 | package org.apache.torque.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.BufferedOutputStream; |
|
20 | import java.io.ByteArrayOutputStream; |
|
21 | import java.io.ObjectOutputStream; |
|
22 | import java.io.OutputStream; |
|
23 | import java.io.Serializable; |
|
24 | import java.math.BigDecimal; |
|
25 | import java.util.Hashtable; |
|
26 | import java.util.Iterator; |
|
27 | ||
28 | import org.apache.torque.TorqueException; |
|
29 | import org.apache.torque.om.SimpleKey; |
|
30 | ||
31 | import com.workingdogs.village.QueryDataSet; |
|
32 | import com.workingdogs.village.Record; |
|
33 | import com.workingdogs.village.TableDataSet; |
|
34 | ||
35 | /** |
|
36 | * Some Village related code factored out of the BasePeer. |
|
37 | * |
|
38 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
39 | * @version $Id: VillageUtils.java 239636 2005-08-24 12:38:09Z henning $ |
|
40 | */ |
|
41 | 0 | public abstract class VillageUtils |
42 | implements Serializable |
|
43 | { |
|
44 | /** |
|
45 | * Convenience Method to close a Table Data Set without |
|
46 | * Exception check. |
|
47 | * |
|
48 | * @param tds A TableDataSet |
|
49 | */ |
|
50 | public static final void close(class="keyword">final TableDataSet tds) |
|
51 | { |
|
52 | 0 | if (tds != null) |
53 | { |
|
54 | try |
|
55 | { |
|
56 | 0 | tds.close(); |
57 | } |
|
58 | 0 | catch (Exception ignored) |
59 | { |
|
60 | 0 | } |
61 | } |
|
62 | 0 | } |
63 | ||
64 | /** |
|
65 | * Convenience Method to close a Table Data Set without |
|
66 | * Exception check. |
|
67 | * |
|
68 | * @param qds A TableDataSet |
|
69 | */ |
|
70 | public static final void close(class="keyword">final QueryDataSet qds) |
|
71 | { |
|
72 | 0 | if (qds != null) |
73 | { |
|
74 | try |
|
75 | { |
|
76 | 0 | qds.close(); |
77 | } |
|
78 | 0 | catch (Exception ignored) |
79 | { |
|
80 | 0 | } |
81 | } |
|
82 | 0 | } |
83 | ||
84 | /** |
|
85 | * Convenience Method to close an Output Stream without |
|
86 | * Exception check. |
|
87 | * |
|
88 | * @param os An OutputStream |
|
89 | */ |
|
90 | public static final void close(class="keyword">final OutputStream os) |
|
91 | { |
|
92 | try |
|
93 | { |
|
94 | 0 | if (os != null) |
95 | { |
|
96 | 0 | os.close(); |
97 | } |
|
98 | } |
|
99 | 0 | catch (Exception ignored) |
100 | { |
|
101 | 0 | } |
102 | 0 | } |
103 | ||
104 | /** |
|
105 | * Converts a hashtable to a byte array for storage/serialization. |
|
106 | * |
|
107 | * @param hash The Hashtable to convert. |
|
108 | * @return A byte[] with the converted Hashtable. |
|
109 | * @throws TorqueException Any exceptions caught during processing will be |
|
110 | * rethrown wrapped into a TorqueException. |
|
111 | */ |
|
112 | public static final byte[] hashtableToByteArray(class="keyword">final Hashtable hash) |
|
113 | throws Exception |
|
114 | { |
|
115 | 0 | Hashtable saveData = new Hashtable(hash.size()); |
116 | 0 | String key = null; |
117 | 0 | Object value = null; |
118 | 0 | byte[] byteArray = null; |
119 | ||
120 | 0 | Iterator keys = hash.keySet().iterator(); |
121 | 0 | while (keys.hasNext()) |
122 | { |
|
123 | 0 | key = (String) keys.next(); |
124 | 0 | value = hash.get(key); |
125 | 0 | if (value instanceof Serializable) |
126 | { |
|
127 | 0 | saveData.put(key, value); |
128 | } |
|
129 | } |
|
130 | ||
131 | 0 | ByteArrayOutputStream baos = null; |
132 | 0 | BufferedOutputStream bos = null; |
133 | 0 | ObjectOutputStream out = null; |
134 | try |
|
135 | { |
|
136 | // These objects are closed in the finally. |
|
137 | 0 | baos = new ByteArrayOutputStream(); |
138 | 0 | bos = new BufferedOutputStream(baos); |
139 | 0 | out = new ObjectOutputStream(bos); |
140 | ||
141 | 0 | out.writeObject(saveData); |
142 | ||
143 | 0 | out.flush(); |
144 | 0 | bos.flush(); |
145 | 0 | baos.flush(); |
146 | 0 | byteArray = baos.toByteArray(); |
147 | } |
|
148 | finally |
|
149 | { |
|
150 | 0 | close(out); |
151 | 0 | close(bos); |
152 | 0 | close(baos); |
153 | 0 | } |
154 | 0 | return byteArray; |
155 | } |
|
156 | ||
157 | /** |
|
158 | * Factored out setting of a Village Record column from a Criteria Key |
|
159 | * |
|
160 | * @param crit The Criteria |
|
161 | * @param key The Criterion Key |
|
162 | * @param rec The Village Record |
|
163 | * @param colName The name of the Column in the Record |
|
164 | */ |
|
165 | public static final void setVillageValue(class="keyword">final Criteria crit, |
|
166 | final String key, |
|
167 | final Record rec, |
|
168 | final String colName) |
|
169 | throws Exception |
|
170 | { |
|
171 | // A village Record.setValue( String, Object ) would |
|
172 | // be nice here. |
|
173 | 0 | Object obj = crit.getValue(key); |
174 | 0 | if (obj instanceof SimpleKey) |
175 | { |
|
176 | 0 | obj = ((SimpleKey) obj).getValue(); |
177 | } |
|
178 | 0 | if (obj == null) |
179 | { |
|
180 | 0 | rec.setValueNull(colName); |
181 | } |
|
182 | 0 | else if (obj instanceof String) |
183 | { |
|
184 | 0 | rec.setValue(colName, (String) obj); |
185 | } |
|
186 | 0 | else if (obj instanceof Integer) |
187 | { |
|
188 | 0 | rec.setValue(colName, |
189 | crit.getInt(key)); |
|
190 | } |
|
191 | 0 | else if (obj instanceof BigDecimal) |
192 | { |
|
193 | 0 | rec.setValue(colName, (BigDecimal) obj); |
194 | } |
|
195 | 0 | else if (obj instanceof Boolean) |
196 | { |
|
197 | 0 | rec.setValue(colName, |
198 | ((Boolean) obj).booleanValue()); |
|
199 | } |
|
200 | 0 | else if (obj instanceof java.util.Date) |
201 | { |
|
202 | 0 | rec.setValue(colName, |
203 | (java.util.Date) obj); |
|
204 | } |
|
205 | 0 | else if (obj instanceof Float) |
206 | { |
|
207 | 0 | rec.setValue(colName, |
208 | crit.getFloat(key)); |
|
209 | } |
|
210 | 0 | else if (obj instanceof Double) |
211 | { |
|
212 | 0 | rec.setValue(colName, |
213 | crit.getDouble(key)); |
|
214 | } |
|
215 | 0 | else if (obj instanceof Byte) |
216 | { |
|
217 | 0 | rec.setValue(colName, |
218 | ((Byte) obj).byteValue()); |
|
219 | } |
|
220 | 0 | else if (obj instanceof Long) |
221 | { |
|
222 | 0 | rec.setValue(colName, |
223 | crit.getLong(key)); |
|
224 | } |
|
225 | 0 | else if (obj instanceof Short) |
226 | { |
|
227 | 0 | rec.setValue(colName, |
228 | ((Short) obj).shortValue()); |
|
229 | } |
|
230 | 0 | else if (obj instanceof Hashtable) |
231 | { |
|
232 | 0 | rec.setValue(colName, |
233 | hashtableToByteArray((Hashtable) obj)); |
|
234 | } |
|
235 | 0 | else if (obj instanceof byte[]) |
236 | { |
|
237 | 0 | rec.setValue(colName, (byte[]) obj); |
238 | } |
|
239 | 0 | } |
240 | } |
|
241 | ||
242 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |