diff --git a/applications/order/servicedef/services.xml b/applications/order/servicedef/services.xml index 6b0be14962..438935b9ff 100644 --- a/applications/order/servicedef/services.xml +++ b/applications/order/servicedef/services.xml @@ -34,6 +34,7 @@ under the License. + diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java b/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java index 96270a5f05..45a86a3a3d 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/order/OrderServices.java @@ -83,6 +83,9 @@ import org.apache.ofbiz.service.ServiceUtil; import com.ibm.icu.util.Calendar; +import org.apache.commons.lang.StringUtils; +import org.apache.fop.apps.MimeConstants; + /** * Order Processing Services */ @@ -2533,8 +2536,10 @@ public class OrderServices { GenericValue userLogin = (GenericValue) context.get("userLogin"); String orderId = (String) context.get("orderId"); String orderItemSeqId = (String) context.get("orderItemSeqId"); + String shipGroupSeqId = (String) context.get("shipGroupSeqId"); String sendTo = (String) context.get("sendTo"); String sendCc = (String) context.get("sendCc"); + String sendBcc = (String) context.get("sendBcc"); String note = (String) context.get("note"); String screenUri = (String) context.get("screenUri"); GenericValue temporaryAnonymousUserLogin = (GenericValue) context.get("temporaryAnonymousUserLogin"); @@ -2589,10 +2594,17 @@ public class OrderServices { String xslfoAttachScreenLocation = productStoreEmail.getString("xslfoAttachScreenLocation"); sendMap.put("xslfoAttachScreenLocation", xslfoAttachScreenLocation); // add attachmentName param to get an attachment namend "[oderId].pdf" instead of default "Details.pdf" - sendMap.put("attachmentName", orderId + ".pdf"); + sendMap.put("attachmentName", (UtilValidate.isNotEmpty(shipGroupSeqId) ? orderId + "-" + StringUtils.stripStart(shipGroupSeqId, "0") : orderId) + ".pdf"); + sendMap.put("attachmentType", MimeConstants.MIME_PDF); } else { sendMap.put("bodyScreenUri", screenUri); } + + if (context.containsKey("xslfoAttachScreenLocationList")) { + sendMap.put("xslfoAttachScreenLocationList", context.get("xslfoAttachScreenLocationList")); + sendMap.put("attachmentNameList", context.get("attachmentNameList")); + sendMap.put("attachmentTypeList", context.get("attachmentTypeList")); + } // website sendMap.put("webSiteId", orderHeader.get("webSiteId")); @@ -2636,6 +2648,7 @@ public class OrderServices { bodyParameters.put("partyId", placingParty.get("partyId")); } bodyParameters.put("note", note); + bodyParameters.put("shipGroupSeqId", shipGroupSeqId); sendMap.put("bodyParameters", bodyParameters); sendMap.put("userLogin",userLogin); @@ -2657,6 +2670,10 @@ public class OrderServices { sendMap.put("sendCc", productStoreEmail.get("ccAddress")); } + if ((sendBcc != null) && UtilValidate.isEmailList(sendBcc)) { + sendMap.put("sendBcc", sendBcc); + } + // send the notification Map sendResp = null; try { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java index e48cdeea03..dd1a0b59ca 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilValidate.java @@ -691,6 +691,20 @@ public final class UtilValidate { if (isEmpty(s)) return defaultEmptyOK; return EmailValidator.getInstance().isValid(s); } + + /** + * Checks a String for a valid Email-List seperated by ",". + */ + public static boolean isEmailList(String s) { + if (isEmpty(s)) return defaultEmptyOK; + String[] emails = s.split(","); + for (String email : emails) { + if (!EmailValidator.getInstance().isValid(email)) { + return false; + } + } + return true; + } /** isUrl returns true if the string contains :// * @param s String to validate diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java index 3c609a7066..4b15f5f090 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/email/EmailServices.java @@ -51,6 +51,7 @@ import javax.mail.internet.MimeMultipart; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.stream.StreamSource; +import org.apache.fop.apps.FOPException; import org.apache.fop.apps.Fop; import org.apache.fop.apps.MimeConstants; import org.apache.ofbiz.base.util.Debug; @@ -438,6 +439,12 @@ public class EmailServices { if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationListParam)) xslfoAttachScreenLocationList.addAll(xslfoAttachScreenLocationListParam); if (UtilValidate.isNotEmpty(attachmentNameListParam)) attachmentNameList.addAll(attachmentNameListParam); + List attachmentTypeList = new LinkedList(); + String attachmentTypeParam = (String) serviceContext.remove("attachmentType"); + List attachmentTypeListParam = UtilGenerics.checkList(serviceContext.remove("attachmentTypeList")); + if (UtilValidate.isNotEmpty(attachmentTypeParam)) attachmentTypeList.add(attachmentTypeParam); + if (UtilValidate.isNotEmpty(attachmentTypeListParam)) attachmentTypeList.addAll(attachmentTypeListParam); + Locale locale = (Locale) serviceContext.get("locale"); Map bodyParameters = UtilGenerics.checkMap(serviceContext.remove("bodyParameters")); if (bodyParameters == null) { @@ -504,9 +511,9 @@ public class EmailServices { List> bodyParts = new LinkedList>(); if (bodyText != null) { bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale); - bodyParts.add(UtilMisc.toMap("content", bodyText, "type", "text/html")); + bodyParts.add(UtilMisc.toMap("content", bodyText, "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html")); } else { - bodyParts.add(UtilMisc.toMap("content", bodyWriter.toString(), "type", "text/html")); + bodyParts.add(UtilMisc.toMap("content", bodyWriter.toString(), "type", UtilValidate.isNotEmpty(contentType) ? contentType : "text/html")); } for (int i = 0; i < xslfoAttachScreenLocationList.size(); i++) { @@ -515,39 +522,51 @@ public class EmailServices { if (UtilValidate.isNotEmpty(attachmentNameList) && attachmentNameList.size() >= i) { attachmentName = attachmentNameList.get(i); } + + String attachmentType = MimeConstants.MIME_PDF; + if (UtilValidate.isNotEmpty(attachmentTypeList) && attachmentTypeList.size() >= i) { + attachmentType = attachmentTypeList.get(i); + } + isMultiPart = true; // start processing fo pdf attachment try { Writer writer = new StringWriter(); - MapStack screenContextAtt = MapStack.create(); // substitute the freemarker variables... - ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", dctx.getDelegator()), - EntityUtilProperties.getPropertyValue("widget", "screenfop.screenrenderer", dctx.getDelegator())); + ScreenStringRenderer foScreenStringRenderer = null; + if(MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)){ + foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screentext.name", dctx.getDelegator()), + EntityUtilProperties.getPropertyValue("widget", "screentext.screenrenderer", dctx.getDelegator())); + }else{ + foScreenStringRenderer = new MacroScreenRenderer(EntityUtilProperties.getPropertyValue("widget", "screenfop.name", dctx.getDelegator()), + EntityUtilProperties.getPropertyValue("widget", "screenfop.screenrenderer", dctx.getDelegator())); + } ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContext, foScreenStringRenderer); screensAtt.populateContextForService(dctx, bodyParameters); - screenContextAtt.putAll(bodyParameters); screensAtt.render(xslfoAttachScreenLocation); - // create the input stream for the generation - StreamSource src = new StreamSource(new StringReader(writer.toString())); - // create the output stream for the generation ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF); - ApacheFopWorker.transform(src, null, fop); + if (MimeConstants.MIME_PLAIN_TEXT.equals(attachmentType)) { + baos.write(writer.toString().getBytes()); + } else { + // create the input stream for the generation + StreamSource src = new StreamSource(new StringReader(writer.toString())); + Fop fop = ApacheFopWorker.createFopInstance(baos, attachmentType); + ApacheFopWorker.transform(src, null, fop); + } - // and generate the PDF + // and generate the attachment baos.flush(); baos.close(); // store in the list of maps for sendmail.... - bodyParts.add(UtilMisc. toMap("content", baos.toByteArray(), "type", "application/pdf", "filename", - attachmentName)); - } catch (Exception e) { - Debug.logError(e, "Error rendering PDF attachment for email: " + e.toString(), module); - return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenPdfError", - UtilMisc.toMap("errorString", e.toString()), locale)); + bodyParts.add(UtilMisc.toMap("content", baos.toByteArray(), "type", attachmentType, "filename", attachmentName)); + + } catch (GeneralException|IOException|SAXException|ParserConfigurationException |TemplateException ge) { + Debug.logError(ge, "Error rendering PDF attachment for email: " + ge.toString(), module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonEmailSendRenderingScreenPdfError", UtilMisc.toMap("errorString", ge.toString()), locale)); } serviceContext.put("bodyParts", bodyParts);