protected void initFromOrder(String orderNum, ShoppingCartActionForm form, Site site, HttpServletRequest request, ActionMessages messages) throws Exception {
EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
Long custId = ContentLookupDispatchAction.getCustId(request);
ContentBean contentBean = getContentBean(request);
Formatter formatter = contentBean.getFormatter();
OrderHeader orderHeader = null;
String sql = "select orderHeader " +
"from OrderHeader orderHeader " +
"where orderHeader.siteDomain.site.siteId = :siteId " +
"and orderHeader.orderNum = :orderNum ";
Query query = em.createQuery(sql);
query.setParameter("siteId", site.getSiteId());
query.setParameter("orderNum", orderNum);
List<?> list = query.getResultList();
if (list.size() > 0) {
orderHeader = (OrderHeader) list.get(0);
}
if (!orderHeader.getCustomer().getCustId().equals(custId)) {
throw new SecurityException("Customer should not have access to other's customer order");
}
OrderEngine orderEngine = new OrderEngine(orderHeader, null);
Iterator<?> iterator = orderHeader.getOrderItemDetails().iterator();
Vector<ShoppingCartItemBean> vector = new Vector<ShoppingCartItemBean>();
while (iterator.hasNext()) {
OrderItemDetail orderItemDetail = (OrderItemDetail) iterator.next();
ShoppingCartItemBean bean = new ShoppingCartItemBean();
Item item = orderItemDetail.getItem();
Item master = item;
if (item != null) {
if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_SKU)) {
master = item.getItemSkuParent();
}
}
bean.setItemId("");
if (item != null) {
bean.setItemId(item.getItemId().toString());
}
bean.setItemNum(orderItemDetail.getItemNum());
bean.setItemShortDesc(orderItemDetail.getItemShortDesc());
bean.setItemQty(formatter.formatNumber(orderItemDetail.getItemOrderQty()));
bean.setItemPrice(formatItemPrice(contentBean, orderItemDetail.getItemTierQty(), orderItemDetail.getItemTierPrice()));
bean.setItemSubTotal(formatter.formatCurrency(orderItemDetail.getItemDetailAmount()));
bean.setItemQtyError("");
bean.setImageId(null);
bean.setItemShortDesc(orderItemDetail.getItemShortDesc());
if (item != null) {
ItemImage itemImage = orderItemDetail.getItem().getItemLanguage().getImage();
if (itemImage != null) {
bean.setImageId(itemImage.getImageId().toString());
}
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (ItemLanguage language : master.getItemLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getItemImageOverride().equals(String.valueOf(Constants.VALUE_YES))) {
bean.setImageId("");
if (language.getImage() != null) {
bean.setImageId(language.getImage().getImageId().toString());
}
}
if (language.getItemShortDesc() != null) {
bean.setItemShortDesc(language.getItemShortDesc());
}
break;
}
}
}
}
Vector<ShoppingCartItemAttributeBean> shoppingCartItemAttributes = new Vector<ShoppingCartItemAttributeBean>();
if (item != null && item.getItemTypeCd().equals(Constants.ITEM_TYPE_SKU)) {
for (ItemAttributeDetail itemAttributeDetail : item.getItemAttributeDetails()) {
CustomAttribute customAttribute = itemAttributeDetail.getCustomAttributeDetail().getCustomAttribute();
if (customAttribute.getCustomAttribTypeCode() != Constants.CUSTOM_ATTRIBUTE_TYPE_SKU_MAKEUP) {
continue;
}
ShoppingCartItemAttributeBean shoppingCartItemAttributeBean = new ShoppingCartItemAttributeBean();
shoppingCartItemAttributeBean.setCustomAttribDesc(customAttribute.getCustomAttributeLanguage().getCustomAttribDesc());
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CustomAttributeLanguage language : customAttribute.getCustomAttributeLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCustomAttribDesc() != null) {
shoppingCartItemAttributeBean.setCustomAttribDesc(language.getCustomAttribDesc());
}
break;
}
}
}
CustomAttributeOption customAttributeOption = itemAttributeDetail.getCustomAttributeOption();
shoppingCartItemAttributeBean.setCustomAttribValue(customAttributeOption.getCustomAttributeOptionLanguage().getCustomAttribValue());
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CustomAttributeOptionLanguage language : customAttributeOption.getCustomAttributeOptionLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCustomAttribValue() != null) {
shoppingCartItemAttributeBean.setCustomAttribValue(language.getCustomAttribValue());
}
break;
}
}
}
shoppingCartItemAttributes.add(shoppingCartItemAttributeBean);
}
}
for (OrderAttributeDetail orderAttributeDetail : orderItemDetail.getOrderAttributeDetails()) {
ShoppingCartItemAttributeBean shoppingCartItemAttributeBean = new ShoppingCartItemAttributeBean();
CustomAttribute customAttribute = orderAttributeDetail.getCustomAttributeDetail().getCustomAttribute();
shoppingCartItemAttributeBean.setCustomAttribDesc(customAttribute.getCustomAttributeLanguage().getCustomAttribDesc());
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CustomAttributeLanguage language : customAttribute.getCustomAttributeLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCustomAttribDesc() != null) {
shoppingCartItemAttributeBean.setCustomAttribDesc(language.getCustomAttribDesc());
}
break;
}
}
}
if (customAttribute.getCustomAttribTypeCode() == Constants.CUSTOM_ATTRIBUTE_TYPE_CUST_INPUT) {
shoppingCartItemAttributeBean.setCustomAttribValue(orderAttributeDetail.getOrderAttribValue());
}
else {
CustomAttributeOption customAttributeOption = orderAttributeDetail.getCustomAttributeOption();
shoppingCartItemAttributeBean.setCustomAttribValue(customAttributeOption.getCustomAttributeOptionLanguage().getCustomAttribValue());
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CustomAttributeOptionLanguage language : customAttributeOption.getCustomAttributeOptionLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCustomAttribValue() != null) {
shoppingCartItemAttributeBean.setCustomAttribValue(language.getCustomAttribValue());
}
break;
}
}
}
}
shoppingCartItemAttributes.add(shoppingCartItemAttributeBean);
}
bean.setShoppingCartItemAttributes(shoppingCartItemAttributes);
vector.add(bean);
}
form.setShoppingCartItemInfos(vector);
Vector<ShoppingCartCouponBean> couponVector = new Vector<ShoppingCartCouponBean>();
iterator = orderHeader.getOrderOtherDetails().iterator();
while (iterator.hasNext()) {
OrderOtherDetail orderOtherDetail = (OrderOtherDetail) iterator.next();
Coupon coupon = orderOtherDetail.getCoupon();
ShoppingCartCouponBean bean = new ShoppingCartCouponBean();
bean.setCouponCode(coupon.getCouponCode());
bean.setCouponName(coupon.getCouponLanguage().getCouponName());
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CouponLanguage language : coupon.getCouponLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCouponName() != null) {
bean.setCouponName(language.getCouponName());
}
break;
}
}
}
bean.setCouponAmount(Format.getFloat(orderOtherDetail.getOrderOtherDetailAmount()));
couponVector.add(bean);
}
form.setShoppingCartCouponInfos(couponVector);
sql = "select orderDetailTax.taxName, sum(orderDetailTax.taxAmount) " +
"from OrderDetailTax orderDetailTax " +
"left join orderDetailTax.orderHeader orderHeader " +
"where orderHeader.orderHeaderId = :orderHeaderId " +
"group by orderDetailTax.taxName " +
"order by orderDetailTax.taxName ";
query = em.createQuery(sql);
query.setParameter("orderHeaderId", orderHeader.getOrderHeaderId());
iterator = query.getResultList().iterator();
Vector<ShoppingCartTaxInfo> taxVector = new Vector<ShoppingCartTaxInfo>();
while (iterator.hasNext()) {
Object object[] = (Object[]) iterator.next();
String taxName = (String) object[0];
Double taxAmount = (Double) object[1];
ShoppingCartTaxInfo taxInfo = new ShoppingCartTaxInfo();
taxInfo.setTaxName(taxName);
taxInfo.setTaxAmount(formatter.formatCurrency(taxAmount.floatValue()));
taxVector.add(taxInfo);
}
form.setShoppingCartTaxInfos(taxVector);
form.setPriceTotal(formatter.formatCurrency(orderEngine.getOrderSubTotal()));
form.setTaxTotal(formatter.formatCurrency(orderEngine.getOrderTaxTotal()));
form.setShippingTotal(formatter.formatCurrency(orderHeader.getShippingTotal()));
form.setShippingDiscountTotal(formatter.formatCurrency(orderHeader.getShippingDiscountTotal()));
form.setShippingOrderTotal(formatter.formatCurrency(orderHeader.getShippingTotal() - orderHeader.getShippingDiscountTotal()));
form.setOrderTotal(formatter.formatCurrency(orderHeader.getOrderTotal()));
OrderAddress custAddress = orderHeader.getCustAddress();
if (custAddress != null) {
ShoppingCartAddressActionForm custAddressForm = new ShoppingCartAddressActionForm();
custAddressForm.setCustUseAddress(custAddress.getCustUseAddress());
custAddressForm.setCustPrefix(custAddress.getCustPrefix());
custAddressForm.setCustFirstName(custAddress.getCustFirstName());
custAddressForm.setCustMiddleName(custAddress.getCustMiddleName());
custAddressForm.setCustLastName(custAddress.getCustLastName());
custAddressForm.setCustSuffix(custAddress.getCustSuffix());
custAddressForm.setCustAddressLine1(custAddress.getCustAddressLine1());
custAddressForm.setCustAddressLine2(custAddress.getCustAddressLine2());
custAddressForm.setCustCityName(custAddress.getCustCityName());
custAddressForm.setCustStateName(custAddress.getCustStateName());
custAddressForm.setCustStateCode(custAddress.getCustStateCode());
custAddressForm.setCustZipCode(custAddress.getCustZipCode());
custAddressForm.setCustCountryName(custAddress.getCustCountryName());
custAddressForm.setCustCountryCode(custAddress.getCustCountryCode());
custAddressForm.setCustPhoneNum(custAddress.getCustPhoneNum());
custAddressForm.setCustFaxNum(custAddress.getCustFaxNum());
form.setCustAddress(custAddressForm);
}
OrderAddress billingAddress = orderHeader.getBillingAddress();
OrderAddress shippingAddress = orderHeader.getShippingAddress();
if (billingAddress != null) {
ShoppingCartAddressActionForm billingAddressForm = new ShoppingCartAddressActionForm();
billingAddressForm.setCustUseAddress(billingAddress.getCustUseAddress());
billingAddressForm.setCustPrefix(billingAddress.getCustPrefix());
billingAddressForm.setCustFirstName(billingAddress.getCustFirstName());
billingAddressForm.setCustMiddleName(billingAddress.getCustMiddleName());
billingAddressForm.setCustLastName(billingAddress.getCustLastName());
billingAddressForm.setCustSuffix(billingAddress.getCustSuffix());
billingAddressForm.setCustAddressLine1(billingAddress.getCustAddressLine1());
billingAddressForm.setCustAddressLine2(billingAddress.getCustAddressLine2());
billingAddressForm.setCustCityName(billingAddress.getCustCityName());
billingAddressForm.setCustStateName(billingAddress.getCustStateName());
billingAddressForm.setCustStateCode(billingAddress.getCustStateCode());
billingAddressForm.setCustZipCode(billingAddress.getCustZipCode());
billingAddressForm.setCustCountryName(billingAddress.getCustCountryName());
billingAddressForm.setCustCountryCode(billingAddress.getCustCountryCode());
billingAddressForm.setCustPhoneNum(billingAddress.getCustPhoneNum());
billingAddressForm.setCustFaxNum(billingAddress.getCustFaxNum());
form.setBillingAddress(billingAddressForm);
}
if (shippingAddress != null) {
ShoppingCartAddressActionForm shippingAddressForm = new ShoppingCartAddressActionForm();
shippingAddressForm.setCustUseAddress(shippingAddress.getCustUseAddress());
shippingAddressForm.setCustPrefix(shippingAddress.getCustPrefix());
shippingAddressForm.setCustFirstName(shippingAddress.getCustFirstName());
shippingAddressForm.setCustMiddleName(shippingAddress.getCustMiddleName());
shippingAddressForm.setCustLastName(shippingAddress.getCustLastName());
shippingAddressForm.setCustSuffix(shippingAddress.getCustSuffix());
shippingAddressForm.setCustAddressLine1(shippingAddress.getCustAddressLine1());
shippingAddressForm.setCustAddressLine2(shippingAddress.getCustAddressLine2());
shippingAddressForm.setCustCityName(shippingAddress.getCustCityName());
shippingAddressForm.setCustStateName(shippingAddress.getCustStateName());
shippingAddressForm.setCustStateCode(shippingAddress.getCustStateCode());
shippingAddressForm.setCustZipCode(shippingAddress.getCustZipCode());
shippingAddressForm.setCustCountryName(shippingAddress.getCustCountryName());
shippingAddressForm.setCustCountryCode(shippingAddress.getCustCountryCode());
shippingAddressForm.setCustPhoneNum(shippingAddress.getCustPhoneNum());
shippingAddressForm.setCustFaxNum(shippingAddress.getCustFaxNum());
form.setShippingAddress(shippingAddressForm);
}
form.setCustEmail(orderHeader.getCustEmail());
form.setShippingMethodName(orderHeader.getShippingMethodName());
PaymentTran payment = orderHeader.getPaymentTran();
if (payment == null) {
for (InvoiceHeader invoiceHeader : orderHeader.getInvoiceHeaders()) {
payment = invoiceHeader.getPaymentTran();
}
}
if (payment != null) {
form.setPaymentGatewayProvider(orderHeader.getPaymentGatewayProvider());
if (orderHeader.getPaymentGatewayProvider().equals(PayPalEngine.class.getSimpleName())) {
form.setPayPal(true);
form.setAuthCode(payment.getAuthCode());
}
else {
form.setPayPal(false);
form.setCreditCardDesc(orderHeader.getCreditCardDesc());
String custCreditCardNum = AESEncoder.getInstance().decode(orderHeader.getCustCreditCardNum());
custCreditCardNum = Utility.maskCreditCardNumber(custCreditCardNum);
form.setCustCreditCardNum(custCreditCardNum);
form.setAuthCode(payment.getAuthCode());
}
}
else {
form.setCashPaymentOrder(true);
}
form.setOrderNum(orderNum);
form.setOrderDatetime(formatter.formatFullDate(orderHeader.getOrderDate()));
PaymentGateway paymentGateway = null;
if (paymentGateway != null) {
if (orderHeader.getPaymentGatewayProvider().equals(PayPalEngine.class.getSimpleName())) {
form.setPayPal(true);
}
PaymentTran paymentTran = orderHeader.getPaymentTran();
if (paymentTran == null) {
Iterator<?> invoiceIterator = orderHeader.getInvoiceHeaders().iterator();
if (invoiceIterator.hasNext()) {
InvoiceHeader invoiceHeader = (InvoiceHeader) invoiceIterator.next();
paymentTran = invoiceHeader.getPaymentTran();
}
}
if (paymentTran != null) {
form.setAuthCode(paymentTran.getAuthCode());
}
}
Long siteProfileId = contentBean.getContentSessionBean().getSiteProfile().getSiteProfileId();
SiteDomainLanguage siteDomainLanguage = contentBean.getContentSessionBean().getSiteDomain().getSiteDomainLanguage();
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (SiteDomainLanguage language : contentBean.getContentSessionBean().getSiteDomain().getSiteDomainLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(siteProfileId)) {
siteDomainLanguage = language;
}
}
}
SiteDomainParamBean siteDomainParamBean = SiteDomainDAO.getSiteDomainParamBean(contentBean.getContentSessionBean().getSiteDomain().getSiteDomainLanguage(), siteDomainLanguage);
form.setShoppingCartMessage(siteDomainParamBean.getCheckoutShoppingCartMessage());
form.setCurrencyCode(orderHeader.getSiteCurrency().getSiteCurrencyClass().getCurrency().getCurrencyCode());
}