コード例 #1
0
ファイル: SubscriptionAction.java プロジェクト: dmonga/work
  /**
   * 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;
  }