View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  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  {
36      private static final String DELIM = ",";
37  
38      /***
39       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
40       */
41      public Object javaToSql(Object arg0) throws ConversionException
42      {
43          if (arg0 instanceof List)
44          {
45              List csvList = (List) arg0;
46              if (csvList.size() > 1)
47              {
48                  StringBuffer buffer = null;
49                  Iterator values = csvList.iterator();
50                  while (values.hasNext())
51                  {
52                      String value = (String)values.next();
53                      if (value.length() > 0)
54                      {
55                          if (buffer == null)
56                          {
57                              buffer = new StringBuffer(255);
58                          }
59                          else
60                          {
61                              buffer.append(DELIM);
62                          }
63                          buffer.append(value);
64                      }
65                  }
66                  if (buffer != null)
67                  {
68                      return buffer.toString();
69                  }
70              }
71              else if (!csvList.isEmpty())
72              {
73                  String value = (String)csvList.get(0);
74                  if (value.length() > 0)
75                  {
76                      return value;
77                  }
78              }
79              return "";
80          }
81          return arg0;
82      }
83  
84      /***
85       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
86       */
87      public Object sqlToJava(Object arg0) throws ConversionException
88      {
89          if (arg0 instanceof String)
90          {
91              List aclList = new ArrayList(4);
92              StringTokenizer tokens = new StringTokenizer((String) arg0, DELIM);
93              while (tokens.hasMoreTokens())
94              {
95                  String value = tokens.nextToken().trim();
96                  if (value.length() > 0)
97                  {
98                      aclList.add(value);
99                  }
100             }
101             return aclList;
102         }
103         return arg0;
104     }
105 }