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.runtag.asset;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  import COM.FutureTense.Interfaces.IList;
21  import COM.FutureTense.Util.IterableIListWrapper;
22  
23  import com.fatwire.assetapi.data.AssetId;
24  import com.fatwire.gst.foundation.IListUtils;
25  import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
26  import com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner;
27  import com.fatwire.gst.foundation.facade.sql.IListIterable;
28  import com.fatwire.gst.foundation.facade.sql.Row;
29  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
30  
31  /**
32   * Exposes ASSET.LIST <asset:list type="assetType" list="nameOfList"
33   * [order="order"] [pubid="siteId"] [excludevoided="trueOrFalse"]
34   * [field[n]="fieldName"] [value[n]="fieldValue"]> <asset:argument
35   * name="fieldName" value="fieldValue"/&gt; &lt;/asset:list&gt;
36   *
37   * @author Tony Field
38   * @since Sep 28, 2008
39   */
40  public class AssetList extends AbstractTagRunner {
41      public AssetList() {
42          super("ASSET.LIST");
43      }
44  
45      public void setType(String type) {
46          this.set("TYPE", type);
47      }
48  
49      public void setList(String list) {
50          this.set("LIST", list);
51      }
52  
53      public void setOrder(String order) {
54          this.set("ORDER", order);
55      }
56  
57      public void setPubid(String pubid) {
58          this.set("PUBID", pubid);
59      }
60  
61      public void setExcludeVoided(boolean b) {
62          this.set("EXCLUDEVOIDED", b ? "TRUE" : "FALSE");
63      }
64  
65      private int index = 1;
66  
67      public void setField(String name, String value) {
68          this.set("FIELD" + index, name);
69          this.set("VALUE" + index, value);
70          index++;
71      }
72  
73      /**
74       * Return true if the asset exists in the database, and false if it does
75       * not.
76       *
77       * @param ics context
78       * @param c asset type
79       * @param cid asset id
80       * @return true if the asset exists, false if it does not
81       */
82      public static boolean assetExists(ICS ics, String c, String cid) {
83          if (c == null || c.length() == 0)
84              return false;
85          if (cid == null || cid.length() == 0)
86              return false;
87          ics.RegisterList("spu-gtfa", null);
88          AssetList al = new AssetList();
89          al.setField("id", cid);
90          al.setType(c);
91          al.setExcludeVoided(false);
92          al.setList("spu-gtfa");
93          al.execute(ics);
94          IList spugtfa = ics.GetList("spu-gtfa");
95          ics.RegisterList("spu-gtfa", null);
96          return spugtfa != null && spugtfa.hasData();
97      }
98  
99      public static boolean assetExistsByName(ICS ics, String c, String name) {
100         if (c == null || c.length() == 0) return false;
101 
102         if (!SqlHelper.tableExists(ics, c)) return false;
103 
104         if (name == null || name.length() == 0) return false;
105         ics.RegisterList("spu-gtfa", null);
106         AssetList al = new AssetList();
107         al.setField("name", name);
108         al.setType(c);
109         al.setExcludeVoided(false);
110         al.setList("spu-gtfa");
111         al.execute(ics);
112         IList spugtfa = ics.GetList("spu-gtfa");
113         ics.RegisterList("spu-gtfa", null);
114         return spugtfa != null && spugtfa.hasData();
115     }
116 
117     /**
118      * Look up asset id by name
119      * @param ics context
120      * @param c type
121      * @param name name field value
122      * @return asset id or null if not found.
123      */
124     public static AssetId lookupAssetId(ICS ics, String c, String name) {
125         if (c == null || c.length() == 0) throw new IllegalArgumentException("No such asset type: " + c);
126 
127         if (!SqlHelper.tableExists(ics, c)) throw new IllegalArgumentException("No such asset type: " + c);
128 
129         if (name == null || name.length() == 0)
130             throw new IllegalArgumentException("Cannot look up asset by name with an actual name.  Type: " + c);
131         ics.RegisterList("spu-gtfa", null);
132         AssetList al = new AssetList();
133         al.setField("name", name);
134         al.setType(c);
135         al.setExcludeVoided(false);
136         al.setList("spu-gtfa");
137         al.execute(ics);
138         IList spugtfa = ics.GetList("spu-gtfa");
139         ics.RegisterList("spu-gtfa", null);
140         if (spugtfa != null && spugtfa.hasData()) {
141             for (Row listRow : new IListIterable(spugtfa)) {
142                 return AssetIdUtils.createAssetId(c, listRow.getString("id"));
143             }
144         }
145         return null;
146     }
147 
148     /**
149      * Get a single field from a specified asset. Only one result must be
150      * returned from this search or an exception will be thrown. Pubid is
151      * optional.
152      *
153      * @param ics
154      * @param c
155      * @param cid
156      * @param field
157      * @return single field value
158      */
159     public static String getRequiredSingleField(ICS ics, String c, String cid, String field) {
160         ics.RegisterList("spu-gtfa", null);
161         AssetList al = new AssetList();
162         al.setField("id", cid);
163         al.setType(c);
164         al.setExcludeVoided(true);
165         al.setList("spu-gtfa");
166         al.execute(ics);
167         IList spugtfa = ics.GetList("spu-gtfa");
168         ics.RegisterList("spu-gtfa", null);
169         if (spugtfa != null && spugtfa.hasData()) {
170             if (spugtfa.numRows() > 1) {
171                 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:"
172                         + spugtfa.numRows());
173             }
174             spugtfa.moveTo(1);
175             String ret = IListUtils.getStringValue(spugtfa, field);
176             if (ret == null || ret.length() == 0) {
177                 throw new IllegalStateException("No " + field + " found for asset " + c + ":" + cid);
178             }
179             return ret;
180         } else {
181             throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": no data found.");
182         }
183     }
184 
185     public static IList listSingleAsset(ICS ics, String c, String cid) {
186         ics.RegisterList("spu-gtfa", null);
187         AssetList al = new AssetList();
188         al.setField("id", cid);
189         al.setType(c);
190         al.setExcludeVoided(true);
191         al.setList("spu-gtfa");
192         al.execute(ics);
193         IList spugtfa = ics.GetList("spu-gtfa");
194         ics.RegisterList("spu-gtfa", null);
195         if (spugtfa != null && spugtfa.hasData()) {
196             if (spugtfa.numRows() > 1) {
197                 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:"
198                         + spugtfa.numRows());
199             }
200             return new IterableIListWrapper(spugtfa).iterator().next();
201         } else {
202             throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": no data found.");
203         }
204     }
205 
206     /*
207      * (non-Javadoc)
208      * 
209      * @see
210      * com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner#handleError
211      * (COM.FutureTense.Interfaces.ICS)
212      */
213     @Override
214     protected void handleError(ICS ics) {
215         if (ics.GetErrno() == -101)
216             return;
217         super.handleError(ics);
218     }
219 
220 }