View Javadoc

1   /*
2    * Copyright 2011 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.include;
17  
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import org.apache.commons.lang.StringUtils;
24  
25  import COM.FutureTense.Interfaces.ICS;
26  
27  import com.fatwire.assetapi.data.AssetId;
28  import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate;
29  import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Style;
30  import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Type;
31  
32  /**
33   * @author Dolf Dijkstra
34   * @since Apr 13, 2011
35   */
36  public class IncludeTemplate implements Include {
37  
38      public static final List<String> FORBIDDEN_VARS = Collections.unmodifiableList(Arrays.asList("c", "cid", "eid",
39              "seid", "packedargs", "variant", "context", "pagename", "childpagename", "site", "sitepfx", "tid",
40              "rendermode", "ft_ss", "SystemAssetsRoot", "cshttp", "errno", "tablename", "empty", "errdetail", "null"));
41  
42      private final CallTemplate tag;
43      private final List<String> pc;
44      private final ICS ics;
45  
46      /**
47       * @param ics Content Server context
48       * @param asset asset to render
49       * @param tname template name
50       */
51      public IncludeTemplate(final ICS ics, final AssetId asset, final String tname) {
52          this.ics = ics;
53          tag = new CallTemplate();
54          tag.setTname(tname);
55  
56          final String eid = ics.GetVar("eid");
57          if (eid != null) {
58              tag.setTid(eid);
59              tag.setTtype(Type.CSElement);
60          } else {
61              tag.setTid(ics.GetVar("tid"));
62              tag.setTtype(Type.Template);
63          }
64          tag.setContext("");
65          final String site = ics.GetVar("site");
66          tag.setSite(site);
67          final String packedargs = ics.GetVar("packedargs");
68          if (packedargs != null && packedargs.length() > 0) {
69              tag.setPackedargs(packedargs);
70          }
71  
72          tag.setAsset(asset);
73          tag.setFixPageCriteria(false); // for some reason the check pagecriteria
74                                         // code in CallTemplate is not working.
75          tag.setSlotname("foo");
76          final String target = tname.startsWith("/") ? site + tname : site + "/" + asset.getType() + "/" + tname;
77          final String[] keys = ics.pageCriteriaKeys(target);
78          if (keys == null) {
79              throw new IllegalArgumentException("Can't find page criteria for " + target
80                      + ". Please check if pagecriteria are set for " + target + ".");
81          }
82          pc = Arrays.asList(keys);
83          // copy the current available arguments
84          // developer can override later by calling method argument
85          for (final String key : keys) {
86              if (!FORBIDDEN_VARS.contains(key.toLowerCase(Locale.US))) {
87                  final String value = ics.GetVar(key);
88                  if (StringUtils.isNotBlank(value)) {
89                      tag.setArgument(key, value);
90                  }
91              }
92          }
93      }
94  
95      /*
96       * (non-Javadoc)
97       * 
98       * @see
99       * com.fatwire.gst.foundation.include.Include#include(COM.FutureTense.Interfaces
100      * .ICS)
101      */
102     public void include(final ICS ics) {
103         final String s = tag.execute(ics);
104         if (s != null) {
105             ics.StreamText(s);
106         }
107 
108     }
109 
110     /**
111      * @param name
112      * @param value
113      * @return this
114      * @see com.fatwire.gst.foundation.facade.runtag.render.CallTemplate#setArgument(java.lang.String,
115      *      java.lang.String)
116      */
117     public IncludeTemplate argument(final String name, final String value) {
118         if (StringUtils.isBlank(name)) {
119             return this;
120         }
121         if (FORBIDDEN_VARS.contains(name.toLowerCase(Locale.US))) {
122             throw new IllegalArgumentException("Can't deal with " + name
123                     + ". It is a forbidden argument to set as an argument. Forbidden arguments are: "
124                     + FORBIDDEN_VARS.toString());
125         }
126         if (pc.contains(name)) {
127             tag.setArgument(name, value);
128         } else {
129             throw new IllegalArgumentException("Can't deal with " + name
130                     + ". It not part of page criteria. PageCriteria are: " + pc.toString());
131         }
132         return this;
133     }
134 
135     /**
136      * Copies the ics variables identified by the name array
137      * 
138      * @param name
139      * @return this
140      */
141     public IncludeTemplate copyArguments(final String... name) {
142         if (name == null) {
143             return this;
144         }
145         for (final String n : name) {
146             argument(n, ics.GetVar(n));
147         }
148         return this;
149     }
150 
151     /**
152      * Adds packedargs.
153      * 
154      * @param s packedargs
155      * @return this
156      * @see com.fatwire.gst.foundation.facade.runtag.render.CallTemplate#setPackedargs(java.lang.String)
157      */
158     public IncludeTemplate packedargs(final String s) {
159         tag.setPackedargs(s);
160         return this;
161     }
162 
163     /**
164      * @param s style
165      * @return this
166      * @see com.fatwire.gst.foundation.facade.runtag.render.CallTemplate#setStyle(com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Style)
167      */
168     public IncludeTemplate style(final Style s) {
169         tag.setStyle(s);
170         return this;
171     }
172 
173     /**
174      * Sets Style to element
175      * 
176      * @return this
177      */
178     public IncludeTemplate element() {
179         return style(Style.element);
180     }
181 
182     /**
183      * Sets Style to embedded
184      * 
185      * @return this
186      */
187     public IncludeTemplate embedded() {
188         return style(Style.embedded);
189     }
190 
191     /**
192      * Sets Style to pagelet
193      * 
194      * @return this
195      */
196     public IncludeTemplate pagelet() {
197         return style(Style.pagelet);
198     }
199 
200 }