1 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 ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 54 public final class MoveMenuItemAction extends BaseAction { 55 64 public ActionForward execute(ActionMapping mapping, ActionForm form, 65 HttpServletRequest request, HttpServletResponse response) throws Exception { 66 67 MenuItemForm menuItemForm = (MenuItemForm) form; 68 Long 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 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 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 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 MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId); 114 115 if ( menuItem == null ) { 116 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 parentItemId = null; 124 if ( menuItem.getParentItem() != null ) { 125 parentItemId = menuItem.getParentItem().getId(); 126 } 127 128 MenuItem redefinitionItem = null; 130 if ( menuItem.getOwner() != null && menuItem.getOwner().getId().equals(ownerId) ) { 131 redefinitionItem = menuItem; 133 } else { 134 redefinitionItem = menuManager.findRedefinitionItem(menuItem.getId(), ownerId); 135 } 136 137 MenuItem existingItem = menuManager.findMenuItemByPositionAndParentAndOwner(position, parentItemId, ownerId); 139 if ( existingItem != null ) { 140 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 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 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 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 CacheUtil cacheUtil = CacheUtil.getInstance(request); 188 cacheUtil.flushMenuCache(); 189 cacheUtil.flushContentPageCache(); 190 191 return mapping.findForward("listMenuItems"); 192 } 193 194 195 } | Popular Tags |