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.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.apache.commons.lang.StringUtils; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class StringList2VarcharFieldConversion implements FieldConversion |
34 | |
{ |
35 | |
|
36 | |
private static final String NULLVALUE = "#NULL#"; |
37 | |
private static final String EMPTYCOLLEC = "#EMTPY#"; |
38 | |
|
39 | |
public StringList2VarcharFieldConversion() |
40 | |
{ |
41 | |
} |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public Object javaToSql(Object source) throws ConversionException |
47 | |
{ |
48 | |
|
49 | |
if (source == null) |
50 | |
{ |
51 | |
return NULLVALUE; |
52 | |
} |
53 | |
|
54 | |
try |
55 | |
{ |
56 | |
List stringList = (List) source; |
57 | |
if (stringList.isEmpty()) |
58 | |
{ |
59 | |
return NULLVALUE; |
60 | |
} |
61 | |
|
62 | |
StringBuffer result = new StringBuffer(); |
63 | |
for (int i = 0; i < stringList.size(); i++) |
64 | |
{ |
65 | |
String newSt = (String) stringList.get(i); |
66 | |
|
67 | |
|
68 | |
newSt = StringUtils.replace(newSt, "#", "##"); |
69 | |
if (i > 0) |
70 | |
{ |
71 | |
result.append("#"); |
72 | |
} |
73 | |
result.append(newSt); |
74 | |
} |
75 | |
return result.toString(); |
76 | |
} |
77 | |
catch (ClassCastException e) |
78 | |
{ |
79 | |
throw new ConversionException("Object is not a List of String it is a" |
80 | |
+ source.getClass().getName()); |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public Object sqlToJava(Object source) throws ConversionException |
88 | |
{ |
89 | |
if (source == null) |
90 | |
{ |
91 | |
return null; |
92 | |
} |
93 | |
if (source.toString().equals(NULLVALUE)) |
94 | |
{ |
95 | |
return null; |
96 | |
} |
97 | |
if (source.toString().equals(EMPTYCOLLEC)) |
98 | |
{ |
99 | |
return new ArrayList(); |
100 | |
} |
101 | |
List v = new ArrayList(); |
102 | |
StringBuffer input = new StringBuffer(); |
103 | |
StringBuffer newString = new StringBuffer(); |
104 | |
int pos = 0; |
105 | |
int length; |
106 | |
|
107 | |
input.append(source.toString()); |
108 | |
length = input.length(); |
109 | |
while (pos < length) |
110 | |
{ |
111 | |
if (input.charAt(pos) != '#') |
112 | |
{ |
113 | |
newString.append(input.charAt(pos)); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | |
if (input.charAt(pos + 1) != '#') |
118 | |
{ |
119 | |
v.add(newString.toString()); |
120 | |
newString = new StringBuffer(); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | |
newString.append('#'); |
125 | |
++pos; |
126 | |
} |
127 | |
} |
128 | |
++pos; |
129 | |
} |
130 | |
v.add(newString.toString()); |
131 | |
return v; |
132 | |
} |
133 | |
} |