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