KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionPage;
21 import com.blandware.atleap.model.core.ContentPage;
22 import com.blandware.atleap.model.core.Page;
23 import com.blandware.atleap.persistence.core.PageDAO;
24
25 import java.util.*;
26
27 /**
28  * <p>Hibernate implementation of PageDAO</p>
29  * <p><a HREF="PageDAOHibernate.java.htm"><i>View Source</i></a></p>
30  *
31  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
32  * @version $Revision: 1.22 $ $Date: 2005/10/10 08:30:00 $
33  */

34 public class PageDAOHibernate extends LocalizableDAOHibernate implements PageDAO {
35
36     /**
37      * Creates new instance of PageDAOHibernate
38      */

39     public PageDAOHibernate() {
40     }
41
42     /**
43      * @see com.blandware.atleap.persistence.core.PageDAO#retrievePage(java.lang.Long)
44      */

45     public Page retrievePage(Long JavaDoc pageId) {
46         return (Page) getHibernateTemplate().get(Page.class, pageId);
47     }
48
49     /**
50      * @see com.blandware.atleap.persistence.core.PageDAO#findPageByUri(java.lang.String)
51      */

52     public Page findPageByUri(String JavaDoc uri) {
53         return (Page) findUniqueResult("from Page p where p.uri = ?", new Object JavaDoc[]{uri});
54     }
55
56     /**
57      * @see com.blandware.atleap.persistence.core.PageDAO#updatePage(com.blandware.atleap.model.core.Page)
58      */

59     public void updatePage(Page page) {
60         getHibernateTemplate().update(page);
61     }
62
63     /**
64      * @see com.blandware.atleap.persistence.core.PageDAO#listLinkableItems(com.blandware.atleap.common.util.QueryInfo)
65      */

66     public PartialCollection listLinkableItems(QueryInfo queryInfo) {
67         String JavaDoc whereClause = new String JavaDoc();
68         String JavaDoc orderByClause = new String JavaDoc();
69         if ( queryInfo != null ) {
70             whereClause = queryInfo.getWhereClause();
71             orderByClause = queryInfo.getOrderByClause();
72             if ( whereClause == null || whereClause.length() == 0 ) {
73                 whereClause = new String JavaDoc();
74             }
75             if ( orderByClause != null && orderByClause.length() != 0 ) {
76                 orderByClause = " order by " + orderByClause;
77             } else {
78                 orderByClause = new String JavaDoc();
79             }
80         }
81
82         List list = null;
83         Integer JavaDoc total = null;
84         String JavaDoc localeIdentifier = null;
85         if ( queryInfo != null ) {
86             if ( queryInfo.getQueryParameters() != null ) {
87                 localeIdentifier = (String JavaDoc) queryInfo.getQueryParameters().get("localeIdentifier");
88             }
89         }
90
91         boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0;
92
93         String JavaDoc hqlPart = new String JavaDoc();
94         ArrayList args = new ArrayList();
95         if ( localeIdentifierPresent ) {
96             args.add(localeIdentifier);
97             hqlPart = new StringBuffer JavaDoc("from Page as page ")
98                     .append("left outer join page.contentFields as titleField ")
99                     .append("left outer join titleField.contentFieldValues as title ")
100                     .append("left outer join page.roles as role ").append("where ((page.className != '").append(ActionPage.class.getName()).append("') and (page.className != '").append(ContentPage.class.getName()).append("')) and ")
101                     .append("titleField.identifier = 'title' ")
102                     .append("and title.contentLocale.identifier = ? ")
103                     .toString();
104             if ( whereClause.length() > 0 ) {
105                 hqlPart += "and " + whereClause;
106             }
107         } else {
108             hqlPart = new StringBuffer JavaDoc("from Page page left outer join page.roles as role ").append("where ((page.className != '").append(ActionPage.class.getName()).append("') and (page.className != '").append(ContentPage.class.getName()).append("')) ")
109                     .toString();
110             if ( whereClause.length() > 0 ) {
111                 hqlPart += "and " + whereClause;
112             }
113         }
114
115         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
116             // query count
117
String JavaDoc hqlForTotal = "select count(distinct page.id) " + hqlPart;
118             total = (Integer JavaDoc) findUniqueResult(hqlForTotal, args.toArray());
119             if ( total == null ) {
120                 total = new Integer JavaDoc(0);
121             }
122         }
123
124         // If we don't have any info about the total number of results yet or
125
// we know that there's something that will be found, then fetch data
126
if ( total == null || total.intValue() > 0 ) {
127             String JavaDoc hql = new String JavaDoc();
128             if ( localeIdentifierPresent ) {
129                 hql = "select distinct page, title.simpleValue " + hqlPart + orderByClause;
130             } else {
131                 hql = "select page " + hqlPart + orderByClause;
132             }
133             list = executeFind(hql, queryInfo, args.toArray());
134             if ( total == null ) {
135                 total = new Integer JavaDoc(list.size());
136             }
137             if ( localeIdentifierPresent ) {
138                 for ( ListIterator i = list.listIterator(); i.hasNext(); ) {
139                     Object JavaDoc[] objects = (Object JavaDoc[]) i.next();
140                     Page page = (Page) objects[0];
141                     page.getTitle().put(localeIdentifier, objects[1]);
142                     i.set(page);
143                 }
144             }
145         } else {
146             list = new ArrayList();
147         }
148
149
150         return new PartialCollection(list, total);
151     }
152
153     /**
154      * @see com.blandware.atleap.persistence.core.PageDAO#listPagesFetching()
155      */

156     public Collection listPagesFetching() {
157         String JavaDoc hql = new StringBuffer JavaDoc("from Page as page ")
158                 .append("left outer join fetch page.contentFields as field ")
159                 .append("left outer join fetch field.contentFieldValues as value where page.active = 'T'").toString();
160         return new HashSet(executeFind(hql));
161     }
162
163     /**
164      * @see com.blandware.atleap.persistence.core.PageDAO#hasDuplicates(com.blandware.atleap.model.core.Page)
165      */

166     public boolean hasDuplicates(Page page) {
167
168         ArrayList args = new ArrayList();
169         args.add(page.getUri());
170         String JavaDoc hql = "select count(p.id) from Page p where p.uri = ?";
171         if ( page.getId() != null ) {
172             hql += " and p.id != ?";
173             args.add(page.getId());
174         }
175
176         int count = ((Integer JavaDoc) findUniqueResult(hql, args.toArray())).intValue();
177
178         return count > 0;
179
180     }
181
182 }
183
Popular Tags