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.ContentResource; 21 import com.blandware.atleap.model.core.ResourceData; 22 import com.blandware.atleap.model.core.Role; 23 import com.blandware.atleap.persistence.core.ContentResourceDAO; 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 ContentResourceDAOHibernate extends BaseDAOHibernate implements ContentResourceDAO { 38 39 42 public ContentResourceDAOHibernate() { 43 } 44 45 47 50 public Long createContentResource(ContentResource contentResource, ResourceData resourceData) { 51 contentResource.setLastUpdatedDatetime(new Date ()); 52 contentResource.setUsageCounter(new Integer (0)); 53 getHibernateTemplate().save(resourceData); 54 contentResource.setResourceData(resourceData); 55 return (Long ) getHibernateTemplate().save(contentResource); 56 } 57 58 61 public ContentResource retrieveContentResource(Long contentResourceId) { 62 return (ContentResource) getHibernateTemplate().get(ContentResource.class, contentResourceId); 63 } 64 65 68 public void updateContentResource(ContentResource contentResource, ResourceData resourceData) { 69 contentResource.setLastUpdatedDatetime(new Date ()); 70 getHibernateTemplate().saveOrUpdate(resourceData); 71 contentResource.setResourceData(resourceData); 72 getHibernateTemplate().update(contentResource); 73 } 74 75 78 public void deleteContentResource(ContentResource contentResource) throws DeleteException { 79 getHibernateTemplate().delete(contentResource); 80 81 List roles = new ArrayList (contentResource.getRoles()); 83 for ( int i = 0; i < roles.size(); i++ ) { 84 Role role = (Role) roles.get(i); 85 contentResource.removeRole(role); 86 } 87 } 88 89 91 94 public PartialCollection listContentResources(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 = new String (); 102 } 103 if ( orderByClause != null && orderByClause.length() != 0 ) { 104 orderByClause = " order by " + orderByClause; 105 } else { 106 orderByClause = new String (); 107 } 108 } 109 110 List list = null; 111 Integer total = null; 112 String hqlPart = null; 113 hqlPart = "from ContentResource r left outer join r.roles as role"; 114 115 ArrayList args = new ArrayList (); 116 117 Object contentResourceType = queryInfo.getQueryParameters().get("contentResourceType"); 118 if ( contentResourceType != null ) { 119 hqlPart += " where (r.type = ?)"; 120 args.add(contentResourceType); 121 if ( whereClause.length() > 0 ) { 122 whereClause = " and " + whereClause; 123 } 124 } else if ( whereClause.length() > 0 ) { 125 whereClause = " where " + whereClause; 126 } 127 128 hqlPart += whereClause; 129 130 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 131 String hqlForTotal = "select count(distinct r.id) " + hqlPart; 132 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 133 if ( total == null ) { 134 total = new Integer (0); 135 } 136 137 } 138 139 if ( total == null || total.intValue() > 0 ) { 142 String hql = "select distinct r " + hqlPart + orderByClause; 143 list = executeFind(hql, queryInfo, args.toArray()); 144 if ( total == null ) { 145 total = new Integer (list.size()); 146 } 147 } else { 148 list = new ArrayList (); 149 } 150 return new PartialCollection(list, total); 151 152 } 153 154 157 public boolean hasDuplicates(ContentResource contentResource) { 158 159 ArrayList args = new ArrayList (); 160 args.add(contentResource.getUri()); 161 162 String hql = "select count(r.id) from ContentResource r where r.uri = ?"; 163 if ( contentResource.getId() != null ) { 164 hql += " and r.id != ?"; 165 args.add(contentResource.getId()); 166 } 167 168 int count = ((Integer ) findUniqueResult(hql, args.toArray())).intValue(); 169 170 return count > 0; 171 } 172 173 176 public ContentResource findContentResourceByUri(String uri) { 177 return (ContentResource) findUniqueResult("from ContentResource r where r.uri = ?", new Object []{uri}); 178 } 179 } 180 | Popular Tags |