Java tutorial
/** * $Id$ */ package com.lm.lic.manager.controller; import java.sql.Timestamp; import java.util.Calendar; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.lm.lic.manager.form.WithdrawLicForm; import com.lm.lic.manager.hibernate.HashedLicMechConfig; 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.management.HashedLicKeyGenerator; import com.lm.lic.manager.service.LicenseBlockService; import com.lm.lic.manager.service.LicenseService; import com.lm.lic.manager.service.RequestForLicenseService; /** * @author Ibrahim Mustafa */ public class AbstractWithdrawLicController extends BaseLicenseManagerFormController { static Logger logger = Logger.getLogger(AbstractWithdrawLicController.class); protected LicenseService licenseService; protected LicenseBlockService licenseBlockService; protected RequestForLicenseService requestForLicenseService; protected LicenseAvailabilityAdvisor licenseAvailabilityAdvisor; protected HashedLicKeyGenerator hashedLicKeyGenerator; /** * Determine if this request for a license for the product from thsi device exceeds the maximum * num trial downloads or not. This is only for trial lics and not permanent ones. * * @param product * @param wlf * @param request * @return */ protected boolean exceedsMaxAllowedTrialLicsPerProduc(Product product, RequestForLicense rfl) { if (rfl == null) return false; Integer numDeviceRequests = rfl.getNumDeviceRequests(); if (numDeviceRequests > product.getNumTrialDownloads()) return true; return false; } /** * @param product * @param wlf * @param request * @return */ protected RequestForLicense findExistingLicWithdrawalRecord(Product product, WithdrawLicForm wlf, HttpServletRequest request) { String requestorIp = request.getRemoteAddr(); String deviceId = wlf.getDeviceId(); if (StringUtils.isEmpty(deviceId)) deviceId = wlf.getPIN(); RequestForLicense rfl = requestForLicenseService.findByDevIdProdId(deviceId, product.getId(), requestorIp); return rfl; } /** * If the product was deleted or disabled, no license can be acquired for it. * * @param licKey * @param numLics * @param product * @param deviceId * @return */ protected List<License> findDecentLicenses(String licKey, int numLics, Product product, String deviceId) { String prodId = product.getId() + EMPTY; List<License> licenses = null; if (!product.canAcquireLicenseFor()) return licenses; if (StringUtils.isNotEmpty(licKey) && numLics == 1) { licenses = licenseService.findLicenses(LicenseService.KEY, licKey, prodId); License lic = licenses.get(0); if (lic.getInUse()) licenses = licenseService.findUnusedProdLicenses(prodId, numLics); } else licenses = licenseService.findUnusedProdLicenses(prodId, numLics); /** * The initial set of licenses that are given for free when adding a product are not real * licenses. Do the hashing again of the secret key and the device ID to produce a real hashed * license key. */ if (product.isHashedLicMechType()) hashStubbedLicenses(licenses, product, deviceId); return licenses; } /** * When a new product is added, a set of free licenses are given for the product. in the case of * hashed license mechanism type, the keys are generated as stubs since the hash is a function of * the deviceId which is not available when the product is added. Do the rehash now if the license * is part of those set of keys making use of the deviceId along witht he values in the product * confgiuration. * * @param licenses * @param product * @param deviceId */ private void hashStubbedLicenses(List<License> licenses, Product product, String deviceId) { if (!product.isHashedLicMechType()) return; for (License lic : licenses) { HashedLicMechConfig hlmConfig = product.getOwnHashedLicMechConfig(); String hashedKey = hashedLicKeyGenerator.generate(deviceId, hlmConfig, LicensePaymentStatus.valueOf(lic.getPaymentStatus())); lic.setLicKey(hashedKey); } } /** * @param form * @param license */ protected void adjustDrawnLicenses(WithdrawLicForm form, List<License> licenses, RequestForLicense prevRfl, RequestForLicense currRfl) { for (License lic : licenses) { lic.setRequestForLicense(currRfl); adjustDrawnLicense(form, lic); } } /** * @param form * @param license */ protected void adjustDrawnLicense(WithdrawLicForm form, License license) { license.setInUse(true); Timestamp dateActivated = new Timestamp(System.currentTimeMillis()); license.setDateActivated(dateActivated); String endUserEmail = form.getEmail(); if (StringUtils.isEmpty(endUserEmail)) endUserEmail = form.getEmailAddress(); license.setEndUserEmail(chooseSecondIfFirstIsNull(endUserEmail, EMPTY)); license.setEndUserCell(chooseSecondIfFirstIsNull(form.getPhone(), EMPTY)); String endUserPin = form.getPIN(); if (StringUtils.isEmpty(endUserPin)) endUserPin = form.getDeviceId(); if (StringUtils.isEmpty(endUserPin)) endUserPin = form.getDeviceID(); license.setEndUserPin(chooseSecondIfFirstIsNull(endUserPin, EMPTY)); String isvReqTrans = form.getTransactionid(); if (StringUtils.isEmpty(isvReqTrans)) isvReqTrans = form.getRefId(); license.setIsvReqTrans(chooseSecondIfFirstIsNull(isvReqTrans, EMPTY)); String name = chooseSecondIfFirstIsNull(form.getFirstName(), EMPTY) + SPACE + chooseSecondIfFirstIsNull(form.getLastName(), EMPTY); license.setEndUserName(chooseSecondIfFirstIsNull(name, EMPTY)); String productVersion = form.getVersion(); license.setProductVersion(chooseSecondIfFirstIsNull(productVersion, EMPTY)); Timestamp dateExpires = calcLicenseLife(license); license.setDateExpires(dateExpires); } /** * @param license * @return */ protected Timestamp calcLicenseLife(License license) { Calendar cal = Calendar.getInstance(); int nowDayOfyear = cal.get(Calendar.DAY_OF_YEAR); int licLife = nowDayOfyear + license.getLifeInDays(); cal.set(Calendar.DAY_OF_YEAR, licLife); Timestamp dateExpires = new Timestamp(cal.getTimeInMillis()); return dateExpires; } protected void adjustLicenseBlock(LicenseBlock licenseBlock, Integer numLics, Integer numOverdraft) { int numLeft = licenseBlock.getNumLeft(); numLeft -= numLics; numLeft = numLeft < 0 ? 0 : numLeft; licenseBlock.setNumLeft(numLeft); licenseBlock.setNumOverdraft(numOverdraft); } /** * The requestForLicense object written to the db is either in the form through refData() when the * user is withfrawing a license manually or in the request if done through an automatic form * submission in which case, the refData is not called/executed. * * @param request * @param form * @return */ protected RequestForLicense findRequestForLicenseTrace(HttpServletRequest request, WithdrawLicForm form) { RequestForLicense requestForLicense = form.getRequestForLicense(); if (requestForLicense == null) requestForLicense = (RequestForLicense) request.getAttribute("requestForLicense"); return requestForLicense; } protected List<License> generateOverDraftLicenses(RequestForLicense currRfl, RequestForLicense prevRfl) { return null; } /** * @param product * @param currRfl * @param licenses */ protected void adjustRequestForLicenseTransaction(Product product, RequestForLicense currRfl, RequestForLicense prevRfl, List<License> licenses, int numOverdraft) { currRfl.setProduct(product); currRfl.setIsv(product.getIsv()); String response = "Fulfilled"; if (numOverdraft > 0) response = "Overdraft"; if (licenses == null || licenses.size() == 0 && !product.canAcquireLicenseFor()) response = "Denied - Product deleted or disabled"; currRfl.setResponse(response); if (licenses == null) { currRfl.setNumReturned(0); currRfl.setNumOverDraft(0); } else { currRfl.setNumReturned(licenses.size()); currRfl.setNumOverDraft(numOverdraft); } String licKeys = formulateLicKeys(licenses); currRfl.setLicKeys(licKeys); currRfl.setNumDeviceRequests(1); Set<License> licsSet = null; if (licenses != null && licenses.size() != 0) licsSet = new HashSet<License>(licenses); currRfl.setLicenses(licsSet); requestForLicenseService.update(currRfl); if (prevRfl != null) { prevRfl.setNumDeviceRequests(prevRfl.getNumDeviceRequests() + 1); prevRfl.setLicenses(licsSet); requestForLicenseService.update(prevRfl); } logger.info("Adjusted RequestForLicense table with response: " + response); } protected String formulateLicKeys(List<License> licenses) { StringBuilder sb = new StringBuilder(EMPTY); if (licenses != null) { for (License lic : licenses) sb.append(lic.getLicKey()).append(SPACE); } String licKeys = sb.toString().trim(); return licKeys; } /** * @return the licenseService */ @Override public LicenseService getLicenseService() { return licenseService; } /** * @param licenseService * the licenseService to set */ @Override public void setLicenseService(LicenseService licenseService) { this.licenseService = licenseService; } /** * @return the licenseBlockService */ public LicenseBlockService getLicenseBlockService() { return licenseBlockService; } /** * @param licenseBlockService * the licenseBlockService to set */ public void setLicenseBlockService(LicenseBlockService licenseBlockService) { this.licenseBlockService = licenseBlockService; } /** * @return the requestForLicenseService */ public RequestForLicenseService getRequestForLicenseService() { return requestForLicenseService; } /** * @param requestForLicenseService * the requestForLicenseService to set */ public void setRequestForLicenseService(RequestForLicenseService requestForLicenseService) { this.requestForLicenseService = requestForLicenseService; } /** * @return the licenseAvailabilityAdvisor */ public LicenseAvailabilityAdvisor getLicenseAvailabilityAdvisor() { return licenseAvailabilityAdvisor; } /** * @param licenseAvailabilityAdvisor * the licenseAvailabilityAdvisor to set */ public void setLicenseAvailabilityAdvisor(LicenseAvailabilityAdvisor licenseAvailabilityAdvisor) { this.licenseAvailabilityAdvisor = licenseAvailabilityAdvisor; } /** * @return the hashedLicKeyGenerator */ public HashedLicKeyGenerator getHashedLicKeyGenerator() { return hashedLicKeyGenerator; } /** * @param hashedLicKeyGenerator * the hashedLicKeyGenerator to set */ public void setHashedLicKeyGenerator(HashedLicKeyGenerator hashedLicKeyGenerator) { this.hashedLicKeyGenerator = hashedLicKeyGenerator; } }