PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/order/src/org/ofbiz/order/quote/QuoteServices.java

https://github.com/thanhvc/ofbiz
Java | 283 lines | 209 code | 39 blank | 35 comment | 32 complexity | f449c71869e6a4cd2622299325b275ee 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.order.quote;
  20. import java.sql.Timestamp;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import javolution.util.FastMap;
  26. import org.ofbiz.base.util.Debug;
  27. import org.ofbiz.base.util.UtilGenerics;
  28. import org.ofbiz.base.util.UtilMisc;
  29. import org.ofbiz.base.util.UtilProperties;
  30. import org.ofbiz.base.util.UtilValidate;
  31. import org.ofbiz.entity.Delegator;
  32. import org.ofbiz.entity.GenericEntityException;
  33. import org.ofbiz.entity.GenericValue;
  34. import org.ofbiz.service.DispatchContext;
  35. import org.ofbiz.service.GenericServiceException;
  36. import org.ofbiz.service.LocalDispatcher;
  37. import org.ofbiz.service.ServiceUtil;
  38. public class QuoteServices {
  39. public static final String module = QuoteServices.class.getName();
  40. public static final String resource = "OrderUiLabels";
  41. public static final String resource_error = "OrderErrorUiLabels";
  42. public static final String resourceProduct = "ProductUiLabels";
  43. public static Map<String, Object> sendQuoteReportMail(DispatchContext dctx, Map<String, ? extends Object> context) {
  44. LocalDispatcher dispatcher = dctx.getDispatcher();
  45. Delegator delegator = dctx.getDelegator();
  46. GenericValue userLogin = (GenericValue) context.get("userLogin");
  47. Locale locale = (Locale) context.get("locale");
  48. String emailType = (String) context.get("emailType");
  49. String quoteId = (String) context.get("quoteId");
  50. String sendTo = (String) context.get("sendTo");
  51. String sendCc = (String) context.get("sendCc");
  52. String note = (String) context.get("note");
  53. // prepare the order information
  54. Map<String, Object> sendMap = FastMap.newInstance();
  55. // get the quote and store
  56. GenericValue quote = null;
  57. try {
  58. quote = delegator.findByPrimaryKey("Quote", UtilMisc.toMap("quoteId", quoteId));
  59. } catch (GenericEntityException e) {
  60. Debug.logError(e, "Problem getting Quote", module);
  61. }
  62. if (quote == null) {
  63. return ServiceUtil.returnError(UtilProperties.getMessage(resource,
  64. "OrderOrderQuoteCannotBeFound",
  65. UtilMisc.toMap("quoteId", quoteId), locale));
  66. }
  67. GenericValue productStoreEmail = null;
  68. try {
  69. productStoreEmail = delegator.findByPrimaryKey("ProductStoreEmailSetting", UtilMisc.toMap("productStoreId", quote.get("productStoreId"), "emailType", emailType));
  70. } catch (GenericEntityException e) {
  71. Debug.logError(e, "Problem getting the ProductStoreEmailSetting for productStoreId=" + quote.get("productStoreId") + " and emailType=" + emailType, module);
  72. }
  73. if (productStoreEmail == null) {
  74. return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceProduct,
  75. "ProductProductStoreEmailSettingsNotValid",
  76. UtilMisc.toMap("productStoreId", quote.get("productStoreId"),
  77. "emailType", emailType), locale));
  78. }
  79. String bodyScreenLocation = productStoreEmail.getString("bodyScreenLocation");
  80. if (UtilValidate.isEmpty(bodyScreenLocation)) {
  81. return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceProduct,
  82. "ProductProductStoreEmailSettingsNotValidBodyScreenLocation",
  83. UtilMisc.toMap("productStoreId", quote.get("productStoreId"),
  84. "emailType", emailType), locale));
  85. }
  86. sendMap.put("bodyScreenUri", bodyScreenLocation);
  87. String xslfoAttachScreenLocation = productStoreEmail.getString("xslfoAttachScreenLocation");
  88. sendMap.put("xslfoAttachScreenLocation", xslfoAttachScreenLocation);
  89. if ((sendTo == null) || !UtilValidate.isEmail(sendTo)) {
  90. return ServiceUtil.returnError(UtilProperties.getMessage(resourceProduct,
  91. "ProductProductStoreEmailSettingsNoSendToFound", locale));
  92. }
  93. Map<String, Object> bodyParameters = UtilMisc.<String, Object>toMap("quoteId", quoteId, "userLogin", userLogin, "locale", locale);
  94. bodyParameters.put("note", note);
  95. bodyParameters.put("partyId", quote.getString("partyId")); // This is set to trigger the "storeEmailAsCommunication" seca
  96. sendMap.put("bodyParameters", bodyParameters);
  97. sendMap.put("userLogin", userLogin);
  98. String subjectString = productStoreEmail.getString("subject");
  99. sendMap.put("subject", subjectString);
  100. sendMap.put("contentType", productStoreEmail.get("contentType"));
  101. sendMap.put("sendFrom", productStoreEmail.get("fromAddress"));
  102. sendMap.put("sendCc", productStoreEmail.get("ccAddress"));
  103. sendMap.put("sendBcc", productStoreEmail.get("bccAddress"));
  104. sendMap.put("sendTo", sendTo);
  105. if ((sendCc != null) && UtilValidate.isEmail(sendCc)) {
  106. sendMap.put("sendCc", sendCc);
  107. } else {
  108. sendMap.put("sendCc", productStoreEmail.get("ccAddress"));
  109. }
  110. // send the notification
  111. Map<String, Object> sendResp = null;
  112. try {
  113. sendResp = dispatcher.runSync("sendMailFromScreen", sendMap);
  114. } catch (Exception e) {
  115. Debug.logError(e, module);
  116. return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderServiceExceptionSeeLogs",locale));
  117. }
  118. // check for errors
  119. if (sendResp != null && !ServiceUtil.isError(sendResp)) {
  120. sendResp.put("emailType", emailType);
  121. }
  122. return sendResp;
  123. }
  124. public static Map<String, Object> storeQuote(DispatchContext dctx, Map<String, ? extends Object> context) {
  125. LocalDispatcher dispatcher = dctx.getDispatcher();
  126. GenericValue userLogin = (GenericValue) context.get("userLogin");
  127. String quoteTypeId = (String) context.get("quoteTypeId");
  128. String partyId = (String) context.get("partyId");
  129. Timestamp issueDate = (Timestamp) context.get("issueDate");
  130. String statusId = (String) context.get("statusId");
  131. String currencyUomId = (String) context.get("currencyUomId");
  132. String productStoreId = (String) context.get("productStoreId");
  133. String salesChannelEnumId = (String) context.get("salesChannelEnumId");
  134. Timestamp validFromDate = (Timestamp) context.get("validFromDate");
  135. Timestamp validThruDate = (Timestamp) context.get("validThruDate");
  136. String quoteName = (String) context.get("quoteName");
  137. String description = (String) context.get("description");
  138. List<GenericValue> quoteItems = UtilGenerics.checkList(context.get("quoteItems"));
  139. List<GenericValue> quoteAttributes = UtilGenerics.checkList(context.get("quoteAttributes"));
  140. List<GenericValue> quoteCoefficients = UtilGenerics.checkList(context.get("quoteCoefficients"));
  141. List<GenericValue> quoteRoles = UtilGenerics.checkList(context.get("quoteRoles"));
  142. List<GenericValue> quoteWorkEfforts = UtilGenerics.checkList(context.get("quoteWorkEfforts"));
  143. List<GenericValue> quoteAdjustments = UtilGenerics.checkList(context.get("quoteAdjustments"));
  144. Locale locale = (Locale) context.get("locale");
  145. //TODO create Quote Terms still to be implemented
  146. //List<GenericValue> quoteTerms = UtilGenerics.cast(context.get("quoteTerms"));
  147. //TODO create Quote Term Attributes still to be implemented
  148. //List<GenericValue> quoteTermAttributes = UtilGenerics.cast(context.get("quoteTermAttributes"));
  149. Map<String, Object> result = FastMap.newInstance();
  150. try {
  151. Map<String, Object> quoteIn = UtilMisc.toMap("quoteTypeId", quoteTypeId, "partyId", partyId, "issueDate", issueDate, "statusId", statusId, "currencyUomId", currencyUomId);
  152. quoteIn.put("productStoreId", productStoreId);
  153. quoteIn.put("salesChannelEnumId", salesChannelEnumId);
  154. quoteIn.put("productStoreId", productStoreId);
  155. quoteIn.put("validFromDate", validFromDate);
  156. quoteIn.put("validThruDate", validThruDate);
  157. quoteIn.put("quoteName", quoteName);
  158. quoteIn.put("description", description);
  159. if (userLogin != null) {
  160. quoteIn.put("userLogin", userLogin);
  161. }
  162. // create Quote
  163. Map<String, Object> quoteOut = dispatcher.runSync("createQuote", quoteIn);
  164. if (UtilValidate.isNotEmpty(quoteOut) && UtilValidate.isNotEmpty(quoteOut.get("quoteId"))) {
  165. String quoteId = (String)quoteOut.get("quoteId");
  166. result.put("quoteId", quoteId);
  167. // create Quote Items
  168. if (UtilValidate.isNotEmpty(quoteItems)) {
  169. Iterator<GenericValue> quoteIt = quoteItems.iterator();
  170. while (quoteIt.hasNext()) {
  171. GenericValue quoteItem = quoteIt.next();
  172. quoteItem.set("quoteId", quoteId);
  173. Map<String, Object> quoteItemIn = quoteItem.getAllFields();
  174. quoteItemIn.put("userLogin", userLogin);
  175. dispatcher.runSync("createQuoteItem", quoteItemIn);
  176. }
  177. }
  178. // create Quote Attributes
  179. if (UtilValidate.isNotEmpty(quoteAttributes)) {
  180. Iterator<GenericValue> quoteAttrIt = quoteAttributes.iterator();
  181. while (quoteAttrIt.hasNext()) {
  182. GenericValue quoteAttr = quoteAttrIt.next();
  183. quoteAttr.set("quoteId", quoteId);
  184. Map<String, Object> quoteAttrIn = quoteAttr.getAllFields();
  185. quoteAttrIn.put("userLogin", userLogin);
  186. dispatcher.runSync("createQuoteAttribute", quoteAttrIn);
  187. }
  188. }
  189. // create Quote Coefficients
  190. if (UtilValidate.isNotEmpty(quoteCoefficients)) {
  191. Iterator<GenericValue> quoteCoefficientIt = quoteCoefficients.iterator();
  192. while (quoteCoefficientIt.hasNext()) {
  193. GenericValue quoteCoefficient = quoteCoefficientIt.next();
  194. quoteCoefficient.set("quoteId", quoteId);
  195. Map<String, Object> quoteCoefficientIn = quoteCoefficient.getAllFields();
  196. quoteCoefficientIn.put("userLogin", userLogin);
  197. dispatcher.runSync("createQuoteCoefficient", quoteCoefficientIn);
  198. }
  199. }
  200. // create Quote Roles
  201. if (UtilValidate.isNotEmpty(quoteRoles)) {
  202. Iterator<GenericValue> quoteRoleIt = quoteRoles.iterator();
  203. while (quoteRoleIt.hasNext()) {
  204. GenericValue quoteRole = quoteRoleIt.next();
  205. quoteRole.set("quoteId", quoteId);
  206. Map<String, Object> quoteRoleIn = quoteRole.getAllFields();
  207. quoteRoleIn.put("userLogin", userLogin);
  208. dispatcher.runSync("createQuoteRole", quoteRoleIn);
  209. }
  210. }
  211. // create Quote WorkEfforts
  212. if (UtilValidate.isNotEmpty(quoteWorkEfforts)) {
  213. Iterator<GenericValue> quoteWorkEffortIt = quoteWorkEfforts.iterator();
  214. while (quoteWorkEffortIt.hasNext()) {
  215. GenericValue quoteWorkEffort = quoteWorkEffortIt.next();
  216. quoteWorkEffort.set("quoteId", quoteId);
  217. Map<String, Object> quoteWorkEffortIn = quoteWorkEffort.getAllFields();
  218. quoteWorkEffortIn.put("userLogin", userLogin);
  219. dispatcher.runSync("createQuoteWorkEffort", quoteWorkEffortIn);
  220. }
  221. }
  222. // create Quote Adjustments
  223. if (UtilValidate.isNotEmpty(quoteAdjustments)) {
  224. Iterator<GenericValue> quoteAdjustmentIt = quoteAdjustments.iterator();
  225. while (quoteAdjustmentIt.hasNext()) {
  226. GenericValue quoteAdjustment = quoteAdjustmentIt.next();
  227. quoteAdjustment.set("quoteId", quoteId);
  228. Map<String, Object> quoteAdjustmentIn = quoteAdjustment.getAllFields();
  229. quoteAdjustmentIn.put("userLogin", userLogin);
  230. dispatcher.runSync("createQuoteAdjustment", quoteAdjustmentIn);
  231. }
  232. }
  233. //TODO create Quote Terms still to be implemented the base service createQuoteTerm
  234. //TODO create Quote Term Attributes still to be implemented the base service createQuoteTermAttribute
  235. } else {
  236. return ServiceUtil.returnFailure(UtilProperties.getMessage(resource,
  237. "OrderOrderQuoteCannotBeStored", locale));
  238. }
  239. } catch (GenericServiceException e) {
  240. Debug.logError(e, "Problem storing Quote", module);
  241. }
  242. return result;
  243. }
  244. }