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