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  
17  package com.fatwire.gst.foundation.wra;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  
21  import com.fatwire.assetapi.data.AssetId;
22  import com.fatwire.gst.foundation.facade.runtag.asset.AssetList;
23  import com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl;
24  
25  import org.apache.commons.lang.StringUtils;
26  
27  /**
28   * @author Dolf Dijkstra
29   * @since Mar 13, 2011
30   */
31  public class WraUriBuilder {
32  
33      private final GetTemplateUrl tag = new GetTemplateUrl();
34      private String tid = null;
35      private String slotname = null;
36      private String site = null;
37  
38      /**
39       * Constructor with AssetId and default wrapper (GST/Dispatcher).
40       * 
41       * @param id
42       */
43      public WraUriBuilder(AssetId id) {
44          this(id, "GST/Dispatcher");
45  
46      }
47  
48      /**
49       * Constructor with AssetId and wrapper.
50       * 
51       * @param id
52       * @param wrapper
53       */
54      private WraUriBuilder(AssetId id, String wrapper) {
55          tag.setWrapperpage(wrapper);
56          tag.setC(id.getType());
57          tag.setCid(Long.toString(id.getId()));
58          tag.setAssembler("wrapath");
59  
60      }
61  
62      public WraUriBuilder(VanityAsset wra, String wrapper) {
63          this(wra.getId(), wrapper);
64          if (StringUtils.isBlank(wra.getTemplate()))
65              throw new IllegalArgumentException("The template attribute for asset " + wra.getId().getType() + ":"
66                      + wra.getId().getId() + " is not provided.");
67          tag.setTname(wra.getTemplate());
68      }
69  
70      /**
71       * Returns the uri to the asset rendered with the specified template and
72       * wrapper (if provided).
73       * 
74       * @param ics
75       * @return the URI as a String for this WRA
76       * @see com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner#execute(COM.FutureTense.Interfaces.ICS)
77       */
78      public String toURI(ICS ics) {
79          ensureTid(ics);
80          ensureSlotname();
81          ensureSite(ics);
82          ics.RemoveVar("uri__");
83          tag.setOutstr("uri__");
84          tag.execute(ics);
85          String uri = ics.GetVar("uri__");
86          ics.RemoveVar("uri__");
87          return uri;
88      }
89  
90      private void ensureTid(ICS ics) {
91          String _ttype = null;
92          String _tid = null;
93          if (tid == null) {
94              _ttype = "Template";
95              _tid = ics.GetVar("tid");
96              if (!AssetList.assetExists(ics, _ttype, _tid)) {
97                  _ttype = "CSElement";
98                  _tid = ics.GetVar("eid");
99                  if (!AssetList.assetExists(ics, _ttype, _tid)) {
100                     throw new IllegalArgumentException(
101                             "tid was not specified and neither tid nor eid were found valid in the variable scope");
102                 }
103             }
104         }
105         ttype(_ttype);
106         tid(_tid);
107     }
108 
109     private void ensureSlotname() {
110         if (slotname == null) {
111             // The user has not specified slotname.
112             // This implies that the link has no special characteristics
113             // therefore other usages of this non-special link can be re-used
114             // so we can re-use slot by giving it a constant name.
115             slotname("WraUriBuilderLink");
116         }
117     }
118 
119     private void ensureSite(ICS ics) {
120         if (site == null) {
121             site(ics.GetVar("site"));
122         }
123     }
124 
125     /**
126      * @param name
127      * @param value
128      * @see com.fatwire.gst.foundation.facade.runtag.render.TagRunnerWithArguments#setArgument(java.lang.String,
129      *      java.lang.String)
130      */
131     public WraUriBuilder argument(String name, String value) {
132         tag.setArgument(name, value);
133         return this;
134     }
135 
136     /**
137      * @param s
138      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setAssembler(java.lang.String)
139      */
140     public WraUriBuilder assembler(String s) {
141         tag.setAssembler(s);
142         return this;
143     }
144 
145     /**
146      * @param s
147      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setAuthority(java.lang.String)
148      */
149     public WraUriBuilder authority(String s) {
150         tag.setAuthority(s);
151         return this;
152     }
153 
154     /**
155      * @param s
156      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setContainer(java.lang.String)
157      */
158     public WraUriBuilder container(String s) {
159         tag.setContainer(s);
160         return this;
161     }
162 
163     /**
164      * @param s
165      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setDynamic(java.lang.String)
166      */
167     public WraUriBuilder dynamic(boolean s) {
168         tag.setDynamic(s);
169         return this;
170     }
171 
172     /**
173      * @param s
174      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setFragment(java.lang.String)
175      */
176     public WraUriBuilder fragment(String s) {
177         tag.setFragment(s);
178         return this;
179     }
180 
181     /**
182      * @param s
183      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setPackedargs(java.lang.String)
184      */
185     public WraUriBuilder packedargs(String s) {
186         tag.setPackedargs(s);
187         return this;
188     }
189 
190     /**
191      * @param s
192      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setSatellite(java.lang.String)
193      */
194     public WraUriBuilder satellite(String s) {
195         tag.setSatellite("TRUE".equalsIgnoreCase(s));
196         return this;
197     }
198 
199     /**
200      * @param s
201      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setScheme(java.lang.String)
202      */
203     public WraUriBuilder scheme(String s) {
204         tag.setScheme(s);
205         return this;
206     }
207 
208     /**
209      * @param s
210      * @see com.fatwire.gst.foundation.facade.runtag.render.GetTemplateUrl#setWrapperpage(java.lang.String)
211      */
212     public WraUriBuilder wrapper(String s) {
213         tag.setWrapperpage(s);
214         return this;
215     }
216 
217     public WraUriBuilder template(String template) {
218         tag.setTname(template);
219         return this;
220     }
221 
222     public WraUriBuilder tid(String tid) {
223         this.tid = tid;
224         tag.setTid(tid);
225         return this;
226     }
227 
228     public WraUriBuilder ttype(String ttype) {
229         tag.setTtype(ttype);
230         return this;
231     }
232 
233     public WraUriBuilder slotname(String slotname) {
234         this.slotname = slotname;
235         tag.setSlotname(slotname);
236         return this;
237     }
238 
239     public WraUriBuilder site(String site) {
240         this.site = site;
241         tag.setSite(site);
242         return this;
243     }
244 
245 }