PageRenderTime 35ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

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