View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.sql;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Date;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import COM.FutureTense.Interfaces.IList;
26  import COM.FutureTense.Interfaces.Utilities;
27  import COM.FutureTense.Util.IterableIListWrapper;
28  
29  import com.fatwire.assetapi.data.AssetId;
30  import com.fatwire.cs.core.db.Util;
31  import tools.gsf.facade.assetapi.AssetIdUtils;
32  
33  import org.apache.commons.lang3.StringUtils;
34  
35  /**
36   * Miscellaneous IList Utilities
37   *
38   * @author Tony Field
39   * @since 21-Nov-2008
40   */
41  public final class IListUtils {
42      private IListUtils() {
43      }
44  
45      /**
46       * Return a string value for a column. Wraps the possible checked exception
47       * NoSuchFieldException with an IllegalArgumentException
48       *
49       * @param list IList to interrogate
50       * @param colname name of column to return
51       * @return string value
52       * @throws IllegalArgumentException if the column name is not found
53       */
54      public static String getStringValue(IList list, String colname) {
55          try {
56              return list.getValue(colname);
57          } catch (NoSuchFieldException e) {
58              throw new IllegalArgumentException("No such field: " + colname, e);
59          }
60      }
61  
62      /**
63       * Return a long value for a column. Wraps the possible checked exception
64       * NoSuchFieldException with an IllegalArgumentException
65       *
66       * @param list IList to interrogate
67       * @param colname name of column to return
68       * @return long value
69       * @throws IllegalArgumentException if the column name is not found
70       * @throws NumberFormatException if the column being queried does not
71       *             contain a long.
72       */
73      public static long getLongValue(IList list, String colname) {
74          try {
75              String s = list.getValue(colname);
76              return Long.valueOf(s);
77          } catch (NoSuchFieldException e) {
78              throw new IllegalArgumentException("No such field: " + colname, e);
79          }
80      }
81  
82      /**
83       * Return a Date value for a column. The date must be in standard JDBC date
84       * format. Wraps possible checked exception NoSuchFieldException with an
85       * IllegalArgumentException
86       *
87       * @param list IList to interrogate
88       * @param colname name of column to return
89       * @return date value
90       */
91      public static Date getDateValue(IList list, String colname) {
92          String s = getStringValue(list, colname);
93          return !Utilities.goodString(s) ? null : Util.parseJdbcDate(s);
94      }
95  
96      private static final ThreadLocal<Long> counter = new ThreadLocal<Long>() {
97  
98          /*
99           * (non-Javadoc)
100          *
101          * @see java.lang.ThreadLocal#initialValue()
102          */
103         @Override
104         protected Long initialValue() {
105             return System.currentTimeMillis();
106         }
107 
108         /*
109          * (non-Javadoc)
110          *
111          * @see java.lang.ThreadLocal#get()
112          */
113         @Override
114         public Long get() {
115             long c = super.get();
116             c++;
117             set(c);
118             return c;
119 
120         }
121     };
122 
123     /**
124      * Generates a random String to assign to a IList name. It is optimized for
125      * performance.
126      *
127      * @return String that can be used for an IList name.
128      */
129     public static String generateRandomListName() {
130         return generateRandomListName("rnd-");
131     }
132 
133     /**
134      * Generates a random String to assign to a IList name. It is optimized for
135      * performance.
136      *
137      * @param prefix the prefix to assign to the list name, cannot be empty or
138      *            null.
139      * @return String for list name
140      */
141     public static String generateRandomListName(String prefix) {
142         if (StringUtils.isBlank(prefix))
143             throw new IllegalArgumentException("prefix must not be blank.");
144         return prefix + counter.get();
145     }
146 
147     /**
148      * Reads an IList with collumns <tt>assettype</tt> and <tt>assetid</tt> and
149      * creates a Collection of AssetId objects.
150      *
151      * @param result list of asset type and asset id pairs
152      * @return Collection of AssetIds, never null.
153      */
154     public static Collection<AssetId> toAssetIdCollection(IList result) {
155         if (result == null || !result.hasData())
156             return Collections.emptyList();
157         final List<AssetId> list = new LinkedList<AssetId>();
158         for (IList row : new IterableIListWrapper(result)) {
159             AssetId id;
160             try {
161                 id = AssetIdUtils.createAssetId(row.getValue("assettype"), row.getValue("assetid"));
162                 list.add(id);
163             } catch (NoSuchFieldException e) {
164                 throw new RuntimeException(e.getMessage());
165             }
166 
167         }
168         return list;
169     }
170 
171 }