KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > template > UITemplate


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.repo.component.template;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.context.FacesContext;
25 import javax.faces.el.ValueBinding;
26
27 import org.alfresco.repo.template.DateCompareMethod;
28 import org.alfresco.repo.template.HasAspectMethod;
29 import org.alfresco.repo.template.I18NMessageMethod;
30 import org.alfresco.service.ServiceRegistry;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.TemplateException;
33 import org.alfresco.service.cmr.repository.TemplateImageResolver;
34 import org.alfresco.service.cmr.repository.TemplateNode;
35 import org.alfresco.service.cmr.repository.TemplateService;
36 import org.alfresco.web.app.Application;
37 import org.alfresco.web.bean.repository.Repository;
38 import org.alfresco.web.bean.repository.User;
39 import org.alfresco.web.ui.common.Utils;
40 import org.alfresco.web.ui.common.component.SelfRenderingComponent;
41 import org.apache.log4j.Logger;
42
43 /**
44  * @author Kevin Roast
45  */

46 public class UITemplate extends SelfRenderingComponent
47 {
48    private final static String JavaDoc ENGINE_DEFAULT = "freemarker";
49    private final static String JavaDoc TEMPLATE_KEY = "_template_";
50    
51    private static Logger logger = Logger.getLogger(UITemplate.class);
52    
53    /** Template engine name */
54    private String JavaDoc engine = null;
55    
56    /** Template name/path */
57    private String JavaDoc template = null;
58    
59    /** Data model reference */
60    private Object JavaDoc model = null;
61    
62    
63    // ------------------------------------------------------------------------------
64
// Component implementation
65

66    /**
67     * @see javax.faces.component.UIComponent#getFamily()
68     */

69    public String JavaDoc getFamily()
70    {
71       return "org.alfresco.faces.Template";
72    }
73    
74    /**
75     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
76     */

77    public void restoreState(FacesContext context, Object JavaDoc state)
78    {
79       Object JavaDoc values[] = (Object JavaDoc[])state;
80       // standard component attributes are restored by the super class
81
super.restoreState(context, values[0]);
82       this.engine = (String JavaDoc)values[1];
83       this.template = (String JavaDoc)values[2];
84       this.model = (Object JavaDoc)values[3];
85    }
86    
87    /**
88     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
89     */

90    public Object JavaDoc saveState(FacesContext context)
91    {
92       Object JavaDoc values[] = new Object JavaDoc[4];
93       // standard component attributes are saved by the super class
94
values[0] = super.saveState(context);
95       values[1] = this.engine;
96       values[2] = this.template;
97       values[3] = this.model;
98       return (values);
99    }
100    
101    /**
102     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
103     */

104    public void encodeBegin(FacesContext context) throws IOException JavaDoc
105    {
106       if (isRendered() == false)
107       {
108          return;
109       }
110       
111       // get the data model to use - building default if required
112
Object JavaDoc model = getModel();
113       
114       // get the template to process
115
String JavaDoc template = getTemplate();
116       if (template != null && template.length() != 0)
117       {
118          // get the class name of the processor to instantiate
119
String JavaDoc engine = getEngine();
120          
121          long startTime = 0;
122          if (logger.isDebugEnabled())
123          {
124             logger.debug("Using template processor name: " + engine);
125             startTime = System.currentTimeMillis();
126          }
127          
128          // process the template against the model
129
try
130          {
131             TemplateService templateService = Repository.getServiceRegistry(context).getTemplateService();
132             templateService.processTemplate(engine, getTemplate(), model, context.getResponseWriter());
133          }
134          catch (TemplateException err)
135          {
136             Utils.addErrorMessage(err.getMessage(), err);
137          }
138          
139          if (logger.isDebugEnabled())
140          {
141             long endTime = System.currentTimeMillis();
142             logger.debug("Time to process template: " + (endTime - startTime) + "ms");
143          }
144       }
145    }
146    
147    
148    // ------------------------------------------------------------------------------
149
// Strongly typed component property accessors
150

151    /**
152     * @return the name of the template engine to use.
153     */

154    public String JavaDoc getEngine()
155    {
156       ValueBinding vb = getValueBinding("engine");
157       if (vb != null)
158       {
159          this.engine = (String JavaDoc)vb.getValue(getFacesContext());
160       }
161       
162       return this.engine != null ? this.engine : ENGINE_DEFAULT;
163    }
164    
165    /**
166     * @param engine the name of the template engine to use. A default is provided if none is set.
167     */

168    public void setEngine(String JavaDoc engine)
169    {
170       this.engine = engine;
171    }
172
173    /**
174     * Return the data model to bind template against.
175     * <p>
176     * By default we return a Map model containing root references to the Company Home Space,
177     * the users Home Space and the Person Node for the current user.
178     *
179     * @return Returns the data model to bind template against.
180     */

181    public Object JavaDoc getModel()
182    {
183       if (this.model == null)
184       {
185          Object JavaDoc model = null;
186          ValueBinding vb = getValueBinding("model");
187          if (vb != null)
188          {
189             model = vb.getValue(getFacesContext());
190          }
191          
192          if (getEngine().equals(ENGINE_DEFAULT))
193          {
194             // create an instance of the default FreeMarker template object model
195
FacesContext fc = FacesContext.getCurrentInstance();
196             ServiceRegistry services = Repository.getServiceRegistry(fc);
197             User user = Application.getCurrentUser(fc);
198             Map JavaDoc root = DefaultModelHelper.buildDefaultModel(services, user);
199             
200             // merge models
201
if (model instanceof Map JavaDoc)
202             {
203                if (logger.isDebugEnabled())
204                   logger.debug("Found valid Map model to merge with FreeMarker: " + model);
205                
206                root.putAll((Map JavaDoc)model);
207             }
208             
209             model = root;
210          }
211          
212          return model;
213       }
214       else
215       {
216          return this.model;
217       }
218    }
219
220    /**
221     * @param model The model to set.
222     */

223    public void setModel(Object JavaDoc model)
224    {
225       this.model = model;
226    }
227
228    /**
229     * @return Returns the template name.
230     */

231    public String JavaDoc getTemplate()
232    {
233       ValueBinding vb = getValueBinding("template");
234       if (vb != null)
235       {
236          // convert object to string - then we can handle either a path, NodeRef instance or noderef string
237
Object JavaDoc val = vb.getValue(getFacesContext());
238          if (val != null)
239          {
240             this.template = val.toString();
241          }
242       }
243       
244       return this.template;
245    }
246
247    /**
248     * @param template The template name to set.
249     */

250    public void setTemplate(String JavaDoc template)
251    {
252       this.template = template;
253    }
254    
255    /** Template Image resolver helper */
256    private TemplateImageResolver imageResolver = new TemplateImageResolver()
257    {
258        public String JavaDoc resolveImagePathForName(String JavaDoc filename, boolean small)
259        {
260            return Utils.getFileTypeImage(filename, small);
261        }
262    };
263 }
264
Popular Tags