KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > layout > DeleteLayoutAction


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.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 JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.servlet.ServletContext JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Set JavaDoc;
46 import java.util.TreeSet JavaDoc;
47
48 /**
49  * <p>Deletes layout
50  * </p>
51  * <p><a HREF="DeleteLayoutAction.java.htm"><i>View Source</i></a></p>
52  * <p/>
53  *
54  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
55  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
56  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
57  * @version $Revision: 1.20 $ $Date: 2006/03/16 15:18:04 $
58  * @struts.action path="/core/layout/delete"
59  * name="layoutForm"
60  * scope="request"
61  * validate="false"
62  * roles="core-layout-delete"
63  * @struts.action-forward name="listLayouts"
64  * path="/core/layout/list.do"
65  * redirect="true"
66  * @struts.action-forward name="unsatisfiable"
67  * path="/core/layout/list.do"
68  */

69 public final class DeleteLayoutAction extends BaseAction {
70     /**
71      * @param mapping The ActionMapping used to select this instance
72      * @param form The optional ActionForm bean for this request (if any)
73      * @param request The HTTP request we are proceeding
74      * @param response The HTTP response we are creating
75      * @return an ActionForward instance describing where and how
76      * control should be forwarded, or null if response
77      * has already been completed
78      */

79     public ActionForward execute(ActionMapping mapping, ActionForm form,
80                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
81
82         LayoutForm layoutForm = (LayoutForm) form;
83         Long JavaDoc 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 JavaDoc servletContext = request.getSession().getServletContext();
99         DefinitionUtil definitionUtil = DefinitionUtil.getInstance(servletContext);
100         List JavaDoc descendants = null;
101         String JavaDoc 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 JavaDoc allDescendants = new TreeSet JavaDoc(descendants);
111
112         // Check if there's some layout based on this one
113
for (int i = 0; i < descendants.size(); i++) {
114             String JavaDoc definition = (String JavaDoc) 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         // Check all paths from forward configs for all actions
127
ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, servletContext);
128         String JavaDoc 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 JavaDoc path = forwardConfig.getPath();
137                 if (allDescendants.contains(path)) {
138                     referencesDescendant = true;
139                     break;
140                 }
141             }
142             if (referencesDescendant) {
143                 String JavaDoc 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         //flush menu cache
169
CacheUtil.getInstance(request).flushMenuCache();
170
171         return mapping.findForward("listLayouts");
172     }
173 }
Popular Tags