KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > menuItem > DeleteMenuItemAction


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.webapp.action.core.menuItem;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.MenuItem;
20 import com.blandware.atleap.service.core.MenuManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.MenuItemForm;
23 import com.blandware.atleap.webapp.util.core.CacheUtil;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import org.apache.commons.validator.GenericValidator;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 /**
36  * <p>Deletes menu item
37  * </p>
38  * <p><a HREF="DeleteMenuItemAction.java.htm"><i>View Source</i></a></p>
39  * <p/>
40  *
41  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
42  * @version $Revision: 1.22 $ $Date: 2006/03/10 17:10:28 $
43  * @struts.action path="/core/menuItem/delete"
44  * name="menuItemForm"
45  * input="inputForward"
46  * scope="request"
47  * validate="false"
48  * roles="core-menuItem-delete"
49  * @struts.action-forward name="listMenuItems"
50  * path="/core/menuItem/list.do"
51  * redirect="false"
52  * @struts.action-forward name="inputForward"
53  * path=".core.menuItem.list"
54  * @struts.action-forward name="unsatisfiable"
55  * path="/core/menuItem/list.do"
56  */

57 public final class DeleteMenuItemAction extends BaseAction {
58     /**
59      * @param mapping The ActionMapping used to select this instance
60      * @param form The optional ActionForm bean for this request (if any)
61      * @param request The HTTP request we are proceeding
62      * @param response The HTTP response we are creating
63      * @return an ActionForward instance describing where and how
64      * control should be forwarded, or null if response
65      * has already been completed
66      */

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         MenuItemForm menuItemForm = (MenuItemForm) form;
71         Long JavaDoc ownerId = null;
72         if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
73             ownerId = Long.valueOf(menuItemForm.getOwnerId());
74         } else {
75             if ( log.isWarnEnabled() ) {
76                 log.warn("Missing owner ID. Returning to index...");
77             }
78             return mapping.findForward("admin");
79         }
80
81         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
82
83         Long JavaDoc menuItemId = null;
84         if ( !GenericValidator.isBlankOrNull(menuItemForm.getId()) ) {
85             menuItemId = Long.valueOf(menuItemForm.getId());
86         } else {
87             if ( log.isWarnEnabled() ) {
88                 log.warn("Missing menu item ID. Returning to list");
89             }
90             return mapping.findForward("listMenuItems");
91         }
92
93         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
94
95         // check competence of deletion
96
MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId);
97
98         // if menu item is null simply ignore it. nothing will be deleted
99
if ( menuItem == null ) {
100             // menu item not found. it might be deleted by someone else
101
ActionMessages errors = new ActionMessages();
102             errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound"));
103             saveErrors(request, errors);
104             return mapping.findForward("listMenuItems");
105         }
106
107         if ( menuItem.isRedefinition() || !menuItem.isDynamic() || !menuItem.getOwner().getId().equals(ownerId) ) {
108             // cannot delete this item
109
if ( log.isWarnEnabled() ) {
110                 log.warn("Tried to delete item, which is hardcoded or defined on another layer. It is prohibited. Returning to list.");
111             }
112             ActionMessages errors = new ActionMessages();
113             errors.add("menuItemCannotDelete", new ActionMessage("core.menuItem.errors.cannotDelete"));
114             saveErrors(request, errors);
115             return mapping.findForward("listMenuItems");
116         }
117         menuManager.deleteMenuItem(menuItemId);
118
119         // flush cache
120
CacheUtil cacheUtil = CacheUtil.getInstance(request);
121         cacheUtil.flushMenuCache();
122         cacheUtil.flushContentPageCache();
123
124         return mapping.findForward("listMenuItems");
125     }
126
127
128 }
Popular Tags