📄 contentservices.java
字号:
Object value = entry.getValue(); mapOut.put(key, value); } } } return result; } public static Map splitString(DispatchContext dctx, Map context) throws GenericServiceException{ Map result = new HashMap(); List outputList = new ArrayList(); String delimiter = UtilFormatOut.checkEmpty((String)context.get("delimiter"), "|"); String inputString = (String)context.get("inputString"); if (UtilValidate.isNotEmpty(inputString)) { outputList = StringUtil.split(inputString, delimiter); } result.put("outputList", outputList); return result; } public static Map joinString(DispatchContext dctx, Map context) throws GenericServiceException{ Map result = new HashMap(); String outputString = null; String delimiter = UtilFormatOut.checkEmpty((String)context.get("delimiter"), "|"); List inputList = (List)context.get("inputList"); if (inputList != null) { outputString = StringUtil.join(inputList, delimiter); } result.put("outputString", outputString); return result; } public static Map urlEncodeArgs(DispatchContext dctx, Map context) throws GenericServiceException{ Map result = new HashMap(); Map mapFiltered = new HashMap(); Map mapIn = (Map)context.get("mapIn"); if (mapIn != null) { Set entrySet = mapIn.entrySet(); Iterator iter = entrySet.iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry)iter.next(); String key = (String)entry.getKey(); Object value = entry.getValue(); if (value instanceof String ) { if (UtilValidate.isNotEmpty((String)value)) { mapFiltered.put(key, value); } } else if (value != null) { mapFiltered.put(key, value); } } String outputString = UtilHttp.urlEncodeArgs(mapFiltered); result.put("outputString", outputString ); } return result; } /** * foFileIn - path to FO template, can be null if template is in the CMS * Normally, this parameter will only be used once to get the template * into the CMS, as this option will create a new entry in CMS each time. * inputDataResourceTypeId - can be LOCAL_FILE or OFBIZ_FILE. Used if foFileIn is not null. * pdfFileOut - path to where output PDF will be stored.Can be null if either PDF will be * stored as ELECTRONIC_TEXT or not stored at all, but returned as pdfByteWrapper * outputDataResourceTypeId - can be LOCAL_FILE, OFBIZ_FILE or ELECTRONIC_TEXT. * outputContentId - The CMS id of PDF output Content entity. Type is INOUT, but can be null, * in which case it will be generated. * foContentId - The CMS id of FO template Content entity. Type is INOUT, but can be null, * in which case it will be generated. * If not null, then foFileIn will be ignored. * There is no current way to specify what you want the generated FO template ID to be. * This is the preferred way to use the template - it should be in the CMS, * else it will be created each time. * fmContext - the Map that will be used in processing the FO template. * fmPrefixMap - If fmContext is null, this Map will be used. It is generated by using the * prefix, "CTX_" on form parameters. Allows this service to be used as * in request event handler. * @param dctx * @param context * @return * @throws GenericServiceException */ public static Map foToPdf(DispatchContext dctx, Map context) throws GenericServiceException{ LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue)context.get("userLogin"); Map result = new HashMap(); String foFileIn = (String)context.get("foFileIn"); String pdfFileOut = (String)context.get("pdfFileOut"); String outputDataResourceTypeId = (String)context.get("outputDataResourceTypeId"); String inputDataResourceTypeId = (String)context.get("inputDataResourceTypeId"); if (UtilValidate.isEmpty(inputDataResourceTypeId)) { inputDataResourceTypeId = "LOCAL_FILE"; } String outputContentId = (String)context.get("outputContentId"); String foContentId = (String)context.get("foContentId"); String templateDataResourceId = (String)context.get("templateDataResourceId"); Map fmContext = (Map)context.get("fmContext"); Map fmPrefixMap = (Map)context.get("fmPrefixMap"); // configure logging for the FOP Logger logger = new Log4JLogger(Debug.getLogger(module)); MessageHandler.setScreenLogger(logger); // Get input FO. Process thru template, if required. String processedFo = null; // If the FO template is not already in the CMS, put it there, // else, if the FTL template id is not there in the exisiting content, add it. if (UtilValidate.isEmpty(foContentId)) { if (UtilValidate.isEmpty(foFileIn)) { return ServiceUtil.returnError("No FO file or contentId available."); } Map mapIn = new HashMap(); mapIn.put("drObjectInfo", foFileIn); mapIn.put("drDataResourceTypeId", inputDataResourceTypeId); mapIn.put("contentTypeId", "DOCUMENT"); //mapIn.put("contentPurposeString", "SOURCE"); mapIn.put("templateDataResourceId", templateDataResourceId); mapIn.put("drDataTemplateTypeId", "FTL"); mapIn.put("userLogin", userLogin); try { Map thisResult = dispatcher.runSync("persistContentAndAssoc", mapIn); foContentId = (String)thisResult.get("contentId"); if (UtilValidate.isEmpty(foContentId)) { Debug.logError("Could not add FO content - foContentId is null.", "ContentServices"); return ServiceUtil.returnError("Could not add FO conten - foContentId is null."); } result.put("foContentId", foContentId); } catch (GenericServiceException e) { Debug.logError(e, "Problem adding FO content.", "ContentServices"); return ServiceUtil.returnError("Problem adding FO content."); } } else { if (UtilValidate.isNotEmpty(templateDataResourceId)) { try { GenericDelegator delegator = dctx.getDelegator(); GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", foContentId)); String thisTemplateDataResourceId = content.getString("templateDataResourceId"); if (thisTemplateDataResourceId == null || !thisTemplateDataResourceId.equals(templateDataResourceId)) { content.put("templateDataResourceId", templateDataResourceId); content.store(); } } catch(GenericEntityException e) { Debug.logError(e, "ContentServices"); return ServiceUtil.returnError(e.getMessage()); } } } // Now that the FO file is in the CMS and the FTL template Id is in place // Get the processed FO file by running "renderContentAsText" file Map mapIn = new HashMap(); mapIn.put("contentId", foContentId); if (fmContext != null) { mapIn.put("templateContext", fmContext); } else { mapIn.put("templateContext", fmPrefixMap); } StringWriter sw = new StringWriter(); mapIn.put("outWriter", sw); try { Map thisResult = dispatcher.runSync("renderContentAsText", mapIn); processedFo = (String)thisResult.get("textData"); if (UtilValidate.isEmpty(processedFo)) { Debug.logError("Could not get FO text", "ContentServices"); return ServiceUtil.returnError("Could not get FO text"); } } catch (GenericServiceException e) { Debug.logError(e, "Problem getting FO text", "ContentServices"); return ServiceUtil.returnError("Problem getting FO text"); } // load the FOP driver Driver driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); driver.setLogger(logger); // get the XSL-FO XML in Document format Document xslfo = null; try { xslfo = UtilXml.readXmlDocument(processedFo); } catch (FileNotFoundException e) { return ServiceUtil.returnError("Error getting FO file: " + e.toString()); } catch (IOException e2) { return ServiceUtil.returnError("Error getting FO file: " + e2.toString()); } catch (ParserConfigurationException e3) { return ServiceUtil.returnError("Error getting FO file: " + e3.toString()); } catch (SAXException e4) { return ServiceUtil.returnError("Error getting FO file: " + e4.toString()); } // create the output stream for the PDF ByteArrayOutputStream out = new ByteArrayOutputStream(); driver.setOutputStream(out); // set the input source (XSL-FO) and generate the PDF InputSource is = new DocumentInputSource(xslfo); driver.setInputSource(is); try { driver.run(); FopImageFactory.resetCache(); } catch (Throwable t) { Debug.logError("Error processing PDF." + t.getMessage(), "ContentServices"); return ServiceUtil.returnError("Error processing PDF." + t.getMessage()); } ByteWrapper pdfByteWrapper = new ByteWrapper(out.toByteArray()); result.put("pdfByteWrapper", pdfByteWrapper ); // Put output into CMS if dataResourceTypeId is present // else, just write it to a file if (UtilValidate.isNotEmpty(outputDataResourceTypeId)) { if (pdfByteWrapper != null) { Map mapIn2 = new HashMap(); mapIn2.put("contentId", outputContentId); mapIn2.put("drDataResourceTypeId", outputDataResourceTypeId); mapIn2.put("contentTypeId", "DOCUMENT"); mapIn2.put("imageData", pdfByteWrapper); mapIn2.put("_imageData_contentType", "application/pdf"); mapIn2.put("_imageData_fileName", pdfFileOut); mapIn2.put("drObjectInfo", pdfFileOut); mapIn2.put("userLogin", userLogin); try { Map thisResult = dispatcher.runSync("persistContentAndAssoc", mapIn2); outputContentId = (String)thisResult.get("contentId"); if (UtilValidate.isEmpty(foContentId)) { Debug.logError("Could not add PDF content - contentId is null.", "ContentServices"); return ServiceUtil.returnError("Could not add PDF conten - contentId is null."); } result.put("outputContentId", outputContentId); } catch (GenericServiceException e) { Debug.logError(e, "Problem adding FO content.", module); return ServiceUtil.returnError("Problem adding FO content."); } result.put("outputContentId", outputContentId); } } else { if (UtilValidate.isEmpty(pdfFileOut)) { String outputPath = null; String thisDataResourceTypeId = null; String ofbizHome = System.getProperty("ofbiz.home"); int pos = pdfFileOut.indexOf("${OFBIZ_HOME}"); if (pos > 0 ) { outputPath = pdfFileOut.substring(pos + 13); thisDataResourceTypeId = "OFBIZ_FILE"; } else { outputPath = pdfFileOut; thisDataResourceTypeId = "LOCAL_FILE"; } Map mapIn3 = new HashMap(); mapIn3.put("objectInfo", outputPath); mapIn3.put("drDataResourceTypeId", thisDataResourceTypeId); mapIn3.put("binData", pdfByteWrapper); try { Map thisResult = dispatcher.runSync("createFile", mapIn3); } catch (GenericServiceException e) { Debug.logError(e, "Problem writing FO content.", module); return ServiceUtil.returnError("Problem adding FO content."); } } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -