Coverage Report - org.apache.ojb.broker.accesslayer.conversions.IntList2VarcharFieldConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
IntList2VarcharFieldConversion
N/A
N/A
7.333
 
 1  
 package org.apache.ojb.broker.accesslayer.conversions;
 2  
 
 3  
 /* Copyright 2004-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 
 23  
 /**
 24  
  * This implementation of the {@link FieldConversion} interface converts
 25  
  * between a {@link java.util.List} of {@link java.lang.Integer} objects and a database
 26  
  * <em>varchar</em> field.
 27  
  *
 28  
  * @author Guillaume Nodet
 29  
  * @version $Id: IntList2VarcharFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
 30  
  */
 31  
 public class IntList2VarcharFieldConversion implements FieldConversion
 32  
 {
 33  
 
 34  
     private static final String NULLVALUE = "#NULL#";
 35  
     private static final String EMPTYCOLLEC = "#EMTPY#";
 36  
 
 37  
     public IntList2VarcharFieldConversion()
 38  
     {
 39  
     }
 40  
 
 41  
     /* (non-Javadoc)
 42  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
 43  
      */
 44  
     public Object javaToSql(Object source) throws ConversionException
 45  
     {
 46  
         if (source == null)
 47  
         {
 48  
             return NULLVALUE;
 49  
         }
 50  
 
 51  
         try
 52  
         {
 53  
             List intList = (List) source;
 54  
             if (intList.isEmpty())
 55  
             {
 56  
                 return EMPTYCOLLEC;
 57  
             }
 58  
 
 59  
             StringBuffer result = new StringBuffer();
 60  
             for (int i = 0; i < intList.size(); i++)
 61  
             {
 62  
                 Integer obj = (Integer) intList.get(i);
 63  
                 String newSt = obj.toString();
 64  
                 newSt = StringUtils.replace(newSt, "#", "##");
 65  
                 result.append(newSt);
 66  
                 result.append("#");
 67  
             }
 68  
             return result.toString();
 69  
         }
 70  
         catch (ClassCastException e)
 71  
         {
 72  
             throw new ConversionException("Object is not a List of Integer it is a"
 73  
                     + source.getClass().getName());
 74  
         }
 75  
     }
 76  
 
 77  
     /* (non-Javadoc)
 78  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
 79  
      */
 80  
     public Object sqlToJava(Object source) throws ConversionException
 81  
     {
 82  
 
 83  
         if (source == null)
 84  
         {
 85  
             return null;
 86  
         }
 87  
         if (!(source instanceof String))
 88  
         {
 89  
             throw new ConversionException("Object is not a String it is a"
 90  
                     + source.getClass().getName());
 91  
         }
 92  
         if (source.toString().equals(NULLVALUE))
 93  
         {
 94  
             return null;
 95  
         }
 96  
         if (source.toString().equals(EMPTYCOLLEC))
 97  
         {
 98  
             return new ArrayList();
 99  
         }
 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  
 }