KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentDocument;
21 import com.blandware.atleap.model.core.ResourceData;
22 import com.blandware.atleap.model.core.ContentLocale;
23 import com.blandware.atleap.model.core.Role;
24 import com.blandware.atleap.persistence.core.ContentDocumentDAO;
25 import com.blandware.atleap.persistence.exception.DeleteException;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Date JavaDoc;
30
31 /**
32  * <p>Hibernate implementation of ContentDocumentDAO</p>
33  * <p><a HREF="ContentDocumentDAOHibernate.java.htm"><i>View Source</i></a></p>
34  *
35  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
36  * @version $Revision: 1.8 $ $Date: 2005/08/06 17:13:04 $
37  */

38 public class ContentDocumentDAOHibernate extends ContentResourceDAOHibernate implements ContentDocumentDAO {
39
40     /**
41      * Creates new instance of ContentDocumentDAOHibernate
42      */

43     public ContentDocumentDAOHibernate() {
44     }
45
46     // ~ CRUD Methods ================================================================
47

48     /**
49      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#createContentDocument(com.blandware.atleap.model.core.ContentDocument, com.blandware.atleap.model.core.ResourceData, com.blandware.atleap.model.core.ContentLocale)
50      */

51     public Long JavaDoc createContentDocument(ContentDocument contentDocument, ResourceData resourceData, ContentLocale contentLocale) {
52         contentDocument.setLastUpdatedDatetime(new Date JavaDoc());
53         contentDocument.setContentLocale(contentLocale);
54         contentDocument.setUsageCounter(new Integer JavaDoc(0));
55         getHibernateTemplate().save(resourceData);
56         contentDocument.setResourceData(resourceData);
57         return (Long JavaDoc) getHibernateTemplate().save(contentDocument);
58     }
59
60     /**
61      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#retrieveContentDocument(Long)
62      */

63     public ContentDocument retrieveContentDocument(Long JavaDoc contentDocumentId) {
64         return (ContentDocument) getHibernateTemplate().get(ContentDocument.class, contentDocumentId);
65     }
66
67     /**
68      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#updateContentDocument(com.blandware.atleap.model.core.ContentDocument, com.blandware.atleap.model.core.ResourceData, com.blandware.atleap.model.core.ContentLocale)
69      */

70     public void updateContentDocument(ContentDocument contentDocument, ResourceData resourceData, ContentLocale contentLocale) {
71         contentDocument.setLastUpdatedDatetime(new Date JavaDoc());
72         contentDocument.setContentLocale(contentLocale);
73         getHibernateTemplate().saveOrUpdate(resourceData);
74         contentDocument.setResourceData(resourceData);
75         getHibernateTemplate().update(contentDocument);
76     }
77
78     /**
79      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#deleteContentDocument(com.blandware.atleap.model.core.ContentDocument)
80      */

81     public void deleteContentDocument(ContentDocument contentDocument) throws DeleteException {
82         getHibernateTemplate().delete(contentDocument);
83
84         // break relations between roles and deleted document
85
List JavaDoc roles = new ArrayList JavaDoc(contentDocument.getRoles());
86         for ( int i = 0; i < roles.size(); i++ ) {
87             Role role = (Role) roles.get(i);
88             contentDocument.removeRole(role);
89         }
90     }
91
92     // ~ Additional methods ================================================================
93

94     /**
95      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#listContentDocuments(com.blandware.atleap.common.util.QueryInfo)
96      */

97     public PartialCollection listContentDocuments(QueryInfo queryInfo) {
98         String JavaDoc whereClause = new String JavaDoc();
99         String JavaDoc orderByClause = new String JavaDoc();
100         if ( queryInfo != null ) {
101             whereClause = queryInfo.getWhereClause();
102             orderByClause = queryInfo.getOrderByClause();
103             if ( whereClause != null && whereClause.length() != 0 ) {
104                 whereClause = " where " + whereClause;
105             } else {
106                 whereClause = new String JavaDoc();
107             }
108             if ( orderByClause != null && orderByClause.length() != 0 ) {
109                 orderByClause = " order by " + orderByClause;
110             } else {
111                 orderByClause = new String JavaDoc();
112             }
113         }
114
115         List JavaDoc list = null;
116         Integer JavaDoc total = null;
117         String JavaDoc hqlPart = null;
118         hqlPart = "from ContentDocument r left outer join r.roles as role";
119
120         hqlPart += whereClause;
121
122         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
123             String JavaDoc hqlForTotal = "select count(distinct r.id) " + hqlPart;
124             total = (Integer JavaDoc) findUniqueResult(hqlForTotal);
125             if ( total == null ) {
126                 total = new Integer JavaDoc(0);
127             }
128         }
129
130         // If we don't have any info about the total number of results yet or
131
// we know that there's something that will be found, then fetch data
132
if ( total == null || total.intValue() > 0 ) {
133             String JavaDoc hql = "select distinct r " + hqlPart + orderByClause;
134             list = executeFind(hql, queryInfo);
135             if ( total == null ) {
136                 total = new Integer JavaDoc(list.size());
137             }
138         } else {
139             list = new ArrayList JavaDoc();
140         }
141         return new PartialCollection(list, total);
142
143     }
144
145     /**
146      * @see com.blandware.atleap.persistence.core.ContentDocumentDAO#findContentDocumentByUri(String)
147      */

148     public ContentDocument findContentDocumentByUri(String JavaDoc uri) {
149         return (ContentDocument) findUniqueResult("from ContentDocument r where r.uri = ?", new Object JavaDoc[]{uri});
150     }
151 }
152
Popular Tags