KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > dao > hibernate > HibernateComponentDao


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) 2006
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.components.dao.hibernate;
25
26 import java.util.List JavaDoc;
27
28 import org.hibernate.Query;
29 import org.hibernate.SessionFactory;
30 import org.riotfamily.components.ComponentList;
31 import org.riotfamily.components.Location;
32 import org.riotfamily.components.VersionContainer;
33 import org.riotfamily.components.dao.AbstractComponentDao;
34 import org.riotfamily.riot.hibernate.support.HibernateHelper;
35 import org.springframework.util.Assert;
36
37 /**
38  * Default ComponentDAO implementation that uses Hibernate. All mappings
39  * a specified in <code>component.hbm.xml</code> which can be found in the
40  * same package.
41  */

42 public class HibernateComponentDao extends AbstractComponentDao {
43
44     private HibernateHelper hibernate;
45
46     public HibernateComponentDao() {
47     }
48
49     public void setSessionFactory(SessionFactory sessionFactory) {
50         this.hibernate = new HibernateHelper(sessionFactory, "components");
51     }
52
53     protected void initDao() {
54         Assert.notNull(hibernate, "A SessionFactory must be set.");
55     }
56
57     public ComponentList findComponentList(Location location) {
58         Query query = hibernate.createCacheableQuery("from "
59                 + ComponentList.class.getName() + " list where "
60                 + "list.location = :location and list.parent is null");
61
62         hibernate.setParameter(query, "location", location);
63         return (ComponentList) hibernate.uniqueResult(query);
64     }
65
66     public ComponentList findComponentList(VersionContainer parent, String JavaDoc slot) {
67         Query query = hibernate.createCacheableQuery("from "
68                 + ComponentList.class.getName() + " list where list.parent = "
69                 +":parent and list.location.slot = :slot");
70
71         hibernate.setParameter(query, "parent", parent);
72         hibernate.setParameter(query, "slot", slot);
73         return (ComponentList) query.uniqueResult();
74     }
75
76     public List JavaDoc findComponentLists(String JavaDoc type, String JavaDoc path) {
77         Query query = hibernate.createCacheableQuery("from "
78                 + ComponentList.class.getName() + " list where "
79                 + "list.location.type = :type "
80                 + "and list.location.path = :path "
81                 + "and list.parent is null");
82
83         hibernate.setParameter(query, "type", type);
84         hibernate.setParameter(query, "path", path);
85         return hibernate.list(query);
86     }
87
88     public List JavaDoc findDirtyComponentLists() {
89         Query query = hibernate.createCacheableQuery("from "
90                 + ComponentList.class.getName()
91                 + " list where list.dirty = true");
92
93         return hibernate.list(query);
94     }
95
96     protected Object JavaDoc loadObject(Class JavaDoc clazz, Long JavaDoc id) {
97         return hibernate.load(clazz, id);
98     }
99
100     protected void saveObject(Object JavaDoc object) {
101         hibernate.save(object);
102     }
103
104     protected void updateObject(Object JavaDoc object) {
105         hibernate.update(object);
106         //hibernate.flush();
107
}
108
109     protected void deleteObject(Object JavaDoc object) {
110         hibernate.delete(object);
111     }
112
113 }
114
Popular Tags