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.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import org.apache.commons.lang.StringUtils;
26  
27  import COM.FutureTense.ContentServer.PageData;
28  import COM.FutureTense.Interfaces.FTValList;
29  import COM.FutureTense.Interfaces.ICS;
30  
31  import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Style;
32  import com.fatwire.gst.foundation.facade.runtag.render.ContentServer;
33  import com.fatwire.gst.foundation.facade.runtag.render.SatellitePage;
34  
35  /**
36   * Class that calls render:contentserver or render:satellitepage based on style.
37   * 
38   * @author Dolf Dijkstra
39   * @since Apr 13, 2011
40   */
41  
42  public class IncludePage implements Include {
43      public static final List<String> FORBIDDEN_VARS = Collections.unmodifiableList(Arrays.asList("tid", "eid", "seid",
44              "packedargs", "variant", "context", "pagename", "rendermode", "ft_ss"));
45  
46      private Style style;
47      private final Map<String, String> list = new HashMap<String, String>();
48      private final String pagename;
49      private String packedArgs;
50  
51      private final List<String> pc;
52      private final ICS ics;
53  
54      /**
55       * @param ics
56       * @param pagename
57       */
58      public IncludePage(final ICS ics, final String pagename) {
59          this(ics, pagename, Style.embedded);
60      }
61  
62      /**
63       * @param ics
64       * @param pagename
65       * @param style
66       */
67      public IncludePage(final ICS ics, final String pagename, final Style style) {
68          this.ics = ics;
69          this.style = style;
70          this.pagename = pagename;
71          final String[] keys = ics.pageCriteriaKeys(pagename);
72          if (keys == null) {
73              throw new IllegalArgumentException("Can't find page criteria for " + pagename
74                      + ". Please check if pagecriteria are set for " + pagename + ".");
75          }
76          pc = Arrays.asList(keys);
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see
83       * com.fatwire.gst.foundation.include.Include#include(COM.FutureTense.Interfaces
84       * .ICS)
85       */
86      public void include(final ICS ics) {
87          switch (style) {
88              case embedded: {
89                  final ContentServer tag = new ContentServer(pagename);
90                  for (final Entry<String, String> e : list.entrySet()) {
91                      tag.setArgument(e.getKey(), e.getValue());
92                  }
93                  if (StringUtils.isNotBlank(packedArgs)) {
94                      tag.setArgument("PACKEDARGS", packedArgs);
95                  }
96                  final String s = tag.execute(ics);
97                  if (s != null) {
98                      ics.StreamText(s);
99                  }
100             }
101                 break;
102             case pagelet: {
103                 final SatellitePage tag = new SatellitePage(pagename);
104                 for (final Entry<String, String> e : list.entrySet()) {
105                     tag.setArgument(e.getKey(), e.getValue());
106                 }
107                 if (StringUtils.isNotBlank(packedArgs)) {
108                     tag.setPackedArgs(packedArgs);
109                 }
110 
111                 final String s = tag.execute(ics);
112                 if (s != null) {
113                     ics.StreamText(s);
114                 }
115             }
116                 break;
117             case element: {
118                 PageData data = ics.getPageData(pagename);
119                 if (!data.isRegistered())
120                     throw new IllegalArgumentException(pagename + " is not a registered page.");
121                 String element = data.getRootElement();
122                 FTValList ftv = argsToFTValList();
123                 ics.CallElement(element, ftv);
124             }
125             default:
126                 throw new IllegalStateException("Can't handle style " + style);
127         }
128 
129     }
130 
131     /**
132      * @return
133      */
134     @SuppressWarnings("unchecked")
135     private FTValList argsToFTValList() {
136         FTValList ftv = new FTValList();
137         ftv.putAll(list);
138         return ftv;
139     }
140 
141     /**
142      * @param name
143      * @param value
144      * @return this
145      * @see com.fatwire.gst.foundation.facade.runtag.render.CallTemplate#setArgument(java.lang.String,
146      *      java.lang.String)
147      */
148     public IncludePage argument(final String name, final String value) {
149         if (FORBIDDEN_VARS.contains(name)) {
150             throw new IllegalArgumentException("Can't deal with " + name);
151         }
152 
153         if (pc.contains(name)) {
154             list.put(name, value);
155         } else {
156             throw new IllegalArgumentException( name + " is not part of the page criteria: " + pc.toString());
157         }
158         return this;
159     }
160 
161     /**
162      * Adds packedargs.
163      * 
164      * @param s
165      * @return this
166      * @see com.fatwire.gst.foundation.facade.runtag.render.CallTemplate#setPackedargs(java.lang.String)
167      */
168     public IncludePage packedargs(final String s) {
169         this.packedArgs = s;
170         return this;
171     }
172 
173     /**
174      * Copies the ics variables identified by the name array
175      * 
176      * @param name
177      * @return this
178      */
179     public IncludePage copyArguments(final String... name) {
180         if (name == null) {
181             return this;
182         }
183         for (final String n : name) {
184             argument(n, ics.GetVar(n));
185         }
186         return this;
187     }
188 
189     public IncludePage style(final Style s) {
190         style = s;
191         return this;
192     }
193 
194     /**
195      * Sets Style to embedded
196      * 
197      * @return this
198      */
199     public IncludePage embedded() {
200         return style(Style.embedded);
201     }
202 
203     /**
204      * Sets Style to pagelet
205      * 
206      * @return this
207      */
208     public IncludePage pagelet() {
209         return style(Style.pagelet);
210     }
211 }