Coverage Report - org.apache.ojb.broker.accesslayer.conversions.TimeList2VarcharFieldConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
TimeList2VarcharFieldConversion
N/A
N/A
6.667
 
 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.sql.Time;
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 
 24  
 /**
 25  
  * This implementation of the {@link FieldConversion} interface converts
 26  
  * between a {@link java.util.List} of {@link java.sql.Time} objects and a database
 27  
  * <em>varchar</em> field.
 28  
  *
 29  
  * @author Guillaume Nodet
 30  
  * @version $Id: TimeList2VarcharFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
 31  
  */
 32  
 public class TimeList2VarcharFieldConversion implements FieldConversion
 33  
 {
 34  
 
 35  
     private static final String NULLVALUE = "#NULL#";
 36  
     private static final String EMPTYCOLLEC = "#EMTPY#";
 37  
 
 38  
     public TimeList2VarcharFieldConversion()
 39  
     {
 40  
     }
 41  
 
 42  
     /* (non-Javadoc)
 43  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
 44  
      */
 45  
     public Object javaToSql(Object source) throws ConversionException
 46  
     {
 47  
         if (source == null)
 48  
         {
 49  
             return NULLVALUE;
 50  
         }
 51  
 
 52  
         try
 53  
         {
 54  
             List timeList = (List) source;
 55  
             if (timeList.isEmpty())
 56  
             {
 57  
                 return NULLVALUE;
 58  
             }
 59  
 
 60  
             StringBuffer result = new StringBuffer();
 61  
             for (int i = 0; i < timeList.size(); i++)
 62  
             {
 63  
                 Time obj = (Time) timeList.get(i);
 64  
                 String newSt = obj.toString();
 65  
                 // introduced in JDK 1.4, replace with commons-lang
 66  
                 // newSt = newSt.replaceAll("#", "##");
 67  
                 newSt = StringUtils.replace(newSt, "#", "##");
 68  
                 result.append(newSt);
 69  
                 result.append("#");
 70  
             }
 71  
             return result.toString();
 72  
         }
 73  
         catch (ClassCastException e)
 74  
         {
 75  
             throw new ConversionException("Object is not a List of Time it is a" + source.getClass().getName());
 76  
         }
 77  
     }
 78  
 
 79  
     /* (non-Javadoc)
 80  
      * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
 81  
      */
 82  
     public Object sqlToJava(Object source) throws ConversionException
 83  
     {
 84  
         if (source == null)
 85  
         {
 86  
             return null;
 87  
         }
 88  
         if (source.toString().equals(NULLVALUE))
 89  
         {
 90  
             return null;
 91  
         }
 92  
         if (source.toString().equals(EMPTYCOLLEC))
 93  
         {
 94  
             return new ArrayList();
 95  
         }
 96  
 
 97  
         List v = new ArrayList();
 98  
         String input = source.toString();
 99  
         int pos = input.indexOf("#");
 100  
 
 101  
         while (pos >= 0)
 102  
         {
 103  
             if (pos == 0)
 104  
             {
 105  
                 v.add("");
 106  
             }
 107  
             else
 108  
             {
 109  
                 v.add(Time.valueOf(input.substring(0, pos)));
 110  
             }
 111  
 
 112  
             if (pos + 1 > input.length())
 113  
             {
 114  
                 //# at end causes outof bounds
 115  
                 break;
 116  
             }
 117  
 
 118  
             input = input.substring(pos + 1, input.length());
 119  
             pos = input.indexOf("#");
 120  
         }
 121  
         return v;
 122  
     }
 123  
 }