KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.springframework.orm.ObjectOptimisticLockingFailureException;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.*;
36
37 /**
38  * <p>Switches menu item visibility
39  * </p>
40  * <p><a HREF="SwitchMenuItemVisibilityAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
45  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
46  * @version $Revision: 1.20 $ $Date: 2006/03/10 17:10:28 $
47  * @struts.action path="/core/menuItem/switchVisibility"
48  * name="menuItemForm"
49  * validate="false"
50  * roles="core-menuItem-switchVisibility"
51  * @struts.action-forward name="listMenuItems"
52  * path="/core/menuItem/list.do"
53  * redirect="false"
54  * @struts.action-forward name="unsatisfiable"
55  * path="/core/menuItem/list.do"
56  */

57 public final class SwitchMenuItemVisibilityAction 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         List selectedIds = (List) request.getAttribute(WebappConstants.MENU_ITEM_SELECTED_IDS_KEY);
73         Map positions = (Map) request.getAttribute(WebappConstants.MENU_ITEM_POSITIONS_KEY);
74         String JavaDoc ownerIdStr = (String JavaDoc) request.getAttribute(WebappConstants.MENU_ITEM_OWNER_ID_FOR_SELECTED_KEY);
75         String JavaDoc modeStr = (String JavaDoc) request.getAttribute(WebappConstants.MENU_ITEM_VISIBILITY_MODE_FOR_SELECTED_KEY);
76         request.removeAttribute(WebappConstants.MENU_ITEM_SELECTED_IDS_KEY);
77         request.removeAttribute(WebappConstants.MENU_ITEM_POSITIONS_KEY);
78         request.removeAttribute(WebappConstants.MENU_ITEM_OWNER_ID_FOR_SELECTED_KEY);
79         request.removeAttribute(WebappConstants.MENU_ITEM_VISIBILITY_MODE_FOR_SELECTED_KEY);
80         boolean multipleOperation = selectedIds != null;
81
82         Long JavaDoc ownerId = null;
83         if (multipleOperation) {
84             if ( !GenericValidator.isBlankOrNull(ownerIdStr) ) {
85                 ownerId = Long.valueOf(ownerIdStr);
86             }
87         } else {
88             if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
89                 ownerId = Long.valueOf(menuItemForm.getOwnerId());
90             }
91         }
92         if (ownerId == null) {
93             if ( log.isWarnEnabled() ) {
94                 log.warn("Missing owner ID. Returning to index...");
95             }
96             return mapping.findForward("admin");
97         }
98
99         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
100
101         byte visibility = MenuItem.VISIBILITY_HERITABLE;
102         String JavaDoc givenMode = null;
103         if (multipleOperation) {
104             givenMode = modeStr;
105         } else {
106             givenMode = request.getParameter("mode");
107         }
108         if ( givenMode != null ) {
109             if ( "visible".equalsIgnoreCase(givenMode) ) {
110                 visibility = MenuItem.VISIBILITY_VISIBLE;
111             } else if ( "invisible".equalsIgnoreCase(givenMode) ) {
112                 visibility = MenuItem.VISIBILITY_INVISIBLE;
113             }
114         }
115
116         if (!multipleOperation) {
117             if (GenericValidator.isBlankOrNull(menuItemForm.getId())) {
118                 if ( log.isWarnEnabled() ) {
119                     log.warn("Missing menu item ID. Returning to list");
120                 }
121                 return mapping.findForward("listMenuItems");
122             }
123             selectedIds = new ArrayList();
124             selectedIds.add(menuItemForm.getId());
125             String JavaDoc alonePosition = null;
126             if ( !GenericValidator.isBlankOrNull(request.getParameter("position")) ) {
127                 alonePosition = request.getParameter("position");
128             }
129             positions = new HashMap();
130             positions.put(menuItemForm.getId(), alonePosition);
131         }
132
133         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
134
135         for (Iterator i = selectedIds.iterator(); i.hasNext(); ) {
136             String JavaDoc menuItemIdStr = (String JavaDoc) i.next();
137             if (GenericValidator.isBlankOrNull(menuItemIdStr)) {
138                 continue;
139             }
140
141             Long JavaDoc menuItemId = null;
142             menuItemId = Long.valueOf(menuItemIdStr);
143
144             Integer JavaDoc position = null;
145             position = Integer.valueOf((String JavaDoc) positions.get(menuItemIdStr));
146
147             // lookup for item with specified ID
148
MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId);
149
150             if ( menuItem == null ) {
151                 // menuItem not found. it might be deleted by someone else
152
ActionMessages errors = new ActionMessages();
153                 errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound"));
154                 saveErrors(request, errors);
155                 return mapping.findForward("listMenuItems");
156             }
157
158             Long JavaDoc parentItemId = null;
159             if ( menuItem.getParentItem() != null ) {
160                 parentItemId = menuItem.getParentItem().getId();
161             }
162
163             // lookup for redefinition on current layer
164
MenuItem redefinitionItem = null;
165             if ( menuItem.getOwner() != null && menuItem.getOwner().getId().equals(ownerId) ) {
166                 // if this item was created on this layer, there is no need to create its redefinition
167
redefinitionItem = menuItem;
168             } else {
169                 redefinitionItem = menuManager.findRedefinitionItem(menuItem.getId(), ownerId);
170             }
171
172             if ( redefinitionItem == null ) {
173                 // redefinition does not exist, we must create it
174
redefinitionItem = new MenuItem();
175                 redefinitionItem.setIdentifier(null);
176                 redefinitionItem.setPosition(position);
177                 redefinitionItem.setVisibility(new Byte JavaDoc(visibility));
178                 redefinitionItem.setOrigItem(menuItem);
179                 menuManager.createMenuItem(redefinitionItem, parentItemId, ownerId);
180             } else {
181                 // redefinition already exists, change its visibility
182
redefinitionItem.setVisibility(new Byte JavaDoc(visibility));
183                 try {
184                     menuManager.updateMenuItem(redefinitionItem, parentItemId, ownerId);
185                 } catch ( ObjectOptimisticLockingFailureException e ) {
186                     ActionMessages errors = new ActionMessages();
187                     errors.add("updateFailed", new ActionMessage("core.menuItem.errors.updateFailed"));
188                     saveErrors(request, errors);
189                     return mapping.findForward("listMenuItems");
190                 }
191             }
192
193         }
194
195         // flush cache
196
CacheUtil cacheUtil = CacheUtil.getInstance(request);
197         cacheUtil.flushMenuCache();
198         cacheUtil.flushContentPageCache();
199
200         return mapping.findForward("listMenuItems");
201     }
202
203
204 }
Popular Tags