KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         String JavaDoc redirectUrl = request.getParameter("redirectUrl");
73         if (!GenericValidator.isBlankOrNull(redirectUrl)) {
74             request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
75         }
76
77         MenuItemForm menuItemForm = (MenuItemForm) form;
78
79         Long JavaDoc ownerId = null;
80         if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
81             ownerId = Long.valueOf(menuItemForm.getOwnerId());
82         } else {
83             if ( log.isWarnEnabled() ) {
84                 log.warn("Missing owner ID. Returning to index...");
85             }
86             return mapping.findForward("admin");
87         }
88
89         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
90
91         Long JavaDoc menuItemId = null;
92         if ( !GenericValidator.isBlankOrNull(menuItemForm.getId()) ) {
93             menuItemId = Long.valueOf(menuItemForm.getId());
94         } else {
95             if ( log.isWarnEnabled() ) {
96                 log.warn("Missing menu item ID. Returning to list...");
97             }
98             return mapping.findForward("listMenuItems");
99         }
100
101         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
102         MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId);
103
104         if ( menuItem == null ) {
105             // menuItem not found. it might be deleted by someone else
106
ActionMessages errors = new ActionMessages();
107             errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound"));
108             saveErrors(request, errors);
109             return mapping.findForward("listMenuItems");
110         }
111
112         Boolean JavaDoc canUpdate = Boolean.TRUE;
113         if ( menuItem.isRedefinition() || !menuItem.isDynamic() || !menuItem.getOwner().getId().equals(ownerId) ) {
114             canUpdate = Boolean.FALSE;
115         }
116
117         request.setAttribute("canUpdate", canUpdate);
118
119         // set uri
120
MenuItem tmp = new MenuItem();
121         WebappUtil.copyProperties(tmp, menuItem, request);
122         tmp.setLocation(new MenuUtil(request).getLocation(menuItem, WebappConstants.URL_TYPE_CONTEXT_RELATIVE));
123
124         request.setAttribute("menuItem", tmp);
125
126         // get list of locales
127
List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
128
129         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
130
131         // save owner info
132
if ( menuItem.getOwner() != null ) {
133             request.setAttribute(WebappConstants.OWNER_INFO_KEY, WebappUtil.getLocalizableInfo(menuItem.getOwner(), request));
134             request.setAttribute(WebappConstants.MENU_ITEM_OWNER_ID_KEY, menuItem.getOwner().getId());
135         }
136
137         // save path
138
MenuItem parentItem = menuItem.getParentItem();
139         if ( parentItem != null ) {
140             // parent item is not null, save list of parents
141
LinkedList JavaDoc parents = new LinkedList JavaDoc();
142             while ( parentItem != null ) {
143                 parents.addFirst(parentItem);
144                 parentItem = parentItem.getParentItem();
145             }
146             request.setAttribute(WebappConstants.MENU_ITEM_PARENTS_LIST_KEY, parents);
147         }
148
149         return mapping.findForward("viewMenuItem");
150     }
151 }
Popular Tags