edu.byu.softwareDist.manager.impl.LicenseManagerImpl.java Source code

Java tutorial

Introduction

Here is the source code for edu.byu.softwareDist.manager.impl.LicenseManagerImpl.java

Source

package edu.byu.softwareDist.manager.impl;

import edu.byu.softwareDist.dao.LicenseDao;
import edu.byu.softwareDist.dao.LicenseSetDao;
import edu.byu.softwareDist.dao.ProductDao;
import edu.byu.softwareDist.dao.PurchaseKeyDao;
import edu.byu.softwareDist.domain.*;
import edu.byu.softwareDist.domain.licenses.UniqueKeyLicense;
import edu.byu.softwareDist.manager.LicenseManager;
import edu.byu.softwareDist.manager.LicenseUnavailableException;
import edu.byu.softwareDist.manager.SendEmail;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.*;

import static org.springframework.transaction.annotation.Propagation.REQUIRED;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

/**
 * @author Tyler Southwick (tyler_southwick@byu.edu)
 */
@Service("licenseManager")
@SuppressWarnings("unused")
public class LicenseManagerImpl implements LicenseManager {
    private static final Logger LOG = Logger.getLogger(LicenseManagerImpl.class);

    private final LicenseSetDao licenseSetDao;
    private final LicenseDao licenseDao;
    private final PurchaseKeyDao purchaseKeyDao;
    private final ProductDao productDao;
    private final SendEmail sendEmail;

    @Autowired
    public LicenseManagerImpl(LicenseSetDao licenseSetDao, LicenseDao licenseDao, PurchaseKeyDao purchaseKeyDao,
            ProductDao productDao, SendEmail sendEmail) {
        this.licenseSetDao = licenseSetDao;
        this.licenseDao = licenseDao;
        this.purchaseKeyDao = purchaseKeyDao;
        this.productDao = productDao;
        this.sendEmail = sendEmail;
    }

    @Override
    @Transactional(readOnly = false, propagation = REQUIRED)
    public LicenseSet saveLicenseSet(LicenseSet licenseSet, List<License> licenses) {
        final Set<License> newLicenses = new HashSet<License>(licenses);
        LicenseSet saved;
        if (licenseSet.getLicenseSetId() == null) {
            saved = licenseSetDao.save(licenseSet);

            for (License license : newLicenses) {
                license.setLicenseSet(saved);
                licenseDao.save(license);
            }
        } else {
            //find the old one
            final LicenseSet old = licenseSetDao.findById(licenseSet.getLicenseSetId());
            if (old.getSetType() != licenseSet.getSetType()) {
                throw new IllegalArgumentException("Cannot change type of a license set");
            }
            saved = licenseSetDao.update(licenseSet);
            int licenseSetId = saved.getLicenseSetId();
            //find all the licenses that are new -- we don't delete licenseSets
            final List<License> oldLicenses = licenseDao.findAllByLicenseSetId(licenseSetId);
            Collections.sort(oldLicenses);
            switch (licenseSet.getSetType()) {
            case COUNT_NO_KEY: {
                for (final License license : newLicenses) {
                    license.setLicenseSet(saved);
                    licenseDao.save(license);
                }
                break;
            }
            case COUNT_KEY: {
                if (oldLicenses == null || oldLicenses.isEmpty()) {//add the license to the set
                    for (final License nl : newLicenses) {
                        nl.setLicenseSet(saved);
                        licenseDao.save(nl);
                    }
                } else {//update the existing license
                    //all the keys in this license set are the same, so the first license is as good as any
                    Assert.isTrue(oldLicenses.size() == 1,
                            "There should only be one license key in this type of license set.");
                    final License oldLic = oldLicenses.get(0);
                    final String key = oldLic.getLicenseKey();
                    Assert.notNull(key);
                    for (final License license : newLicenses) {
                        license.setLicenseSet(saved);
                        if (!key.equals(license.getLicenseKey())) {
                            throw new IllegalStateException(
                                    "License key must be the same for all licenses in this licence set");
                        }
                        if (license.getMaxQty() != null)
                            oldLic.setMaxQty(license.getMaxQty());
                        if (license.getStatus() != null)
                            oldLic.setStatus(license.getStatus());
                        licenseDao.update(oldLic);
                    }
                }
                break;
            }
            case UNIQUE: {
                //verify that we aren't adding any new keys that already exist in the license set
                for (License license : newLicenses) {
                    final String key = license.getLicenseKey();
                    Assert.notNull(key);
                    final UniqueKeyLicense x = new UniqueKeyLicense(key);
                    license.setLicenseSet(old);
                    if (!oldLicenses.contains(x)) {
                        licenseDao.save(license);
                    }
                }
                break;
            }
            case UNLIMIT_KEY: {
                /**
                 * It doesn't make sense to add another unlimited with same key license because it will never be used.
                 * It should be added to a new license set
                 */
                break;
            }
            case UNLIMIT_NO_KEY: {
                /**
                 * It doesn't make sense to add another unlimited with no key license because it will never be used.
                 * It should be added as a new license set if needed
                 */
                break;
            }
            default:
                assert false;
            }
        }

        return saved;
    }

