1 | |
package org.apache.ojb.broker.accesslayer.conversions; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
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 | |
|
43 | |
|
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 | |
|
66 | |
|
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 | |
|
80 | |
|
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 | |
|
115 | |
break; |
116 | |
} |
117 | |
|
118 | |
input = input.substring(pos + 1, input.length()); |
119 | |
pos = input.indexOf("#"); |
120 | |
} |
121 | |
return v; |
122 | |
} |
123 | |
} |