View Javadoc

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  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(final TableDataSet tds)
51      {
52          if (tds != null)
53          {
54              try
55              {
56                  tds.close();
57              }
58              catch (Exception ignored)
59              {
60              }
61          }
62      }
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(final QueryDataSet qds)
71      {
72          if (qds != null)
73          {
74              try
75              {
76                  qds.close();
77              }
78              catch (Exception ignored)
79              {
80              }
81          }
82      }
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(final OutputStream os)
91      {
92          try
93          {
94              if (os != null)
95              {
96                  os.close();
97              }
98          }
99          catch (Exception ignored)
100         {
101         }
102     }
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(final Hashtable hash)
113         throws Exception
114     {
115         Hashtable saveData = new Hashtable(hash.size());
116         String key = null;
117         Object value = null;
118         byte[] byteArray = null;
119 
120         Iterator keys = hash.keySet().iterator();
121         while (keys.hasNext())
122         {
123             key = (String) keys.next();
124             value = hash.get(key);
125             if (value instanceof Serializable)
126             {
127                 saveData.put(key, value);
128             }
129         }
130 
131         ByteArrayOutputStream baos = null;
132         BufferedOutputStream bos = null;
133         ObjectOutputStream out = null;
134         try
135         {
136             // These objects are closed in the finally.
137             baos = new ByteArrayOutputStream();
138             bos = new BufferedOutputStream(baos);
139             out = new ObjectOutputStream(bos);
140 
141             out.writeObject(saveData);
142 
143             out.flush();
144             bos.flush();
145             baos.flush();
146             byteArray = baos.toByteArray();
147         }
148         finally
149         {
150             close(out);
151             close(bos);
152             close(baos);
153         }
154         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(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         Object obj = crit.getValue(key);
174         if (obj instanceof SimpleKey)
175         {
176             obj = ((SimpleKey) obj).getValue();
177         }
178         if (obj == null)
179         {
180             rec.setValueNull(colName);
181         }
182         else if (obj instanceof String)
183         {
184             rec.setValue(colName, (String) obj);
185         }
186         else if (obj instanceof Integer)
187         {
188             rec.setValue(colName,
189                     crit.getInt(key));
190         }
191         else if (obj instanceof BigDecimal)
192         {
193             rec.setValue(colName, (BigDecimal) obj);
194         }
195         else if (obj instanceof Boolean)
196         {
197             rec.setValue(colName,
198                     ((Boolean) obj).booleanValue());
199         }
200         else if (obj instanceof java.util.Date)
201         {
202             rec.setValue(colName,
203                     (java.util.Date) obj);
204         }
205         else if (obj instanceof Float)
206         {
207             rec.setValue(colName,
208                     crit.getFloat(key));
209         }
210         else if (obj instanceof Double)
211         {
212             rec.setValue(colName,
213                     crit.getDouble(key));
214         }
215         else if (obj instanceof Byte)
216         {
217             rec.setValue(colName,
218                     ((Byte) obj).byteValue());
219         }
220         else if (obj instanceof Long)
221         {
222             rec.setValue(colName,
223                     crit.getLong(key));
224         }
225         else if (obj instanceof Short)
226         {
227             rec.setValue(colName,
228                     ((Short) obj).shortValue());
229         }
230         else if (obj instanceof Hashtable)
231         {
232             rec.setValue(colName,
233                     hashtableToByteArray((Hashtable) obj));
234         }
235         else if (obj instanceof byte[])
236         {
237             rec.setValue(colName, (byte[]) obj);
238         }
239     }
240 }
241 
242