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

Java tutorial

Introduction

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

Source

/**
 * $Id$
 */
package com.lm.lic.manager.controller;

import java.util.List;

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

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

import com.lm.lic.manager.form.WithdrawLicForm;
import com.lm.lic.manager.hibernate.License;
import com.lm.lic.manager.hibernate.LicenseBlock;
import com.lm.lic.manager.hibernate.LicensePaymentStatus;
import com.lm.lic.manager.hibernate.Product;
import com.lm.lic.manager.hibernate.RequestForLicense;
import com.lm.lic.manager.util.GenUtil;

/**
 * @author Ibrahim Mustafa
 */
public class IsvManWithdrawLicHandler extends AbstractWithdrawLicController implements WithdrawLicHandler {

    private final Logger logger = Logger.getLogger(IsvManWithdrawLicHandler.class);

    /**
     * @see com.lm.lic.manager.controller.WithdrawLicHandler#handleWithdrawal(javax.servlet.http.HttpServletRequest,
     *      com.lm.lic.manager.form.WithdrawLicForm)
     */
    @SuppressWarnings("deprecation")
    @Override
    public ModelAndView handleWithdrawal(HttpServletRequest request, HttpServletResponse response,
            WithdrawLicForm wlf) {
        logger.info("Start ISV Handling of License Withdrawal Request");
        boolean valid = verifyIsvRequest(request);

        logger.info("ISV_MANUAL_REQUEST_VALID? : " + valid);

        Product product = null;
        String productKey = wlf.getProductKey();
        int numLics = wlf.getQty();
        String isvId = wlf.getIsvId();

        if (StringUtils.isNotEmpty(isvId))
            product = productService.findProductByProductKey(isvId, productKey);

        if (product != null)
            logger.info("ISV_MANUAL License Withdrawal for product: " + product.getName() + " "
                    + product.getVersion() + " ISV: " + product.getIsv().getName());

        RequestForLicense prevRfl = findExistingLicWithdrawalRecord(product, wlf, request);

        String prodId = product.getId() + EMPTY;
        isvId = product.getIsv().getId() + EMPTY;

        int numOverdraft = 0;
        LicenseBlock licenseBlock = licenseBlockService.findByIsvIdProdId(isvId, prodId);
        String localeLang = extractLocaleLang(request);
        if (licenseBlock == null) {
            numOverdraft = numLics;
            logger.info("Found 0 " + "Lics to withdraw for ISV - Going OVERDRAFT");
            licenseAvailabilityAdvisor.generateLicenses(wlf.getDeviceId(), localeLang, product, numLics,
                    numOverdraft, LicensePaymentStatus.OVERDRAFT);
            licenseBlock = licenseBlockService.findByIsvIdProdId(isvId, prodId);
        }

        List<License> licenses = findDecentLicenses(wlf.getLicKey(), numLics, product, wlf.getDeviceId());
        if (licenses == null || licenses.size() < numLics) {
            numOverdraft = numLics - licenses.size();
            licenseAvailabilityAdvisor.generateLicenses(wlf.getDeviceId(), localeLang, product, numOverdraft,
                    numOverdraft, LicensePaymentStatus.OVERDRAFT);
            licenses = findDecentLicenses(wlf.getLicKey(), numLics, product, wlf.getDeviceId());
        }

        RequestForLicense currRfl = findRequestForLicenseTrace(request, wlf);
        if (licenses != null) {
            adjustDrawnLicenses(wlf, licenses, prevRfl, currRfl);
            licenseService.update(licenses);

            adjustLicenseBlock(licenseBlock, numLics, numOverdraft);
            licenseBlockService.update(licenseBlock);
            logger.info("Found " + licenses.size() + " Lics for ISV");
        } else
            licenses = generateOverDraftLicenses(prevRfl, currRfl);

        for (License l : licenses)
            logger.info("Generated license for ISV_MANUAL request: " + l.getLicKey() + " for product: "
                    + l.getProduct().getName() + " " + l.getProduct().getVersion());

        if (currRfl != null)
            adjustRequestForLicenseTransaction(product, currRfl, prevRfl, licenses, numOverdraft);

        String successView = getSuccessView();
        ModelAndView modelAndView = new ModelAndView(successView);
        modelAndView.addObject("isvId", isvId);
        modelAndView.addObject("prodId", prodId);
        modelAndView.addObject("licenses", licenses);
        modelAndView.addObject("rfl", currRfl);
        modelAndView.addObject("product", product);
        return modelAndView;
    }

    public boolean verifyIsvRequest(HttpServletRequest request) {
        boolean valid = false;
        String isvId = GenUtil.findLoggedinIsv(request, loginService);
        valid = StringUtils.isNotEmpty(isvId) && StringUtils.isNumeric(isvId);
        return valid;
    }
}