com.lm.lic.manager.controller.WithdrawLicController.java Source code

Java tutorial

Introduction

Here is the source code for com.lm.lic.manager.controller.WithdrawLicController.java

Source

/**
 * $Id: WithdrawLicController.java 1977 2011-03-14 09:52:03Z imustafa $
 */
package com.lm.lic.manager.controller;

import java.util.HashMap;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import com.lm.lic.manager.form.PGWithdrawLicForm;
import com.lm.lic.manager.form.WithdrawLicForm;
import com.lm.lic.manager.hibernate.Product;
import com.lm.lic.manager.hibernate.RequestForLicense;
import com.lm.lic.manager.service.LicenseBlockService;
import com.lm.lic.manager.service.LicenseService;
import com.lm.lic.manager.service.RequestForLicenseService;
import com.lm.lic.manager.util.GenUtil;

/**
 * @author Ibrahim Mustafa
 */
public class WithdrawLicController extends BaseLicenseManagerFormController {

    /**
     * 
     */
    private static final String WITHDRAW_LIC = "WITHDRAW_LIC - ";

    public static final String CLASS_VERSION = "$Id: WithdrawLicController.java 1977 2011-03-14 09:52:03Z imustafa $";

    private static Logger logger = Logger.getLogger(WithdrawLicController.class);

    private LicenseService licenseService;
    private LicenseBlockService licenseBlockService;
    private RequestForLicenseService requestForLicenseService;

    private LicenseAvailabilityAdvisor licenseAvailabilityAdvisor;

    WithdrawLicHandler bbwlHandler;
    WithdrawLicHandler hgwlHandler;
    WithdrawLicHandler mhwlHandler;
    WithdrawLicHandler pgwlHandler;
    WithdrawLicHandler ypwlHandler;
    WithdrawLicHandler isvmanwlHandler;
    WithdrawLicHandler isvdynwlHandler;

    /**
     * Floating License Product Instance Withdraw License Handler
     */
    WithdrawLicHandler isvflpimanwlHandler;
    WithdrawLicHandler isvflpidynwlHandler;

    public WithdrawLicController() {
        super();
        setSupportedMethods(new String[] { METHOD_GET, METHOD_POST });
    }

    @Override
    protected Map<String, Object> referenceData(HttpServletRequest request, Object command, Errors errors)
            throws Exception {
        WithdrawLicForm wlc = (WithdrawLicForm) command;
        Map<String, Object> refData = new HashMap<String, Object>();

        Product product = null;
        String prodName = wlc.getProduct();
        String prodVer = wlc.getVersion();

        String licKey = wlc.getLicKey();
        if (StringUtils.isNotBlank(licKey)) {
            if (wlc.getQty() == null || wlc.getQty() < 1)
                wlc.setQty(1);
        }

        String isvProductId = wlc.getProductId();
        String productKey = wlc.getProductKey();
        String isvId = GenUtil.findLoggedinIsv(request, loginService);
        if (StringUtils.isNotBlank(prodName) && StringUtils.isNotBlank(prodVer)) {
            product = productService.findProductByNameAndVersion(isvId, prodName, prodVer);
            if (product == null)
                product = productService.findProductByName(isvId, prodName);
        } else if (StringUtils.isNotBlank(isvProductId)) {
            product = productService.findProductByIsvProdId(isvId, isvProductId);
        } else if (StringUtils.isNotBlank(productKey)) {
            product = productService.findProductByProductKey(isvId, productKey);
        }

        if (product == null && StringUtils.isNumeric(wlc.getProdId()))
            product = productService.findProdById(isvId, wlc.getProdId());

        if (product != null) {
            wlc.setProductKey(product.getProductKey());
            wlc.setParamProductID(product.getIsvProductId());
        }

        RequestForLicense requestForLicense = (RequestForLicense) request.getAttribute("requestForLicense");
        wlc.setRequestForLicense(requestForLicense);

        wlc.setIsvId(isvId);

        String prodId = EMPTY;
        Boolean enabled = false;
        if (product != null) {
            popDefaultValues(wlc, product);
            prodId += product.getId();
            enabled = product.getEnabled();
            wlc.setProdDefKey(product.getProductDefKey());
            wlc.setIsvKey(product.getIsv().getIsvKey());
            wlc.setProductKey(product.getProductKey());
            wlc.setProdInstKey(product.getProductKey());
        }

        refData.put("isvId", isvId);
        refData.put("prodId", prodId);
        refData.put("product", product);
        refData.put("enabled", enabled);
        return refData;
    }

