KickJava   Java API By Example, From Geeks To Geeks.

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


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.Localizable;
21 import com.blandware.atleap.model.core.MenuItem;
22 import com.blandware.atleap.model.core.Role;
23 import com.blandware.atleap.persistence.core.MenuDAO;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * <p>Hibernate implementation of MenuDAO</p>
30  * <p><a HREF="MenuDAOHibernate.java.htm"><i>View Source</i></a></p>
31  *
32  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
33  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
34  * @version $Revision: 1.38 $ $Date: 2005/10/10 08:29:59 $
35  */

36 public class MenuDAOHibernate extends LocalizableDAOHibernate implements MenuDAO {
37
38     /**
39      * Creates new instance of MenuDAOHibernate
40      */

41     public MenuDAOHibernate() {
42     }
43
44     // ~ CRUD Methods ================================================================
45

46     /**
47      * @see com.blandware.atleap.persistence.core.MenuDAO#createMenuItem(com.blandware.atleap.model.core.MenuItem, com.blandware.atleap.model.core.MenuItem, com.blandware.atleap.model.core.Localizable)
48      */

49     public Long JavaDoc createMenuItem(MenuItem menuItem, MenuItem parent, Localizable owner) {
50         menuItem.setParentItem(parent);
51         menuItem.setOwner(owner);
52         return (Long JavaDoc) getHibernateTemplate().save(menuItem);
53     }
54
55     /**
56      * @see com.blandware.atleap.persistence.core.MenuDAO#retrieveMenuItem(Long)
57      */

58     public MenuItem retrieveMenuItem(Long JavaDoc menuItemId) {
59         return (MenuItem) getHibernateTemplate().get(MenuItem.class, menuItemId);
60     }
61
62     /**
63      * @see com.blandware.atleap.persistence.core.MenuDAO#updateMenuItem(com.blandware.atleap.model.core.MenuItem, com.blandware.atleap.model.core.MenuItem, com.blandware.atleap.model.core.Localizable)
64      */

65     public void updateMenuItem(MenuItem menuItem, MenuItem parent, Localizable owner) {
66         menuItem.setParentItem(parent);
67         menuItem.setOwner(owner);
68         getHibernateTemplate().update(menuItem);
69     }
70
71     /**
72      * @see com.blandware.atleap.persistence.core.MenuDAO#deleteMenuItem(com.blandware.atleap.model.core.MenuItem)
73      */

74     public void deleteMenuItem(MenuItem menuItem) {
75         getHibernateTemplate().delete(menuItem);
76
77         // break relations between roles and deleted menu item
78
List JavaDoc roles = new ArrayList JavaDoc(menuItem.getRoles());
79         for ( int i = 0; i < roles.size(); i++ ) {
80             Role role = (Role) roles.get(i);
81             menuItem.removeRole(role);
82         }
83     }
84
85     // ~ Additional methods ================================================================
86

87     /**
88      * @see com.blandware.atleap.persistence.core.MenuDAO#listMenuItems(com.blandware.atleap.common.util.QueryInfo)
89      */

90     public PartialCollection listMenuItems(QueryInfo queryInfo) {
91         String JavaDoc whereClause = new String JavaDoc();
92         String JavaDoc orderByClause = " order by item.position asc, item.identifier asc";
93         if ( queryInfo != null ) {
94             whereClause = queryInfo.getWhereClause();
95             if ( whereClause != null && whereClause.length() != 0 ) {
96                 whereClause = " and " + whereClause;
97             }
98         }
99
100         List JavaDoc list = null;
101         Integer JavaDoc total = null;
102         Long JavaDoc parentItemId = null;
103         Long JavaDoc ownerId = null;
104         if ( queryInfo != null ) {
105             parentItemId = (Long JavaDoc) queryInfo.getQueryParameters().get("parentItemId");
106             ownerId = (Long JavaDoc) queryInfo.getQueryParameters().get("ownerId");
107         }
108         if ( log.isDebugEnabled() ) {
109             log.debug("Parent ID: " + parentItemId);
110             log.debug("Owner ID: " + ownerId);
111         }
112         String JavaDoc hql = "select distinct item from MenuItem as item left outer join item.roles as role where";
113
114         ArrayList JavaDoc args = new ArrayList JavaDoc();
115
116         if ( parentItemId != null ) {
117             hql += " item.parentItem.id = ?";
118             args.add(parentItemId);
119         } else {
120             hql += " item.parentItem is null";
121         }
122
123         if ( ownerId != null ) {
124             hql += " and item.owner.id = ?";
125             args.add(ownerId);
126         } else {
127             hql += " and item.owner is null";
128         }
129
130         hql += whereClause + orderByClause;
131
132         list = executeFind(hql, args.toArray());
133
134         if ( list == null ) {
135             list = new ArrayList JavaDoc();
136         } else {
137             total = new Integer JavaDoc(list.size());
138         }
139
140         return new PartialCollection(list, total);
141
142     }
143
144     // ~ Finders ================================================================
145

146     /**
147      * @see com.blandware.atleap.persistence.core.MenuDAO#findMenuItemByIdentifierAndParentAndOwner(String, Long, Long)
148      */

149     public MenuItem findMenuItemByIdentifierAndParentAndOwner(String JavaDoc identifier, Long JavaDoc parentItemId, Long JavaDoc ownerId) {
150
151         String JavaDoc hql = "from MenuItem item where item.identifier = ?";
152
153         ArrayList JavaDoc args = new ArrayList JavaDoc();
154
155         args.add(identifier);
156
157         String JavaDoc parentHqlPart = " and item.parentItem.id = ?";
158         if ( parentItemId == null ) {
159             parentHqlPart = " and item.parentItem is null";
160         } else {
161             args.add(parentItemId);
162         }
163
164         String JavaDoc ownerHqlPart = " and item.owner.id = ?";
165         if ( ownerId == null ) {
166             ownerHqlPart = " and item.owner is null";
167         } else {
168             args.add(ownerId);
169         }
170
171         hql += parentHqlPart + ownerHqlPart;
172
173         List JavaDoc menuItems = executeFind(hql, args.toArray());
174
175         if ( menuItems != null && menuItems.size() > 0 ) {
176             return (MenuItem) menuItems.get(0);
177         } else {
178             return null;
179         }
180     }
181
182     /**
183      * @see com.blandware.atleap.persistence.core.MenuDAO#findMenuItemByPositionAndParentAndOwner(Integer, Long, Long)
184      */

185     public MenuItem findMenuItemByPositionAndParentAndOwner(Integer JavaDoc position, Long JavaDoc parentItemId, Long JavaDoc ownerId) {
186
187         String JavaDoc hql = "from MenuItem item where item.position = ?";
188         ArrayList JavaDoc args = new ArrayList JavaDoc();
189
190         args.add(position);
191
192         String JavaDoc parentHqlPart = " and item.parentItem.id = ?";
193         if ( parentItemId == null ) {
194             parentHqlPart = " and item.parentItem is null";
195         } else {
196             args.add(parentItemId);
197         }
198
199         String JavaDoc ownerHqlPart = " and item.owner.id = ?";
200         if ( ownerId == null ) {
201             ownerHqlPart = " and item.owner is null";
202         } else {
203             args.add(ownerId);
204         }
205
206         hql += parentHqlPart + ownerHqlPart;
207
208         return (MenuItem) findUniqueResult(hql, args.toArray());
209
210     }
211
212     /**
213      * @see com.blandware.atleap.persistence.core.MenuDAO#findMenuItemsByParentAndOwner(Long, Long)
214      */

215     public List JavaDoc findMenuItemsByParentAndOwner(Long JavaDoc parentItemId, Long JavaDoc ownerId) {
216
217         String JavaDoc hql = "from MenuItem item where ";
218         ArrayList JavaDoc args = new ArrayList JavaDoc();
219
220
221         String JavaDoc parentHqlPart = " item.parentItem.id = ?";
222         if ( parentItemId == null ) {
223             parentHqlPart = " item.parentItem is null";
224         } else {
225             args.add(parentItemId);
226         }
227
228         String JavaDoc ownerHqlPart = " and item.owner.id = ?";
229         if ( ownerId == null ) {
230             ownerHqlPart = " and item.owner is null";
231         } else {
232             args.add(ownerId);
233         }
234
235         hql += parentHqlPart + ownerHqlPart;
236
237         return executeFind(hql, args.toArray());
238     }
239
240
241     /**
242      * @see com.blandware.atleap.persistence.core.MenuDAO#findRedefinitionItem(Long, Long)
243      */

244     public MenuItem findRedefinitionItem(Long JavaDoc origItemId, Long JavaDoc ownerId) {
245
246         String JavaDoc hql = "from MenuItem item where ";
247         ArrayList JavaDoc args = new ArrayList JavaDoc();
248
249         String JavaDoc origItemHqlPart = "item.origItem.id = ?";
250         if ( origItemId == null ) {
251             origItemHqlPart = "item.origItem is null";
252         } else {
253             args.add(origItemId);
254         }
255
256         String JavaDoc ownerHqlPart = " and item.owner.id = ?";
257         if ( ownerId == null ) {
258             ownerHqlPart = " and item.owner is null";
259         } else {
260             args.add(ownerId);
261         }
262
263         hql += origItemHqlPart + ownerHqlPart;
264
265         return (MenuItem) findUniqueResult(hql, args.toArray());
266
267     }
268
269     // ~ Helper methods
270

271 }
272
Popular Tags