001: package org.apache.torque.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.BufferedOutputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.OutputStream;
026: import java.io.Serializable;
027: import java.math.BigDecimal;
028: import java.util.Hashtable;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.torque.om.SimpleKey;
035:
036: import com.workingdogs.village.QueryDataSet;
037: import com.workingdogs.village.Record;
038: import com.workingdogs.village.TableDataSet;
039:
040: /**
041: * Some Village related code factored out of the BasePeer.
042: *
043: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
044: * @version $Id: VillageUtils.java 476550 2006-11-18 16:08:37Z tfischer $
045: */
046: public final class VillageUtils {
047: /** The log. */
048: private static Log log = LogFactory.getLog(VillageUtils.class);
049:
050: /**
051: * Private constructor to prevent instantiation.
052: *
053: * Class contains only static method ans should therefore not be
054: * instantiated.
055: */
056: private VillageUtils() {
057: }
058:
059: /**
060: * Convenience Method to close a Table Data Set without
061: * Exception check.
062: *
063: * @param tds A TableDataSet
064: */
065: public static final void close(final TableDataSet tds) {
066: if (tds != null) {
067: try {
068: tds.close();
069: } catch (Exception ignored) {
070: log.debug(
071: "Caught exception when closing a TableDataSet",
072: ignored);
073: }
074: }
075: }
076:
077: /**
078: * Convenience Method to close a Table Data Set without
079: * Exception check.
080: *
081: * @param qds A TableDataSet
082: */
083: public static final void close(final QueryDataSet qds) {
084: if (qds != null) {
085: try {
086: qds.close();
087: } catch (Exception ignored) {
088: log.debug(
089: "Caught exception when closing a QueryDataSet",
090: ignored);
091: }
092: }
093: }
094:
095: /**
096: * Convenience Method to close an Output Stream without
097: * Exception check.
098: *
099: * @param os An OutputStream
100: */
101: public static final void close(final OutputStream os) {
102: try {
103: if (os != null) {
104: os.close();
105: }
106: } catch (Exception ignored) {
107: log.debug("Caught exception when closing an OutputStream",
108: ignored);
109: }
110: }
111:
112: /**
113: * Converts a hashtable to a byte array for storage/serialization.
114: *
115: * @param hash The Hashtable to convert.
116: * @return A byte[] with the converted Hashtable.
117: * @throws Exception If an error occurs.
118: */
119: public static final byte[] hashtableToByteArray(final Hashtable hash)
120: throws Exception {
121: Hashtable saveData = new Hashtable(hash.size());
122: byte[] byteArray = null;
123:
124: Iterator keys = hash.entrySet().iterator();
125: while (keys.hasNext()) {
126: Map.Entry entry = (Map.Entry) keys.next();
127: if (entry.getValue() instanceof Serializable) {
128: saveData.put(entry.getKey(), entry.getValue());
129: }
130: }
131:
132: ByteArrayOutputStream baos = null;
133: BufferedOutputStream bos = null;
134: ObjectOutputStream out = null;
135: try {
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: } finally {
148: close(out);
149: close(bos);
150: close(baos);
151: }
152: return byteArray;
153: }
154:
155: /**
156: * Factored out setting of a Village Record column from a Criteria Key
157: *
158: * @param crit The Criteria
159: * @param key The Criterion Key
160: * @param rec The Village Record
161: * @param colName The name of the Column in the Record
162: */
163: public static final void setVillageValue(final Criteria crit,
164: final String key, final Record rec, final String colName)
165: throws Exception {
166: // A village Record.setValue( String, Object ) would
167: // be nice here.
168: Object obj = crit.getValue(key);
169: if (obj instanceof SimpleKey) {
170: obj = ((SimpleKey) obj).getValue();
171: }
172: if (obj == null) {
173: rec.setValueNull(colName);
174: } else if (obj instanceof String) {
175: rec.setValue(colName, (String) obj);
176: } else if (obj instanceof Integer) {
177: rec.setValue(colName, crit.getInt(key));
178: } else if (obj instanceof BigDecimal) {
179: rec.setValue(colName, (BigDecimal) obj);
180: } else if (obj instanceof Boolean) {
181: rec.setValue(colName, ((Boolean) obj).booleanValue());
182: } else if (obj instanceof java.util.Date) {
183: rec.setValue(colName, (java.util.Date) obj);
184: } else if (obj instanceof Float) {
185: rec.setValue(colName, crit.getFloat(key));
186: } else if (obj instanceof Double) {
187: rec.setValue(colName, crit.getDouble(key));
188: } else if (obj instanceof Byte) {
189: rec.setValue(colName, ((Byte) obj).byteValue());
190: } else if (obj instanceof Long) {
191: rec.setValue(colName, crit.getLong(key));
192: } else if (obj instanceof Short) {
193: rec.setValue(colName, ((Short) obj).shortValue());
194: } else if (obj instanceof Hashtable) {
195: rec
196: .setValue(colName,
197: hashtableToByteArray((Hashtable) obj));
198: } else if (obj instanceof byte[]) {
199: rec.setValue(colName, (byte[]) obj);
200: }
201: }
202: }
|