    private void popDefaultValues(WithdrawLicForm wlc, Product product) {
        wlc.setProdId(product.getId() + EMPTY);
        wlc.setProductId(product.getIsvProductId());
        wlc.setProduct(product.getName());
        wlc.setVersion(product.getVersion());
        if (wlc instanceof PGWithdrawLicForm)
            ((PGWithdrawLicForm) wlc).setProductID(product.getIsvProductId());
    }

    /**
     *
     */
    @SuppressWarnings("deprecation")
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
            BindException errors) throws Exception {
        WithdrawLicForm wlf = (WithdrawLicForm) command;
        String isvId = GenUtil.findLoggedinIsv(request, loginService);
        wlf.setIsvId(isvId);
        logger.info("Start Handling of License Withdrawal Request");
        debugWithdrawLicForm(wlf);

        ModelAndView modelAndView = null;
        LicWithdrawerIdentifier lwi = (LicWithdrawerIdentifier) request.getAttribute("licWithdrawerIdentifier");
        if (lwi != null) {
            if (lwi.isBlackBerry())
                modelAndView = bbwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isHandAnGo())
                modelAndView = hgwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isMobiHand())
                modelAndView = mhwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isPocketGear())
                modelAndView = pgwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isYoupark())
                modelAndView = ypwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isIsvManual())
                modelAndView = isvmanwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isIsvDynamic())
                modelAndView = isvdynwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isIsvManFlpi())
                modelAndView = isvflpimanwlHandler.handleWithdrawal(request, response, wlf);
            else if (lwi.isIsvDynFlpi())
                modelAndView = isvflpidynwlHandler.handleWithdrawal(request, response, wlf);
        }

        if (modelAndView != null) {
            modelAndView.addObject("command", wlf);

            // This is usually the case when withdrawing a license manually
            String successView = getSuccessView();
            if (StringUtils.isNotEmpty(successView) && StringUtils.isNotEmpty(isvId))
                modelAndView.setViewName(successView);
        }

        return modelAndView;
    }

    /**
     * @param wlf
     */
    private void debugWithdrawLicForm(WithdrawLicForm wlf) {
        logger.info(WITHDRAW_LIC + "Withdraw For Customer: " + wlf.isWithdrwingForCustomer());
    }

    @SuppressWarnings("deprecation")
    @Override
    protected ModelAndView onCancel(HttpServletRequest request, HttpServletResponse response, Object command) {
        // WithdrawLicForm form = (WithdrawLicForm) command;
        // String cancelView = getCancelView() + form.getProdId();
        ModelAndView modelAndView = new ModelAndView(getCancelView());
        return modelAndView;
    }

    /**
     * @return the licenseService
     */
    @Override
    public LicenseService getLicenseService() {
        return this.licenseService;
    }

    /**
     * @param licenseService
     *        the licenseService to set
     */
    @Override
    public void setLicenseService(LicenseService licenseService) {
        this.licenseService = licenseService;
    }

    /**
     * @return the licenseBlockService
     */
    public LicenseBlockService getLicenseBlockService() {
        return this.licenseBlockService;
    }

    /**
     * @param licenseBlockService
     *        the licenseBlockService to set
     */
    public void setLicenseBlockService(LicenseBlockService licenseBlockService) {
        this.licenseBlockService = licenseBlockService;
    }

    /**
     * @return the licenseAvailabilityAdvisor
     */
    public LicenseAvailabilityAdvisor getLicenseAvailabilityAdvisor() {
        return this.licenseAvailabilityAdvisor;
    }

    /**
     * @param licenseAvailabilityAdvisor
     *        the licenseAvailabilityAdvisor to set
     */
    public void setLicenseAvailabilityAdvisor(LicenseAvailabilityAdvisor licenseAvailabilityAdvisor) {
        this.licenseAvailabilityAdvisor = licenseAvailabilityAdvisor;
    }

    /**
     * @return the requestForLicenseService
     */
    @Override
    public RequestForLicenseService getRequestForLicenseService() {
        return this.requestForLicenseService;
    }

    /**
     * @param requestForLicenseService
     *        the requestForLicenseService to set
     */
    @Override
    public void setRequestForLicenseService(RequestForLicenseService requestForLicenseService) {
        this.requestForLicenseService = requestForLicenseService;
    }

    /**
     * @return the bbwlHandler
     */
    public WithdrawLicHandler getBbwlHandler() {
        return bbwlHandler;
    }

    /**
     * @param bbwlHandler
     *        the bbwlHandler to set
     */
    public void setBbwlHandler(WithdrawLicHandler bbwlHandler) {
        this.bbwlHandler = bbwlHandler;
    }

    /**
     * @return the hgwlHandler
     */
    public WithdrawLicHandler getHgwlHandler() {
        return hgwlHandler;
    }

    /**
     * @param hgwlHandler
     *        the hgwlHandler to set
     */
    public void setHgwlHandler(WithdrawLicHandler hgwlHandler) {
        this.hgwlHandler = hgwlHandler;
    }

    /**
     * @return the mhwlHandler
     */
    public WithdrawLicHandler getMhwlHandler() {
        return mhwlHandler;
    }

    /**
     * @param mhwlHandler
     *        the mhwlHandler to set
     */
    public void setMhwlHandler(WithdrawLicHandler mhwlHandler) {
        this.mhwlHandler = mhwlHandler;
    }

    /**
     * @return the pgwlHandler
     */
    public WithdrawLicHandler getPgwlHandler() {
        return pgwlHandler;
    }

    /**
     * @param pgwlHandler
     *        the pgwlHandler to set
     */
    public void setPgwlHandler(WithdrawLicHandler pgwlHandler) {
        this.pgwlHandler = pgwlHandler;
    }

    /**
     * @return the ypwlHandler
     */
    public WithdrawLicHandler getYpwlHandler() {
        return ypwlHandler;
    }

    /**
     * @param ypwlHandler
     *        the ypwlHandler to set
     */
    public void setYpwlHandler(WithdrawLicHandler ypwlHandler) {
        this.ypwlHandler = ypwlHandler;
    }

    /**
     * @return the isvmanwlHandler
     */
    public WithdrawLicHandler getIsvmanwlHandler() {
        return isvmanwlHandler;
    }

    /**
     * @param isvmanwlHandler
     *        the isvmanwlHandler to set
     */
    public void setIsvmanwlHandler(WithdrawLicHandler isvmanwlHandler) {
        this.isvmanwlHandler = isvmanwlHandler;
    }

    /**
     * @return the isvdynwlHandler
     */
    public WithdrawLicHandler getIsvdynwlHandler() {
        return isvdynwlHandler;
    }

    /**
     * @param isvdynwlHandler
     *        the isvdynwlHandler to set
     */
    public void setIsvdynwlHandler(WithdrawLicHandler isvdynwlHandler) {
        this.isvdynwlHandler = isvdynwlHandler;
    }

    /**
     * @return the isvflpimanwlHandler
     */
    public WithdrawLicHandler getIsvflpimanwlHandler() {
        return isvflpimanwlHandler;
    }

    /**
     * @param isvflpimanwlHandler the isvflpimanwlHandler to set
     */
    public void setIsvflpimanwlHandler(WithdrawLicHandler isvflpimanwlHandler) {
        this.isvflpimanwlHandler = isvflpimanwlHandler;
    }

    /**
     * @return the isvflpidynwlHandler
     */
    public WithdrawLicHandler getIsvflpidynwlHandler() {
        return isvflpidynwlHandler;
    }

    /**
     * @param isvflpidynwlHandler the isvflpidynwlHandler to set
     */
    public void setIsvflpidynwlHandler(WithdrawLicHandler isvflpidynwlHandler) {
        this.isvflpidynwlHandler = isvflpidynwlHandler;
    }

}