View Javadoc

1   /*
2    * Copyright 2010 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  package com.fatwire.gst.foundation.taglib;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import javax.servlet.jsp.JspException;
23  
24  import COM.FutureTense.Interfaces.ICS;
25  import COM.FutureTense.Interfaces.IList;
26  
27  import com.fatwire.assetapi.data.AssetId;
28  import com.fatwire.gst.foundation.facade.assetapi.AssetIdIList;
29  import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
30  import com.fatwire.gst.foundation.facade.sql.IListIterable;
31  import com.fatwire.gst.foundation.facade.sql.Row;
32  import com.fatwire.mda.DimensionFilterInstance;
33  
34  /**
35   * simple tag for translating an asset that's in the form of an ASSETID,ASSETTYPE IList.
36   *
37   * @author Tony Field
38   * @since 11-09-20
39   */
40  public final class TranslateListTag extends MultilingualGsfSimpleTag {
41  
42      private String inlist = null;
43      private String outlist = null;
44  
45      public void setInlist(String s) {
46          this.inlist = s;
47      }
48  
49      public void setOutlist(String s) {
50          outlist = s;
51      }
52  
53      public void doTag() throws JspException, IOException {
54          LOG.trace("gsf:translate-list start");
55  
56          final ICS ics = getICS();
57  
58          List<AssetId> toFilterList = _getInputList();
59          String outputListName = _getOutputListName();
60  
61          Collection<AssetId> result;
62  
63          DimensionFilterInstance filter = getDimensionFilter();
64          if (filter == null) {
65              LOG.debug("Unable to locate dimension filter. Not filtering assets.  Returning input list");
66              result = toFilterList;
67          } else {
68              result = filter.filterAssets(toFilterList);
69              if (LOG.isDebugEnabled()) {
70                  LOG.debug("Filtered " + toFilterList + " using " + filter + " and got " + result);
71              }
72          }
73  
74          // register the IList in ICS
75          IList resultIList = new AssetIdIList(outputListName, result);
76          ics.RegisterList(outputListName, resultIList);
77          // bonus!
78          getJspContext().setAttribute(outputListName, result);
79  
80          super.doTag();
81          LOG.trace("gsf:translate-list end");
82      }
83  
84      private List<AssetId> _getInputList() throws JspException {
85          if (inlist == null || inlist.length() == 0)
86              throw new JspException("No inlist specified in gsf:translate-list tag");
87          IList in = getICS().GetList(inlist);
88          List<AssetId> result = new ArrayList<AssetId>();
89          for (Row row : new IListIterable(in)) {
90              AssetId id = AssetIdUtils.createAssetId(row.getString("assettype"), row.getString("assetid"));
91              result.add(id);
92          }
93          if (result.size() == 0) {
94              LOG.debug("Input list does not contain any items in gsf:translate-list tag");
95          }
96          return result;
97      }
98  
99      private String _getOutputListName() {
100         if (outlist == null) return inlist;
101         return outlist;
102     }
103 }