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