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.Set;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.springframework.util.Assert;
22
23 import COM.FutureTense.Interfaces.FTValList;
24 import COM.FutureTense.Interfaces.ICS;
25
26 import com.fatwire.assetapi.data.AssetId;
27 import com.fatwire.gst.foundation.CSRuntimeException;
28 import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Style;
29 import com.fatwire.gst.foundation.facade.runtag.satellite.Page;
30
31 public class GsfCallTemplate {
32 private String site;
33 private String tname;
34 private String assettype;
35 private long assetid;
36 private String packedargs;
37 private Style style;
38 private final FTValList arguments = new FTValList();
39
40 public GsfCallTemplate argument(final String name, final String value) {
41 arguments.setValString(name, value);
42 return this;
43 }
44
45 public String include(final ICS ics, final FTValList vIn) {
46
47 Assert.hasText(site, "Site must not be blank.");
48 Assert.hasText(tname, "Tname must not be blank.");
49 Assert.hasText(assettype, "Assettype must not be blank.");
50 if (assetid < 1) {
51 throw new IllegalArgumentException("Assetid must be set.");
52 }
53
54 if (StringUtils.isNotBlank(packedargs)) {
55 ics.decode(packedargs, arguments);
56 }
57
58 try {
59 arguments.setValString("c", assettype);
60 arguments.setValString("cid", Long.toString(assetid));
61 final String rm = ics.GetVar("rendermode");
62 if (rm != null) {
63 arguments.setValString("rendermode", rm);
64 }
65
66 String pagename;
67 String elementname;
68 if (tname.startsWith("/"))
69 {
70 elementname = tname;
71 pagename = site + tname;
72 } else {
73 elementname = assettype + "/" + tname;
74 pagename = site + "/" + assettype + "/" + tname;
75 }
76
77 ics.ClearErrno();
78 switch (style) {
79 case element: {
80 ics.PushVars();
81
82
83 ics.RemoveVar("tid");
84 ics.RemoveVar("context");
85 final boolean ret = ics.CallElement(elementname, arguments);
86 ics.PopVars();
87 if (!ret) {
88 throw new CSRuntimeException("Calling element " + elementname + " failed",
89 ics.getComplexError(), ics.GetErrno());
90 }
91 return null;
92 }
93 case embedded: {
94 ics.PushVars();
95
96
97 ics.RemoveVar("tid");
98 ics.RemoveVar("context");
99 final boolean ret = ics.InsertPage(pagename, arguments);
100 ics.PopVars();
101 if (!ret) {
102 throw new CSRuntimeException("Inserting page " + pagename + " failed", ics.getComplexError(),
103 ics.GetErrno());
104 }
105
106 return null;
107
108 }
109 default: {
110 final Page p = new Page();
111 p.setPagename(pagename);
112
113 @SuppressWarnings("unchecked")
114 final Set<String> keys = arguments.keySet();
115 for (final String key : keys) {
116 p.set(key, arguments.getValString(key));
117 }
118
119 return p.execute(ics);
120 }
121 }
122 } finally {
123 ics.ClearErrno();
124 }
125
126 }
127
128
129
130
131 public String getSite() {
132 return site;
133 }
134
135
136
137
138 public void setSite(final String site) {
139 this.site = site;
140 }
141
142
143
144
145 public String getTname() {
146 return tname;
147 }
148
149
150
151
152 public void setTname(final String tname) {
153 this.tname = tname;
154 }
155
156
157
158
159 public String getAssettype() {
160 return assettype;
161 }
162
163
164
165
166 public void setAssettype(final String assettype) {
167 this.assettype = assettype;
168 }
169
170
171
172
173 public long getAssetid() {
174 return assetid;
175 }
176
177
178
179
180 public void setAssetid(final long assetid) {
181 this.assetid = assetid;
182 }
183
184
185
186
187 public void setAssetid(final AssetId id) {
188 this.assetid = id.getId();
189 this.assettype = id.getType();
190 }
191
192
193
194
195 public String getPackedArgs() {
196 return packedargs;
197 }
198
199
200
201
202 public void setPackedArgs(final String packedargs) {
203 this.packedargs = packedargs;
204 }
205
206
207
208
209 public Style getStyle() {
210 return style;
211 }
212
213
214
215
216 public void setStyle(final Style style) {
217 this.style = style;
218 }
219
220 }