001: package org.apache.ojb.broker.accesslayer.conversions;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.sql.Time;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.commons.lang.StringUtils;
023:
024: /**
025: * This implementation of the {@link FieldConversion} interface converts
026: * between a {@link java.util.List} of {@link java.sql.Time} objects and a database
027: * <em>varchar</em> field.
028: *
029: * @author Guillaume Nodet
030: * @version $Id: TimeList2VarcharFieldConversion.java,v 1.2.2.1 2005/12/21 22:23:38 tomdz Exp $
031: */
032: public class TimeList2VarcharFieldConversion implements FieldConversion {
033:
034: private static final String NULLVALUE = "#NULL#";
035: private static final String EMPTYCOLLEC = "#EMTPY#";
036:
037: public TimeList2VarcharFieldConversion() {
038: }
039:
040: /* (non-Javadoc)
041: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
042: */
043: public Object javaToSql(Object source) throws ConversionException {
044: if (source == null) {
045: return NULLVALUE;
046: }
047:
048: try {
049: List timeList = (List) source;
050: if (timeList.isEmpty()) {
051: return NULLVALUE;
052: }
053:
054: StringBuffer result = new StringBuffer();
055: for (int i = 0; i < timeList.size(); i++) {
056: Time obj = (Time) timeList.get(i);
057: String newSt = obj.toString();
058: // introduced in JDK 1.4, replace with commons-lang
059: // newSt = newSt.replaceAll("#", "##");
060: newSt = StringUtils.replace(newSt, "#", "##");
061: result.append(newSt);
062: result.append("#");
063: }
064: return result.toString();
065: } catch (ClassCastException e) {
066: throw new ConversionException(
067: "Object is not a List of Time it is a"
068: + source.getClass().getName());
069: }
070: }
071:
072: /* (non-Javadoc)
073: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
074: */
075: public Object sqlToJava(Object source) throws ConversionException {
076: if (source == null) {
077: return null;
078: }
079: if (source.toString().equals(NULLVALUE)) {
080: return null;
081: }
082: if (source.toString().equals(EMPTYCOLLEC)) {
083: return new ArrayList();
084: }
085:
086: List v = new ArrayList();
087: String input = source.toString();
088: int pos = input.indexOf("#");
089:
090: while (pos >= 0) {
091: if (pos == 0) {
092: v.add("");
093: } else {
094: v.add(Time.valueOf(input.substring(0, pos)));
095: }
096:
097: if (pos + 1 > input.length()) {
098: //# at end causes outof bounds
099: break;
100: }
101:
102: input = input.substring(pos + 1, input.length());
103: pos = input.indexOf("#");
104: }
105: return v;
106: }
107: }
|