1 17 package org.alfresco.web.ui.repo.component.template; 18 19 import java.io.IOException ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.Map ; 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 46 public class UITemplate extends SelfRenderingComponent 47 { 48 private final static String ENGINE_DEFAULT = "freemarker"; 49 private final static String TEMPLATE_KEY = "_template_"; 50 51 private static Logger logger = Logger.getLogger(UITemplate.class); 52 53 54 private String engine = null; 55 56 57 private String template = null; 58 59 60 private Object model = null; 61 62 63 66 69 public String getFamily() 70 { 71 return "org.alfresco.faces.Template"; 72 } 73 74 77 public void restoreState(FacesContext context, Object state) 78 { 79 Object values[] = (Object [])state; 80 super.restoreState(context, values[0]); 82 this.engine = (String )values[1]; 83 this.template = (String )values[2]; 84 this.model = (Object )values[3]; 85 } 86 87 90 public Object saveState(FacesContext context) 91 { 92 Object values[] = new Object [4]; 93 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 104 public void encodeBegin(FacesContext context) throws IOException 105 { 106 if (isRendered() == false) 107 { 108 return; 109 } 110 111 Object model = getModel(); 113 114 String template = getTemplate(); 116 if (template != null && template.length() != 0) 117 { 118 String 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 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 151 154 public String getEngine() 155 { 156 ValueBinding vb = getValueBinding("engine"); 157 if (vb != null) 158 { 159 this.engine = (String )vb.getValue(getFacesContext()); 160 } 161 162 return this.engine != null ? this.engine : ENGINE_DEFAULT; 163 } 164 165 168 public void setEngine(String engine) 169 { 170 this.engine = engine; 171 } 172 173 181 public Object getModel() 182 { 183 if (this.model == null) 184 { 185 Object 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 FacesContext fc = FacesContext.getCurrentInstance(); 196 ServiceRegistry services = Repository.getServiceRegistry(fc); 197 User user = Application.getCurrentUser(fc); 198 Map root = DefaultModelHelper.buildDefaultModel(services, user); 199 200 if (model instanceof Map ) 202 { 203 if (logger.isDebugEnabled()) 204 logger.debug("Found valid Map model to merge with FreeMarker: " + model); 205 206 root.putAll((Map )model); 207 } 208 209 model = root; 210 } 211 212 return model; 213 } 214 else 215 { 216 return this.model; 217 } 218 } 219 220 223 public void setModel(Object model) 224 { 225 this.model = model; 226 } 227 228 231 public String getTemplate() 232 { 233 ValueBinding vb = getValueBinding("template"); 234 if (vb != null) 235 { 236 Object val = vb.getValue(getFacesContext()); 238 if (val != null) 239 { 240 this.template = val.toString(); 241 } 242 } 243 244 return this.template; 245 } 246 247 250 public void setTemplate(String template) 251 { 252 this.template = template; 253 } 254 255 256 private TemplateImageResolver imageResolver = new TemplateImageResolver() 257 { 258 public String resolveImagePathForName(String filename, boolean small) 259 { 260 return Utils.getFileTypeImage(filename, small); 261 } 262 }; 263 } 264 | Popular Tags |