KickJava   Java API By Example, From Geeks To Geeks.

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


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
36 /**
37  * <p>Changes menu item position on specified layer
38  * </p>
39  * <p><a HREF="MoveMenuItemAction.java.htm"><i>View Source</i></a></p>
40  * <p/>
41  *
42  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
43  * @version $Revision: 1.20 $ $Date: 2006/03/10 17:10:28 $
44  * @struts.action path="/core/menuItem/move"
45  * name="menuItemForm"
46  * validate="false"
47  * roles="core-menuItem-move"
48  * @struts.action-forward name="listMenuItems"
49  * path="/core/menuItem/list.do"
50  * redirect="false"
51  * @struts.action-forward name="unsatisfiable"
52  * path="/core/menuItem/list.do"
53  */

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

64     public ActionForward execute(ActionMapping mapping, ActionForm form,
65                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
66
67         MenuItemForm menuItemForm = (MenuItemForm) form;
68         Long JavaDoc ownerId = null;
69         if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
70             ownerId = Long.valueOf(menuItemForm.getOwnerId());
71         } else {
72             if ( log.isWarnEnabled() ) {
73                 log.warn("Missing owner ID. Returning to index...");
74             }
75             return mapping.findForward("admin");
76         }
77
78         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
79
80         Long JavaDoc menuItemId = null;
81         if ( !GenericValidator.isBlankOrNull(menuItemForm.getId()) ) {
82             menuItemId = Long.valueOf(menuItemForm.getId());
83         } else {
84             if ( log.isWarnEnabled() ) {
85                 log.warn("Missing menu item ID. Returning to list");
86             }
87             return mapping.findForward("listMenuItems");
88         }
89
90         Integer JavaDoc position = null;
91         if ( !GenericValidator.isBlankOrNull(request.getParameter("position")) ) {
92             position = Integer.valueOf(request.getParameter("position"));
93         } else {
94             if ( log.isWarnEnabled() ) {
95                 log.warn("Position is not specified. Returning to list...");
96             }
97             return mapping.findForward("listMenuItems");
98         }
99
100         Integer JavaDoc index = null;
101         if ( !GenericValidator.isBlankOrNull(request.getParameter("position")) ) {
102             index = Integer.valueOf(request.getParameter("index"));
103         } else {
104             if ( log.isWarnEnabled() ) {
105                 log.warn("Index is not specified. Returning to list...");
106             }
107             return mapping.findForward("listMenuItems");
108         }
109
110         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
111
112         // lookup for item with specified ID
113
MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId);
114
115         if ( menuItem == null ) {
116             // menuItem not found. it might be deleted by someone else
117
ActionMessages errors = new ActionMessages();
118             errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound"));
119             saveErrors(request, errors);
120             return mapping.findForward("listMenuItems");
121         }
122
123         Long JavaDoc parentItemId = null;
124         if ( menuItem.getParentItem() != null ) {
125             parentItemId = menuItem.getParentItem().getId();
126         }
127
128         // lookup for redefinition on current layer
129
MenuItem redefinitionItem = null;
130         if ( menuItem.getOwner() != null && menuItem.getOwner().getId().equals(ownerId) ) {
131             // if this item was created on this layer, there is no need to create its redefinition
132
redefinitionItem = menuItem;
133         } else {
134             redefinitionItem = menuManager.findRedefinitionItem(menuItem.getId(), ownerId);
135         }
136
137         // lookup item on position where to move
138
MenuItem existingItem = menuManager.findMenuItemByPositionAndParentAndOwner(position, parentItemId, ownerId);
139         if ( existingItem != null ) {
140             // move existing item temporarily to the end of list
141
existingItem.setPosition(null);
142             try {
143                 menuManager.updateMenuItem(existingItem, parentItemId, ownerId);
144             } catch ( ObjectOptimisticLockingFailureException e ) {
145                 ActionMessages errors = new ActionMessages();
146                 errors.add("updateFailed", new ActionMessage("core.menuItem.errors.updateFailed"));
147                 saveErrors(request, errors);
148                 return mapping.findForward("listMenuItems");
149             }
150         }
151
152         if ( redefinitionItem == null ) {
153             // redefinition does not exist, we must create it
154
redefinitionItem = new MenuItem();
155             redefinitionItem.setIdentifier(null);
156             redefinitionItem.setPosition(position);
157             redefinitionItem.setVisibility(menuItem.getVisibility());
158             redefinitionItem.setOrigItem(menuItem);
159             menuManager.createMenuItem(redefinitionItem, parentItemId, ownerId);
160         } else {
161             // redefinition already exists, move it to position
162
redefinitionItem.setPosition(position);
163             try {
164                 menuManager.updateMenuItem(redefinitionItem, parentItemId, ownerId);
165             } catch ( ObjectOptimisticLockingFailureException e ) {
166                 ActionMessages errors = new ActionMessages();
167                 errors.add("updateFailed", new ActionMessage("core.menuItem.errors.updateFailed"));
168                 saveErrors(request, errors);
169                 return mapping.findForward("listMenuItems");
170             }
171         }
172
173         if ( existingItem != null ) {
174             // move existing item to index
175
existingItem.setPosition(index);
176             try {
177                 menuManager.updateMenuItem(existingItem, parentItemId, ownerId);
178             } catch ( ObjectOptimisticLockingFailureException e ) {
179                 ActionMessages errors = new ActionMessages();
180                 errors.add("updateFailed", new ActionMessage("core.menuItem.errors.updateFailed"));
181                 saveErrors(request, errors);
182                 return mapping.findForward("listMenuItems");
183             }
184         }
185
186         // flush cache
187
CacheUtil cacheUtil = CacheUtil.getInstance(request);
188         cacheUtil.flushMenuCache();
189         cacheUtil.flushContentPageCache();
190
191         return mapping.findForward("listMenuItems");
192     }
193
194
195 }
Popular Tags