com.sycros.SCWebConsole.business.service.LicenseBzSvcImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sycros.SCWebConsole.business.service.LicenseBzSvcImpl.java

Source

package com.sycros.SCWebConsole.business.service;

import java.security.MessageDigest;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sycros.AppFrameWork.constant.ProductEnum;
import com.sycros.SCWebConsole.business.dao.LicenseBzDao;
import com.sycros.SCWebConsole.business.domain.dto.LicenseBzDto;
import com.sycros.SCWebConsole.business.domain.vo.LicenseBzVo;

@Service(value = "licenseBzSvc")
@Transactional(readOnly = true)
public class LicenseBzSvcImpl implements LicenseBzSvc {

    private static final Logger logger = LoggerFactory.getLogger(LicenseBzSvcImpl.class);

    @Resource(name = "sqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private LicenseBzDao licenseBzDao;

    @Override
    public List<LicenseBzVo> retrieveLicenseList(LicenseBzDto licenseBzDto) throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<LicenseBzVo> licensBzVoList = null;

        StringBuffer finalQuerySql = new StringBuffer();
        String productName = null;
        String productCount = "productCount";
        String sqlLicenseProduct = "SUM(CASE WHEN p.PRODUCT_NAME = \'%1\' THEN COUNT ELSE 0 END ) AS %2,";
        ProductEnum[] productList = ProductEnum.values();

        try {
            licenseBzDto.setDatabase(sqlSession.getConfiguration().getDatabaseId());

            int postFix = 0;
            for (ProductEnum pEnum : productList) {

                productCount = String.format("pCount%d", postFix++);
                productName = pEnum.getString();
                finalQuerySql.append(sqlLicenseProduct.replace("%1", productName).replace("%2", productCount));
            }

            finalQuerySql.deleteCharAt(finalQuerySql.length() - 1);
            logger.info("Final sql : " + finalQuerySql.toString());

            licenseBzDto.setDynamicSql(finalQuerySql.toString());
            licensBzVoList = licenseBzDao.selectLicenseList(sqlSession, licenseBzDto);

            logger.info("Result : " + licensBzVoList.size());

            for (LicenseBzVo lvo : licensBzVoList) {
                logger.info("PublishedNumber    :" + lvo.getPublishedNumber());
                logger.info("CustomerName   :" + lvo.getCustomerName());
                logger.info("ExpiryUnlimited   :" + lvo.getExpiryUnlimited());
                logger.info("ExpiryDate      :" + lvo.getExpiryDate());
                logger.info("License      :" + lvo.getLicense());
                logger.info("CreateDate      :" + lvo.getCreateDate());
                logger.info("Description   :" + lvo.getDescription());
                logger.info("ProductCountList   :" + lvo.getProductCountList());
                for (ProductEnum pEnum : productList) {
                    logger.info(pEnum.getString() + " : " + lvo.getProductCountMap().get(pEnum.name()));
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return licensBzVoList;
    }

    @Override
    public LicenseBzVo retrieveLicenseInfo(LicenseBzDto licenseBzDto) throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LicenseBzVo licensBzVoData = null;

        StringBuffer finalQuerySql = new StringBuffer();
        String productName = null;
        String productCount = "productCount";
        String sqlLicenseProduct = "SUM(CASE WHEN p.PRODUCT_NAME = \'%1\' THEN COUNT ELSE 0 END ) AS %2,";
        ProductEnum[] productList = ProductEnum.values();

        try {

            licenseBzDto.setDatabase(sqlSession.getConfiguration().getDatabaseId());

            int postFix = 0;
            for (ProductEnum pEnum : productList) {

                productCount = String.format("pCount%d", postFix++);
                productName = pEnum.getString();
                finalQuerySql.append(sqlLicenseProduct.replace("%1", productName).replace("%2", productCount));
            }

            finalQuerySql.deleteCharAt(finalQuerySql.length() - 1);
            logger.info("Final sql : " + finalQuerySql.toString());

            licenseBzDto.setDynamicSql(finalQuerySql.toString());
            licensBzVoData = licenseBzDao.selectLicenseInfo(sqlSession, licenseBzDto);
            if (null == licensBzVoData) {
                return null;
            }

            logger.info("PublishedNumber    :" + licensBzVoData.getPublishedNumber());
            logger.info("CustomerName   :" + licensBzVoData.getCustomerName());
            logger.info("ExpiryUnlimited   :" + licensBzVoData.getExpiryUnlimited());
            logger.info("ExpiryDate      :" + licensBzVoData.getExpiryDate());
            logger.info("License      :" + licensBzVoData.getLicense());
            logger.info("CreateDate      :" + licensBzVoData.getCreateDate());
            logger.info("Description   :" + licensBzVoData.getDescription());
            logger.info("ProductCountList   :" + licensBzVoData.getProductCountList());
            for (ProductEnum pEnum : productList) {
                logger.info(pEnum.getString() + " : " + licensBzVoData.getProductCountMap().get(pEnum.name()));
            }

        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return licensBzVoData;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = { Exception.class })
    public int createLicenseInfo(LicenseBzDto licenseBzDto) throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<LicenseBzVo> licenseBzVoList = null;
        StringBuffer seed = new StringBuffer();
        ProductEnum[] productList = ProductEnum.values();

        try {
            seed.append(licenseBzDto.getPublishedNumber());
            seed.append(licenseBzDto.getCustomerName());
            seed.append((1 == licenseBzDto.getExpiryUnlimited()) ? "Unlimited" : licenseBzDto.getExpiryDate());

            for (ProductEnum pNum : productList) {
                if (0 == licenseBzDto.getProductCounts()[pNum.getInteger()]) {
                    continue;
                }
                seed.append(pNum.getString());
                seed.append(licenseBzDto.getProductCounts()[pNum.getInteger()]);
            }

            if (!isLicenseValidate(licenseBzDto.getLicense(), seed.toString())) {
                throw new Exception("Invalid License");
            }

            licenseBzDto.setDynamicSql("1");
            licenseBzDto.setExpiryDate(licenseBzDto.getExpiryDate().replaceAll("-", ""));
            licenseBzVoList = licenseBzDao.selectLicenseList(sqlSession, licenseBzDto);
            if (null != licenseBzVoList && 0 < licenseBzVoList.size()) {
                throw new Exception("Duplicated PUB_NUMBER or LICENSE" + licenseBzVoList.size());
            }

            if (0 >= licenseBzDao.insertLicenseInfo(sqlSession, licenseBzDto)) {
                throw new Exception("Failed to insert data into LICENSE tbl");
            }

            for (ProductEnum pNum : productList) {
                licenseBzDto.setProductName(pNum.getString());
                licenseBzDto.setCount(licenseBzDto.getProductCounts()[pNum.getInteger()]);
                if (0 >= licenseBzDao.insertLicenseProductInfo(sqlSession, licenseBzDto)) {
                    throw new Exception("Failed to insert data into LICENSE_PRODUCT tbl");
                }
            }

            logger.info("Succeed to insert data into LICENSE & LICENSE_PRODUCT tbl");

        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return 0;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = { Exception.class })
    public int changeLicenseInfo(LicenseBzDto licenseBzDto) throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StringBuffer seed = new StringBuffer();
        ProductEnum[] productList = ProductEnum.values();

        try {
            seed.append(licenseBzDto.getPublishedNumber());
            seed.append(licenseBzDto.getCustomerName());
            seed.append((1 == licenseBzDto.getExpiryUnlimited()) ? "Unlimited" : licenseBzDto.getExpiryDate());
            for (ProductEnum pNum : productList) {
                if (0 == licenseBzDto.getProductCounts()[pNum.getInteger()]) {
                    continue;
                }
                seed.append(pNum.getString());
                seed.append(licenseBzDto.getProductCounts()[pNum.getInteger()]);
            }

            if (!isLicenseValidate(licenseBzDto.getLicense(), seed.toString())) {
                throw new Exception("Invalid License");
            }

            licenseBzDto.setExpiryDate(licenseBzDto.getExpiryDate().replaceAll("-", ""));
            if (0 >= licenseBzDao.updateLicenseInfo(sqlSession, licenseBzDto)) {
                if (0 >= licenseBzDao.insertLicenseInfo(sqlSession, licenseBzDto)) {
                    throw new Exception("Failed to update LICENSE tbl");
                }
            }

            if (0 < licenseBzDao.deleteLicenseProductInfo(sqlSession, licenseBzDto)) {

                for (ProductEnum pNum : productList) {
                    licenseBzDto.setProductName(pNum.getString());
                    licenseBzDto.setCount(licenseBzDto.getProductCounts()[pNum.getInteger()]);
                    if (0 >= licenseBzDao.insertLicenseProductInfo(sqlSession, licenseBzDto)) {
                        throw new Exception("Failed to update LICENSE_PRODUCT tbl");
                    }
                }
            }

            logger.info("Succeed to update LICENSE & LICENSE_PRODUCT tbl");

        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return 0;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = { Exception.class })
    public int removeLicenseInfo(LicenseBzDto licenseBzDto) throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            if (0 >= licenseBzDao.deleteLicenseInfo(sqlSession, licenseBzDto)) {
                throw new Exception("Failed to remove data from LICENSE tbl");
            }
            if (0 >= licenseBzDao.deleteLicenseProductInfo(sqlSession, licenseBzDto)) {
                throw new Exception("Failed to remove data from LICENSE_PRODUCT tbl");
            }

            logger.info("Succeed to remove data from LICENSE & LICENSE_PRODUCT tbl");

        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return 0;
    }

    public boolean isLicenseValidate(String license, String strLicense) {

        StringBuffer sb = new StringBuffer();

        try {
            MessageDigest sh = MessageDigest.getInstance("MD5");
            sh.update(strLicense.getBytes());
            byte byteData[] = sh.digest();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }

            logger.info("Coded by MD5 : " + sb.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        license = license.replaceAll("-", "").toLowerCase();
        logger.info("license:" + license);
        if (license.equals(sb.toString())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<String> retrieveProductList() throws Exception {
        // TODO Auto-generated method stub
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<String> productList = null;

        try {

            productList = licenseBzDao.selectProductList(sqlSession);
            logger.info("Result : " + productList.size());

        } catch (Exception e) {
            throw e;
        } finally {
            sqlSession.close();
        }

        return productList;
    }
}