KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > accesslayer > conversions > TimeList2VarcharFieldConversion


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 JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
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.2.2.1 2005/12/21 22:23:38 tomdz Exp $
31  */

32 public class TimeList2VarcharFieldConversion implements FieldConversion
33 {
34
35     private static final String JavaDoc NULLVALUE = "#NULL#";
36     private static final String JavaDoc 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 JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
46     {
47         if (source == null)
48         {
49             return NULLVALUE;
50         }
51
52         try
53         {
54             List JavaDoc timeList = (List JavaDoc) source;
55             if (timeList.isEmpty())
56             {
57                 return NULLVALUE;
58             }
59
60             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
61             for (int i = 0; i < timeList.size(); i++)
62             {
63                 Time JavaDoc obj = (Time JavaDoc) timeList.get(i);
64                 String JavaDoc 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 JavaDoc 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 JavaDoc sqlToJava(Object JavaDoc 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 JavaDoc();
95         }
96
97         List JavaDoc v = new ArrayList JavaDoc();
98         String JavaDoc 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 }
124
Popular Tags