/** * Validates input parameters for a new subscription request * * @return */ public boolean validateAddSubscription() { boolean success = true; if (value.getMerchantId() == 0) { addActionError( getText("error.validation.parameter.missing", new String[] {MERCHANT_ID_PARAM})); success = false; } if (value.getProductId() == 0) { addActionError( getText("error.validation.parameter.missing", new String[] {PRODUCT_ID_PARAM})); success = false; } if (success) { try { store = mservice.getMerchantStore(value.getMerchantId()); Collection<MerchantUserInformation> minfo = mservice.getMerchantUserInfo(value.getMerchantId()); if (store == null) { addActionError( getText( "error.merchant.unavailable", new String[] {String.valueOf(value.getMerchantId())})); return false; } // maybe this has to be done value.setCountryId(store.getCountry()); // @TODO log to CommonService Product product = cservice.getProduct(value.getProductId()); if (product == null || product.getMerchantId() != value.getMerchantId()) { LogMerchantUtil.log( value.getMerchantId(), getText( "error.validation.merchant.product.ids", new String[] { String.valueOf(value.getProductId()), String.valueOf(value.getMerchantId()) })); addActionError( getText( "error.validation.merchant.product.ids", new String[] { String.valueOf(value.getProductId()), String.valueOf(value.getMerchantId()) })); success = false; } else { if (product.getProductDateAvailable().after(new Date())) { LogMerchantUtil.log( value.getMerchantId(), getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); addActionError( getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); success = false; } if (product.getProductQuantity() == OUT_OF_STOCK_PRODUCT_QUANTITY) { LogMerchantUtil.log( value.getMerchantId(), getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); addActionError( getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); Configuration config = PropertiesUtil.getConfiguration(); // MerchantProfile profile = // mservice.getMerchantProfile(value.getMerchantId()); String l = config.getString("core.system.defaultlanguage", "en"); if (minfo == null) { log.error("MerchantUserInformation is null for merchantId " + value.getMerchantId()); addActionError( getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); // goto global error throw new Exception("Invalid MerchantId,Unable to find MerchantProfile"); } MerchantUserInformation user = (MerchantUserInformation) ((List) minfo).get(0); if (!StringUtils.isBlank(user.getUserlang())) { l = user.getUserlang(); } String description = ""; Collection descriptionslist = product.getDescriptions(); if (descriptionslist != null) { Iterator i = descriptionslist.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof ProductDescription) { ProductDescription desc = (ProductDescription) o; description = desc.getProductName(); if (desc.getId().getLanguageId() == LanguageUtil.getLanguageNumberCode(l)) { description = desc.getProductName(); break; } } } } List params = new ArrayList(); params.add(description); params.add(product.getProductId()); LabelUtil lhelper = LabelUtil.getInstance(); String subject = lhelper.getText(super.getLocale(), "label.email.store.outofstock.subject"); String productId = lhelper.getText(super.getLocale(), "label.email.store.outofstock.product", params); Map emailctx = new HashMap(); emailctx.put("EMAIL_STORE_NAME", store.getStorename()); emailctx.put("EMAIL_PRODUCT_TEXT", productId); CommonService cservice = new CommonService(); cservice.sendHtmlEmail( store.getStoreemailaddress(), subject, store, emailctx, "email_template_outofstock.ftl", store.getDefaultLang()); success = false; } else if (product.getProductQuantity() < LOW_STOCK_PRODUCT_QUANTITY) { Configuration config = PropertiesUtil.getConfiguration(); // MerchantProfile profile = // mservice.getMerchantProfile(value.getMerchantId()); String l = config.getString("core.system.defaultlanguage", "en"); if (minfo == null) { log.error("MerchantUserInformationis null for merchantId " + value.getMerchantId()); addActionError( getText( "error.product.unavailable.purchase", new String[] {String.valueOf(value.getProductId())})); // goto global error throw new Exception("Invalid MerchantId,Unable to find MerchantProfile"); } MerchantUserInformation user = (MerchantUserInformation) ((List) minfo).get(0); if (!StringUtils.isBlank(user.getUserlang())) { l = user.getUserlang(); } String description = ""; Collection descriptionslist = product.getDescriptions(); if (descriptionslist != null) { Iterator i = descriptionslist.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof ProductDescription) { ProductDescription desc = (ProductDescription) o; description = desc.getProductName(); if (desc.getId().getLanguageId() == LanguageUtil.getLanguageNumberCode(l)) { description = desc.getProductName(); break; } } } } List params = new ArrayList(); params.add(description); params.add(product.getProductId()); LabelUtil lhelper = LabelUtil.getInstance(); String subject = lhelper.getText(l, "label.email.store.lowinventory.subject"); String productId = lhelper.getText( super.getLocale(), "label.email.store.lowinventory.product", params); Map emailctx = new HashMap(); emailctx.put("EMAIL_STORE_NAME", store.getStorename()); emailctx.put("EMAIL_PRODUCT_TEXT", productId); CommonService cservice = new CommonService(); cservice.sendHtmlEmail( store.getStoreemailaddress(), subject, store, emailctx, "email_template_lowstock.ftl", store.getDefaultLang()); } } } catch (Exception e) { log.error("Exception occurred while getting product by Id", e); addActionError(getText("errors.technical")); } } return success; }
/** * Invoked after addSubscriptionItem * * @throws Exception */ public void addItem() throws Exception { boolean quantityUpdated = false; // get store country Map lcountries = RefCache.getAllcountriesmap(LanguageUtil.getLanguageNumberCode(value.getLang())); if (lcountries != null) { Country country = (Country) lcountries.get(store.getCountry()); getServletRequest().getSession().setAttribute("COUNTRY", country); } // check if language is supported by the store if (lcountries != null) { Country country = (Country) lcountries.get(store.getCountry()); getServletRequest().getSession().setAttribute("COUNTRY", country); } // store can not be null, if it is the case, generic error page if (store == null) { throw new Exception("Invalid Store!"); } // check if order product already exist. If that orderproduct already // exist // and has no ptoperties, so just update the quantity if (value.getAttributeId() == null || (value.getAttributeId() != null && value.getAttributeId().size() == 0)) { Map savedProducts = SessionUtil.getOrderProducts(getServletRequest()); if (savedProducts != null) { Iterator it = savedProducts.keySet().iterator(); while (it.hasNext()) { String line = (String) it.next(); OrderProduct op = (OrderProduct) savedProducts.get(line); if (op.getProductId() == value.getProductId()) { Set attrs = op.getOrderattributes(); if (attrs.size() == 0) { int qty = op.getProductQuantity(); qty = qty + value.getQty(); op.setProductQuantity(qty); quantityUpdated = true; break; } } } } } // create an order with merchantId and all dates // will need to create a new order id when submited Order order = SessionUtil.getOrder(getServletRequest()); if (order == null) { order = new Order(); } order.setMerchantId(store.getMerchantId()); order.setDatePurchased(new Date()); SessionUtil.setOrder(order, getServletRequest()); if (!StringUtils.isBlank(value.getReturnUrl())) { // Return to merchant site Url is set from store. value.setReturnUrl(store.getContinueshoppingurl()); } SessionUtil.setMerchantStore(store, getServletRequest()); if (!quantityUpdated) { // new submission // Prepare order OrderProduct orderProduct = com.salesmanager.core.util.CheckoutUtil.createOrderProduct( value.getProductId(), getLocale(), store.getCurrency()); orderProduct.setProductQuantity(value.getQty()); orderProduct.setProductId(value.getProductId()); List<OrderProductAttribute> attributes = new ArrayList<OrderProductAttribute>(); if (value.getAttributeId() != null && value.getAttributeId().size() > 0) { for (Long attrId : value.getAttributeId()) { if (attrId != null && attrId != 0) { ProductAttribute pAttr = cservice.getProductAttributeByOptionValueAndProduct(value.getProductId(), attrId); if (pAttr != null && pAttr.getProductId() == value.getProductId()) { OrderProductAttribute orderAttr = new OrderProductAttribute(); orderAttr.setProductOptionValueId(pAttr.getOptionValueId()); attributes.add(orderAttr); } else { LogMerchantUtil.log( value.getMerchantId(), getText( "error.validation.product.attributes.ids", new String[] {String.valueOf(attrId), String.valueOf(value.getProductId())})); } } } } if (!attributes.isEmpty()) { // ShoppingCartUtil.addAttributesFromRawObjects(attributes, // orderProduct, store.getCurrency(), getServletRequest()); com.salesmanager.core.util.CheckoutUtil.addAttributesToProduct( attributes, orderProduct, store.getCurrency(), getLocale()); } Set attributesSet = new HashSet(attributes); orderProduct.setOrderattributes(attributesSet); SessionUtil.addOrderProduct(orderProduct, getServletRequest()); } // because this is a submission, cannot continue browsing, so that's it // for the OrderProduct Map orderProducts = SessionUtil.getOrderProducts(super.getServletRequest()); // transform to a list List products = new ArrayList(); if (orderProducts != null) { Iterator i = orderProducts.keySet().iterator(); while (i.hasNext()) { String line = (String) i.next(); OrderProduct op = (OrderProduct) orderProducts.get(line); products.add(op); } super.getServletRequest().getSession().setAttribute("ORDER_PRODUCT_LIST", products); } // for displaying the order summary, need to create an OrderSummary // entity OrderTotalSummary summary = oservice.calculateTotal(order, products, store.getCurrency(), super.getLocale()); Map totals = OrderUtil.getOrderTotals( order.getOrderId(), summary, store.getCurrency(), super.getLocale()); HttpSession session = getServletRequest().getSession(); // transform totals to a list List totalsList = new ArrayList(); if (totals != null) { Iterator totalsIterator = totals.keySet().iterator(); while (totalsIterator.hasNext()) { String key = (String) totalsIterator.next(); OrderTotal total = (OrderTotal) totals.get(key); totalsList.add(total); } } SessionUtil.setOrderTotals(totalsList, getServletRequest()); value.setLangId(LanguageUtil.getLanguageNumberCode(value.getLang())); prepareZones(); // set locale according to the language passed in parameters and store // information Locale locale = LocaleUtil.getLocaleFromStoreEntity(store, value.getLang()); setLocale(locale); }