001    package org.apache.ojb.broker.accesslayer.conversions;
002    
003    /* Copyright 2002-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.Vector;
019    
020    /**
021     * Converts a Vector of string elements back and forth from a database varchar field
022     * Strings may not contain "#" as this is used as separator.
023     * This class maybe useful if it's important to have the string vector stored in a 
024     * human readable form that allows editing.
025     * @see Object2ByteArrFieldConversion uses Java serialization and is not suited for
026     * this purpose. 
027     * 
028     * @author  sschloesser  mailto: stefan.schl@gmx.de 
029     * @version $Id: StringVector2VarcharFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
030     */
031    public class StringVector2VarcharFieldConversion implements FieldConversion
032    {
033    
034        private static final String NULLVALUE = "#NULL#";
035        private static final String EMPTYCOLLEC = "#EMTPY#";
036        private static final String SEPARATOR = "#";
037    
038        /** Creates a new instance of StringVector2VarcharFieldConversion */
039        public StringVector2VarcharFieldConversion()
040        {
041        }
042    
043        public Object javaToSql(Object obj)
044            throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
045        {
046    
047            if (obj == null)
048            {
049                return NULLVALUE;
050            }
051    
052            if (!(obj instanceof Vector))
053            {
054                throw new ConversionException(
055                    "Object is not a vector it is a" + obj.getClass().getName());
056            }
057    
058            Vector v = (Vector) obj;
059            if (v.size() == 0)
060            {
061                return EMPTYCOLLEC;
062            }
063    
064            StringBuffer result = new StringBuffer();
065            for (int i = 0; i < v.size(); i++)
066            {
067                String newSt = v.get(i).toString();
068                if (newSt.indexOf(SEPARATOR) >= 0)
069                {
070                    throw new ConversionException(
071                        "An entry in the Vector contains the forbidden "
072                            + SEPARATOR
073                            + " character used to separate the strings on the DB");
074                }
075                result.append(newSt);
076                result.append(SEPARATOR);
077            }
078            return result.toString();
079        }
080    
081        public Object sqlToJava(Object obj)
082            throws org.apache.ojb.broker.accesslayer.conversions.ConversionException
083        {
084    
085            if (obj == null)
086            {
087                return null;
088            }
089            if (obj.toString().equals(NULLVALUE))
090            {
091                return null;
092            }
093            if (obj.toString().equals(EMPTYCOLLEC))
094            {
095                return new Vector();
096            }
097    
098            Vector v = new Vector();
099            String input = obj.toString();
100            int pos = input.indexOf(SEPARATOR);
101    
102            while (pos >= 0)
103            {
104                if (pos == 0)
105                {
106                    v.add("");
107                }
108                else
109                {
110                    v.add(input.substring(0, pos));
111                }
112    
113                if (pos + 1 > input.length()) //# at end causes outof bounds
114                {
115                    break;
116                }
117    
118                input = input.substring(pos + 1, input.length());
119                pos = input.indexOf(SEPARATOR);
120            }
121            return v;
122        }
123    }