Java tutorial
package com.lm.lic.manager.hibernate; 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.criterion.Criterion; import org.hibernate.criterion.LogicalExpression; 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; import com.lm.lic.manager.util.GenConst; import com.lm.lic.manager.util.GenUtil; /** * A data access object (DAO) providing persistence and search support for LicenseVerifIncident 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.exp.LicenseVerifIncident * @author Ibrahim Mustafa */ public class LicenseVerifIncidentDAO extends AbstractSortableEntityDAO { private static final Log log = LogFactory.getLog(LicenseVerifIncidentDAO.class); public static final String VER_RESP = "verResp"; public static final String LIC_KEY = "licKey"; public static final String DEVICE_ID = "deviceId"; public static final String REQUESTOR_NETWORK = "requestorNetwork"; public static final String REQUESTOR_IP = "requestorIp"; public static final String USER_FIRST_NAME = "userFirstName"; public static final String USER_LAST_NAME = "userLastName"; public static final String USER_EMAIL = "userEmail"; public static final String USER_COUNTRY = "userCountry"; public static final String USER_STATE = "userState"; public static final String CREATED_BY = "createdBy"; public static final String MODIFIED_BY = "modifiedBy"; public static final String TIME_REQUESTED = "timeRequested"; @Override protected void initDao() { } public List<LicenseVerifIncident> findIsvVerifs(String isvId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(isvId); return findIsvVerifs(id, licType, respType, firstResult, maxResults, orderBy, orderDir, null, null); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } public List<LicenseVerifIncident> findIsvVerifs(String isvId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { try { Long id = Long.parseLong(isvId); return findIsvVerifs(id, licType, respType, 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<LicenseVerifIncident> findIsvVerifs(Long isvId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { List<LicenseVerifIncident> rfls = null; try { Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); mainCriteria.add(Restrictions.eq("isv.id", isvId)); Criteria prodCriteria = addEnabledUnDeletedProductCriteria(mainCriteria); addLicTypeRespTypeCrit(licType, respType, mainCriteria, prodCriteria); mainCriteria.setFirstResult(firstResult); mainCriteria.setMaxResults(maxResults); if (startDate != null && endDate != null) mainCriteria.add(Restrictions.between(TIME_REQUESTED, startDate, endDate)); else { if (startDate != null) mainCriteria.add(Restrictions.gt(TIME_REQUESTED, startDate)); if (endDate != null) mainCriteria.add(Restrictions.lt(TIME_REQUESTED, endDate)); } createOrderByFieldAliasIfApplicable(orderBy, orderDir, mainCriteria, prodCriteria); rfls = mainCriteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return rfls; } private Criteria addEnabledUnDeletedProductCriteria(Criteria mainCriteria) { Criteria prodCriteria = mainCriteria.createCriteria("product"); prodCriteria.add(Restrictions.eq("enabled", true)); prodCriteria.add(Restrictions.eq("deleted", false)); return prodCriteria; } private void createOrderByFieldAliasIfApplicable(String orderBy, String orderDir, Criteria mainCriteria, Criteria prodCriteria) { Criteria criteria = mainCriteria; HibernateCriteriaAlias critAlias = criteriaAliases.get(orderBy); if (critAlias != null) { criteria = prodCriteria; orderBy = critAlias.getUnaliasedFieldProp(); if (orderBy.startsWith("product.")) { int begIndex = orderBy.indexOf(".") + 1; orderBy = orderBy.substring(begIndex); } } if (GenUtil.isAscendingSortOrder(orderDir)) criteria.addOrder(Order.asc(orderBy)); else criteria.addOrder(Order.desc(orderBy)); } public List<LicenseVerifIncident> findIsvProdVerifs(String isvId, String prodId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { return findProdVerifs(isvId, prodId, null, null, null, licType, respType, firstResult, maxResults, orderBy, orderDir, null, null); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } public List<LicenseVerifIncident> findIsvProdVerifs(String isvId, String prodId, String prodDefId, String storeId, String platformId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { try { return findProdVerifs(isvId, prodId, prodDefId, storeId, platformId, licType, respType, 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<LicenseVerifIncident> findProdVerifs(String isvId, String prodId, String prodDefId, String storeId, String platformId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir, Date startDate, Date endDate) { List<LicenseVerifIncident> lvis = null; try { Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); ProductInstanceCriterias prodInstCriterias = addIsvProductInstProductDefStorePlatfrmCriteria(isvId, prodId, prodDefId, storeId, platformId, mainCriteria); Criteria prodCriteria = prodInstCriterias.getProdCriteria(); addLicTypeRespTypeCrit(licType, respType, mainCriteria, prodCriteria); mainCriteria.setFirstResult(firstResult); mainCriteria.setMaxResults(maxResults); addDateRangeRestrictions(TIME_REQUESTED, startDate, endDate, mainCriteria); addOrderByToCriteria(orderBy, orderDir, mainCriteria, prodInstCriterias); lvis = mainCriteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return lvis; } public Integer findNumIsvVerifs(String isvId, String licType, String respType, Date startDate, Date endDate) { Integer numVerifs = 0; try { Long lisvid = Long.parseLong(isvId); Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); mainCriteria.add(Restrictions.eq("isv.id", lisvid)); Criteria prodCriteria = mainCriteria.createCriteria("product"); prodCriteria.add(Restrictions.eq("deleted", false)); addLicTypeRespTypeCrit(licType, respType, mainCriteria, prodCriteria); if (startDate != null && endDate != null) mainCriteria.add(Restrictions.between(TIME_REQUESTED, startDate, endDate)); else { if (startDate != null) mainCriteria.add(Restrictions.gt(TIME_REQUESTED, startDate)); if (endDate != null) mainCriteria.add(Restrictions.lt(TIME_REQUESTED, endDate)); } mainCriteria.setProjection(Projections.rowCount()); numVerifs = (Integer) mainCriteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numVerifs; } public Integer findNumLicVerifs(String licKey, String deviceId) { Integer numVerifs = 0; logger.info("Trying to find NumVerifs for licKey: " + licKey); if (StringUtils.hasText(licKey)) { try { Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); mainCriteria.add(Restrictions.eq("licKey", licKey)); mainCriteria.add(Restrictions.eq("deviceId", deviceId)); mainCriteria.setProjection(Projections.rowCount()); numVerifs = (Integer) mainCriteria.list().get(0); } catch (RuntimeException re) { throw re; } } return numVerifs; } public Integer findNumVerifs(String isvId, String prodId, String prodDefId, String storeId, String platformId, String licType, String respType, Date startDate, Date endDate) { Integer numVerifs = 0; try { Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); ProductInstanceCriterias prodInstCriterias = addIsvProductInstProductDefStorePlatfrmCriteria(isvId, prodId, prodDefId, storeId, platformId, mainCriteria); Criteria prodCriteria = prodInstCriterias.getProdCriteria(); addLicTypeRespTypeCrit(licType, respType, mainCriteria, prodCriteria); addDateRangeRestrictions(TIME_REQUESTED, startDate, endDate, mainCriteria); mainCriteria.setProjection(Projections.rowCount()); numVerifs = (Integer) mainCriteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numVerifs; } public Integer findNumProdVerifs(String prodId, String licType, String respType, Date startDate, Date endDate) { Integer numLics = 0; try { Long lpid = Long.parseLong(prodId); Criteria mainCriteria = getSession().createCriteria(LicenseVerifIncident.class); mainCriteria.add(Restrictions.eq("product.id", lpid)); Criteria prodCriteria = mainCriteria.createCriteria("product"); prodCriteria.add(Restrictions.eq("deleted", false)); addLicTypeRespTypeCrit(licType, respType, mainCriteria, prodCriteria); if (startDate != null && endDate != null) mainCriteria.add(Restrictions.between(TIME_REQUESTED, startDate, endDate)); else { if (startDate != null) mainCriteria.add(Restrictions.gt(TIME_REQUESTED, startDate)); if (endDate != null) mainCriteria.add(Restrictions.lt(TIME_REQUESTED, endDate)); } mainCriteria.setProjection(Projections.rowCount()); numLics = (Integer) mainCriteria.list().get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return numLics; } public List<LicenseVerifIncident> findVerifs(String isvId, String prodId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { return findIsvProdVerifs(isvId, prodId, licType, respType, firstResult, maxResults, orderBy, orderDir); } public List<LicenseVerifIncident> findProdVerifs(String prodId, String licType, String respType, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { return findProdVerifs(null, prodId, null, null, null, licType, respType, firstResult, maxResults, orderBy, orderDir, null, null); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } public void save(LicenseVerifIncident transientInstance) { log.debug("saving LicenseVerifIncident instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(LicenseVerifIncident persistentInstance) { log.debug("deleting LicenseVerifIncident instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public LicenseVerifIncident findById(java.lang.Long id) { log.debug("getting LicenseVerifIncident instance with id: " + id); try { LicenseVerifIncident instance = (LicenseVerifIncident) getHibernateTemplate() .get("com.lm.lic.manager.hibernate.exp.LicenseVerifIncident", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } @SuppressWarnings("unchecked") public List<LicenseVerifIncident> findByExample(LicenseVerifIncident instance) { log.debug("finding LicenseVerifIncident instance by example"); try { List<LicenseVerifIncident> 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<LicenseVerifIncident> findByProperty(String propertyName, Object value) { log.debug("finding LicenseVerifIncident instance with property: " + propertyName + ", value: " + value); try { String queryString = "from LicenseVerifIncident as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } private void addLicTypeRespTypeCrit(String licType, String respType, Criteria mainCriteria, Criteria prodCriteria) { if (StringUtils.hasText(respType)) { if (respType.equals(LicenseVerificationStatus.YES_AUTHORIZED.value())) { Criterion afCrit = Restrictions.eq("verResp", LicenseVerificationStatus.AUTHORIZED_FOREVER.value()); Criterion apCrit = Restrictions.eq("verResp", LicenseVerificationStatus.AUTHORIZED_PERMANENT.value()); Criterion atCrit = Restrictions.eq("verResp", LicenseVerificationStatus.AUTHORIZED_TRIAL.value()); LogicalExpression orExp = Restrictions.or(afCrit, apCrit); orExp = Restrictions.or(orExp, atCrit); mainCriteria.add(orExp); } else mainCriteria.add(Restrictions.eq("verResp", respType)); } if (CoarseLicenseType.isForever(licType)) prodCriteria.add(Restrictions.gt("licLifeInDays", GenConst.YEAR_LIFE_LONG)); else if (CoarseLicenseType.isPermanent(licType)) prodCriteria.add(Restrictions.eq("licLifeInDays", GenConst.YEAR_LIFE_LONG)); else if (CoarseLicenseType.isTrial(licType)) prodCriteria.add(Restrictions.lt("licLifeInDays", GenConst.YEAR_LIFE_LONG)); } public List<LicenseVerifIncident> findByVerResp(Object verResp) { return findByProperty(VER_RESP, verResp); } public List<LicenseVerifIncident> findByLicKey(Object licKey) { return findByProperty(LIC_KEY, licKey); } public List<LicenseVerifIncident> findByDeviceId(Object deviceId) { return findByProperty(DEVICE_ID, deviceId); } public List<LicenseVerifIncident> findByRequestorNetwork(Object requestorNetwork) { return findByProperty(REQUESTOR_NETWORK, requestorNetwork); } public List<LicenseVerifIncident> findByRequestorIp(Object requestorIp) { return findByProperty(REQUESTOR_IP, requestorIp); } public List<LicenseVerifIncident> findByUserFirstName(Object userFirstName) { return findByProperty(USER_FIRST_NAME, userFirstName); } public List<LicenseVerifIncident> findByUserLastName(Object userLastName) { return findByProperty(USER_LAST_NAME, userLastName); } public List<LicenseVerifIncident> findByUserEmail(Object userEmail) { return findByProperty(USER_EMAIL, userEmail); } public List<LicenseVerifIncident> findByUserCountry(Object userCountry) { return findByProperty(USER_COUNTRY, userCountry); } public List<LicenseVerifIncident> findByUserState(Object userState) { return findByProperty(USER_STATE, userState); } public List<LicenseVerifIncident> findByCreatedBy(Object createdBy) { return findByProperty(CREATED_BY, createdBy); } public List<LicenseVerifIncident> findByModifiedBy(Object modifiedBy) { return findByProperty(MODIFIED_BY, modifiedBy); } @SuppressWarnings("unchecked") public List<LicenseVerifIncident> findAll() { log.debug("finding all LicenseVerifIncident instances"); try { String queryString = "from LicenseVerifIncident"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public LicenseVerifIncident merge(LicenseVerifIncident detachedInstance) { log.debug("merging LicenseVerifIncident instance"); try { LicenseVerifIncident result = getHibernateTemplate().merge(detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(LicenseVerifIncident instance) { log.debug("attaching dirty LicenseVerifIncident instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(LicenseVerifIncident instance) { log.debug("attaching clean LicenseVerifIncident instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static AbstractSortableEntityDAO getFromApplicationContext(ApplicationContext ctx) { return (AbstractSortableEntityDAO) ctx.getBean("LicenseVerifIncidentDAO"); } }