KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > core > impl > MenuManagerImpl


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.service.core.impl;
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.persistence.core.LocalizableDAO;
23 import com.blandware.atleap.persistence.core.MenuDAO;
24 import com.blandware.atleap.service.core.MenuManager;
25 import com.blandware.atleap.service.exception.BeanNotFoundException;
26 import com.blandware.atleap.service.exception.OwnerNotFoundException;
27 import com.blandware.atleap.service.exception.ParentItemNotFoundException;
28
29 import java.util.List JavaDoc;
30
31 /**
32  * <p>Implementation of MenuManager</p>
33  * <p><a HREF="MenuManagerImpl.java.htm"><i>View Source</i></a>
34  * </p>
35  *
36  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
37  * @version $Revision: 1.16 $ $Date: 2005/08/02 14:53:41 $
38  */

39 public class MenuManagerImpl extends BaseManagerImpl implements MenuManager {
40
41     /**
42      * Menu DAO
43      */

44     protected MenuDAO menuDAO;
45
46     /**
47      * Localizable DAO
48      */

49     protected LocalizableDAO localizableDAO;
50
51     /**
52      * Creates new instance of MenuManagerImpl
53      */

54     public MenuManagerImpl() {
55     }
56
57     /**
58      * Sets DAO for operating with menu items
59      *
60      * @param menuDAO the DAO to set
61      */

62     public void setMenuDAO(MenuDAO menuDAO) {
63         this.menuDAO = menuDAO;
64     }
65
66     /**
67      * Sets DAO for operating with menu items
68      *
69      * @param localizableDAO the DAO to set
70      */

71     public void setLocalizableDAO(LocalizableDAO localizableDAO) {
72         this.localizableDAO = localizableDAO;
73     }
74
75     /**
76      * @see com.blandware.atleap.service.core.MenuManager#createMenuItem(com.blandware.atleap.model.core.MenuItem, Long, Long)
77      */

78     public Long JavaDoc createMenuItem(MenuItem menuItem, Long JavaDoc parentItemId, Long JavaDoc ownerId) throws BeanNotFoundException {
79
80         if ( log.isDebugEnabled() ) {
81             log.debug("Creating new menu item...");
82         }
83
84         // search for parent's and owner's existence
85
MenuItem parentItem = null;
86         if ( parentItemId != null ) {
87             parentItem = menuDAO.retrieveMenuItem(parentItemId);
88             if ( parentItem == null ) {
89                 String JavaDoc errorMessage = "MenuItem with ID=" + parentItemId + " does not exist";
90                 if ( log.isErrorEnabled() ) {
91                     log.error(errorMessage);
92                 }
93                 throw new ParentItemNotFoundException(errorMessage);
94             }
95         }
96
97         Localizable owner = null;
98         if ( ownerId != null ) {
99             owner = localizableDAO.retrieveLocalizable(ownerId);
100             if ( owner == null ) {
101                 String JavaDoc errorMessage = "Localizable with ID=" + ownerId + " does not exist";
102                 if ( log.isErrorEnabled() ) {
103                     log.error(errorMessage);
104                 }
105                 throw new OwnerNotFoundException(errorMessage);
106             }
107         }
108
109         // create item
110
Long JavaDoc menuItemId = menuDAO.createMenuItem(menuItem, parentItem, owner);
111
112         // if item has no identifier and it is not the redifinition for another one, set identifier and update item
113
if ( (menuItem.getIdentifier() == null || menuItem.getIdentifier().trim().length() == 0) && !menuItem.isRedefinition() ) {
114             menuItem.setIdentifier("MenuItem-" + menuItemId);
115             menuDAO.updateMenuItem(menuItem, parentItem, owner);
116         }
117
118         if ( log.isDebugEnabled() ) {
119             log.debug("New menu item has been created succesfully. Its ID is " + menuItemId);
120         }
121
122         return menuItemId;
123     }
124
125     /**
126      * @see com.blandware.atleap.service.core.MenuManager#retrieveMenuItem(Long)
127      */

128     public MenuItem retrieveMenuItem(Long JavaDoc menuItemId) {
129         return menuDAO.retrieveMenuItem(menuItemId);
130     }
131
132     /**
133      * @see com.blandware.atleap.service.core.MenuManager#updateMenuItem(com.blandware.atleap.model.core.MenuItem, Long, Long)
134      */

135     public void updateMenuItem(MenuItem menuItem, Long JavaDoc parentItemId, Long JavaDoc ownerId) throws BeanNotFoundException {
136
137         // remove item from cache in order to prevent Hibernate from assigning new version number
138
menuDAO.removeFromCache(menuItem);
139
140         if ( log.isDebugEnabled() ) {
141             log.debug("Updating menu item with ID=" + menuItem.getId() + "...");
142         }
143
144         // search for parent's and owner's existence
145
MenuItem parentItem = null;
146         if ( parentItemId != null ) {
147             parentItem = menuDAO.retrieveMenuItem(parentItemId);
148             if ( parentItem == null ) {
149                 String JavaDoc errorMessage = "MenuItem with ID=" + parentItemId + " does not exist";
150                 if ( log.isErrorEnabled() ) {
151                     log.error(errorMessage);
152                 }
153                 throw new ParentItemNotFoundException(errorMessage);
154             }
155         }
156
157         Localizable owner = null;
158         if ( ownerId != null ) {
159             owner = localizableDAO.retrieveLocalizable(ownerId);
160             if ( owner == null ) {
161                 String JavaDoc errorMessage = "Localizable with ID=" + ownerId + " does not exist";
162                 if ( log.isErrorEnabled() ) {
163                     log.error(errorMessage);
164                 }
165                 throw new OwnerNotFoundException(errorMessage);
166             }
167         }
168
169         // update menu item
170
menuDAO.updateMenuItem(menuItem, parentItem, owner);
171
172         if ( log.isDebugEnabled() ) {
173             log.debug("Menu item has been updated successfully.");
174         }
175
176     }
177
178     /**
179      * @see com.blandware.atleap.service.core.MenuManager#deleteMenuItem(Long)
180      */

181     public void deleteMenuItem(Long JavaDoc menuItemId) throws BeanNotFoundException {
182         MenuItem menuItem = menuDAO.retrieveMenuItem(menuItemId);
183         if ( menuItem == null ) {
184             String JavaDoc errorMessage = "No content resource with ID=" + menuItem + "could be found";
185             throw new BeanNotFoundException(errorMessage);
186         }
187         menuDAO.deleteMenuItem(menuItem);
188     }
189
190     /**
191      * @see com.blandware.atleap.service.core.MenuManager#listMenuItems(com.blandware.atleap.common.util.QueryInfo)
192      */

193     public PartialCollection listMenuItems(QueryInfo queryInfo) {
194         return menuDAO.listMenuItems(queryInfo);
195     }
196
197     /**
198      * @see com.blandware.atleap.service.core.MenuManager#findMenuItemByIdentifierAndParentAndOwner(String, Long, Long)
199      */

200     public MenuItem findMenuItemByIdentifierAndParentAndOwner(String JavaDoc identifier, Long JavaDoc parentItemId, Long JavaDoc ownerId) {
201         return menuDAO.findMenuItemByIdentifierAndParentAndOwner(identifier, parentItemId, ownerId);
202     }
203
204
205     /**
206      * @see com.blandware.atleap.service.core.MenuManager#findMenuItemByPositionAndParentAndOwner(Integer, Long, Long)
207      */

208     public MenuItem findMenuItemByPositionAndParentAndOwner(Integer JavaDoc position, Long JavaDoc parentItemId, Long JavaDoc ownerId) {
209         return menuDAO.findMenuItemByPositionAndParentAndOwner(position, parentItemId, ownerId);
210     }
211
212     /**
213      * @see com.blandware.atleap.service.core.MenuManager#findMenuItemsByParentAndOwner(Long, Long)
214      */

215     public List JavaDoc findMenuItemsByParentAndOwner(Long JavaDoc parentItemId, Long JavaDoc ownerId) {
216         return menuDAO.findMenuItemsByParentAndOwner(parentItemId, ownerId);
217     }
218
219     /**
220      * @see com.blandware.atleap.service.core.MenuManager#findRedefinitionItem(Long, Long)
221      */

222     public MenuItem findRedefinitionItem(Long JavaDoc origItemId, Long JavaDoc ownerId) {
223         return menuDAO.findRedefinitionItem(origItemId, ownerId);
224     }
225
226 }
227
Popular Tags