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.util.ArrayList;
019 import java.util.List;
020
021 import org.apache.commons.lang.StringUtils;
022
023 /**
024 * This implementation of the {@link FieldConversion} interface converts
025 * between a {@link java.util.List} of {@link java.lang.String} objects and a database
026 * <em>varchar</em> field.
027 * <br/>
028 * Strings may not contain "#" as this is used as separator.
029 *
030 * @author Guillaume Nodet
031 * @version $Id: StringList2VarcharFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
032 */
033 public class StringList2VarcharFieldConversion implements FieldConversion
034 {
035
036 private static final String NULLVALUE = "#NULL#";
037 private static final String EMPTYCOLLEC = "#EMTPY#";
038
039 public StringList2VarcharFieldConversion()
040 {
041 }
042
043 /* (non-Javadoc)
044 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
045 */
046 public Object javaToSql(Object source) throws ConversionException
047 {
048
049 if (source == null)
050 {
051 return NULLVALUE;
052 }
053
054 try
055 {
056 List stringList = (List) source;
057 if (stringList.isEmpty())
058 {
059 return NULLVALUE;
060 }
061
062 StringBuffer result = new StringBuffer();
063 for (int i = 0; i < stringList.size(); i++)
064 {
065 String newSt = (String) stringList.get(i);
066 // introduced in JDK 1.4, replace with commons-lang
067 // newSt = newSt.replaceAll("#", "##");
068 newSt = StringUtils.replace(newSt, "#", "##");
069 if (i > 0)
070 {
071 result.append("#");
072 }
073 result.append(newSt);
074 }
075 return result.toString();
076 }
077 catch (ClassCastException e)
078 {
079 throw new ConversionException("Object is not a List of String it is a"
080 + source.getClass().getName());
081 }
082 }
083
084 /* (non-Javadoc)
085 * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
086 */
087 public Object sqlToJava(Object source) throws ConversionException
088 {
089 if (source == null)
090 {
091 return null;
092 }
093 if (source.toString().equals(NULLVALUE))
094 {
095 return null;
096 }
097 if (source.toString().equals(EMPTYCOLLEC))
098 {
099 return new ArrayList();
100 }
101 List v = new ArrayList();
102 StringBuffer input = new StringBuffer();
103 StringBuffer newString = new StringBuffer();
104 int pos = 0;
105 int length;
106
107 input.append(source.toString());
108 length = input.length();
109 while (pos < length)
110 {
111 if (input.charAt(pos) != '#')
112 {
113 newString.append(input.charAt(pos));
114 }
115 else
116 {
117 if (input.charAt(pos + 1) != '#')
118 {
119 v.add(newString.toString());
120 newString = new StringBuffer();
121 }
122 else
123 {
124 newString.append('#');
125 ++pos;
126 }
127 }
128 ++pos;
129 }
130 v.add(newString.toString());
131 return v;
132 }
133 }