Java tutorial
package com.lm.lic.manager.hibernate; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Criteria; import org.hibernate.LockMode; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.springframework.context.ApplicationContext; import org.springframework.util.StringUtils; /** * A data access object (DAO) providing persistence and search support for License entities. Transaction control of the save(), * update() and delete() operations can directly support Spring container-managed transactions or they can be augmented to handle * user-managed Spring transactions. Each of these methods provides additional information for how to configure it for the desired * type of transaction control. * * @see com.lm.lic.manager.hibernate.License * @author Ibrahim Mustafa */ public class LicenseDAO extends AbstractSortableEntityDAO { /** * */ private static final String ENABLED_AND_NOT_DELETED = "and lic.product.deleted=false and lic.product.enabled=true"; private static final Log log = LogFactory.getLog(LicenseDAO.class); public static final String ACTIVE = "active"; public static final String KEY = "licKey"; public static final String END_USER_NAME = "endUserName"; public static final String END_USER_CELL = "endUserCell"; public static final String END_USER_PIN = "endUserPin"; public static final String COST = "cost"; public static final String PRICE = "price"; public static final String LOCALE = "locale"; public static final String CREATED_BY = "createdBy"; public static final String MODIFIED_BY = "modifiedBy"; public static final String TIME_ACTIVATED = "dateActivated"; @Override protected void initDao() { } public Integer findNumLics(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumUnpaidForLics(Long isvId, Long prodId) { Integer numLics = 0; try { Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", isvId)); criteria.add(Restrictions.eq("product.id", prodId)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); criteria.add(Restrictions.ilike("paymentStatus", LicensePaymentStatus.OVERDRAFT.name())); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } /** * Find the number of licenses who are credited (paid for) but not yet used. * * @param isvId * @param prodId * @return */ public Integer findNumCreditedLics(Long isvId, Long prodId) { Integer numLics = 0; try { Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", isvId)); criteria.add(Restrictions.eq("product.id", prodId)); criteria.add(Restrictions.eq("inUse", false)); criteria.add(Restrictions.eq("decom", false)); criteria.add(Restrictions.ne("paymentStatus", LicensePaymentStatus.OVERDRAFT.name())); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumInUseLics(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumInUseLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Date startDate, Date endDate) { Integer numLics = 0; try { Long lisvid = null; if (StringUtils.hasText(isvId)) lisvid = Long.parseLong(isvId); Long lpid = null; if (StringUtils.hasText(prodId)) lpid = Long.parseLong(prodId); Long lppid = null; if (StringUtils.hasText(prodDefId)) lppid = Long.parseLong(prodDefId); Long lsid = null; if (StringUtils.hasText(storeId)) lsid = Long.parseLong(storeId); Long lfid = null; if (StringUtils.hasText(platformId)) lfid = Long.parseLong(platformId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); Criteria prodCriteria = criteria.createCriteria("product"); // If the child product is specified, this supercedes the wider selection of the product // definition. // Only check for htye product definition if the child product was not selected. if (lpid != null) criteria.add(Restrictions.eq("product.id", lpid)); else if (lppid != null) prodCriteria.add(Restrictions.eq("productDef.id", lppid)); if (lsid != null) prodCriteria.add(Restrictions.eq("listingStore.id", lsid)); if (lfid != null) prodCriteria.add(Restrictions.eq("platform.id", lfid)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); if (startDate != null && endDate != null) criteria.add(Restrictions.between(TIME_ACTIVATED, startDate, endDate)); else { if (startDate != null) criteria.add(Restrictions.gt(TIME_ACTIVATED, startDate)); if (endDate != null) criteria.add(Restrictions.lt(TIME_ACTIVATED, endDate)); } criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumIssuedLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Date startDate, Date endDate) { Integer numLics = 0; try { Long lisvid = null; if (StringUtils.hasText(isvId)) lisvid = Long.parseLong(isvId); Long lpid = null; if (StringUtils.hasText(prodId)) lpid = Long.parseLong(prodId); Long lppid = null; if (StringUtils.hasText(prodDefId)) lppid = Long.parseLong(prodDefId); Long lsid = null; if (StringUtils.hasText(storeId)) lsid = Long.parseLong(storeId); Long lfid = null; if (StringUtils.hasText(platformId)) lfid = Long.parseLong(platformId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); Criteria prodCriteria = criteria.createCriteria("product"); // If the child product is specified, this supercedes the wider selection of the product // definition. // Only check for htye product definition if the child product was not selected. if (lpid != null) criteria.add(Restrictions.eq("product.id", lpid)); else if (lppid != null) prodCriteria.add(Restrictions.eq("productDef.id", lppid)); if (lsid != null) prodCriteria.add(Restrictions.eq("listingStore.id", lsid)); if (lfid != null) prodCriteria.add(Restrictions.eq("platform.id", lfid)); criteria.add(Restrictions.eq("inUse", true)); // criteria.add(Restrictions.eq("decom", false)); if (startDate != null && endDate != null) criteria.add(Restrictions.between(TIME_ACTIVATED, startDate, endDate)); else { if (startDate != null) criteria.add(Restrictions.gt(TIME_ACTIVATED, startDate)); if (endDate != null) criteria.add(Restrictions.lt(TIME_ACTIVATED, endDate)); } criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumIsvLicenses(String isvId) { Integer numLics = 0; try { Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("decom", false)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumInUseIsvLicenses(String isvId) { Integer numLics = 0; try { Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumInUseIsvLicenses(String isvId, String storeId, String platformId, Date startDate, Date endDate) { Integer numLics = 0; try { Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); Criteria prodCriteria = null; if (StringUtils.hasText(storeId)) { Long lsid = Long.parseLong(storeId); prodCriteria = criteria.createCriteria("product"); prodCriteria.add(Restrictions.eq("listingStore.id", lsid)); } if (StringUtils.hasText(platformId)) { Long lfid = Long.parseLong(platformId); if (prodCriteria == null) prodCriteria = criteria.createCriteria("product"); prodCriteria.add(Restrictions.eq("platform.id", lfid)); } if (startDate != null && endDate != null) criteria.add(Restrictions.between(TIME_ACTIVATED, startDate, endDate)); else { if (startDate != null) criteria.add(Restrictions.gt(TIME_ACTIVATED, startDate)); if (endDate != null) criteria.add(Restrictions.lt(TIME_ACTIVATED, endDate)); } criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumOverdraftLics(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); criteria.add(Restrictions.eq("paymentStatus", LicensePaymentStatus.OVERDRAFT.name())); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumBoughtLics(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); MatchMode matchMode = MatchMode.START; criteria.add(Restrictions.ilike("paymentStatus", LicensePaymentStatus.PAID.name(), matchMode)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumLeftLics(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); criteria.add(Restrictions.ne("paymentStatus", LicensePaymentStatus.OVERDRAFT.name())); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", false)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public Integer findNumFree(String isvId, String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("product.id", lpid)); criteria.add(Restrictions.eq("decom", false)); criteria.add(Restrictions.eq("paymentStatus", LicensePaymentStatus.FREE.name())); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public void save(License lic) { log.debug("saving License instance"); try { getHibernateTemplate().save(lic); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(License persistentInstance) { log.debug("deleting License instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public License findById(java.lang.Long id) { log.debug("getting License instance with id: " + id); try { License instance = (License) getHibernateTemplate().get("com.lm.lic.manager.hibernate.License", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } @SuppressWarnings("unchecked") public License findUnExpiredLicense(String pin, String key, int gracePeriodInDays) { log.debug("finding unexpired License instance by pin and key: " + pin + ", key: " + key); License license = null; try { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, gracePeriodInDays); Date maxDate = cal.getTime(); String qs = "from License as lic where lic.endUserPin= :pin and lic.licKey= :key and lic.dateExpires >= :maxDate"; Query query = getSession().createQuery(qs); query.setParameter("pin", pin); query.setParameter("key", key); query.setParameter("maxDate", maxDate); List<License> licenses = query.list(); if (licenses != null && licenses.size() > 0) license = licenses.get(0); } catch (RuntimeException re) { log.error("findUnExpiredLicense", re); throw re; } return license; } @SuppressWarnings("unchecked") public License findLicenseByPinAndKey(String pin, String key) { log.debug("finding License instance by pin and key: " + pin + ", key: " + key); License license = null; try { String queryString = "from License as lic where lic.endUserPin=? and lic.licKey=? order by lic.endUserPin desc"; List<License> licenses = getHibernateTemplate().find(queryString, pin, key); if (licenses != null && licenses.size() > 0) license = licenses.get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return license; } @SuppressWarnings("unchecked") public List<License> findLicenseByPinAndKey(String deviceId, String licKey, String prodDefKey, Boolean decomStatus, Boolean inUse) { List<License> licenses = null; Criteria criteria = getSession().createCriteria(License.class); addLicenseStatucCrit(decomStatus, inUse, criteria); criteria.add(Restrictions.eq("endUserPin", deviceId)); criteria.add(Restrictions.eq("licKey", licKey)); Criteria prodCrit = addValidProductStateRestrictions(criteria); addProdDefCritIfProdDefKeyAvailable(prodDefKey, prodCrit); criteria.addOrder(Order.desc("lifeInDays")); licenses = criteria.list(); return licenses; } @SuppressWarnings("unchecked") public List<License> findLicenseByEmailAndKey(String email, String licKey, String prodDefKey, Boolean decomStatus, Boolean inUse) { List<License> licenses = null; Criteria criteria = getSession().createCriteria(License.class); addLicenseStatucCrit(decomStatus, inUse, criteria); criteria.add(Restrictions.eq("endUserEmail", email)); criteria.add(Restrictions.eq("licKey", licKey)); Criteria prodCrit = addValidProductStateRestrictions(criteria); addProdDefCritIfProdDefKeyAvailable(prodDefKey, prodCrit); criteria.addOrder(Order.desc("lifeInDays")); licenses = criteria.list(); return licenses; } /** * @param decomStatus * @param inUse * @param criteria */ private void addLicenseStatucCrit(Boolean decomStatus, Boolean inUse, Criteria criteria) { if (inUse != null) criteria.add(Restrictions.eq("inUse", inUse)); if (decomStatus != null) criteria.add(Restrictions.eq("decom", decomStatus)); } /** * Add the product definition key restriction if supplied. The prodDefKey is a new parameter introduced for extra security. * Prior to this, it was not required. To accomodate applications running on phones that don't pass this argument, try to not * add the restriction unless it is physically supplied. If it is supplied, then simply add the restriction as is. NOTE THAT * THIS IS ONLY TEMPORARY AND SHOULD ALWAYS ADD THE RESATRICTION REGARDLESS OF THE PARAMETER BEING PRESENT OR NOT. * * @param prodDefKey * @param prodCrit */ private void addProdDefCritIfProdDefKeyAvailable(String prodDefKey, Criteria prodCrit) { Criteria prodDefCrit = prodCrit.createCriteria("productDef"); prodDefCrit.add(Restrictions.eq("deleted", false)); prodDefCrit.add(Restrictions.eq("locked", false)); prodDefCrit.add(Restrictions.eq("enabled", true)); prodDefCrit.add(Restrictions.eq("productKey", prodDefKey)); } /** * Find license by device ID and productKey or isvProductId. Note that the license is searched for by productKey. If no license * is found, it is searched against isvProductId (storeProductId). */ @SuppressWarnings("unchecked") public License findLicByDevIdAndProductKeyOrIsvProductId(String deviceId, String productKey, Boolean decomStatus, Boolean inUse) { License license = null; try { String queryString = "from License as lic where lic.endUserPin=? and lic.product.productKey=? and lic.inUse=? " + "and decom=? order by lic.endUserPin desc"; List<License> licenses = getHibernateTemplate().find(queryString, deviceId, productKey, inUse, decomStatus); if (licenses != null && licenses.size() > 0) license = licenses.get(0); else { queryString = "from License as lic where lic.endUserPin=? and lic.product.isvProductId=? and lic.inUse=? " + "and decom=? order by lic.endUserPin desc"; licenses = getHibernateTemplate().find(queryString, deviceId, productKey, inUse, decomStatus); if (licenses != null && licenses.size() > 0) license = licenses.get(0); } } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return license; } /** * Find license by device ID and prodDefKey. Note that the license is searched for by prodDefKey. If more than one license exist * per deviceId (in such a case, the user got the trial then paid for the application)the list returned is in descending order * according to the life of the license. The higher life license is at lower index in the list - highest life is at index 0. * Lowest life is at index siz - 1. */ @SuppressWarnings("unchecked") public List<License> findLicsByDevIdAndProdDefKey(String deviceId, String prodDefKey, Boolean decomStatus, Boolean inUse) { List<License> licenses = null; Criteria criteria = getSession().createCriteria(License.class); addLicenseStatucCrit(decomStatus, inUse, criteria); criteria.add(Restrictions.eq("endUserPin", deviceId)); Criteria prodCrit = addValidProductStateRestrictions(criteria); Criteria prodDefCrit = prodCrit.createCriteria("productDef"); prodDefCrit.add(Restrictions.eq("deleted", false)); prodDefCrit.add(Restrictions.eq("locked", false)); prodDefCrit.add(Restrictions.eq("enabled", true)); prodDefCrit.add(Restrictions.eq("productKey", prodDefKey)); criteria.addOrder(Order.desc("lifeInDays")); licenses = criteria.list(); return licenses; } @SuppressWarnings("unchecked") public License findLicenseByKey(String isvId, String key, Boolean decomStatus, Boolean inUse) { log.debug("finding isv license instance by key: " + isvId + ", key: " + key); License license = null; try { Long lisvId = Long.parseLong(isvId); String queryString = "from License as lic where isv.id= ? and licKey=? and inUse=? " + " and decom=? order by licKey desc"; List<License> licenses = getHibernateTemplate().find(queryString, lisvId, key, inUse, decomStatus); if (licenses != null && licenses.size() > 0) license = licenses.get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return license; } @SuppressWarnings("unchecked") public License findLicenseByKey(Long isvId, Long prodId, String key, Boolean decomStatus, Boolean inUse) { log.debug("finding isv license instance by key: " + isvId + ", key: " + key); License license = null; try { String queryString = "from License as lic where isv.id= ? and product.id = ? and licKey=? and inUse=? " + " and decom=? order by licKey desc"; List<License> licenses = getHibernateTemplate().find(queryString, isvId, prodId, key, inUse, decomStatus); if (licenses != null && licenses.size() > 0) license = licenses.get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return license; } @SuppressWarnings("unchecked") public List<License> findByExample(License instance) { log.debug("finding License instance by example"); try { List<License> results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } @SuppressWarnings("unchecked") public List<License> findByProperty(String propertyName, Object value) { log.debug("finding License instance with property: " + propertyName + ", value: " + value); try { String queryString = "from License as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<License> findByPropertyAndProduct(String propertyName, Object value, String prodId) { log.debug("finding License instance with property: " + propertyName + ", value: " + value); Long lpid = Long.parseLong(prodId); try { String queryString = "from License as lic where lic." + propertyName + "= ? and lic.product.id= ?"; return getHibernateTemplate().find(queryString, value, lpid); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List<License> findByActive(Object active) { return findByProperty(ACTIVE, active); } public List<License> findByKey(Object key) { return findByProperty(KEY, key); } public List<License> findByEndUserName(Object endUserName) { return findByProperty(END_USER_NAME, endUserName); } public List<License> findByEndUserCell(Object endUserCell) { return findByProperty(END_USER_CELL, endUserCell); } public List<License> findByEndUserPin(Object endUserPin) { return findByProperty(END_USER_PIN, endUserPin); } public List<License> findByCost(Object cost) { return findByProperty(COST, cost); } public List<License> findByPrice(Object price) { return findByProperty(PRICE, price); } public List<License> findByLocale(Object locale) { return findByProperty(LOCALE, locale); } public List<License> findByCreatedBy(Object createdBy) { return findByProperty(CREATED_BY, createdBy); } public List<License> findByModifiedBy(Object modifiedBy) { return findByProperty(MODIFIED_BY, modifiedBy); } @SuppressWarnings("unchecked") public List<License> findAll() { log.debug("finding all License instances"); try { String queryString = "from License"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public List<License> findIsvLicenses(String isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(isvId); return findIsvLicenses(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findIsvLicenses(Long isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.isv.id= :isvId and lic.decom= :decom order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); query.setParameter("decom", false); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findInUseIsvLicenses(String isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(isvId); return findInUseIsvLicenses(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findInUseIsvLicenses(Long isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.isv.id= :isvId and inUse= :inUse order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); query.setParameter("inUse", true); query.setParameter("decom", false); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findIsvLicenses(String isvId) { try { Long id = Long.parseLong(isvId); return findIsvLicenses(id); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findIsvLicenses(Long isvId) { try { String queryString = "from License as lic where lic.isv.id= ? and lic.decom= ? order by lic.endUserPin desc"; return getHibernateTemplate().find(queryString, isvId, false); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public Integer findNumProdLicenses(String prodId) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("product.id", lpid)); criteria.add(Restrictions.eq("decom", false)); criteria.setProjection(Projections.rowCount()); numLics = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public List<License> findUnusedIsvLicenses(String isvId) { try { Long id = Long.parseLong(isvId); return findUnusedIsvLicenses(id); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findUnusedIsvLicenses(Long isvId) { try { String queryString = "from License as lic where lic.isv.id= ? and lic.inUse=false"; return getHibernateTemplate().find(queryString, isvId); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List<License> findProdLicenses(String prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(prodId); return findProdLicenses(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findProdLicenses(Long prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.product.id= :prodId and lic.decom=false order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("prodId", prodId); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findInUseProdLicenses(String prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(prodId); return findInUseProdLicenses(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findInUseProdLicenses(Long prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.product.id= :prodId and inUse= :inUse and lic.decom= :decom order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("prodId", prodId); query.setParameter("inUse", true); query.setParameter("decom", false); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findIsvProdLicenses(String isvId, String prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long lpid = Long.parseLong(prodId); Long liid = Long.parseLong(isvId); return findIsvProdLicenses(liid, lpid, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findIsvProdLicenses(Long isvId, Long prodId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.isv.id= :isvId and lic.product.id= :prodId and lic.decom= :decom order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); query.setParameter("prodId", prodId); query.setParameter("decom", false); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findInUseIsvProdLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long liid = Long.parseLong(isvId); Long lpid = Long.parseLong(prodId); Long lppid = StringUtils.hasText(prodDefId) ? Long.parseLong(prodDefId) : null; Long lsid = StringUtils.hasText(storeId) ? Long.parseLong(storeId) : null; Long lfid = StringUtils.hasText(platformId) ? Long.parseLong(platformId) : null; return findInUseIsvProdLicenses(liid, lpid, lppid, lsid, lfid, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findInUseIsvProdLicenses(Long isvId, Long prodId, Long prodDefId, Long storeId, Long platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<License> licenses = null; try { String qs = "from License as lic where lic.isv.id= :isvId and "; if (prodId != null) qs += "lic.product.id= :prodId and "; else if (prodDefId != null) qs += "lic.product.productDef.id= :prodDefId and "; if (storeId != null) qs += "lic.product.listingStore.id= :storeId and "; if (platformId != null) qs += "lic.product.platform.id= :platformId and "; qs += "inUse= :inUse and decom= :decom order by lic." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); if (prodId != null) query.setParameter("prodId", prodId); else if (prodDefId != null) query.setParameter("prodDefId", prodDefId); if (storeId != null) query.setParameter("storeId", storeId); if (platformId != null) query.setParameter("platformId", platformId); query.setParameter("inUse", true); query.setParameter("decom", false); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } // public List<License> findInUseIsvProdLicenses(String isvId, String prodId, String prodDefId, // String // storeId, // String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir, // Date // startDate, // Date endDate) { // try { // Long lisvId = StringUtils.hasText(isvId) ? Long.parseLong(isvId) : null; // Long lpid = StringUtils.hasText(prodId) ? Long.parseLong(prodId) : null; // Long lppid = StringUtils.hasText(prodDefId) ? Long.parseLong(prodDefId) : null; // Long lsid = StringUtils.hasText(storeId) ? Long.parseLong(storeId) : null; // Long lfid = StringUtils.hasText(platformId) ? Long.parseLong(platformId) : null; // return findInUseIsvProdLicenses(lisvId, lpid, lppid, lsid, lfid, firstResult, maxResults, // orderBy, // orderDir, // startDate, endDate); // } catch(Exception e) { // log.error("find by property name failed", e); // throw new RuntimeException(e); // } // } @SuppressWarnings("unchecked") public List<License> findInUseIsvProdLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { List<License> licenses = null; try { Criteria mainCriteria = getSession().createCriteria(License.class); ProductInstanceCriterias prodInstCriterias = addIsvProductInstProductDefStorePlatfrmCriteria(isvId, prodId, prodDefId, storeId, platformId, mainCriteria); addDateRangeRestrictions(TIME_ACTIVATED, startDate, endDate, mainCriteria); mainCriteria.add(Restrictions.eq("inUse", true)); mainCriteria.add(Restrictions.eq("decom", false)); mainCriteria.setFirstResult(firstResult); mainCriteria.setMaxResults(maxResults); addOrderByToCriteria(orderBy, orderDir, mainCriteria, prodInstCriterias); licenses = mainCriteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } @SuppressWarnings("unchecked") public List<License> findInUseIsvProdTrialLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { List<License> licenses = null; try { Criteria mainCriteria = getSession().createCriteria(License.class); ProductInstanceCriterias prodInstCriterias = addIsvProductInstProductDefStorePlatfrmCriteria(isvId, prodId, prodDefId, storeId, platformId, mainCriteria); addDateRangeRestrictions(TIME_ACTIVATED, startDate, endDate, mainCriteria); mainCriteria.add(Restrictions.eq("inUse", true)); mainCriteria.add(Restrictions.eq("decom", false)); mainCriteria.add(Restrictions.lt("lifeInDays", 365)); mainCriteria.setFirstResult(firstResult); mainCriteria.setMaxResults(maxResults); addOrderByToCriteria(orderBy, orderDir, mainCriteria, prodInstCriterias); licenses = mainCriteria.list(); } catch (RuntimeException re) { log.error("findInUseIsvProdTrialLicenses failed", re); throw re; } return licenses; } @SuppressWarnings("unchecked") public List<License> findIssuedLicenses(String isvId, String prodId, String prodDefId, String storeId, String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { List<License> licenses = null; try { Criteria mainCriteria = getSession().createCriteria(License.class); ProductInstanceCriterias prodInstCriterias = addIsvProductInstProductDefStorePlatfrmCriteria(isvId, prodId, prodDefId, storeId, platformId, mainCriteria); addDateRangeRestrictions(TIME_ACTIVATED, startDate, endDate, mainCriteria); mainCriteria.add(Restrictions.eq("inUse", true)); mainCriteria.setFirstResult(firstResult); mainCriteria.setMaxResults(maxResults); addOrderByToCriteria(orderBy, orderDir, mainCriteria, prodInstCriterias); licenses = mainCriteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } @SuppressWarnings("unchecked") public List<License> findOverdraftLicenses(Long isvId, Long prodId, Integer maxResults) { List<License> licenses = null; try { String qs = "from License as lic where lic.isv.id= :isvId and lic.product.id= :prodId and lic.decom= :decom and " + "lic.paymentStatus= :paymentStatus"; Query query = getSession().createQuery(qs); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); query.setParameter("prodId", prodId); query.setParameter("decom", false); query.setParameter("paymentStatus", LicensePaymentStatus.OVERDRAFT.name()); licenses = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findUnusedProdLicenses(String prodId) { try { Long id = Long.parseLong(prodId); return findUnusedProdLicenses(id); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findUnusedProdLicenses(Long prodId) { List<License> licenses = null; try { String qs = "from License as lic where lic.product.id= :prodId and lic.inUse=false" + ENABLED_AND_NOT_DELETED; Query hq = getSession().createQuery(qs); hq.setMaxResults(1); hq.setParameter("prodId", prodId); licenses = hq.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public List<License> findUnusedProdLicenses(String prodId, Integer numLics) { try { Long id = Long.parseLong(prodId); return findUnusedProdLicenses(id, numLics); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<License> findUnusedProdLicenses(Long prodId, Integer numLics) { List<License> licenses = null; try { String qs = "from License as lic where lic.product.id= :prodId and lic.inUse=false " + ENABLED_AND_NOT_DELETED; Query hq = getSession().createQuery(qs); hq.setParameter("prodId", prodId); hq.setMaxResults(numLics); licenses = hq.list(); } catch (RuntimeException re) { log.error("findUnusedProdLicenses failed", re); throw re; } return licenses; } public List<License> findUsedLicenses(String isvId, Boolean decomStatus, String licKey, String deviceId, String customerEmail) { List<License> licenses = findUsedLicenses(isvId, decomStatus, licKey, deviceId, customerEmail, 0); return licenses; } @SuppressWarnings("unchecked") public List<License> findUsedLicenses(String isvId, Boolean decomStatus, String licKey, String deviceId, String customerEmail, int licLifeInDays) { List<License> licenses = null; try { Long lisvId = Long.parseLong(isvId); boolean decom = decomStatus == null ? false : decomStatus.booleanValue(); Criteria criteria = getSession().createCriteria(License.class); criteria.add(Restrictions.eq("isv.id", lisvId)); criteria.add(Restrictions.eq("inUse", true)); criteria.add(Restrictions.eq("decom", decom)); if (StringUtils.hasText(licKey)) { Criterion licKeyCrit = Restrictions.ilike("licKey", licKey, MatchMode.ANYWHERE); criteria.add(licKeyCrit); } if (StringUtils.hasText(deviceId)) { Criterion devIdCrit = Restrictions.ilike("endUserPin", deviceId, MatchMode.ANYWHERE); criteria.add(devIdCrit); } if (StringUtils.hasText(customerEmail)) { Criterion emailCrit = Restrictions.ilike("endUserEmail", customerEmail, MatchMode.ANYWHERE); criteria.add(emailCrit); } if (licLifeInDays > 0) { Criterion lifeCrit = Restrictions.eq("lifeInDays", licLifeInDays); criteria.add(lifeCrit); } licenses = criteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return licenses; } public License merge(License detachedInstance) { log.debug("merging License instance"); try { License result = getHibernateTemplate().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void update(License license) { merge(license); } public void attachDirty(License instance) { log.debug("attaching dirty License instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(License instance) { log.debug("attaching clean License instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static LicenseDAO getFromApplicationContext(ApplicationContext ctx) { return (LicenseDAO) ctx.getBean("LicenseDAO"); } }