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 com.fatwire.gst.foundation.facade.assetapi;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import COM.FutureTense.Interfaces.IList;
25  
26  import com.fatwire.assetapi.data.AssetId;
27  import com.fatwire.gst.foundation.facade.sql.AbstractIList;
28  import com.fatwire.gst.foundation.facade.sql.IListIterable;
29  
30  /**
31   * IList implementation that starts with a List<AssetId> and exposes the rows as
32   * ASSETTYPE,ASSETID.  The getter is case-insensitive, so assettype,assetid works too.
33   * 
34   * @author Tony Field
35   * @see IListIterable
36   * @since Aug 13, 2010
37   */
38  public class AssetIdIList extends AbstractIList {
39  
40      public static final String ASSETTYPE = "ASSETTYPE";
41      public static final String ASSETID = "ASSETID";
42      /**
43       * Collection of AssetId objects
44       */
45      protected List<AssetId> ids;
46  
47      /**
48       * Construct a new IList taking the name and the data.
49       * 
50       * @param name IList name
51       * @param ids asset ids.
52       */
53      public AssetIdIList(String name, Collection<AssetId> ids) {
54          super(name);
55          this.ids = Collections.unmodifiableList(new ArrayList<AssetId>(ids));
56      }
57  
58      public IList clone(String newname) {
59          List<AssetId> clone = new ArrayList<AssetId>(ids.size());
60          for (AssetId id : ids) {
61              clone.add(id);
62          }
63          return new AssetIdIList(newname, clone);
64      }
65  
66      public void flush() {
67          ids = new ArrayList<AssetId>();
68      }
69  
70      public int numColumns() {
71          return 2;
72      }
73  
74      public String getColumnName(int i) throws ArrayIndexOutOfBoundsException {
75          if (i == 0)
76              return ASSETTYPE;
77          if (i == 1)
78              return ASSETID;
79          throw new ArrayIndexOutOfBoundsException(i);
80      }
81  
82      public int numRows() {
83          return ids.size();
84      }
85  
86      public String getValue(String s) throws NoSuchFieldException {
87          // ID is column 2 but will likely be called more often that type so
88          // check it first.
89          if (ASSETID.equalsIgnoreCase(s)) {
90              return Long.toString(ids.get(currentRow() - 1).getId());
91          }
92          if (ASSETTYPE.equalsIgnoreCase(s)) {
93              return ids.get(currentRow() - 1).getType();
94          }
95          throw new NoSuchFieldException(s);
96      }
97  
98      public Object getObject(String s) throws NoSuchFieldException {
99          return getValue(s);
100     }
101 
102     public byte[] getFileData(String s) throws IllegalArgumentException, NoSuchFieldException {
103         throw new IllegalArgumentException(s);
104     }
105 
106     public String getFileString(String s) throws NoSuchFieldException {
107         throw new IllegalArgumentException(s);
108     }
109 
110     public int numIndirectColumns() {
111         return 0;
112     }
113 
114     public String getIndirectColumnName(int i) {
115         return null;
116     }
117 
118     public boolean stringInList(String s) {
119         return false;
120     }
121 }