01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.jetspeed.util.ojb;
18:
19: import java.util.ArrayList;
20: import java.util.Iterator;
21: import java.util.List;
22: import java.util.StringTokenizer;
23:
24: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
25: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
26:
27: /**
28: * ACLFieldConversion
29: *
30: * OJB field conversion: Helps transparently map ACL List members
31: * to/from database table column that that contains an ordered
32: * CSV list of strings.
33: */
34: public class ACLFieldConversion implements FieldConversion {
35: private static final String DELIM = ",";
36:
37: /**
38: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
39: */
40: public Object javaToSql(Object arg0) throws ConversionException {
41: if (arg0 instanceof List) {
42: List csvList = (List) arg0;
43: if (csvList.size() > 1) {
44: StringBuffer buffer = null;
45: Iterator values = csvList.iterator();
46: while (values.hasNext()) {
47: String value = (String) values.next();
48: if (value.length() > 0) {
49: if (buffer == null) {
50: buffer = new StringBuffer(255);
51: } else {
52: buffer.append(DELIM);
53: }
54: buffer.append(value);
55: }
56: }
57: if (buffer != null) {
58: return buffer.toString();
59: }
60: } else if (!csvList.isEmpty()) {
61: String value = (String) csvList.get(0);
62: if (value.length() > 0) {
63: return value;
64: }
65: }
66: return "";
67: }
68: return arg0;
69: }
70:
71: /**
72: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
73: */
74: public Object sqlToJava(Object arg0) throws ConversionException {
75: if (arg0 instanceof String) {
76: List aclList = new ArrayList(4);
77: StringTokenizer tokens = new StringTokenizer((String) arg0,
78: DELIM);
79: while (tokens.hasMoreTokens()) {
80: String value = tokens.nextToken().trim();
81: if (value.length() > 0) {
82: aclList.add(value);
83: }
84: }
85: return aclList;
86: }
87: return arg0;
88: }
89: }
|