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.List;
21  import javax.servlet.jsp.JspException;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.gst.foundation.facade.assetapi.asset.ScatteredAssetAccessTemplate;
27  import com.openmarket.xcelerate.asset.AssetIdImpl;
28  
29  import org.apache.commons.lang.StringUtils;
30  
31  /**
32   * 
33   * @author Tony Field
34   * @since 2011-09-20
35   */
36  public final class GetAssetTagsTag extends GsfSimpleTag {
37  
38      private String c;
39      private long cid;
40      private String name;
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
46       */
47      @Override
48      public void doTag() throws JspException, IOException {
49  
50          final ICS ics = getICS();
51          final ScatteredAssetAccessTemplate t = new ScatteredAssetAccessTemplate(ics);
52          // todo: medium: find in page context if it's there (due to presence of
53          // gsf:root
54  
55          if (StringUtils.isBlank(c) || cid == 0) {
56              String tag = (String) t.readCurrent("gsttag").get("gsttag");
57              if (tag != null) {
58                  getJspContext().setAttribute(name, parseTags(tag));
59              }
60          } else {
61              final AssetId id = t.createAssetId(c, cid);
62              String tag = (String) t.read(id, "gsttag").get("gsttag");
63              if (tag != null) {
64                  getJspContext().setAttribute(name, parseTags(tag));
65              }
66          }
67  
68          super.doTag();
69      }
70  
71      private static List<AssetId> parseTags(String tag) {
72          ArrayList<AssetId> tags = new ArrayList<AssetId>();
73          for (String assetEntry : tag.split(",")) {
74              int indexStart = assetEntry.indexOf("-");
75              int indexEnd = assetEntry.indexOf(":");
76              String assetId = assetEntry.substring(indexStart + 1, indexEnd);
77              String assetType = assetEntry.substring(indexEnd + 1, assetEntry.length());
78              // we have the asset type and asset id, load it, if successful add
79              // it
80              AssetId aId = new AssetIdImpl(assetType, Long.parseLong(assetId));
81              tags.add(aId);
82          }
83          return tags;
84      }
85  
86      /**
87       * @param c the c to set
88       */
89      public void setC(final String c) {
90          this.c = c;
91      }
92  
93      /**
94       * @param cid the cid to set
95       */
96      public void setCid(final long cid) {
97          this.cid = cid;
98      }
99  
100     /**
101      * @param name the name to set
102      */
103     public void setName(final String name) {
104         this.name = name;
105     }
106 }