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.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          // required parameters
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("/")) // typeless
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                      // pushvars copies current scope into new scope
82                      // so remove vars that are undesireable
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                      // pushvars copies current scope into new scope
96                      // so remove vars that are undesireable
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      * @return the site
130      */
131     public String getSite() {
132         return site;
133     }
134 
135     /**
136      * @param site the site to set
137      */
138     public void setSite(final String site) {
139         this.site = site;
140     }
141 
142     /**
143      * @return the tname
144      */
145     public String getTname() {
146         return tname;
147     }
148 
149     /**
150      * @param tname the tname to set
151      */
152     public void setTname(final String tname) {
153         this.tname = tname;
154     }
155 
156     /**
157      * @return the assettype
158      */
159     public String getAssettype() {
160         return assettype;
161     }
162 
163     /**
164      * @param assettype the assettype to set
165      */
166     public void setAssettype(final String assettype) {
167         this.assettype = assettype;
168     }
169 
170     /**
171      * @return the assetid
172      */
173     public long getAssetid() {
174         return assetid;
175     }
176 
177     /**
178      * @param assetid the assetid to set
179      */
180     public void setAssetid(final long assetid) {
181         this.assetid = assetid;
182     }
183 
184     /**
185      * @param id the assetid to set
186      */
187     public void setAssetid(final AssetId id) {
188         this.assetid = id.getId();
189         this.assettype = id.getType();
190     }
191 
192     /**
193      * @return the packedargs
194      */
195     public String getPackedArgs() {
196         return packedargs;
197     }
198 
199     /**
200      * @param packedargs the packedargs to set
201      */
202     public void setPackedArgs(final String packedargs) {
203         this.packedargs = packedargs;
204     }
205 
206     /**
207      * @return the style
208      */
209     public Style getStyle() {
210         return style;
211     }
212 
213     /**
214      * @param style the style to set
215      */
216     public void setStyle(final Style style) {
217         this.style = style;
218     }
219 
220 }