1 17 package org.ofbiz.content.webapp.ftl; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.ofbiz.base.util.Debug; 27 import org.ofbiz.base.util.UtilValidate; 28 import org.ofbiz.base.util.template.FreeMarkerWorker; 29 import org.ofbiz.content.content.ContentWorker; 30 import org.ofbiz.entity.GenericDelegator; 31 import org.ofbiz.entity.GenericEntityException; 32 import org.ofbiz.entity.GenericValue; 33 import org.ofbiz.webapp.ftl.LoopWriter; 34 35 import freemarker.core.Environment; 36 import freemarker.template.TemplateModelException; 37 import freemarker.template.TemplateTransformModel; 38 import freemarker.template.TransformControl; 39 40 47 public class InjectNodeTrailCsvTransform implements TemplateTransformModel { 48 49 public static final String module = InjectNodeTrailCsvTransform.class.getName(); 50 51 public static final String [] saveKeyNames = {"nodeTrailCsv","globalNodeTrail", "nodeTrail"}; 52 public static final String [] removeKeyNames = {"nodeTrailCsv"}; 53 54 57 public static Object getWrappedObject(String varName, Environment env) { 58 return FreeMarkerWorker.getWrappedObject(varName, env); 59 } 60 61 public static String getArg(Map args, String key, Environment env) { 62 return FreeMarkerWorker.getArg(args, key, env); 63 } 64 65 public static String getArg(Map args, String key, Map ctx) { 66 return FreeMarkerWorker.getArg(args, key, ctx); 67 } 68 69 70 public Writer getWriter(final Writer out, Map args) { 71 final StringBuffer buf = new StringBuffer (); 72 final Environment env = Environment.getCurrentEnvironment(); 73 final Map templateCtx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 74 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 76 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 77 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 78 FreeMarkerWorker.getSiteParameters(request, templateCtx); 79 FreeMarkerWorker.overrideWithArgs(templateCtx, args); 80 81 return new LoopWriter(out) { 82 83 final String passedCsv = (String )templateCtx.get("nodeTrailCsv"); 84 85 public void write(char cbuf[], int off, int len) { 86 buf.append(cbuf, off, len); 87 } 88 89 public void flush() throws IOException { 90 out.flush(); 91 } 92 93 public int onStart() throws TemplateModelException, IOException { 94 String csvTrail = null; 95 96 List trail = (List )templateCtx.get("globalNodeTrail"); 97 98 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), trail:"+trail,module); 99 String redo = (String )templateCtx.get("redo"); 103 104 if (trail == null || trail.size() == 0 || (redo != null && redo.equalsIgnoreCase("true"))) { 105 String thisContentId = null; 106 String subContentId = (String )templateCtx.get("subContentId"); 107 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), subContentId:"+subContentId,module); 108 String contentId = (String )templateCtx.get("contentId"); 109 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), contentId:"+contentId,module); 110 String contentAssocTypeId = (String )templateCtx.get("contentAssocTypeId"); 111 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), contentAssocTypeId:"+contentAssocTypeId,module); 112 try { 113 if (UtilValidate.isNotEmpty(subContentId)) { 114 csvTrail = ContentWorker.getContentAncestryNodeTrailCsv(delegator, subContentId, contentAssocTypeId, "to"); 115 if (UtilValidate.isNotEmpty(csvTrail)) 116 csvTrail += ","; 117 csvTrail += subContentId; 118 } else if (UtilValidate.isNotEmpty(contentId)) { 119 csvTrail = ContentWorker.getContentAncestryNodeTrailCsv(delegator, contentId, contentAssocTypeId, "to"); 120 if (UtilValidate.isNotEmpty(csvTrail)) 121 csvTrail += ","; 122 csvTrail += contentId; 123 } 124 } catch (GenericEntityException e) { 125 throw new RuntimeException ("Error getting current content. " + e.toString()); 126 } 127 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), csvTrail:"+csvTrail,module); 128 } else { 129 if (UtilValidate.isNotEmpty(passedCsv)) { 133 csvTrail = passedCsv; 134 int lastComma = passedCsv.lastIndexOf(","); 135 String lastPassedContentId = null; 136 if (lastComma >= 0) { 137 lastPassedContentId = passedCsv.substring(lastComma + 1); 138 } else { 139 lastPassedContentId = passedCsv; 140 } 141 142 if (UtilValidate.isNotEmpty(lastPassedContentId)) { 143 if (trail != null && trail.size() > 0) { 144 Map nd = (Map )trail.get(0); 145 String firstTrailContentId = (String )nd.get("contentId"); 146 if (UtilValidate.isNotEmpty(firstTrailContentId) 147 && UtilValidate.isNotEmpty(lastPassedContentId) 148 && firstTrailContentId.equals(lastPassedContentId) ) { 149 csvTrail += "," + ContentWorker.nodeTrailToCsv(trail.subList(1, trail.size())); 150 } else { 151 csvTrail += "," + ContentWorker.nodeTrailToCsv(trail); 152 } 153 } 154 } 155 } else { 156 csvTrail = ContentWorker.nodeTrailToCsv(trail); 157 } 158 } 159 templateCtx.put("nodeTrailCsv", csvTrail); 161 return TransformControl.EVALUATE_BODY; 162 } 163 164 165 public void close() throws IOException { 166 templateCtx.put("nodeTrailCsv", passedCsv); 167 String wrappedContent = buf.toString(); 168 out.write(wrappedContent); 169 } 170 }; 171 } 172 } 173 | Popular Tags |