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

Java tutorial

Introduction

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

Source

/**
 * $Id: GenerateLicensesPerPayController.java 1478 2010-05-14 01:56:14Z imustafa $
 */
package com.lm.lic.manager.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
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.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.lm.lic.manager.command.GenerateLicensesPerPayCommand;
import com.lm.lic.manager.hibernate.DecomLicenseReason;
import com.lm.lic.manager.hibernate.Isv;
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.management.IsvLicensePricingManager;
import com.lm.lic.manager.management.LicenseManager;
import com.lm.lic.manager.service.IsvService;
import com.lm.lic.manager.service.LicenseBlockService;
import com.lm.lic.manager.service.LicenseService;
import com.lm.lic.manager.service.ProductService;
import com.lm.lic.manager.util.GenUtil;

/**
 * @author Ibrahim Mustafa
 */
public class GenerateLicensesPerPayController extends BaseLicenseManagerCommandController {
    public static final String CLASS_VERSION = "$Id: GenerateLicensesPerPayController.java 1478 2010-05-14 01:56:14Z imustafa $";

    static Logger logger = Logger.getLogger(GenerateLicensesPerPayController.class);

    private String formView;

    private IsvService isvService;
    private ProductService productService;
    private IsvLicensePricingManager isvLicensePricingManager;
    private LicenseBlockService licenseBlockService;
    private LicenseService licenseService;
    private LicenseManager licenseManager;

    /**
      * 
      */
    @Override
    protected ModelAndView handle(HttpServletRequest req, HttpServletResponse response, Object command,
            BindException bindException) throws Exception {

        GenerateLicensesPerPayCommand glppc = (GenerateLicensesPerPayCommand) command;
        Product product = pullProductUnderManagement(req);
        Isv isv = product.getIsv();

        LicenseBlock lb = makeLicenseBlock(glppc, isv, product);
        Integer numLicenses = lb.getNumLicenses();
        Integer numLeft = lb.getNumLeft();
        Long totalNumBought = lb.getTotalNumBought();
        LicenseBlock foundLicenseBlock = licenseBlockService.findByIsvIdProdId(isv.getId(), product.getId());

        if (foundLicenseBlock == null) {
            licenseBlockService.save(lb);
        } else {
            numLeft += foundLicenseBlock.getNumLeft();
            foundLicenseBlock.setNumLeft(numLeft);

            Integer newNumLicenses = numLicenses + foundLicenseBlock.getNumLicenses();
            foundLicenseBlock.setNumLicenses(newNumLicenses);

            totalNumBought += foundLicenseBlock.getTotalNumBought();
            foundLicenseBlock.setTotalNumBought(totalNumBought);

            foundLicenseBlock.setPricePerLicense(lb.getPricePerLicense());
            Double totalPrice = foundLicenseBlock.getTotalPrice() + lb.getTotalPrice();
            foundLicenseBlock.setTotalPrice(totalPrice);
            licenseBlockService.update(foundLicenseBlock);
            lb = foundLicenseBlock;
        }

        int lifeInDays = glppc.getOption_selection2();
        List<String> licKeys = licenseManager.generate(numLicenses, product.getLicKeyLength());
        List<License> licenses = extractRawLicenses(req, licKeys, lifeInDays, lb, isv, product);
        licenseService.save(licenses);

        ModelAndView modelAndView = new ModelAndView(formView);

        return modelAndView;
    }

    private Product pullProductUnderManagement(HttpServletRequest request) {
        String prodId = findProductUnderManagement(request);
        Product product = productService.findProdById(prodId);
        removeProductUnderManagement(request);
        return product;
    }

