KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > dao > hibernate > HibernatePageDao


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.dao.hibernate;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.hibernate.Criteria;
33 import org.hibernate.Query;
34 import org.hibernate.SessionFactory;
35 import org.hibernate.criterion.Projections;
36 import org.hibernate.criterion.Restrictions;
37 import org.riotfamily.pages.Page;
38 import org.riotfamily.pages.PageAlias;
39 import org.riotfamily.pages.PageLocation;
40 import org.riotfamily.pages.PageNode;
41 import org.riotfamily.pages.Site;
42 import org.riotfamily.pages.dao.AbstractPageDao;
43 import org.riotfamily.riot.hibernate.support.HibernateHelper;
44 import org.riotfamily.riot.hibernate.support.HibernateUtils;
45 import org.springframework.util.Assert;
46
47 /**
48  * PageDao implementation that uses Hibernate.
49  *
50  * @author Felix Gnass [fgnass at neteye dot de]
51  * @author Jan-Frederic Linde [jfl at neteye dot de]
52  * @since 6.5
53  */

54 public class HibernatePageDao extends AbstractPageDao {
55
56     private static final Log log = LogFactory.getLog(HibernatePageDao.class);
57
58     private HibernateHelper hibernate;
59
60     public void setSessionFactory(SessionFactory sessionFactory) {
61         this.hibernate = new HibernateHelper(sessionFactory, "pages");
62     }
63
64     protected void initDao() {
65         Assert.notNull(hibernate, "A SessionFactory must be set.");
66     }
67
68     protected Object JavaDoc loadObject(Class JavaDoc clazz, Serializable JavaDoc id) {
69         return hibernate.load(clazz, id);
70     }
71
72     protected void saveObject(Object JavaDoc object) {
73         hibernate.save(object);
74     }
75
76     protected void updateObject(Object JavaDoc object) {
77         hibernate.update(object);
78     }
79
80     protected void deleteObject(Object JavaDoc object) {
81         hibernate.delete(object);
82     }
83
84     protected void flush() {
85         hibernate.flush();
86     }
87     
88     public List JavaDoc listSites() {
89         Criteria c = hibernate.createCacheableCriteria(Site.class);
90         return hibernate.list(c);
91     }
92
93     public Site getSite(String JavaDoc name) {
94         if (name == null) {
95             return getDefaultSite();
96         }
97         Criteria c = hibernate.createCacheableCriteria(Site.class);
98         c.add(Restrictions.eq("name", name));
99         return (Site) hibernate.uniqueResult(c);
100     }
101
102     public PageNode findRootNode(Site site) {
103         Criteria c = hibernate.createCacheableCriteria(PageNode.class);
104         c.add(Restrictions.isNull("parent"));
105         c.add(Restrictions.eq("site", site));
106         return (PageNode) hibernate.uniqueResult(c);
107     }
108
109     public Page findPage(PageLocation location) {
110         String JavaDoc siteName = location.getSiteName();
111         Site site = siteName != null ? getSite(siteName) : getDefaultSite();
112         Criteria c = hibernate.createCacheableCriteria(Page.class);
113         c.createCriteria("node").add(Restrictions.eq("site", site));
114         c.add(Restrictions.eq("path", location.getPath()));
115         HibernateUtils.addEqOrNull(c, "locale", location.getLocale());
116         return (Page) hibernate.uniqueResult(c);
117     }
118
119     public PageNode findNodeForHandler(String JavaDoc handlerName) {
120         Criteria c = hibernate.createCacheableCriteria(PageNode.class)
121                 .add(Restrictions.eq("handlerName", handlerName));
122
123         return (PageNode) hibernate.uniqueResult(c);
124     }
125
126     public Page findPageForHandler(String JavaDoc handlerName, Locale JavaDoc locale) {
127         return (Page) hibernate.uniqueResult(
128                 createPageQuery(handlerName, locale));
129     }
130
131     public List JavaDoc findPagesForHandler(String JavaDoc handlerName, Locale JavaDoc locale) {
132         return hibernate.list(createPageQuery(handlerName, locale));
133     }
134     
135     public List JavaDoc getWildcardPaths(PageLocation location) {
136         String JavaDoc siteName = location.getSiteName();
137         Site site = siteName != null ? getSite(siteName) : getDefaultSite();
138         Criteria c = hibernate.createCacheableCriteria(Page.class);
139         c.setProjection(Projections.property("path"));
140         c.add(Restrictions.eq("wildcardInPath", Boolean.TRUE));
141         HibernateUtils.addEqOrNull(c, "locale", location.getLocale());
142         c.createCriteria("node").add(Restrictions.eq("site", site));
143         return hibernate.list(c);
144     }
145
146     private Query createPageQuery(String JavaDoc handlerName, Locale JavaDoc locale) {
147         StringBuffer JavaDoc hql = new StringBuffer JavaDoc("from ")
148             .append(Page.class.getName())
149             .append(" where node.handlerName = :handlerName and locale ")
150             .append(locale != null ? "= :locale" : "is null");
151
152         Query query = hibernate.createCacheableQuery(hql.toString());
153         hibernate.setParameter(query, "handlerName", handlerName);
154         hibernate.setParameter(query, "locale", locale);
155         return query;
156     }
157
158     public void refreshPage(Page page) {
159         hibernate.refresh(page);
160     }
161
162     public PageAlias findPageAlias(PageLocation location) {
163         if (location.getSiteName() == null) {
164             location.setSiteName(getDefaultSite().getName());
165         }
166         return (PageAlias) hibernate.get(PageAlias.class, location);
167     }
168
169
170     protected void clearAliases(Page page) {
171         log.info("Clearing aliases for " + page);
172         Query query = hibernate.createQuery("update " + PageAlias.class.getName()
173                 + " alias set alias.page = null where alias.page = :page");
174
175         hibernate.setParameter(query, "page", page);
176         hibernate.executeUpdate(query);
177         createAlias(null, new PageLocation(page));
178     }
179
180
181 }
182
Popular Tags