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.1 2007-08-24 22:17:31 ewestfal Exp $
031 */
032 public class TimeList2VarcharFieldConversion implements FieldConversion
033 {
034
035 private static final String NULLVALUE = "#NULL#";
036 private static final String EMPTYCOLLEC = "#EMTPY#";
037
038 public TimeList2VarcharFieldConversion()
039 {
040 }
041
042 /* (non-Javadoc)
043 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
044 */
045 public Object javaToSql(Object source) throws ConversionException
046 {
047 if (source == null)
048 {
049 return NULLVALUE;
050 }
051
052 try
053 {
054 List timeList = (List) source;
055 if (timeList.isEmpty())
056 {
057 return NULLVALUE;
058 }
059
060 StringBuffer result = new StringBuffer();
061 for (int i = 0; i < timeList.size(); i++)
062 {
063 Time obj = (Time) timeList.get(i);
064 String newSt = obj.toString();
065 // introduced in JDK 1.4, replace with commons-lang
066 // newSt = newSt.replaceAll("#", "##");
067 newSt = StringUtils.replace(newSt, "#", "##");
068 result.append(newSt);
069 result.append("#");
070 }
071 return result.toString();
072 }
073 catch (ClassCastException e)
074 {
075 throw new ConversionException("Object is not a List of Time it is a" + source.getClass().getName());
076 }
077 }
078
079 /* (non-Javadoc)
080 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
081 */
082 public Object sqlToJava(Object source) throws ConversionException
083 {
084 if (source == null)
085 {
086 return null;
087 }
088 if (source.toString().equals(NULLVALUE))
089 {
090 return null;
091 }
092 if (source.toString().equals(EMPTYCOLLEC))
093 {
094 return new ArrayList();
095 }
096
097 List v = new ArrayList();
098 String input = source.toString();
099 int pos = input.indexOf("#");
100
101 while (pos >= 0)
102 {
103 if (pos == 0)
104 {
105 v.add("");
106 }
107 else
108 {
109 v.add(Time.valueOf(input.substring(0, pos)));
110 }
111
112 if (pos + 1 > input.length())
113 {
114 //# at end causes outof bounds
115 break;
116 }
117
118 input = input.substring(pos + 1, input.length());
119 pos = input.indexOf("#");
120 }
121 return v;
122 }
123 }