    private List<License> extractRawLicenses(HttpServletRequest req, List<String> licKeys, int lifeInDays,
            LicenseBlock lb, Isv isv, Product product) {
        List<License> licenses = new ArrayList<License>(licKeys.size());

        for (String lk : licKeys) {
            boolean inUse = false;
            String endUserName = StringUtils.EMPTY;
            String endUserCell = StringUtils.EMPTY;
            String endUserPin = StringUtils.EMPTY;

            Calendar calendar = Calendar.getInstance();
            Timestamp dateIssued = new Timestamp(calendar.getTimeInMillis());

            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 10);
            Timestamp dateActivated = new Timestamp(calendar.getTimeInMillis());
            Timestamp dateExpires = new Timestamp(calendar.getTimeInMillis());

            Boolean decom = false;
            String decomReason = DecomLicenseReason.NOT_YET.name();
            String shadowLicKey = GenUtil.EMPTY;

            Double cost = lb.getPricePerLicense();
            Double price = lb.getPricePerLicense();
            String localeLang = BaseLicenseManagerFormController.extractLocaleLang(req);

            License lic = new License(lb, isv, product, null, inUse, lk, lifeInDays, decom, decomReason,
                    shadowLicKey, LicensePaymentStatus.PAID_GEN.name(), endUserName, endUserCell, endUserPin,
                    dateIssued, dateActivated, dateExpires, cost, price, localeLang, GenUtil.DEFAULT_USER,
                    GenUtil.CURRENT_TIME, GenUtil.DEFAULT_USER, GenUtil.CURRENT_TIME);
            licenses.add(lic);

        }
        return licenses;
    }

    private LicenseBlock makeLicenseBlock(GenerateLicensesPerPayCommand glppc, Isv isv, Product product) {

        Double pricePerLicense = isvLicensePricingManager.findIsvLicensePrice(isv.getId(), product.getId());
        Integer numLicenses = calculateNumLicenses(glppc, pricePerLicense);
        Integer numLeft = numLicenses;
        long totalNumBought = numLicenses;
        Double totalPrice = numLicenses * pricePerLicense;
        Integer numOverdraft = 0;
        LicenseBlock lb = new LicenseBlock(isv, product, numLeft, numOverdraft, numLicenses, totalNumBought,
                pricePerLicense, totalPrice, GenUtil.CURRENT_TIME, GenUtil.DEFAULT_USER, GenUtil.CURRENT_TIME,
                GenUtil.DEFAULT_USER, GenUtil.CURRENT_TIME, null);
        return lb;
    }

    private Integer calculateNumLicenses(GenerateLicensesPerPayCommand glppc, Double ppl) {
        Double amount = glppc.getPayment_gross();
        Double numLicenses = amount / ppl;
        Integer nl = numLicenses.intValue();
        return nl;
    }

    /**
     * @return the formView
     */
    @Override
    public String getFormView() {
        return this.formView;
    }

    /**
     * @param formView the formView to set
     */
    @Override
    public void setFormView(String formView) {
        this.formView = formView;
    }

    /**
     * @return the isvService
     */
    @Override
    public IsvService getIsvService() {
        return this.isvService;
    }

    /**
     * @param isvService the isvService to set
     */
    @Override
    public void setIsvService(IsvService isvService) {
        this.isvService = isvService;
    }

    /**
     * @return the productService
     */
    @Override
    public ProductService getProductService() {
        return this.productService;
    }

    /**
     * @param productService the productService to set
     */
    @Override
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    /**
     * @return the isvLicensePricingManager
     */
    public IsvLicensePricingManager getIsvLicensePricingManager() {
        return this.isvLicensePricingManager;
    }

    /**
     * @param isvLicensePricingManager the isvLicensePricingManager to set
     */
    public void setIsvLicensePricingManager(IsvLicensePricingManager isvLicensePricingManager) {
        this.isvLicensePricingManager = isvLicensePricingManager;
    }

    /**
     * @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 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 licenseManager
     */
    public LicenseManager getLicenseManager() {
        return this.licenseManager;
    }

    /**
     * @param licenseManager the licenseManager to set
     */
    public void setLicenseManager(LicenseManager licenseManager) {
        this.licenseManager = licenseManager;
    }
}