1 18 package org.ofbiz.content.compdoc; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilHttp; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilProperties; 39 import org.ofbiz.base.util.UtilValidate; 40 import org.ofbiz.base.util.UtilFormatOut; 41 import org.ofbiz.entity.GenericDelegator; 42 import org.ofbiz.entity.GenericEntityException; 43 import org.ofbiz.entity.GenericValue; 44 import org.ofbiz.entity.util.ByteWrapper; 45 import org.ofbiz.security.Security; 46 import org.ofbiz.service.GenericServiceException; 47 import org.ofbiz.service.LocalDispatcher; 48 import org.ofbiz.service.ModelService; 49 import org.ofbiz.service.ServiceAuthException; 50 import org.ofbiz.service.ServiceUtil; 51 import org.ofbiz.webapp.event.CoreEvents; 52 import org.ofbiz.service.GenericServiceException; 53 54 55 61 public class CompDocEvents { 62 public static final String module = CompDocEvents.class.getName(); 63 64 74 75 public static String persistRootCompDoc(HttpServletRequest request, HttpServletResponse response) { 76 Map paramMap = UtilHttp.getParameterMap(request); 77 GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator"); 78 LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher"); 79 Locale locale = UtilHttp.getLocale(request); 80 HttpSession session = request.getSession(); 81 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 83 String contentId = (String )paramMap.get("contentId"); 84 86 boolean contentExists = true; 87 if (UtilValidate.isEmpty(contentId)) { 88 contentExists = false; 89 } else { 90 try { 91 GenericValue val = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 92 if (val == null) contentExists = false; 93 } catch(GenericEntityException e) { 94 Debug.logError(e, "Error running serviceName persistContentAndAssoc", module); 95 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.error_modelservice_for_srv_name", locale); 96 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + "persistContentAndAssoc" + "]: " + e.toString()); 97 return "error"; 98 } 99 } 100 101 ModelService modelService = null; 102 try { 103 modelService = dispatcher.getDispatchContext().getModelService("persistContentAndAssoc"); 104 } catch (GenericServiceException e) { 105 String errMsg = "Error getting model service for serviceName, 'persistContentAndAssoc'. " + e.toString(); 106 Debug.logError(errMsg, module); 107 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>"); 108 return "error"; 109 } 110 Map persistMap = modelService.makeValid(paramMap, ModelService.IN_PARAM); 111 persistMap.put("userLogin", userLogin); 112 try { 113 Map persistResult = dispatcher.runSync("persistContentAndAssoc", persistMap); 114 contentId = (String )persistResult.get("contentId"); 115 Set keySet = persistResult.keySet(); 117 Iterator iter = keySet.iterator(); 118 while (iter.hasNext()) { 119 Object obj = iter.next(); 120 Object val = persistResult.get(obj); 121 request.setAttribute(obj.toString(), val); 122 } 123 Map contentRevisionMap = new HashMap (); 125 contentRevisionMap.put("itemContentId", contentId); 126 contentRevisionMap.put("contentId", contentId); 127 contentRevisionMap.put("userLogin", userLogin); 128 Map result = dispatcher.runSync("persistContentRevisionAndItem", contentRevisionMap); 129 keySet = result.keySet(); 130 iter = keySet.iterator(); 131 while (iter.hasNext()) { 132 Object obj = iter.next(); 133 Object val = persistResult.get(obj); 134 request.setAttribute(obj.toString(), val); 135 } 136 String errorMsg = ServiceUtil.getErrorMessage(result); 137 if (UtilValidate.isNotEmpty(errorMsg)) { 138 String errMsg = "Error running serviceName, 'persistContentRevisionAndItem'. " + errorMsg; 139 Debug.logError(errMsg, module); 140 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>"); 141 return "error"; 142 } 143 144 } catch(GenericServiceException e) { 145 String errMsg = "Error running serviceName, 'persistContentAndAssoc'. " + e.toString(); 146 Debug.logError(errMsg, module); 147 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>"); 148 return "error"; 149 } 150 return "success"; 151 } 152 153 public static String padNumberWithLeadingZeros(Long num, Integer padLen) { 154 String s = UtilFormatOut.formatPaddedNumber(num.longValue(), padLen.intValue()); 155 return s; 156 } 157 158 public static String genCompDocPdf(HttpServletRequest request, HttpServletResponse response) { 159 String responseStr = "success"; 160 HttpSession session = request.getSession(); 162 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 163 ServletContext servletContext = session.getServletContext(); 164 LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher"); 165 Map paramMap = UtilHttp.getParameterMap(request); 166 String contentId = (String )paramMap.get("contentId"); 167 Locale locale = UtilHttp.getLocale(request); 168 String rootDir = null; 169 String webSiteId = null; 170 String https = null; 171 172 if (UtilValidate.isEmpty(rootDir)) { 173 rootDir = servletContext.getRealPath("/"); 174 } 175 if (UtilValidate.isEmpty(webSiteId)) { 176 webSiteId = (String ) servletContext.getAttribute("webSiteId"); 177 } 178 if (UtilValidate.isEmpty(https)) { 179 https = (String ) servletContext.getAttribute("https"); 180 } 181 182 Map mapIn = new HashMap (); 183 mapIn.put("contentId", contentId); 184 mapIn.put("locale", locale); 185 mapIn.put("rootDir", rootDir); 186 mapIn.put("webSiteId", webSiteId); 187 mapIn.put("https", https); 188 mapIn.put("userLogin", userLogin); 189 190 Map results = null; 191 try { 192 results = dispatcher.runSync("renderCompDocPdf", mapIn); 193 } catch(ServiceAuthException e) { 194 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 195 return "error"; 196 } catch(GenericServiceException e) { 197 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 198 return "error"; 199 } catch(Exception e) { 200 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 201 return "error"; 202 } 203 204 if (ServiceUtil.isError(results)) { 205 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(results)); 206 return "error"; 207 } 208 209 ByteWrapper outByteWrapper = (ByteWrapper)results.get("outByteWrapper"); 210 211 String contentType = "application/pdf; charset=ISO-8859-1"; 213 214 ByteArrayInputStream bais = new ByteArrayInputStream (outByteWrapper.getBytes()); 215 216 224 try { 225 UtilHttp.streamContentToBrowser(response, bais, outByteWrapper.getLength(), contentType); 226 } catch(IOException e) { 227 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 228 return "error"; 229 } 230 return responseStr; 231 } 232 public static String genContentPdf(HttpServletRequest request, HttpServletResponse response) { 233 String responseStr = "success"; 234 HttpSession session = request.getSession(); 236 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 237 ServletContext servletContext = session.getServletContext(); 238 LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher"); 239 Map paramMap = UtilHttp.getParameterMap(request); 240 String contentId = (String )paramMap.get("contentId"); 241 Locale locale = UtilHttp.getLocale(request); 242 String rootDir = null; 243 String webSiteId = null; 244 String https = null; 245 246 if (UtilValidate.isEmpty(rootDir)) { 247 rootDir = servletContext.getRealPath("/"); 248 } 249 if (UtilValidate.isEmpty(webSiteId)) { 250 webSiteId = (String ) servletContext.getAttribute("webSiteId"); 251 } 252 if (UtilValidate.isEmpty(https)) { 253 https = (String ) servletContext.getAttribute("https"); 254 } 255 256 Map mapIn = new HashMap (); 257 mapIn.put("contentId", contentId); 258 mapIn.put("locale", locale); 259 mapIn.put("rootDir", rootDir); 260 mapIn.put("webSiteId", webSiteId); 261 mapIn.put("https", https); 262 mapIn.put("userLogin", userLogin); 263 264 Map results = null; 265 try { 266 results = dispatcher.runSync("renderContentPdf", mapIn); 267 } catch(ServiceAuthException e) { 268 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 269 return "error"; 270 } catch(GenericServiceException e) { 271 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 272 return "error"; 273 } catch(Exception e) { 274 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 275 return "error"; 276 } 277 278 if (ServiceUtil.isError(results)) { 279 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(results)); 280 return "error"; 281 } 282 283 ByteWrapper outByteWrapper = (ByteWrapper)results.get("outByteWrapper"); 284 285 String contentType = "application/pdf; charset=ISO-8859-1"; 287 288 ByteArrayInputStream bais = new ByteArrayInputStream (outByteWrapper.getBytes()); 289 290 299 try { 300 UtilHttp.streamContentToBrowser(response, bais, outByteWrapper.getLength(), contentType); 301 } catch(IOException e) { 302 request.setAttribute("_ERROR_MESSAGE_", e.toString()); 303 return "error"; 304 } 305 return responseStr; 306 } 307 } 308 | Popular Tags |