KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentFieldValue > DeleteContentFieldValueAction


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.contentFieldValue;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentField;
20 import com.blandware.atleap.model.core.ContentFieldValue;
21 import com.blandware.atleap.model.core.ContentPage;
22 import com.blandware.atleap.model.core.Layout;
23 import com.blandware.atleap.model.core.Localizable;
24 import com.blandware.atleap.model.core.Page;
25 import com.blandware.atleap.search.SearchManager;
26 import com.blandware.atleap.service.core.ContentFieldManager;
27 import com.blandware.atleap.webapp.action.core.BaseAction;
28 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm;
29 import com.blandware.atleap.webapp.util.core.CacheUtil;
30 import com.blandware.atleap.webapp.util.core.WebappConstants;
31 import org.apache.commons.validator.GenericValidator;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionMessage;
36 import org.apache.struts.action.ActionMessages;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * <p>Deletes content field value
45  * </p>
46  * <p><a HREF="DeleteContentFieldValueAction.java.htm"><i>View Source</i></a></p>
47  * <p/>
48  *
49  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
50  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
51  * @version $Revision: 1.25 $ $Date: 2006/03/10 17:10:18 $
52  * @struts.action path="/core/contentFieldValue/delete"
53  * name="contentFieldValueForm"
54  * scope="request"
55  * validate="false"
56  * roles="core-contentFieldValue-delete"
57  * @struts.action-forward name="listContentFieldValues"
58  * path="/core/contentFieldValue/list.do"
59  * redirect="false"
60  * @struts.action-forward name="unsatisfiable"
61  * path="/core/contentFieldValue/list.do"
62  */

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

73     public ActionForward execute(ActionMapping mapping, ActionForm form,
74                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
75
76         ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form;
77         Long JavaDoc contentFieldId = null;
78         if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) {
79             contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId());
80         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) {
81             contentFieldId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY);
82         } else {
83             if ( log.isWarnEnabled() ) {
84                 log.warn("Missing content field ID. Returning to index...");
85             }
86             return mapping.findForward("admin");
87         }
88
89         List JavaDoc cfvLocales = null;
90
91         if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) {
92             cfvLocales = (List JavaDoc) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY);
93         } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) {
94             String JavaDoc locale = request.getParameter("locale");
95             cfvLocales = new ArrayList JavaDoc();
96             cfvLocales.add(locale);
97         } else {
98             if ( log.isWarnEnabled() ) {
99                 log.warn("Missing content field value locale. Returning to list...");
100             }
101             return mapping.findForward("listContentFieldValues");
102         }
103
104         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
105         ContentField contentField = null;
106         boolean alreadyDeletedExist = false;
107         for ( int i = 0; i < cfvLocales.size(); i++ ) {
108             String JavaDoc localeIdentifier = (String JavaDoc) cfvLocales.get(i);
109             ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier);
110             if ( contentFieldValue == null ) {
111                 alreadyDeletedExist = true;
112             } else {
113
114                 if ( contentField == null ) {
115                     contentField = contentFieldValue.getContentField();
116                     contentFieldId = contentField.getId();
117                 }
118
119                 contentFieldManager.deleteContentFieldValue(contentFieldValue.getId());
120
121                 // flush the cache and unindex
122
CacheUtil cacheUtil = CacheUtil.getInstance(request);
123                 cacheUtil.flushFieldIndices();
124                 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
125
126                 Localizable owner = contentField.getOwner();
127                 if ( owner instanceof Layout ) {
128                     Layout layout = (Layout) owner;
129                     cacheUtil.flushLayoutFieldValueCache(layout.getDefinition());
130
131                     //TODO: unindex search index for every content page of the layout.
132
//TODO: unindex action pages
133
List JavaDoc contentPages = layout.getContentPages();
134                     for ( int j = 0; j < contentPages.size(); j++ ) {
135                         ContentPage contentPage = (ContentPage) contentPages.get(j);
136                         searchManager.reIndexPage(contentPage, request);
137                         cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri());
138                     }
139                 } else if ( owner instanceof Page ) {
140                     Page page = (Page) owner;
141                     cacheUtil.flushPageFieldValueCache(page.getUri(), contentField.getIdentifier(), localeIdentifier);
142                     searchManager.reIndexPage(page, request);
143                     if ( page instanceof ContentPage ) {
144                         cacheUtil.updateContentPageLastModifiedInCache(page.getUri());
145                     }
146                 }
147             }
148         }
149
150         request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId);
151
152         if ( alreadyDeletedExist ) {
153             ActionMessages errors = new ActionMessages();
154             errors.add("someValuesDeleted", new ActionMessage("core.contentFieldValue.errors.someValuesDeleted"));
155             saveErrors(request, errors);
156         }
157
158         return mapping.findForward("listContentFieldValues");
159     }
160 }
Popular Tags