KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > util > VillageUtils


1 package org.apache.torque.util;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */

21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc os)
112     {
113         try
114         {
115             if (os != null)
116             {
117                 os.close();
118             }
119         }
120         catch (Exception JavaDoc 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 JavaDoc hash)
135         throws Exception JavaDoc
136     {
137         Hashtable JavaDoc saveData = new Hashtable JavaDoc(hash.size());
138         byte[] byteArray = null;
139
140         Iterator JavaDoc keys = hash.entrySet().iterator();
141         while (keys.hasNext())
142         {
143             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) keys.next();
144             if (entry.getValue() instanceof Serializable JavaDoc)
145             {
146                 saveData.put(entry.getKey(), entry.getValue());
147             }
148         }
149
150         ByteArrayOutputStream JavaDoc baos = null;
151         BufferedOutputStream JavaDoc bos = null;
152         ObjectOutputStream JavaDoc out = null;
153         try
154         {
155             // These objects are closed in the finally.
156
baos = new ByteArrayOutputStream JavaDoc();
157             bos = new BufferedOutputStream JavaDoc(baos);
158             out = new ObjectOutputStream JavaDoc(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 JavaDoc key,
186             final Record rec,
187             final String JavaDoc colName)
188             throws Exception JavaDoc
189     {
190         // A village Record.setValue( String, Object ) would
191
// be nice here.
192
Object JavaDoc 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 JavaDoc)
202         {
203             rec.setValue(colName, (String JavaDoc) obj);
204         }
205         else if (obj instanceof Integer JavaDoc)
206         {
207             rec.setValue(colName,
208                     crit.getInt(key));
209         }
210         else if (obj instanceof BigDecimal JavaDoc)
211         {
212             rec.setValue(colName, (BigDecimal JavaDoc) obj);
213         }
214         else if (obj instanceof Boolean JavaDoc)
215         {
216             rec.setValue(colName,
217                     ((Boolean JavaDoc) obj).booleanValue());
218         }
219         else if (obj instanceof java.util.Date JavaDoc)
220         {
221             rec.setValue(colName,
222                     (java.util.Date JavaDoc) obj);
223         }
224         else if (obj instanceof Float JavaDoc)
225         {
226             rec.setValue(colName,
227                     crit.getFloat(key));
228         }
229         else if (obj instanceof Double JavaDoc)
230         {
231             rec.setValue(colName,
232                     crit.getDouble(key));
233         }
234         else if (obj instanceof Byte JavaDoc)
235         {
236             rec.setValue(colName,
237                     ((Byte JavaDoc) obj).byteValue());
238         }
239         else if (obj instanceof Long JavaDoc)
240         {
241             rec.setValue(colName,
242                     crit.getLong(key));
243         }
244         else if (obj instanceof Short JavaDoc)
245         {
246             rec.setValue(colName,
247                     ((Short JavaDoc) obj).shortValue());
248         }
249         else if (obj instanceof Hashtable JavaDoc)
250         {
251             rec.setValue(colName,
252                     hashtableToByteArray((Hashtable JavaDoc) obj));
253         }
254         else if (obj instanceof byte[])
255         {
256             rec.setValue(colName, (byte[]) obj);
257         }
258     }
259 }
260
Popular Tags