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.*; 20 import com.blandware.atleap.service.core.MenuManager; 21 import com.blandware.atleap.service.core.RoleManager; 22 import com.blandware.atleap.webapp.action.core.BaseAction; 23 import com.blandware.atleap.webapp.form.MenuItemForm; 24 import com.blandware.atleap.webapp.util.core.CacheUtil; 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.*; 29 import org.springframework.orm.ObjectOptimisticLockingFailureException; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 64 public final class UpdateMenuItemAction extends BaseAction { 65 74 public ActionForward execute(ActionMapping mapping, ActionForm form, 75 HttpServletRequest request, HttpServletResponse response) throws Exception { 76 77 78 MenuItemForm menuItemForm = (MenuItemForm) form; 79 Long 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 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 if ( !isCancelled(request) ) { 102 103 MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN); 104 MenuItem menuItem = menuManager.retrieveMenuItem(menuItemId); 105 106 if ( menuItem == null ) { 107 ActionMessages errors = new ActionMessages(); 109 errors.add("menuItemNotFound", new ActionMessage("core.menuItem.errors.notFound")); 110 saveErrors(request, errors); 111 return mapping.findForward("listMenuItems"); 112 } 113 114 if ( menuItem != null && (menuItem.isRedefinition() || !menuItem.isDynamic() || !menuItem.getOwner().getId().equals(ownerId)) ) { 115 if ( log.isWarnEnabled() ) { 117 log.warn("Tried to update item, which is hardcoded or defined on another layer. It is prohibited. Returning to list."); 118 } 119 return mapping.findForward("listMenuItems"); 120 } 121 122 List linkedPages = new ArrayList (); 124 List linkedResources = new ArrayList (); 125 List roles = null; 126 boolean titleIsSet = WebappUtil.hasCorrectValues(menuItemForm.getTitleMap()); 127 String image = menuItemForm.getImage(); 129 BaseObject linkedObject = WebappUtil.lookupObject(image, request.getSession().getServletContext(), request.getContextPath(), request); 130 if ( linkedObject != null && linkedObject instanceof ContentImage ) { 131 linkedResources.add(linkedObject); 132 } 133 134 String altImage = menuItemForm.getAltImage(); 135 if ( !GenericValidator.isBlankOrNull(altImage) && !altImage.equals(image) ) { 136 linkedObject = WebappUtil.lookupObject(menuItemForm.getAltImage(), request.getSession().getServletContext(), request.getContextPath(), request); 137 if ( linkedObject != null && linkedObject instanceof ContentImage ) { 138 linkedResources.add(linkedObject); 139 } 140 } 141 142 linkedObject = WebappUtil.lookupObject(menuItemForm.getLocation(), request.getSession().getServletContext(), request.getContextPath(), request); 143 if ( linkedObject != null ) { 144 menuItem.setExternalLocation(Boolean.FALSE); 146 if ( linkedObject instanceof ContentResource ) { 147 ContentResource resource = (ContentResource) linkedObject; 148 roles = new ArrayList (resource.getRoles()); 149 linkedResources.add(resource); 150 } else if ( linkedObject instanceof Page ) { 151 Page page = (Page) linkedObject; 152 linkedPages.add(page); 153 154 if ( page instanceof ContentPage ) { 156 ContentPage cp = (ContentPage) page; 157 roles = new ArrayList (cp.getRoles()); 158 } else if ( page instanceof ActionPage ) { 159 ActionPage ap = (ActionPage) page; 160 String [] roleNames = WebappUtil.getAPRoleNames(ap.getUri(), request); 161 roles = new ArrayList (); 162 RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN); 163 for ( int i = 0; i < roleNames.length; i++ ) { 164 String roleName = roleNames[i]; 165 Role role = roleManager.retrieveRole(roleName); 166 if ( role != null ) { 167 roles.add(role); 168 } else { 169 if ( log.isWarnEnabled() ) { 171 log.warn("Role with name '" + roleName + "', specified for Action with URI '" + ap.getUri() + "' could not be found. Skipped."); 172 } 173 } 174 } 175 } 176 } 177 } else { 178 menuItem.setExternalLocation(Boolean.TRUE); 180 } 181 182 if ( !titleIsSet ) { 183 ActionMessages errors = new ActionMessages(); 185 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.menuItem.form.title"))); 186 saveErrors(request, errors, false); 187 saveToken(request); 188 return mapping.getInputForward(); 189 } 190 191 Long parentItemId = null; 192 if ( menuItem.getParentItem() != null ) { 193 parentItemId = menuItem.getParentItem().getId(); 194 } 195 196 try { 197 198 menuItem.setLinkedResources(linkedResources); 199 menuItem.setLinkedPages(linkedPages); 200 menuItem.setRoles(roles); 201 202 WebappUtil.copyProperties(menuItem, menuItemForm, request); 203 menuItem.setTitle(menuItemForm.getTitleMap()); 204 menuItem.setToolTip(menuItemForm.getToolTipMap()); 205 206 menuItem.setAnchor(null); 208 menuItem.setAction(null); 209 menuItem.setForward(null); 210 menuItem.setOnmouseover(null); 211 menuItem.setOnmouseout(null); 212 menuItem.setOnclick(null); 213 214 if ( GenericValidator.isBlankOrNull(menuItemForm.getStyle()) ) { 216 menuItem.setStyle(null); 217 } 218 219 if ( GenericValidator.isBlankOrNull(menuItemForm.getStyleClass()) ) { 220 menuItem.setStyleClass(null); 221 } 222 223 if ( GenericValidator.isBlankOrNull(menuItemForm.getAlign()) ) { 224 menuItem.setAlign(null); 225 } 226 227 if ( GenericValidator.isBlankOrNull(menuItemForm.getImage()) ) { 228 menuItem.setImage(null); 229 } 230 231 if ( GenericValidator.isBlankOrNull(menuItemForm.getAltImage()) ) { 232 menuItem.setAltImage(null); 233 } 234 235 menuManager.updateMenuItem(menuItem, parentItemId, ownerId); 236 } catch ( ObjectOptimisticLockingFailureException e ) { 237 ActionMessages errors = new ActionMessages(); 239 errors.add("updateFailed", new ActionMessage("core.menuItem.errors.updateFailed")); 240 saveErrors(request, errors); 241 return mapping.findForward("callUpdateMenuItem"); 242 } 243 244 } 245 246 CacheUtil cacheUtil = CacheUtil.getInstance(request); 248 cacheUtil.flushMenuCache(); 249 cacheUtil.flushContentPageCache(); 250 251 if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) { 252 String redirectUrl = (String ) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY); 253 return new ActionForward(redirectUrl, true); 254 } 255 256 return mapping.findForward("listMenuItems"); 257 } 258 } | Popular Tags |