1 16 package com.blandware.atleap.persistence.hibernate.core; 17 18 import com.blandware.atleap.common.util.PartialCollection; 19 import com.blandware.atleap.common.util.QueryInfo; 20 import com.blandware.atleap.model.core.ContentImage; 21 import com.blandware.atleap.model.core.ResourceData; 22 import com.blandware.atleap.model.core.Role; 23 import com.blandware.atleap.persistence.core.ContentImageDAO; 24 import com.blandware.atleap.persistence.exception.DeleteException; 25 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.Date ; 29 30 37 public class ContentImageDAOHibernate extends ContentResourceDAOHibernate implements ContentImageDAO { 38 39 42 public ContentImageDAOHibernate() { 43 } 44 45 47 50 public Long createContentImage(ContentImage contentImage, ResourceData resourceData) { 51 contentImage.setLastUpdatedDatetime(new Date ()); 52 contentImage.setUsageCounter(new Integer (0)); 53 getHibernateTemplate().save(resourceData); 54 contentImage.setResourceData(resourceData); 55 return (Long ) getHibernateTemplate().save(contentImage); 56 } 57 58 61 public ContentImage retrieveContentImage(Long contentImageId) { 62 return (ContentImage) getHibernateTemplate().get(ContentImage.class, contentImageId); 63 } 64 65 68 public void updateContentImage(ContentImage contentImage, ResourceData resourceData) { 69 contentImage.setLastUpdatedDatetime(new Date ()); 70 getHibernateTemplate().saveOrUpdate(resourceData); 71 contentImage.setResourceData(resourceData); 72 getHibernateTemplate().update(contentImage); 73 } 74 75 78 public void deleteContentImage(ContentImage contentImage) throws DeleteException { 79 getHibernateTemplate().delete(contentImage); 80 81 List roles = new ArrayList (contentImage.getRoles()); 83 for ( int i = 0; i < roles.size(); i++ ) { 84 Role role = (Role) roles.get(i); 85 contentImage.removeRole(role); 86 } 87 } 88 89 91 94 public PartialCollection listContentImages(QueryInfo queryInfo) { 95 String whereClause = new String (); 96 String orderByClause = new String (); 97 if ( queryInfo != null ) { 98 whereClause = queryInfo.getWhereClause(); 99 orderByClause = queryInfo.getOrderByClause(); 100 if ( whereClause != null ) { 101 whereClause = " where " + whereClause; 102 } else { 103 whereClause = new String (); 104 } 105 if ( orderByClause != null && orderByClause.length() != 0 ) { 106 orderByClause = " order by " + orderByClause; 107 } else { 108 orderByClause = new String (); 109 } 110 } 111 112 List list = null; 113 Integer total = null; 114 String hqlPart = null; 115 hqlPart = "from ContentImage r left outer join r.roles as role"; 116 117 hqlPart += whereClause; 118 119 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 120 String hqlForTotal = "select count(distinct r.id) " + hqlPart; 121 total = (Integer ) findUniqueResult(hqlForTotal); 122 if ( total == null ) { 123 total = new Integer (0); 124 } 125 } 126 127 if ( total == null || total.intValue() > 0 ) { 130 String hql = "select distinct r " + hqlPart + orderByClause; 131 list = executeFind(hql, queryInfo); 132 if ( total == null ) { 133 total = new Integer (list.size()); 134 } 135 } else { 136 list = new ArrayList (); 137 } 138 return new PartialCollection(list, total); 139 140 } 141 142 145 public ContentImage findContentImageByUri(String uri) { 146 return (ContentImage) findUniqueResult("from ContentImage r where r.uri = ?", new Object []{uri}); 147 } 148 } 149 | Popular Tags |