1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
39
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
56
57
58 public IncludePage(final ICS ics, final String pagename) {
59 this(ics, pagename, Style.embedded);
60 }
61
62
63
64
65
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
81
82
83
84
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
133
134 @SuppressWarnings("unchecked")
135 private FTValList argsToFTValList() {
136 FTValList ftv = new FTValList();
137 ftv.putAll(list);
138 return ftv;
139 }
140
141
142
143
144
145
146
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
163
164
165
166
167
168 public IncludePage packedargs(final String s) {
169 this.packedArgs = s;
170 return this;
171 }
172
173
174
175
176
177
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
196
197
198
199 public IncludePage embedded() {
200 return style(Style.embedded);
201 }
202
203
204
205
206
207
208 public IncludePage pagelet() {
209 return style(Style.pagelet);
210 }
211 }