PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java

https://github.com/thanhvc/ofbiz
Java | 255 lines | 201 code | 21 blank | 33 comment | 31 complexity | c7f7e18677babf4301c2a683f12a23f0 MD5 | raw file
  1. /*******************************************************************************
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *******************************************************************************/
  19. package org.ofbiz.birt.email;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.StringWriter;
  24. import java.sql.SQLException;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import javax.xml.parsers.ParserConfigurationException;
  30. import javolution.util.FastList;
  31. import javolution.util.FastMap;
  32. import org.apache.fop.apps.FOPException;
  33. import org.eclipse.birt.report.engine.api.EngineException;
  34. import org.eclipse.birt.report.engine.api.IReportEngine;
  35. import org.eclipse.birt.report.engine.api.IReportRunnable;
  36. import org.ofbiz.base.util.Debug;
  37. import org.ofbiz.base.util.GeneralException;
  38. import org.ofbiz.base.util.UtilGenerics;
  39. import org.ofbiz.base.util.UtilMisc;
  40. import org.ofbiz.base.util.UtilValidate;
  41. import org.ofbiz.base.util.collections.MapStack;
  42. import org.ofbiz.base.util.string.FlexibleStringExpander;
  43. import org.ofbiz.birt.BirtWorker;
  44. import org.ofbiz.birt.container.BirtContainer;
  45. import org.ofbiz.common.email.NotificationServices;
  46. import org.ofbiz.entity.Delegator;
  47. import org.ofbiz.security.Security;
  48. import org.ofbiz.service.DispatchContext;
  49. import org.ofbiz.service.LocalDispatcher;
  50. import org.ofbiz.service.ServiceUtil;
  51. import org.ofbiz.birt.widget.BirtFactory;
  52. import org.ofbiz.widget.html.HtmlScreenRenderer;
  53. import org.ofbiz.widget.screen.ScreenRenderer;
  54. import org.xml.sax.SAXException;
  55. public class BirtEmailServices {
  56. public static final String module = BirtEmailServices.class.getName();
  57. protected static final HtmlScreenRenderer htmlScreenRenderer = new HtmlScreenRenderer();
  58. /**
  59. * send birt mail
  60. *
  61. * @param ctx the dispatch context
  62. * @param context the context
  63. * @return returns the result of the service execution
  64. */
  65. public static Map<String, Object> sendBirtMail(DispatchContext ctx, Map<String, ? extends Object> context) {
  66. Map<String, Object> serviceContext = UtilMisc.makeMapWritable(context);
  67. Delegator delegator = ctx.getDelegator();
  68. LocalDispatcher dispatcher = ctx.getDispatcher();
  69. Security security = ctx.getSecurity();
  70. String webSiteId = (String) serviceContext.remove("webSiteId");
  71. String bodyText = (String) serviceContext.remove("bodyText");
  72. String bodyScreenUri = (String) serviceContext.remove("bodyScreenUri");
  73. String birtReportLocation = (String) serviceContext.remove("birtReportLocation");
  74. String attachmentName = (String) serviceContext.remove("attachmentName");
  75. Locale locale = (Locale) serviceContext.get("locale");
  76. Map<String, Object> bodyParameters = UtilGenerics.cast(serviceContext.remove("bodyParameters"));
  77. Locale birtLocale = (Locale) serviceContext.remove(BirtWorker.BIRT_LOCALE);
  78. Map<String, Object> birtParameters = UtilGenerics.cast(serviceContext.remove(BirtWorker.BIRT_PARAMETERS));
  79. String birtImageDirectory = (String) serviceContext.remove(BirtWorker.BIRT_IMAGE_DIRECTORY);
  80. String birtContentType = (String) serviceContext.remove(BirtWorker.BIRT_CONTENT_TYPE);
  81. if (bodyParameters == null) {
  82. bodyParameters = MapStack.create();
  83. }
  84. if (!bodyParameters.containsKey("locale")) {
  85. bodyParameters.put("locale", locale);
  86. } else {
  87. locale = (Locale) bodyParameters.get("locale");
  88. }
  89. String partyId = (String) bodyParameters.get("partyId");
  90. if (UtilValidate.isNotEmpty(webSiteId)) {
  91. NotificationServices.setBaseUrl(ctx.getDelegator(), webSiteId, bodyParameters);
  92. }
  93. String contentType = (String) serviceContext.remove("contentType");
  94. if (UtilValidate.isEmpty(attachmentName)) {
  95. attachmentName = "Details.pdf";
  96. }
  97. StringWriter bodyWriter = new StringWriter();
  98. MapStack<String> screenContext = MapStack.create();
  99. screenContext.put("locale", locale);
  100. ScreenRenderer screens = new ScreenRenderer(bodyWriter, screenContext, htmlScreenRenderer);
  101. screens.populateContextForService(ctx, bodyParameters);
  102. screenContext.putAll(bodyParameters);
  103. if (bodyScreenUri != null) {
  104. try {
  105. screens.render(bodyScreenUri);
  106. } catch (GeneralException e) {
  107. String errMsg = "Error rendering screen for email: " + e.toString();
  108. Debug.logError(e, errMsg, module);
  109. return ServiceUtil.returnError(errMsg);
  110. } catch (IOException e) {
  111. String errMsg = "Error I/O rendering screen for email: " + e.toString();
  112. Debug.logError(e, errMsg, module);
  113. return ServiceUtil.returnError(errMsg);
  114. } catch (SAXException e) {
  115. String errMsg = "Error SAX rendering screen for email: " + e.toString();
  116. Debug.logError(e, errMsg, module);
  117. return ServiceUtil.returnError(errMsg);
  118. } catch (ParserConfigurationException e) {
  119. String errMsg = "Error parser config rendering screen for email: " + e.toString();
  120. Debug.logError(e, errMsg, module);
  121. return ServiceUtil.returnError(errMsg);
  122. }
  123. }
  124. boolean isMultiPart = false;
  125. // check if attachment screen location passed in
  126. if (UtilValidate.isNotEmpty(birtReportLocation)) {
  127. isMultiPart = true;
  128. // start processing fo pdf attachment
  129. try {
  130. // create the output stream for the generation
  131. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  132. Map<String, Object> birtContext = FastMap.newInstance();
  133. if (birtLocale == null) {
  134. birtLocale = locale;
  135. }
  136. birtContext.put(BirtWorker.BIRT_LOCALE, birtLocale);
  137. if (birtParameters != null) {
  138. birtContext.put(BirtWorker.BIRT_PARAMETERS, birtParameters);
  139. }
  140. if (birtImageDirectory != null) {
  141. birtContext.put(BirtWorker.BIRT_IMAGE_DIRECTORY, birtImageDirectory);
  142. }
  143. if (birtContentType == null) {
  144. birtContentType = "application/pdf";
  145. }
  146. IReportEngine engine = BirtContainer.getReportEngine();
  147. HashMap<String, Object> appContext = UtilGenerics.cast(engine.getConfig().getAppContext());
  148. appContext.put("delegator", delegator);
  149. appContext.put("dispatcher", dispatcher);
  150. appContext.put("security", security);
  151. InputStream reportInputStream = BirtFactory.getReportInputStreamFromLocation(birtReportLocation);
  152. IReportRunnable design = engine.openReportDesign(reportInputStream);
  153. Debug.logInfo("Export report as content type:" + birtContentType, module);
  154. BirtWorker.exportReport(design, context, birtContentType, baos);
  155. baos.flush();
  156. baos.close();
  157. // store in the list of maps for sendmail....
  158. List<Map<String, ? extends Object>> bodyParts = FastList.newInstance();
  159. if (bodyText != null) {
  160. bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale);
  161. bodyParts.add(UtilMisc.toMap("content", bodyText, "type", "text/html"));
  162. } else {
  163. bodyParts.add(UtilMisc.toMap("content", bodyWriter.toString(), "type", "text/html"));
  164. }
  165. bodyParts.add(UtilMisc.toMap("content", baos.toByteArray(), "type", "application/pdf", "filename", attachmentName));
  166. serviceContext.put("bodyParts", bodyParts);
  167. } catch (GeneralException ge) {
  168. String errMsg = "Error rendering " + birtContentType + " attachment for email: " + ge.toString();
  169. Debug.logError(ge, errMsg, module);
  170. return ServiceUtil.returnError(errMsg);
  171. } catch (IOException ie) {
  172. String errMsg = "Error I/O rendering " + birtContentType + " attachment for email: " + ie.toString();
  173. Debug.logError(ie, errMsg, module);
  174. return ServiceUtil.returnError(errMsg);
  175. } catch (FOPException fe) {
  176. String errMsg = "Error FOP rendering " + birtContentType + " attachment for email: " + fe.toString();
  177. Debug.logError(fe, errMsg, module);
  178. return ServiceUtil.returnError(errMsg);
  179. } catch (SAXException se) {
  180. String errMsg = "Error SAX rendering " + birtContentType + " attachment for email: " + se.toString();
  181. Debug.logError(se, errMsg, module);
  182. return ServiceUtil.returnError(errMsg);
  183. } catch (ParserConfigurationException pe) {
  184. String errMsg = "Error parser rendering " + birtContentType + " attachment for email: " + pe.toString();
  185. Debug.logError(pe, errMsg, module);
  186. return ServiceUtil.returnError(errMsg);
  187. } catch (EngineException ee) {
  188. String errMsg = "Error rendering " + birtContentType + " attachment for email: " + ee.toString();
  189. Debug.logError(ee, errMsg, module);
  190. return ServiceUtil.returnError(errMsg);
  191. } catch (SQLException se) {
  192. String errMsg = "Error SQL rendering " + birtContentType + " attachment for email: " + se.toString();
  193. Debug.logError(se, errMsg, module);
  194. return ServiceUtil.returnError(errMsg);
  195. }
  196. } else {
  197. isMultiPart = false;
  198. // store body and type for single part message in the context.
  199. if (bodyText != null) {
  200. bodyText = FlexibleStringExpander.expandString(bodyText, screenContext, locale);
  201. serviceContext.put("body", bodyText);
  202. } else {
  203. serviceContext.put("body", bodyWriter.toString());
  204. }
  205. // Only override the default contentType in case of plaintext, since other contentTypes may be multipart
  206. // and would require specific handling.
  207. if (contentType != null && contentType.equalsIgnoreCase("text/plain")) {
  208. serviceContext.put("contentType", "text/plain");
  209. } else {
  210. serviceContext.put("contentType", "text/html");
  211. }
  212. }
  213. // also expand the subject at this point, just in case it has the FlexibleStringExpander syntax in it...
  214. String subject = (String) serviceContext.remove("subject");
  215. subject = FlexibleStringExpander.expandString(subject, screenContext, locale);
  216. serviceContext.put("subject", subject);
  217. serviceContext.put("partyId", partyId);
  218. if (Debug.verboseOn()) Debug.logVerbose("sendMailFromScreen sendMail context: " + serviceContext, module);
  219. Map<String, Object> result = ServiceUtil.returnSuccess();
  220. try {
  221. if (isMultiPart) {
  222. dispatcher.runSync("sendMailMultiPart", serviceContext);
  223. } else {
  224. dispatcher.runSync("sendMail", serviceContext);
  225. }
  226. } catch (Exception e) {
  227. String errMsg = "Error send email :" + e.toString();
  228. Debug.logError(e, errMsg, module);
  229. return ServiceUtil.returnError(errMsg);
  230. }
  231. result.put("body", bodyWriter.toString());
  232. return result;
  233. }
  234. }