com.its.web.controllers.LicensesManagerController.java Source code

Java tutorial

Introduction

Here is the source code for com.its.web.controllers.LicensesManagerController.java

Source

package com.its.web.controllers;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.its.web.db.mappings.License;
import com.its.web.db.mappings.LicenseType;
import com.its.web.services.LicensesService;
import com.its.web.services.LicensesTypesService;

@Controller
public class LicensesManagerController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    LicensesService licensesService;

    @Autowired
    LicensesTypesService licensesTypesService;

    @RequestMapping(value = "licensesInstancesList.do", method = RequestMethod.GET)
    public @ResponseBody List<License> licensesInstancesList(@RequestParam("licenseTypeId") Long licenseTypeId) {

        log.debug("licenseTypeId :" + licenseTypeId);
        List<License> licenses = licensesService.findByLicenseTypeId(licenseTypeId);
        return licenses;
    }

    @RequestMapping(value = "licensesInstancesNewItem.do", method = RequestMethod.GET)
    public @ResponseBody License licensesInstancesNewItem(@RequestParam("licenseTypeId") Long licenseTypeId) {

        log.debug("licenseTypeId :" + licenseTypeId);

        LicenseType relatedLicenseType = licensesTypesService.getById(licenseTypeId);

        License license = new License();

        license.setActivationCode(licensesService.generateCode());
        license.setEncryptionKey(licensesService.generatePassword());
        license.setLicenseTypeId(relatedLicenseType.getId());
        license.setProductId(relatedLicenseType.getProductId());
        license.setThisTypeIsFull(relatedLicenseType.isThisTypeIsFull());
        license.setTrialPeriod(relatedLicenseType.getTrialPeriod());

        return license;
    }

    @RequestMapping(value = "saveOrUpdateLicenseInstance.do", method = RequestMethod.POST)
    public @ResponseBody Boolean saveOrUpdate(@RequestBody License license) {

        log.debug("getId------------->" + license.getId());
        log.debug("getActivationCode------------->" + license.getActivationCode());
        log.debug("getEncryptionKey------------->" + license.getEncryptionKey());
        log.debug("getLicenseTypeId------------->" + license.getLicenseTypeId());
        log.debug("getStartDate------------->" + license.getStartDate());
        log.debug("getTrialPeriod------------->" + license.getTrialPeriod());
        log.debug("isThisTypeIsFull------------->" + license.isThisTypeIsFull());

        boolean operation = licensesService.saveOrUpdate(license);
        return operation;
    }

    @RequestMapping(value = "deleteLicenseInstance.do", method = RequestMethod.POST)
    public @ResponseBody Boolean delete(@RequestBody License license) {

        log.debug("getId------------->" + license.getId());
        boolean operation = licensesService.remove(license.getId());

        return operation;
    }

    @RequestMapping(value = "generateActivationCodeLicense.do", method = RequestMethod.GET)
    public @ResponseBody String generateCode() {
        String code = licensesService.generateCode();
        log.debug("generateActivationCodeLicense------------->" + code);
        return code;
    }

    @RequestMapping(value = "generatePassword.do", method = RequestMethod.GET)
    public @ResponseBody String generatePassword() {
        String password = licensesService.generatePassword();
        log.debug("generatePassword------------->" + password);
        return password;
    }

    @RequestMapping(value = "generateLicenseFile.do", method = RequestMethod.GET)
    public void generateLicenseFile(@RequestParam("licenseId") Long licenseId, HttpServletResponse response) {

        log.debug("generateLicenseFile------------->" + licenseId);

        //read from database
        License currentLicense = licensesService.getById(licenseId);

        InputStream is = licensesService.generateLicenseFile(currentLicense);

        String fileName = "License_File_" + currentLicense.getClientName().replace(" ", "_") + ".its";

        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

        try {
            // copy it to response's OutputStream
            IOUtils.copy(is, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}