1 17 package org.ofbiz.content.webapp.ftl; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.sql.Timestamp ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.GeneralException; 32 import org.ofbiz.base.util.UtilDateTime; 33 import org.ofbiz.base.util.UtilProperties; 34 import org.ofbiz.base.util.UtilValidate; 35 import org.ofbiz.base.util.template.FreeMarkerWorker; 36 import org.ofbiz.content.content.ContentWorker; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericValue; 39 import org.ofbiz.entity.model.ModelEntity; 40 import org.ofbiz.widget.WidgetWorker; 41 42 import freemarker.core.Environment; 43 import freemarker.template.TemplateTransformModel; 44 46 55 public class RenderSubContentCacheTransform implements TemplateTransformModel { 56 57 public static final String module = RenderSubContentCacheTransform.class.getName(); 58 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 59 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"}; 60 61 public Writer getWriter(final Writer out, Map args) { 62 final StringBuffer buf = new StringBuffer (); 63 final Environment env = Environment.getCurrentEnvironment(); 64 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 67 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 68 final HttpServletResponse response = (HttpServletResponse ) FreeMarkerWorker.getWrappedObject("response", env); 69 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 70 FreeMarkerWorker.getSiteParameters(request, templateRoot); 72 final Map savedValuesUp = new HashMap (); 73 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 74 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 75 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 77 List trail = (List )templateRoot.get( "globalNodeTrail"); 78 String contentAssocPredicateId = (String )templateRoot.get( "contentAssocPredicateId"); 80 String strNullThruDatesOnly = (String )templateRoot.get( "nullThruDatesOnly"); 81 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 82 String thisSubContentId = (String )templateRoot.get("subContentId"); 83 final boolean directAssocMode = UtilValidate.isNotEmpty(thisSubContentId) ? true : false; 85 GenericValue val = null; 87 try { 88 val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId); 89 } catch(GeneralException e) { 90 throw new RuntimeException ("Error getting current content. " + e.toString()); 91 } 92 final GenericValue view = val; 93 94 String dataResourceId = null; 95 String subContentIdSub = null; 96 if (view != null) { 97 try { 98 dataResourceId = (String ) view.get("drDataResourceId"); 99 } catch (Exception e) { 100 dataResourceId = (String ) view.get("dataResourceId"); 101 } 102 subContentIdSub = (String ) view.get("contentId"); 103 } 104 String subDataResourceTypeId = (String )templateRoot.get( "subDataResourceTypeId"); 106 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 107 try { 108 subDataResourceTypeId = (String ) view.get("drDataResourceTypeId"); 109 } catch (Exception e) { 110 } 112 } 116 String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateRoot); 117 templateRoot.put( "drDataResourceId", dataResourceId); 118 templateRoot.put( "mimeTypeId", mimeTypeId); 119 templateRoot.put( "dataResourceId", dataResourceId); 120 templateRoot.put( "subContentId", subContentIdSub); 121 templateRoot.put( "subDataResourceTypeId", subDataResourceTypeId); 122 123 126 return new Writer (out) { 127 128 public void write(char cbuf[], int off, int len) { 129 } 130 131 public void flush() throws IOException { 132 out.flush(); 133 } 134 135 public void close() throws IOException { 136 List globalNodeTrail = (List )templateRoot.get("globalNodeTrail"); 137 try { 139 renderSubContent(); 140 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 141 } catch (IOException e) { 143 throw new IOException (e.getMessage()); 144 } 145 } 146 147 public void renderSubContent() throws IOException { 148 149 Timestamp fromDate = UtilDateTime.nowTimestamp(); 151 List passedGlobalNodeTrail = (List )templateRoot.get("globalNodeTrail"); 152 String editRequestName = (String )templateRoot.get("editRequestName"); 153 GenericValue thisView = null; 155 if (view != null) { 156 thisView = view; 157 } else if (passedGlobalNodeTrail.size() > 0) { 158 Map map = (Map )passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1); 159 if (Debug.infoOn()) Debug.logInfo("in Render(3), map ." + map , module); 160 if (map != null) 161 thisView = (GenericValue)map.get("value"); 162 } 163 166 String mimeTypeId = (String ) templateRoot.get("mimeTypeId"); 167 Locale locale = (Locale ) templateRoot.get("locale"); 168 if (locale == null) 169 locale = Locale.getDefault(); 170 171 if (UtilValidate.isNotEmpty(editRequestName)) { 172 String editStyle = getEditStyle(); 173 openEditWrap(out, editStyle); 174 } 175 176 if (thisView != null) { 177 try { 178 ContentWorker.renderContentAsTextCache(delegator, null, out, templateRoot, thisView, locale, mimeTypeId); 179 } catch (GeneralException e) { 181 Debug.logError(e, "Error rendering content", module); 182 throw new IOException ("Error rendering thisView:" + thisView + " msg:" + e.toString()); 183 } 184 } 185 if (UtilValidate.isNotEmpty(editRequestName)) { 186 closeEditWrap(out, editRequestName); 187 } 188 189 return; 191 } 192 193 public void openEditWrap(Writer out, String editStyle) throws IOException { 194 String divStr = "<div class=\"" + editStyle + "\">"; 195 out.write(divStr); 196 } 197 198 public void closeEditWrap(Writer out, String editRequestName) throws IOException { 199 StringBuffer sb = new StringBuffer (); 202 String fullRequest = editRequestName; 203 String contentId = null; 204 String contentIdTo = null; 205 String contentAssocTypeId = null; 206 String mapKey = null; 207 String fromDate = null; 208 209 if (!directAssocMode) { 210 contentIdTo = (String )templateRoot.get("contentId"); 211 contentAssocTypeId = (String )templateRoot.get("contentAssocTypeId"); 212 mapKey = (String )templateRoot.get("mapKey"); 213 fromDate = (String )templateRoot.get("fromDate"); 214 if (Debug.infoOn()) Debug.logInfo("in Render(0), view ." + view , module); 215 if (view != null) { 216 ModelEntity modelEntity = view.getModelEntity(); 217 if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null ) 218 contentId = view.getString("caContentId"); 219 if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null ) 220 contentId = view.getString("contentId"); 221 if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null ) 222 contentIdTo = view.getString("caContentIdTo"); 223 if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null ) 224 contentIdTo = view.getString("contentIdTo"); 225 if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null ) 226 contentAssocTypeId = view.getString("caContentAssocTypeId"); 227 if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null ) 228 contentAssocTypeId = view.getString("contentAssocTypeId"); 229 if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null ) 230 mapKey = view.getString("caMapKey"); 231 if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null ) 232 mapKey = view.getString("mapKey"); 233 if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null ) 234 fromDate = view.getString("caFromDate"); 235 if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null ) 236 fromDate = view.getString("fromDate"); 237 } 238 } else { 239 contentId = (String )templateRoot.get("subContentId"); 240 } 241 if (Debug.infoOn()) Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo , module); 242 String delim = "?"; 243 if (UtilValidate.isNotEmpty(contentId)) { 244 fullRequest += delim + "contentId=" + contentId; 245 delim = "&"; 246 } 247 if (UtilValidate.isNotEmpty(contentIdTo)) { 248 fullRequest += delim + "contentIdTo=" + contentIdTo; 249 delim = "&"; 250 } 251 if (UtilValidate.isNotEmpty(contentAssocTypeId)) { 252 fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId; 253 delim = "&"; 254 } 255 if (UtilValidate.isNotEmpty(mapKey)) { 256 fullRequest += delim + "mapKey=" + mapKey; 257 delim = "&"; 258 } 259 if (UtilValidate.isNotEmpty(fromDate)) { 260 fullRequest += delim + "fromDate=" + fromDate; 261 delim = "&"; 262 } 263 264 if (Debug.infoOn()) Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo , module); 265 WidgetWorker.appendOfbizUrl(sb, fullRequest, request, response); 266 String url = sb.toString(); 267 String link = "<a HREF=\"" + url + "\">Edit</a>"; 268 out.write(link); 269 String divStr = "</div>"; 270 out.write(divStr); 271 } 272 273 public String getEditStyle() { 274 String editStyle = (String )templateRoot.get("editStyle"); 275 if (UtilValidate.isEmpty(editStyle)) { 276 editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle"); 277 } 278 if (UtilValidate.isEmpty(editStyle)) { 279 editStyle = "buttontext"; 280 } 281 return editStyle; 282 } 283 }; 284 285 } 286 287 } 288 | Popular Tags |