com.etcc.csc.presentation.action.payment.InitializePaymentAction.java Source code

Java tutorial

Introduction

Here is the source code for com.etcc.csc.presentation.action.payment.InitializePaymentAction.java

Source

package com.etcc.csc.presentation.action.payment;

import com.etcc.csc.common.Constants;
import com.etcc.csc.common.DelegateEnum;
import com.etcc.csc.common.DelegateFactory;
import com.etcc.csc.datatype.Invoice;
import com.etcc.csc.datatype.PaymentDetail;
import com.etcc.csc.datatype.Violation;
import com.etcc.csc.dto.AccountLoginDTO;
import com.etcc.csc.dto.LicensePlateDTO;
import com.etcc.csc.dto.TagDTO;
import com.etcc.csc.presentation.datatype.LoginCreationInfo;
import com.etcc.csc.presentation.datatype.PaymentContext;
import com.etcc.csc.presentation.datatype.ZipCashPaymentContext;
import com.etcc.csc.service.AppInterface;
import com.etcc.csc.util.SessionUtil;

import com.etcc.csc.service.PaymentInterface;

import com.etcc.csc.service.ZipCashInterface;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class InitializePaymentAction extends Action {

    private static final String MIN_10 = "OLC_TS_MIN_PMT_AMT_GOOD_BAL";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();

        if (!processValidate(session)) {
            // System.out.println("[PAYMENT]Validation fails");
        }

        processRefresh(session);

        AccountLoginDTO accountLoginDTO = SessionUtil.getSessionAccountLogin(session);
        String docType = accountLoginDTO.getLoginType();
        if (StringUtils.isEmpty(docType)) {
            docType = "AC";
        }
        long docId = accountLoginDTO.getAcctId();
        if ("IN".equals(docType) || "CA".equals(docType)) {
            docId = accountLoginDTO.getInvoiceId();
        }

        BigDecimal existingTransactionId = null;
        if (SessionUtil.getRetailTransId(session) > 0) {
            existingTransactionId = new BigDecimal(SessionUtil.getRetailTransId(session));
        } else {
            existingTransactionId = processExistingTransactionId(request);
        }

        PaymentInterface pi = (PaymentInterface) DelegateFactory.create(DelegateEnum.PAYMENT_DELEGATE);
        PaymentDetail paymentDetail = pi.getPaymentDetail(accountLoginDTO.getDbSessionId(),
                accountLoginDTO.getLastLoginIp(), accountLoginDTO.getLoginId(), docId, docType,
                accountLoginDTO.getLicPlate(), accountLoginDTO.getLicState(), existingTransactionId);

        PaymentContext paymentContext = SessionUtil.getPaymentContext(session);
        paymentContext.setInvoices(paymentDetail.getInvoices());
        paymentContext.setViolations(paymentDetail.getViolations());
        paymentContext.setTags(paymentDetail.getTags());
        paymentContext.setOriginalTagAmount(paymentDetail.getTagAmount());
        paymentContext.setTransactionId(paymentDetail.getTransactionId());
        paymentContext.setCallBack(processCallBack(request));
        paymentContext.setDepAmount(paymentDetail.getDepAmount());
        processAutoAuthorize(paymentContext);

        ZipCashPaymentContext zipCashtContext = new ZipCashPaymentContext();
        ZipCashInterface zipCash = (ZipCashInterface) DelegateFactory.create(DelegateEnum.ZIPCASH_DELEGATE);
        PaymentDetail zipCashPaymentDetail = null;
        if (docType.equals(Constants.LOGIN_TYPE_ACCOUNT)) {
            zipCashPaymentDetail = zipCash.getPaymentDetail(accountLoginDTO.getDbSessionId(),
                    accountLoginDTO.getLastLoginIp(), accountLoginDTO.getLoginId(), docId, docType, null, null,
                    existingTransactionId);
        } else {
            zipCashPaymentDetail = zipCash.getPaymentDetail(accountLoginDTO.getDbSessionId(),
                    accountLoginDTO.getLastLoginIp(), accountLoginDTO.getLoginId(), docId, docType,
                    accountLoginDTO.getLicPlate(), accountLoginDTO.getLicState(), existingTransactionId);
        }
        zipCashtContext.setInvoices(zipCashPaymentDetail.getInvoices());
        zipCashtContext.setViolations(zipCashPaymentDetail.getViolations());
        zipCashtContext.setTags(zipCashPaymentDetail.getTags());
        zipCashtContext.setOriginalTagAmount(zipCashPaymentDetail.getTagAmount());
        zipCashtContext.setTransactionId(zipCashPaymentDetail.getTransactionId());
        zipCashtContext.setCallBack(processCallBack(request));
        SessionUtil.setZipCashPaymentContext(session, zipCashtContext);

        if ("AC".equals(docType)) {
            processVea(paymentContext, accountLoginDTO, docType, docId);
            if (paymentContext.getVeaEligible()) {
                paymentContext.setVeaAccepted(true);
            }
            //Task 2128_1 implementation. To display link to access Monthly Statements 
            List<LicensePlateDTO> licensePlateDTOList = pi.getAccountLicPlates(docId);
            accountLoginDTO
                    .setLicensePlates(licensePlateDTOList.toArray(new LicensePlateDTO[licensePlateDTOList.size()]));
            SessionUtil.setSessionAccountLogin(session, accountLoginDTO);
        }

        /*
        if ( "makePayment".equalsIgnoreCase( paymentContext.getCallBack() ) &&
         paymentContext.getOriginalTagAmount( ).compareTo( new BigDecimal( 40 ) ) < 0 )
        {
          paymentContext.setOriginalTagAmount( new BigDecimal( 40 ) );
        }
        */
        paymentContext.setTagAmount(paymentContext.getOriginalTagAmount());
        if ("makePayment".equalsIgnoreCase(paymentContext.getCallBack()) || paymentContext.getCallBack() == null
                || "".equalsIgnoreCase(paymentContext.getCallBack())) {
            if (paymentContext.getTagAmount() != null
                    && paymentContext.getTagAmount().compareTo(new BigDecimal(0)) > 0) {
                paymentContext.setOriginalTagAmount(paymentDetail.getTagAmount());

            }
        }

        /*
        if ( StringUtils.isEmpty( paymentContext.getCallBack( ) ) &&
         !"Y".equalsIgnoreCase( paymentDetail.getForcePayment( ) ) )
        {
          paymentContext.setOriginalTagAmount( new BigDecimal( 0.0 ) );
          paymentContext.setTagAmount( new BigDecimal( 0.0 ) );
        }
        */

        if (!"addTags".equalsIgnoreCase(paymentContext.getCallBack())) {
            paymentContext.setDepAmount(new BigDecimal(0.0));
        }

        if ("viewViol".equalsIgnoreCase(paymentContext.getCallBack())) {
            paymentContext.setTagAmount(new BigDecimal(0.0));
            paymentContext.setOriginalTagAmount(new BigDecimal(0.0));
        }

        if (!"makePayment".equalsIgnoreCase(paymentContext.getCallBack())
                && !"Y".equalsIgnoreCase(paymentDetail.getForcePayment())) {
            paymentContext.setOriginalTagAmount(new BigDecimal(0.0));
            paymentContext.setTagAmount(new BigDecimal(0.0));
        }

        if ("addTags".equalsIgnoreCase(paymentContext.getCallBack())) {
            paymentContext.setAddedTags(((TagDTO[]) request.getAttribute("addedTags")));
            if (paymentContext.getTagAmount() != null
                    && paymentContext.getTagAmount().compareTo(new BigDecimal(0)) > 0) {
                paymentContext.setOriginalTagAmount(paymentDetail.getTagAmount());
            }
        }

        if ("GetTollTag".equalsIgnoreCase(paymentContext.getCallBack())) {
            paymentContext.setLoginCreation((LoginCreationInfo) request.getAttribute("loginCreationInfo"));
        }
        //        paymentContext.setTagAmount( paymentContext.getOriginalTagAmount( ) );
        SessionUtil.setPaymentContext(session, paymentContext);

        return mapping.findForward("success");
    }

    private boolean processValidate(HttpSession session) {
        AccountLoginDTO accountLoginDTO = SessionUtil.getSessionAccountLogin(session);

        if (accountLoginDTO == null) {
            return false;
        }

        if (!(StringUtils.isEmpty(accountLoginDTO.getLoginType()) || "AC".equals(accountLoginDTO.getLoginType())
                || "IN".equals(accountLoginDTO.getLoginType()) || "CA".equals(accountLoginDTO.getLoginType()))) {
            System.out.println("[PAYMENT] Required information missing for payment: loginType is not valid");
        }

        if (StringUtils.isEmpty(accountLoginDTO.getLoginType()) || "AC".equals(accountLoginDTO.getLoginType())) {
            if (accountLoginDTO.getAcctId() > 0) {
                System.out.println("[PAYMENT] Validation successful");
                return true;
            } else {
                System.out.println("[PAYMENT] Required information missing for payment: account id is not valid");
                return false;
            }
        }

        if ("IN".equals(accountLoginDTO.getLoginType()) || "CA".equals(accountLoginDTO.getLoginType())) {
            if (StringUtils.isEmpty(accountLoginDTO.getLicPlate())) {
                System.out.println("[PAYMENT] Required information missing for payment: license plate empty");
                return false;
            }

            if (accountLoginDTO.getInvoiceId() > 0) {
                System.out.println("[PAYMENT] Validation successful");
                return true;
            } else {
                System.out.println("[PAYMENT] Required information missing for payment: invoice id is not valid");
                return false;
            }
        }

        return true;
    }

    private void processAutoAuthorize(PaymentContext paymentContext) {
        Invoice[] invoices = paymentContext.getInvoices();
        for (int i = 0; invoices != null && i < invoices.length; i++) {
            invoices[i].setAuthorized(true);
        }

        Violation[] violations = paymentContext.getViolations();
        for (int i = 0; violations != null && i < violations.length; i++) {
            violations[i].setAuthorized(true);
        }
    }

    private void processRefresh(HttpSession session) {
        SessionUtil.setPaymentContext(session, new PaymentContext());
    }

    private void processVea(PaymentContext paymentContext, AccountLoginDTO accountLoginDTO, String docType,
            long docId) throws Exception {
        Invoice[] distinctLicPlates = paymentContext.getDistinctLicPlates();
        PaymentInterface pi = (PaymentInterface) DelegateFactory.create(DelegateEnum.PAYMENT_DELEGATE);
        ArrayList veaEligibleLicPlates = new ArrayList();
        if (!ArrayUtils.isEmpty(distinctLicPlates)) {
            for (Invoice invoice : distinctLicPlates) {
                if (pi.veaExists(new BigDecimal(docId), docType, accountLoginDTO.getDbSessionId(),
                        accountLoginDTO.getLastLoginIp(), accountLoginDTO.getLoginId(), invoice.getLicPlateNumber(),
                        invoice.getLicPlateState())) {
                    veaEligibleLicPlates.add(invoice.getLicPlateNumber() + invoice.getLicPlateState());
                }
            }
        }

        Invoice[] invoices = paymentContext.getInvoices();
        for (int i = 0; !ArrayUtils.isEmpty(invoices) && i < invoices.length; i++) {
            invoices[i].setVeaEligible(veaEligibleLicPlates
                    .contains(invoices[i].getLicPlateNumber() + invoices[i].getLicPlateState()));
        }
    }

    private BigDecimal processExistingTransactionId(HttpServletRequest request) {
        BigDecimal result = null;
        Object retailTransactionIdObj = request.getAttribute("retailTransactionId");
        if (retailTransactionIdObj != null) {
            result = new BigDecimal(retailTransactionIdObj.toString());
        }
        return result;
    }

    private String processCallBack(HttpServletRequest request) {
        String callBack = (String) request.getAttribute("callBack");
        if (StringUtils.isEmpty(callBack)) {
            callBack = request.getParameter("callBack");
        }
        return callBack;
    }

    private BigDecimal getGoodBalanceMinPayment() {
        try {
            String value = "";
            AppInterface ai = (AppInterface) DelegateFactory.create(DelegateEnum.APP_DELEGATE);
            value = ai.getSysParam(MIN_10);
            return new BigDecimal(value);
        } catch (Exception e) {
            System.out.println(e);
        }
        return new BigDecimal(10.0);
    }

}