KickJava   Java API By Example, From Geeks To Geeks.

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


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.LocaleUtil;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import com.blandware.atleap.webapp.util.core.WebappUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * <p>Prepares form bean to update menu item's data
40  * </p>
41  * <p><a HREF="CallUpdateMenuItemAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.25 $ $Date: 2006/03/10 17:10:27 $
46  * @struts.action path="/core/menuItem/callUpdate"
47  * name="menuItemForm"
48  * scope="request"
49  * validate="false"
50  * roles="core-menuItem-update, core-menuItem-view"
51  * @struts.action-forward name="updateMenuItem"
52  * path=".core.menuItem.update"
53  * @struts.action-forward name="listMenuItems"
54  * path="/core/menuItem/list.do"
55  * redirect="false"
56  */

57 public final class CallUpdateMenuItemAction 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
72         Long JavaDoc ownerId = null;
73         if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
74             ownerId = Long.valueOf(menuItemForm.getOwnerId());
75         } else if ( request.getSession().getAttribute(WebappConstants.OWNER_ID_KEY) != null ) {
76             ownerId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.OWNER_ID_KEY);
77         } else {
78             if ( log.isWarnEnabled() ) {
79                 log.warn("Missing owner ID. Returning to index...");
80             }
81             return mapping.findForward("admin");
82         }
83
84         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
85
86         // remove attribute under which locale suffix is saved in session
87
request.getSession().removeAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY);
88
89         Long JavaDoc menuItemId = null;
90         if ( !GenericValidator.isBlankOrNull(menuItemForm.getId()) ) {
91             menuItemId = Long.valueOf(menuItemForm.getId());
92         } else {
93             if ( log.isWarnEnabled() ) {
94                 log.warn("Missing menu item ID. Returning to list...");
95             }
96             return mapping.findForward("listMenuItems");
97         }
98
99         if ( isCancelled(request) ) {
100             // if there's redirect URL in session, return to that URL
101
String JavaDoc url = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
102             if (!GenericValidator.isBlankOrNull(url)) {
103                 request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
104                 return new ActionForward(url, true);
105             }
106             return mapping.findForward("listMenuItems");
107         }
108
109         if ( !request.isUserInRole("core-menuItem-update") ) {
110             response.sendError(HttpServletResponse.SC_FORBIDDEN);
111             return null;
112         }
113
114         String JavaDoc redirectUrl = request.getParameter("redirectUrl");
115         if ( GenericValidator.isBlankOrNull(redirectUrl) ) {
116             redirectUrl = (String JavaDoc) request.getAttribute(WebappConstants.REDIRECT_URL_KEY);
117         }
118         if ( GenericValidator.isBlankOrNull(redirectUrl) ) {
119             redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
120         }
121         if ( !GenericValidator.isBlankOrNull(redirectUrl) ) {
122             request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
123         } else {
124             request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
125         }
126
127         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
128         MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId);
129         if ( menuItem == null ) {
130             // menuItem not found. it might be deleted by someone else
131
ActionMessages errors = new ActionMessages();
132             errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound"));
133             saveErrors(request, errors);
134             return mapping.findForward("listMenuItems");
135         }
136
137         if ( menuItem.isRedefinition() || !menuItem.isDynamic() || !menuItem.getOwner().getId().equals(ownerId) ) {
138             // cannot update this item
139
if ( log.isWarnEnabled() ) {
140                 log.warn("Tried to update item, which is hardcoded or defined on another layer. It is prohibited. Returning to list.");
141             }
142             return mapping.findForward("listMenuItems");
143         }
144
145         WebappUtil.copyProperties(menuItemForm, menuItem, request);
146         menuItemForm.setTitleMap(menuItem.getTitle());
147         menuItemForm.setToolTipMap(menuItem.getToolTip());
148         menuItemForm.setOwnerId(ownerId.toString());
149
150         List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
151
152         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
153
154         // save owner info
155
request.setAttribute(WebappConstants.OWNER_INFO_KEY, WebappUtil.getLocalizableInfo(ownerId, request));
156
157         // save path
158
MenuItem parentItem = menuItem.getParentItem();
159         if ( parentItem != null ) {
160             // parent item is not null, save list of parents
161
LinkedList JavaDoc parents = new LinkedList JavaDoc();
162             while ( parentItem != null ) {
163                 parents.addFirst(parentItem);
164                 parentItem = parentItem.getParentItem();
165             }
166             request.setAttribute(WebappConstants.MENU_ITEM_PARENTS_LIST_KEY, parents);
167         }
168
169         // save transaction token in request
170
saveToken(request);
171
172         return mapping.findForward("updateMenuItem");
173     }
174
175 }
Popular Tags