Java tutorial
package com.lm.lic.manager.hibernate; 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.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 Product 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.Product * @author Ibrahim Mustafa */ public class ProductDAO extends AbstractSortableEntityDAO { private static final Log log = LogFactory.getLog(ProductDAO.class); public static final String PRODUCT_KEY = "productKey"; public static final String ISV_PRODUCT_ID = "isvProductId"; public static final String NAME = "name"; public static final String VERSION = "version"; public static final String DESCRIPTION = "description"; public static final String IMGAGE = "imgage"; public static final String LOCALE = "locale"; public static final String CREATED_BY = "createdBy"; public static final String MODIFIED_BY = "modifiedBy"; public static final String IMAGE = "image"; @Override protected void initDao() { } public Integer findNumIsvProducts(String isvId) { Integer numProducts = 0; try { Long lisvid = Long.parseLong(isvId); Criteria criteria = getSession().createCriteria(QuickyProduct.class); criteria.add(Restrictions.eq("isv.id", lisvid)); // criteria.add(Restrictions.eq("enabled", true)); criteria.add(Restrictions.eq("deleted", false)); criteria.setProjection(Projections.rowCount()); numProducts = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("findNumIsvProducts failed", re); throw re; } return numProducts; } public Integer findNumStoreIsvProducts(String isvId, String storeId) { Integer numProducts = 0; try { Long lisvid = Long.parseLong(isvId); Long lstoreid = Long.parseLong(storeId); Criteria criteria = getSession().createCriteria(QuickyProduct.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("listingStore.id", lstoreid)); // criteria.add(Restrictions.eq("enabled", true)); criteria.add(Restrictions.eq("deleted", false)); criteria.setProjection(Projections.rowCount()); numProducts = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("findNumIsvProducts failed", re); throw re; } return numProducts; } public Integer findNumproductDefIsvProducts(String isvId, String prodDefId) { Integer numProducts = 0; try { Long lisvid = Long.parseLong(isvId); Long lppid = Long.parseLong(prodDefId); Criteria criteria = getSession().createCriteria(QuickyProduct.class); criteria.add(Restrictions.eq("isv.id", lisvid)); criteria.add(Restrictions.eq("productDef.id", lppid)); // criteria.add(Restrictions.eq("enabled", true)); criteria.add(Restrictions.eq("deleted", false)); criteria.setProjection(Projections.rowCount()); numProducts = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("findNumproductDefIsvProducts failed", re); throw re; } return numProducts; } public Integer findNumIsvProducts(String isvId, String prodDefId, String storeId, String categoryId, String platformId) { Integer numProducts = 0; try { Long lisvid = Long.parseLong(isvId); Long lppid = null; if (StringUtils.hasText(prodDefId)) lppid = Long.parseLong(prodDefId); Long lstoreid = null; if (StringUtils.hasText(storeId)) lstoreid = Long.parseLong(storeId); Long lcid = null; if (StringUtils.hasText(categoryId)) lcid = Long.parseLong(categoryId); Long lplatId = null; if (StringUtils.hasText(platformId)) lplatId = Long.parseLong(platformId); Criteria criteria = getSession().createCriteria(QuickyProduct.class); criteria.add(Restrictions.eq("isv.id", lisvid)); if (lppid != null) criteria.add(Restrictions.eq("productDef.id", lppid)); if (lstoreid != null) criteria.add(Restrictions.eq("listingStore.id", lstoreid)); if (lcid != null) criteria.add(Restrictions.eq("category.id", lcid)); if (lplatId != null) criteria.add(Restrictions.eq("platform.id", lplatId)); // criteria.add(Restrictions.eq("enabled", true)); criteria.add(Restrictions.eq("deleted", false)); criteria.setProjection(Projections.rowCount()); numProducts = (Integer) criteria.list().get(0); } catch (RuntimeException re) { log.error("findNumproductDefIsvProducts failed", re); throw re; } return numProducts; } public List<Product> findIsvProducts(String isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(isvId); return findIsvProducts(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<Product> findIsvProducts(Long isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<Product> products = null; try { String qs = "from Product as prod where prod.isv.id= :isvId order by prod." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); products = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return products; } public List<QuickyProduct> findAllIsvQuickyProducts(String isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long id = Long.parseLong(isvId); return findAllIsvQuickyProducts(id, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<QuickyProduct> findAllIsvQuickyProducts(Long isvId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<QuickyProduct> products = null; try { String qs = "from QuickyProduct as qp where qp.isv.id= :isvId and deleted = false order by qp." + orderBy + " " + orderDir; Query query = getSession().createQuery(qs); query.setFirstResult(firstResult); query.setMaxResults(maxResults); query.setParameter("isvId", isvId); products = query.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return products; } public List<QuickyProduct> findIsvQuickyProducts(String isvId, String prodDefId, String storeId, String categoryId, String platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { try { Long lisvid = Long.parseLong(isvId); Long lppid = StringUtils.hasText(prodDefId) ? Long.parseLong(prodDefId) : null; Long lstoreid = StringUtils.hasText(storeId) ? Long.parseLong(storeId) : null; Long lcatid = StringUtils.hasText(categoryId) ? Long.parseLong(categoryId) : null; Long lplatid = StringUtils.hasText(platformId) ? Long.parseLong(platformId) : null; return findIsvQuickyProducts(lisvid, lppid, lstoreid, lcatid, lplatid, firstResult, maxResults, orderBy, orderDir); } catch (Exception e) { log.error("find by property name failed", e); throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public List<QuickyProduct> findIsvQuickyProducts(Long isvId, Long productDefId, Long storeId, Long categoryId, Long platformId, Integer firstResult, Integer maxResults, String orderBy, String orderDir) { List<QuickyProduct> products = null; try { Criteria criteria = getSession().createCriteria(QuickyProduct.class); criteria.add(Restrictions.eq("isv.id", isvId)); if (productDefId != null) criteria.add(Restrictions.eq("productDef.id", productDefId)); if (storeId != null) criteria.add(Restrictions.eq("listingStore.id", storeId)); if (categoryId != null) criteria.add(Restrictions.eq("category.id", categoryId)); if (platformId != null) criteria.add(Restrictions.eq("platform.id", platformId)); criteria.add(Restrictions.eq("locked", false)); criteria.add(Restrictions.eq("deleted", false)); createOrderByFieldAliasIfApplicable(orderBy, orderDir, criteria); products = criteria.list(); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return products; } public void save(Product transientInstance) { log.debug("saving Product instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(Product persistentInstance) { log.debug("deleting Product instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Product findById(java.lang.Long id) { log.debug("getting Product instance with id: " + id); try { Product instance = (Product) getHibernateTemplate().get("com.lm.lic.manager.hibernate.Product", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public QuickyProduct findQuickyProductById(Long id) { log.debug("getting QuickyProduct instance with id: " + id); try { QuickyProduct qp = (QuickyProduct) getHibernateTemplate() .get("com.lm.lic.manager.hibernate.QuickyProduct", id); return qp; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } @SuppressWarnings("unchecked") public Product findById(Long isvId, Long prodId) { log.debug("getting Product instance with id: " + prodId); Product product = null; try { String qs = "from Product as prod where id= :prodId and prod.isv.id= :isvId"; Query query = getSession().createQuery(qs); query.setParameter("prodId", prodId); query.setParameter("isvId", isvId); List<Product> products = query.list(); if (products != null && products.size() > 0) product = products.get(0); } catch (RuntimeException re) { log.error("get failed", re); throw re; } return product; } @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductById(Long isvId, Long prodId) { log.debug("getting Product instance with id: " + prodId); QuickyProduct product = null; try { String qs = "from QuickyProduct as prod where id= :prodId and prod.isv.id= :isvId"; Query query = getSession().createQuery(qs); query.setParameter("prodId", prodId); query.setParameter("isvId", isvId); List<QuickyProduct> products = query.list(); if (products != null && products.size() > 0) product = products.get(0); } catch (RuntimeException re) { log.error("get failed", re); throw re; } return product; } public Product findProductByKey(String prodKey) { List<Product> products = findByProperty(PRODUCT_KEY, prodKey); Product product = null; if (products != null && products.size() > 0) product = products.get(0); return product; } public Product findProductByIsvProdId(String isvProdId) { List<Product> products = findByProperty(ISV_PRODUCT_ID, isvProdId); Product product = null; if (products != null && products.size() > 0) product = products.get(0); return product; } public QuickyProduct findQuickyProductByIsvProdStoreId(String isvId, String isvProdId, String storeId) { Long lisvId = Long.parseLong(isvId); Long lstoreId = Long.parseLong(storeId); QuickyProduct product = findQuickyProductByIsvProdStoreId(lisvId, isvProdId, lstoreId); return product; } @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductByIsvProdStoreId(Long isvId, String isvProductId, Long storeId) { QuickyProduct product = null; try { String qs = "from QuickyProduct as qp where isvProductId= :isvProductId and qp.isv.id= :isvId " + "and qp.listingStore is not null and qp.listingStore.id= :storeId and qp.deleted= :deleted"; Query query = getSession().createQuery(qs); query.setParameter("isvProductId", isvProductId); query.setParameter("isvId", isvId); query.setParameter("storeId", storeId); query.setParameter("deleted", false); List<QuickyProduct> products = query.list(); if (products != null && products.size() > 0) product = products.get(0); } catch (RuntimeException re) { log.error("findQuickyProductByIsvProdStoreId failed", re); throw re; } return product; } public Product findProductByIsvProdId(String isvId, String isvProdId) { Product product = null; if (!StringUtils.hasText(isvId)) { product = findProductByIsvProdId(isvProdId); } else { Long lisvId = Long.parseLong(isvId); product = findProductByIsvProdId(lisvId, isvProdId); } return product; } @SuppressWarnings("unchecked") public Product findProductByIsvProdId(Long isvId, String isvProductId) { Product product = null; try { String qs = "from Product as prod where isvProductId= :isvProductId and prod.isv.id= :isvId " + "and prod.deleted= :deleted"; Query query = getSession().createQuery(qs); query.setParameter("isvProductId", isvProductId); query.setParameter("isvId", isvId); query.setParameter("deleted", false); List<Product> products = query.list(); if (products != null && products.size() > 0) product = products.get(0); } catch (RuntimeException re) { log.error("get failed", re); throw re; } return product; } public QuickyProduct findProductByProductKey(String isvId, String productKey) { Long lisvId = Long.parseLong(isvId); QuickyProduct product = findProductByProductKey(lisvId, productKey); return product; } @SuppressWarnings("unchecked") public QuickyProduct findProductByProductKey(Long isvId, String productKey) { QuickyProduct product = null; try { String qs = "from QuickyProduct as qp where productKey= :productKey and isv.id= :isvId " + "and deleted = false"; Query query = getSession().createQuery(qs); query.setParameter("productKey", productKey); query.setParameter("isvId", isvId); List<QuickyProduct> products = query.list(); product = findFirstUndeletedAndEnabledQuickyProductFromList(products); } catch (RuntimeException re) { log.error("get failed", re); throw re; } return product; } public Product findProductByName(String prodName) { List<Product> products = findByProperty(NAME, prodName); Product product = null; if (products != null && products.size() > 0) product = products.get(0); return product; } @SuppressWarnings("unchecked") public Product findProductByName(String isvId, String name) { Product product = null; try { Long lisvId = Long.parseLong(isvId); String queryString = "from Product as p where p.name=? and p.isv.id=?"; List<Product> products = getHibernateTemplate().find(queryString, name, lisvId); product = findFirstUndeletedAndEnabledProductFromList(products); } catch (RuntimeException re) { log.error("findProductByNameAndVersion failed", re); throw re; } return product; } public Product findFirstUndeletedAndEnabledProductFromList(List<Product> products) { Product product = null; if (products != null && products.size() > 0) { for (Product p : products) { if (!p.getDeleted() && p.getEnabled()) product = p; } } return product; } public QuickyProduct findFirstUndeletedAndEnabledQuickyProductFromList(List<QuickyProduct> products) { QuickyProduct product = null; if (products != null && products.size() > 0) { for (QuickyProduct p : products) { if (!p.getDeleted() && p.getEnabled()) product = p; } } return product; } @SuppressWarnings("unchecked") public Product findProductByNameAndVersion(String name, String version) { Product product = null; try { String queryString = "from Product as p where p.name=? and p.version=? order by p.name desc"; List<Product> products = getHibernateTemplate().find(queryString, name, version); product = findFirstUndeletedAndEnabledProductFromList(products); } catch (RuntimeException re) { log.error("findProductByNameAndVersion failed", re); throw re; } return product; } @SuppressWarnings("unchecked") public Product findProductByNameAndVersion(String isvId, String name, String version) { Product product = null; try { Long lisvId = Long.parseLong(isvId); String queryString = "from Product as p where p.isv.id=? and p.name=? and p.version=? order by p.name desc"; List<Product> products = getHibernateTemplate().find(queryString, lisvId, name, version); product = findFirstUndeletedAndEnabledProductFromList(products); } catch (RuntimeException re) { log.error("findProductByNameAndVersion failed", re); throw re; } return product; } @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductByNameAndVersion(String name, String version, Long storeId) { QuickyProduct product = null; try { String queryString = "from QuickyProduct as qp where qp.listingStore.id = ? and qp.name=? and qp.version=? order by qp.name desc"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, storeId, name, version); product = findFirstUndeletedAndEnabledQuickyProductFromList(products); } catch (RuntimeException re) { log.error("findProductByNameAndVersion failed", re); throw re; } return product; } @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductByNameAndVersion(String isvId, String name, String version, String storeId) { QuickyProduct product = null; try { Long lisvId = Long.parseLong(isvId); Long lstoreId = Long.parseLong(storeId); String qs = "from QuickyProduct as qp where qp.isv.id= :isvId and qp.name= :name and qp.version= :version and " + "qp.listingStore is not null and qp.listingStore.id= :sid and qp.deleted= :deleted order by qp.name desc"; Query query = getSession().createQuery(qs); query.setParameter("isvId", lisvId); query.setParameter("name", name); query.setParameter("version", version); query.setParameter("sid", lstoreId); query.setParameter("deleted", false); List<QuickyProduct> products = query.list(); product = findFirstUndeletedAndEnabledQuickyProductFromList(products); } catch (RuntimeException re) { log.error("findQuickyProductByNameAndVersion failed", re); throw re; } return product; } /** * @param isvId * @param prodDefKey * @param name * @param version * @param storeId * @return */ @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductByNameAndVersion(String isvId, String prodDefKey, String name, String version, String storeId) { QuickyProduct product = null; try { Long lisvId = Long.parseLong(isvId); Long lstoreId = Long.parseLong(storeId); String qs = "from QuickyProduct as qp where qp.isv.id= :isvId and qp.productDef.productKey= :prodDefKey and qp.name= :name and qp.version= :version and " + "qp.listingStore is not null and qp.listingStore.id= :sid and qp.deleted= :deleted order by qp.name desc"; Query query = getSession().createQuery(qs); query.setParameter("isvId", lisvId); query.setParameter("prodDefKey", prodDefKey); query.setParameter("name", name); query.setParameter("version", version); query.setParameter("sid", lstoreId); query.setParameter("deleted", false); List<QuickyProduct> products = query.list(); product = findFirstUndeletedAndEnabledQuickyProductFromList(products); } catch (RuntimeException re) { log.error("findQuickyProductByNameAndVersion failed", re); throw re; } return product; } @SuppressWarnings("unchecked") public List<Product> findByExample(Product instance) { log.debug("finding Product instance by example"); try { List<Product> 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<Product> findByProperty(String propertyName, Object value) { log.debug("finding Product instance with property: " + propertyName + ", value: " + value); try { String queryString = "from Product as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List<Product> findByName(Object name) { return findByProperty(NAME, name); } public List<Product> findByDescription(Object description) { return findByProperty(DESCRIPTION, description); } public List<Product> findByImgage(Object imgage) { return findByProperty(IMGAGE, imgage); } public List<Product> findByLocale(Object locale) { return findByProperty(LOCALE, locale); } public List<Product> findByCreatedBy(Object createdBy) { return findByProperty(CREATED_BY, createdBy); } public List<Product> findByModifiedBy(Object modifiedBy) { return findByProperty(MODIFIED_BY, modifiedBy); } public List<Product> findByImage(Object image) { return findByProperty(IMAGE, image); } @SuppressWarnings("unchecked") public List<Product> findAll() { log.debug("finding all Product instances"); try { String queryString = "from Product"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } @SuppressWarnings("unchecked") public List<Product> findByIsvId(Long isvId) { try { String queryString = "from Product as prod where prod.isv.id= ?"; List<Product> products = getHibernateTemplate().find(queryString, isvId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<QuickyProduct> findQuicklyByIsvId(Long isvId) { try { String queryString = "from QuickyProduct as qp where qp.isv.id= ?"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<QuickyProduct> findQuicklyByIsvStoreId(Long isvId, Long storeId) { try { String queryString = "from QuickyProduct as qp where qp.isv.id= ? and qp.listingStore.id=?"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvId, storeId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<QuickyProduct> findQuicklyByIsvStoreId(Long isvId, Long storeId, boolean includeFloatingLicensed) { try { String queryString = "from QuickyProduct as qp where qp.isv.id= ? and qp.listingStore.id=?"; if (!includeFloatingLicensed) queryString = "from QuickyProduct as qp where qp.isv.id= ? and qp.listingStore.id=? and qp.licMechType.name not like '%Floating%'"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvId, storeId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<QuickyProduct> findIsvFloatLicensedQuickyProducts(Long isvId) { try { String queryString = "from QuickyProduct as qp where qp.isv.id= ? and qp.licMechType.name like '%Floating%'"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public QuickyProduct findQuickyProductByIsvProdId(String isvProdId, Long storeId) { QuickyProduct qp = null; try { String queryString = "from QuickyProduct as qp where qp.isvProductId= ? and qp.listingStore.id=?"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvProdId, storeId); if (products != null && products.size() > 0) qp = products.get(0); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } return qp; } @SuppressWarnings("unchecked") public List<QuickyProduct> findQuicklyIsvStoreless(Long isvId) { try { String queryString = "from QuickyProduct as qp where qp.isv.id= ? and qp.listingStore is null"; List<QuickyProduct> products = getHibernateTemplate().find(queryString, isvId); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } @SuppressWarnings("unchecked") public List<Product> findByIsvId(Long isvId, Boolean enabled) { try { String queryString = "from Product as prod where prod.isv.id=? and prod.enabled=?"; List<Product> products = getHibernateTemplate().find(queryString, isvId, enabled); return products; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public Product merge(Product detachedInstance) { log.debug("merging Product instance"); try { Product prod = getHibernateTemplate().merge(detachedInstance); log.debug("merge successful"); return prod; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Product instance) { log.debug("attaching dirty Product instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Product instance) { log.debug("attaching clean Product instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static ProductDAO getFromApplicationContext(ApplicationContext ctx) { return (ProductDAO) ctx.getBean("ProductDAO"); } }