1 16 package com.blandware.atleap.webapp.action.core.layout; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.persistence.exception.DeleteException; 20 import com.blandware.atleap.service.core.LayoutManager; 21 import com.blandware.atleap.service.core.PageManager; 22 import com.blandware.atleap.service.exception.BeanNotFoundException; 23 import com.blandware.atleap.webapp.action.core.BaseAction; 24 import com.blandware.atleap.webapp.form.LayoutForm; 25 import com.blandware.atleap.webapp.util.core.CacheUtil; 26 import com.blandware.atleap.webapp.util.core.DefinitionUtil; 27 import com.blandware.atleap.model.core.Layout; 28 import com.blandware.atleap.model.core.ActionPage; 29 import org.apache.commons.validator.GenericValidator; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.action.ActionForward; 32 import org.apache.struts.action.ActionMapping; 33 import org.apache.struts.action.ActionMessage; 34 import org.apache.struts.action.ActionMessages; 35 import org.apache.struts.config.ModuleConfig; 36 import org.apache.struts.config.ActionConfig; 37 import org.apache.struts.config.ForwardConfig; 38 import org.apache.struts.util.ModuleUtils; 39 import org.apache.struts.tiles.DefinitionsFactoryException; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import javax.servlet.ServletContext ; 44 import java.util.List ; 45 import java.util.Set ; 46 import java.util.TreeSet ; 47 48 69 public final class DeleteLayoutAction extends BaseAction { 70 79 public ActionForward execute(ActionMapping mapping, ActionForm form, 80 HttpServletRequest request, HttpServletResponse response) throws Exception { 81 82 LayoutForm layoutForm = (LayoutForm) form; 83 Long layoutId = null; 84 if ( !GenericValidator.isBlankOrNull(layoutForm.getId()) ) { 85 layoutId = Long.valueOf(layoutForm.getId()); 86 } else { 87 if ( log.isWarnEnabled() ) { 88 log.warn("Missing layout ID. Returning to list..."); 89 } 90 return mapping.findForward("listLayouts"); 91 } 92 93 LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN); 94 Layout layout = layoutManager.retrieveLayout(layoutId); 95 96 PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN); 97 98 ServletContext servletContext = request.getSession().getServletContext(); 99 DefinitionUtil definitionUtil = DefinitionUtil.getInstance(servletContext); 100 List descendants = null; 101 String layoutDefinition = layout.getDefinition(); 102 try { 103 descendants = definitionUtil.getDescendantDefinitionsNames(layoutDefinition, request, servletContext); 104 } catch (DefinitionsFactoryException e) { 105 if ( log.isErrorEnabled() ) { 106 log.error("Cannot get definitions list", e); 107 } 108 } 109 descendants.add(layoutDefinition); 110 Set allDescendants = new TreeSet (descendants); 111 112 for (int i = 0; i < descendants.size(); i++) { 114 String definition = (String ) descendants.get(i); 115 if (!definition.equals(layoutDefinition)) { 116 Layout l = layoutManager.findLayoutByDefinition(definition); 117 if (l != null) { 118 ActionMessages errors = new ActionMessages(); 119 errors.add("layoutCannotDelete", new ActionMessage("core.layout.errors.cannotDeleteBecauseOfLayout")); 120 saveErrors(request, errors); 121 return mapping.findForward("listLayouts"); 122 } 123 } 124 } 125 126 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, servletContext); 128 String prefix = moduleConfig.getPrefix(); 129 ActionConfig actionConfigs[] = moduleConfig.findActionConfigs(); 130 for (int j = 0; j < actionConfigs.length; j++) { 131 ActionConfig actionConfig = actionConfigs[j]; 132 boolean referencesDescendant = false; 133 ForwardConfig forwardConfigs[] = actionConfig.findForwardConfigs(); 134 for (int k = 0; k < forwardConfigs.length; k++) { 135 ForwardConfig forwardConfig = forwardConfigs[k]; 136 String path = forwardConfig.getPath(); 137 if (allDescendants.contains(path)) { 138 referencesDescendant = true; 139 break; 140 } 141 } 142 if (referencesDescendant) { 143 String uri = actionConfig.getPath().substring(prefix.length()); 144 ActionPage actionPage = pageManager.findActionPageByUri(uri); 145 if (actionPage != null) { 146 ActionMessages errors = new ActionMessages(); 147 errors.add("layoutCannotDelete", new ActionMessage("core.layout.errors.cannotDeleteBecauseOfAP")); 148 saveErrors(request, errors); 149 return mapping.findForward("listLayouts"); 150 } 151 } 152 } 153 154 try { 155 layoutManager.deleteLayout(layoutId); 156 } catch ( BeanNotFoundException e ) { 157 ActionMessages errors = new ActionMessages(); 158 errors.add("layoutNotFound", new ActionMessage("core.layout.errors.notFound")); 159 saveErrors(request, errors); 160 return mapping.findForward("listLayouts"); 161 } catch ( DeleteException e ) { 162 ActionMessages errors = new ActionMessages(); 163 errors.add("layoutCannotDelete", new ActionMessage("core.layout.errors.cannotDeleteBecauseOfCP")); 164 saveErrors(request, errors); 165 return mapping.findForward("listLayouts"); 166 } 167 168 CacheUtil.getInstance(request).flushMenuCache(); 170 171 return mapping.findForward("listLayouts"); 172 } 173 } | Popular Tags |