KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > persistence > hibernate > core > ContentResourceDAOHibernate


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Date JavaDoc;
29
30 /**
31  * <p>Hibernate implementation of ContentResourceDAO</p>
32  * <p><a HREF="ContentResourceDAOHibernate.java.htm"><i>View Source</i></a></p>
33  *
34  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
35  * @version $Revision: 1.26 $ $Date: 2005/08/06 17:13:05 $
36  */

37 public class ContentResourceDAOHibernate extends BaseDAOHibernate implements ContentResourceDAO {
38
39     /**
40      * Creates new instance of ContentResourceDAOHibernate
41      */

42     public ContentResourceDAOHibernate() {
43     }
44
45     // ~ CRUD Methods ================================================================
46

47     /**
48      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#createContentResource(com.blandware.atleap.model.core.ContentResource, com.blandware.atleap.model.core.ResourceData)
49      */

50     public Long JavaDoc createContentResource(ContentResource contentResource, ResourceData resourceData) {
51         contentResource.setLastUpdatedDatetime(new Date JavaDoc());
52         contentResource.setUsageCounter(new Integer JavaDoc(0));
53         getHibernateTemplate().save(resourceData);
54         contentResource.setResourceData(resourceData);
55         return (Long JavaDoc) getHibernateTemplate().save(contentResource);
56     }
57
58     /**
59      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#retrieveContentResource(java.lang.Long)
60      */

61     public ContentResource retrieveContentResource(Long JavaDoc contentResourceId) {
62         return (ContentResource) getHibernateTemplate().get(ContentResource.class, contentResourceId);
63     }
64
65     /**
66      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#updateContentResource(com.blandware.atleap.model.core.ContentResource, com.blandware.atleap.model.core.ResourceData)
67      */

68     public void updateContentResource(ContentResource contentResource, ResourceData resourceData) {
69         contentResource.setLastUpdatedDatetime(new Date JavaDoc());
70         getHibernateTemplate().saveOrUpdate(resourceData);
71         contentResource.setResourceData(resourceData);
72         getHibernateTemplate().update(contentResource);
73     }
74
75     /**
76      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#deleteContentResource(com.blandware.atleap.model.core.ContentResource)
77      */

78     public void deleteContentResource(ContentResource contentResource) throws DeleteException {
79         getHibernateTemplate().delete(contentResource);
80
81         // break relations between roles and deleted resource
82
List JavaDoc roles = new ArrayList JavaDoc(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     // ~ Additional methods ================================================================
90

91     /**
92      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#listContentResources(com.blandware.atleap.common.util.QueryInfo)
93      */

94     public PartialCollection listContentResources(QueryInfo queryInfo) {
95         String JavaDoc whereClause = new String JavaDoc();
96         String JavaDoc orderByClause = new String JavaDoc();
97         if ( queryInfo != null ) {
98             whereClause = queryInfo.getWhereClause();
99             orderByClause = queryInfo.getOrderByClause();
100             if ( whereClause == null ) {
101                 whereClause = new String JavaDoc();
102             }
103             if ( orderByClause != null && orderByClause.length() != 0 ) {
104                 orderByClause = " order by " + orderByClause;
105             } else {
106                 orderByClause = new String JavaDoc();
107             }
108         }
109
110         List JavaDoc list = null;
111         Integer JavaDoc total = null;
112         String JavaDoc hqlPart = null;
113         hqlPart = "from ContentResource r left outer join r.roles as role";
114
115         ArrayList JavaDoc args = new ArrayList JavaDoc();
116
117         Object JavaDoc 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 JavaDoc hqlForTotal = "select count(distinct r.id) " + hqlPart;
132             total = (Integer JavaDoc) findUniqueResult(hqlForTotal, args.toArray());
133             if ( total == null ) {
134                 total = new Integer JavaDoc(0);
135             }
136
137         }
138
139         // If we don't have any info about the total number of results yet or
140
// we know that there's something that will be found, then fetch data
141
if ( total == null || total.intValue() > 0 ) {
142             String JavaDoc hql = "select distinct r " + hqlPart + orderByClause;
143             list = executeFind(hql, queryInfo, args.toArray());
144             if ( total == null ) {
145                 total = new Integer JavaDoc(list.size());
146             }
147         } else {
148             list = new ArrayList JavaDoc();
149         }
150         return new PartialCollection(list, total);
151
152     }
153
154     /**
155      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#hasDuplicates(com.blandware.atleap.model.core.ContentResource)
156      */

157     public boolean hasDuplicates(ContentResource contentResource) {
158
159         ArrayList JavaDoc args = new ArrayList JavaDoc();
160         args.add(contentResource.getUri());
161
162         String JavaDoc 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 JavaDoc) findUniqueResult(hql, args.toArray())).intValue();
169
170         return count > 0;
171     }
172
173     /**
174      * @see com.blandware.atleap.persistence.core.ContentResourceDAO#findContentResourceByUri(String)
175      */

176     public ContentResource findContentResourceByUri(String JavaDoc uri) {
177         return (ContentResource) findUniqueResult("from ContentResource r where r.uri = ?", new Object JavaDoc[]{uri});
178     }
179 }
180
Popular Tags