    @Transactional(readOnly = false, propagation = REQUIRES_NEW)
    private List<License> acquireLicenses(List<License> licenses, int quantity, Product product) {
        LOG.debug("acquiring licenses...");
        List<License> acquiredLicenses = new LinkedList<License>();
        int left = quantity;
        for (License license : licenses) {
            int count = license.aquire(left);
            if (count > 0) {
                acquiredLicenses.add(license);
                licenseDao.update(license);
            }
            left -= count;
            if (left == 0) {
                break;
            }
        }
        if (left > 0) {
            throw new LicenseUnavailableException(quantity, product);
        }
        return acquiredLicenses;
    }

    @Override
    @Transactional(readOnly = false, propagation = REQUIRES_NEW)
    public List<License> assignLicenses(Product product, int quantity) {
        List<LicenseSet> licenseSets = licenseSetDao.findAllByProduct(product);

        List<License> acquiredLicenses = new LinkedList<License>();
        for (LicenseSet licenseSet : licenseSets) {
            if (licenseSet.getSetType() != LicenseSetType.UNLIMIT_NO_KEY) {
                List<License> licenses = licenseDao.lockAllAvailableByLicenseSetId(licenseSet.getLicenseSetId());
                acquiredLicenses.addAll(acquireLicenses(licenses, quantity, product));
            }
        }

        return acquiredLicenses;
    }

    @Override
    @Transactional(readOnly = false, propagation = REQUIRES_NEW)
    public void returnLicenses(List<License> licenses) {
        for (License license : licenses) {
            long licenseId = license.getLicenseId();
            License newLicense = licenseDao.lockById(licenseId);
            newLicense.giveBack(license);
            licenseDao.update(newLicense);
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = REQUIRES_NEW)
    public void associateLicenses(PurchaseLineItem item, List<License> licenses) {
        for (License license : licenses) {
            PurchaseKey key = new PurchaseKey();
            key.setLicenseKey(license.getLicenseKey());
            key.setLicense(license);
            key.setPurchaseLineItemId(item.getPurchaseLineItemId());
            key.setCount(license.getAquiredCount());
            purchaseKeyDao.save(key);
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = REQUIRES_NEW)
    public void returnLicenses(PurchaseLineItem item) {
        List<PurchaseKey> keys = purchaseKeyDao.findByPurchaseLineItemId(item.getPurchaseLineItemId());
        for (PurchaseKey key : keys) {
            License license = licenseDao.lockById(key.getLicense().getLicenseId());
            license.giveBack(key);
            licenseDao.update(license);
            purchaseKeyDao.delete(key);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void updateLicense(License license, boolean pushToPurchases) {
        long licenseId = license.getLicenseId();
        String key = license.getLicenseKey();
        licenseDao.updateLicense(licenseId, key);

        if (pushToPurchases) {
            purchaseKeyDao.updateLicenses(licenseId, key);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public boolean isReturnable(final LineItem purchaseLineItem) {
        if (purchaseLineItem == null) {
            return false;
        }
        if (!purchaseLineItem.isReturnable()) {//If not returnable
            return false;
        }
        if (purchaseLineItem instanceof MaintenanceLineItem) {
            return false;
        }
        final List<LicenseSet> licenseSets = licenseSetDao
                .findByPurchaseLineItemId(purchaseLineItem.getLineItemId());
        if (licenseSets.size() == 0) {
            return true;
        }
        LOG.debug("LicenseSet size=" + licenseSets.size());
        for (final LicenseSet licenseSet : licenseSets) {
            if (LicenseSetType.UNIQUE.equals(licenseSet.getSetType())) {
                return false;
            }
        }
        return true;
    }

    @Transactional(readOnly = true)
    public boolean isReturnable(final Integer productId) {
        final List<LicenseSet> licenseSets = licenseSetDao.findIsUniqueSetByProductId(productId);
        return licenseSets.size() == 0;
    }
}