KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentField > DeleteContentFieldAction


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.contentField;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.*;
20 import com.blandware.atleap.search.SearchManager;
21 import com.blandware.atleap.service.core.ContentFieldManager;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.ContentFieldForm;
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.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * <p>Deletes content field
41  * </p>
42  * <p><a HREF="DeleteContentFieldAction.java.htm"><i>View Source</i></a></p>
43  * <p/>
44  *
45  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
46  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
47  * @version $Revision: 1.29 $ $Date: 2006/03/17 15:39:52 $
48  * @struts.action path="/core/contentField/delete"
49  * name="contentFieldForm"
50  * scope="request"
51  * validate="false"
52  * roles="core-contentField-delete, core-contentField-deleteOverriden, core-contentField-deleteIndexed"
53  * @struts.action-forward name="listContentFields"
54  * path="/core/contentField/list.do"
55  * redirect="false"
56  * @struts.action-forward name="unsatisfiable"
57  * path="/core/contentField/list.do"
58  */

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

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
73
74         Long JavaDoc ownerId = null;
75         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
76             ownerId = Long.valueOf(contentFieldForm.getOwnerId());
77         } else {
78             if ( log.isWarnEnabled() ) {
79                 log.warn("Missing owner ID. Returning to index...");
80             }
81             return mapping.findForward("admin");
82         }
83
84         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
85
86         Long JavaDoc contentFieldId = null;
87         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) {
88             contentFieldId = Long.valueOf(contentFieldForm.getId());
89         } else {
90             if ( log.isWarnEnabled() ) {
91                 log.warn("Missing content field ID. Returning to list...");
92             }
93             return mapping.findForward("listContentFields");
94         }
95
96         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
97         ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
98
99         if ( contentField == null ) {
100             // content field not found. it might be deleted by someone else
101
ActionMessages errors = new ActionMessages();
102             errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound"));
103             saveErrors(request, errors);
104             return mapping.findForward("listContentFields");
105         }
106
107         Localizable owner = contentField.getOwner();
108         if ( owner instanceof Layout && !request.isUserInRole("core-contentField-delete") ) {
109
110             String JavaDoc fieldIdentifier = contentField.getIdentifier();
111             int k = fieldIdentifier.indexOf('[');
112             String JavaDoc index = null;
113             if ( k != -1 ) {
114                 index = fieldIdentifier.substring(k + 1, fieldIdentifier.indexOf(']', k));
115                 fieldIdentifier = fieldIdentifier.substring(0, k);
116                 contentFieldForm.setIdentifier(fieldIdentifier);
117                 contentFieldForm.setIndex(index);
118             }
119
120             Set JavaDoc identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
121             boolean overridable = identifiers.contains(fieldIdentifier);
122
123             if ( !overridable && (index == null || index.equalsIgnoreCase("0")) ) {
124                 ActionMessages errors = new ActionMessages();
125                 errors.add("contentFieldCannotDelete", new ActionMessage("core.contentField.errors.unindexed.cannotDelete"));
126                 saveErrors(request, errors);
127                 return mapping.findForward("listContentFields");
128             }
129         }
130
131         // Forbid to delete field from action page if this field is not a
132
// "redifinition" and user has no rights to delete fields
133
if (owner instanceof ActionPage && !request.isUserInRole("core-contentField-delete")) {
134             Set JavaDoc identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
135             String JavaDoc identifier = contentField.getIdentifierWithoutIndex();
136             boolean canDelete = identifiers != null && !identifiers.isEmpty() && identifiers.contains(identifier);
137             if (!canDelete) {
138                 ActionMessages errors = new ActionMessages();
139                 errors.add("contentFieldCannotDelete", new ActionMessage("core.contentField.errors.actionPageField.cannotDelete"));
140                 saveErrors(request, errors);
141                 return mapping.findForward("listContentFields");
142             }
143         }
144
145         contentFieldManager.deleteContentField(contentFieldId);
146
147         //flush cache
148
CacheUtil cacheUtil = CacheUtil.getInstance(request);
149         cacheUtil.flushFieldIndices();
150
151         // put into search index
152
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
153         if ( owner instanceof Layout ) {
154             Layout layout = (Layout) owner;
155
156             //add into search index for every content page of the layout.
157
List JavaDoc contentPages = layout.getContentPages();
158             for ( int i = 0; i < contentPages.size(); i++ ) {
159                 ContentPage contentPage = (ContentPage) contentPages.get(i);
160                 cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri());
161                 searchManager.reIndexPage(contentPage, request);
162             }
163             cacheUtil.flushLayoutFieldValueCache(layout.getDefinition());
164
165         } else if ( owner instanceof Page ) {
166             Page page = (Page) owner;
167             searchManager.reIndexPage(page, request);
168             cacheUtil.flushPageFieldValueCache(page.getUri());
169             if ( owner instanceof ContentPage ) {
170                 cacheUtil.updateContentPageLastModifiedInCache(page.getUri());
171             }
172         }
173
174         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
175         return mapping.findForward("listContentFields");
176     }
177 }
Popular Tags