KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.core.LookupManager;
23 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentFieldForm;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import org.apache.commons.validator.GenericValidator;
30 import org.apache.struts.action.*;
31 import org.springframework.orm.ObjectOptimisticLockingFailureException;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.TreeSet JavaDoc;
39
40 /**
41  * <p>Updates content field
42  * </p>
43  * <p><a HREF="UpdateContentFieldAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
48  * @version $Revision: 1.43 $ $Date: 2006/03/26 14:46:23 $
49  * @struts.action path="/core/contentField/update"
50  * name="contentFieldForm"
51  * scope="request"
52  * input="inputForward"
53  * validate="true"
54  * roles="core-contentField-update, core-contentField-updateOverriden, core-contentField-updateIndexed"
55  * @struts.action-forward name="inputForward"
56  * path=".core.contentField.update"
57  * @struts.action-forward name="viewContentField"
58  * path="/core/contentField/view.do"
59  * redirect="true"
60  * @struts.action-forward name="listContentFields"
61  * path="/core/contentField/list.do"
62  * redirect="false"
63  * @struts.action-forward name="callUpdateContentField"
64  * path="/core/contentField/callUpdate.do"
65  * redirect="false"
66  * @struts.action-forward name="unsatisfiable"
67  * path="/core/contentField/list.do"
68  */

69 public final class UpdateContentFieldAction 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         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
82
83         Long JavaDoc ownerId = null;
84         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
85             ownerId = Long.valueOf(contentFieldForm.getOwnerId());
86         } else {
87             if ( log.isWarnEnabled() ) {
88                 log.warn("Missing owner ID. Returning to index...");
89             }
90             return mapping.findForward("admin");
91         }
92
93         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
94
95         Long JavaDoc contentFieldId = null;
96         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) {
97             contentFieldId = Long.valueOf(contentFieldForm.getId());
98         } else {
99             if ( log.isWarnEnabled() ) {
100                 log.warn("Missing field ID. Returning to list");
101             }
102             return mapping.findForward("listContentFields");
103         }
104
105         if ( !isCancelled(request) ) {
106             LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN);
107             Localizable owner = lookupManager.retrieveLocalizable(ownerId);
108
109             if ( owner == null ) {
110                 ActionMessages errors = new ActionMessages();
111                 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound"));
112                 saveErrors(request, errors);
113                 return mapping.findForward("admin");
114             }
115
116             String JavaDoc fieldIdentifier = contentFieldForm.getIdentifier();
117
118             if (owner instanceof ContentPage || owner instanceof ActionPage) {
119                 Set JavaDoc indexedFieldIdentifiers = WebappUtil.getIndexedOverridableFieldIdentifiers(owner, request);
120                 Set JavaDoc unIndexedFieldIdentifiers = WebappUtil.getUnIndexedOverridableFieldIdentifiers(owner, request);
121                 Set JavaDoc intersection = new HashSet JavaDoc(indexedFieldIdentifiers);
122                 intersection.retainAll(unIndexedFieldIdentifiers);
123                 if ( !intersection.contains(fieldIdentifier) ) {
124                     if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
125                         if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) {
126                             ActionMessages errors = new ActionMessages();
127                             errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex"));
128                             saveErrors(request, errors);
129                             saveToken(request);
130                             return mapping.getInputForward();
131                         }
132                     } else {
133                         if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) {
134                             ActionMessages errors = new ActionMessages();
135                             errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
136                             saveErrors(request, errors);
137                             saveToken(request);
138                             return mapping.getInputForward();
139                         }
140                     }
141                 }
142             } else if (owner instanceof Layout) {
143                 Layout ownerLayout = (Layout) owner;
144                 Set JavaDoc indexedFieldIdentifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
145                 Set JavaDoc unIndexedFieldIdentifiers = WebappUtil.getUnIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
146                 Set JavaDoc intersection = new HashSet JavaDoc(indexedFieldIdentifiers);
147                 intersection.retainAll(unIndexedFieldIdentifiers);
148                 if ( !intersection.contains(fieldIdentifier) ) {
149                     if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
150                         if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) {
151                             ActionMessages errors = new ActionMessages();
152                             errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex"));
153                             saveErrors(request, errors);
154                             saveToken(request);
155                             return mapping.getInputForward();
156                         }
157                     } else {
158                         if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) {
159                             ActionMessages errors = new ActionMessages();
160                             errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
161                             saveErrors(request, errors);
162                             saveToken(request);
163                             return mapping.getInputForward();
164                         }
165                     }
166                 }
167             }
168
169             if ( owner instanceof ContentPage || (owner instanceof Layout && !request.isUserInRole("core-contentField-update") || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-update"))) ) {
170                 Set JavaDoc identifiers = null;
171                 if ( owner instanceof ContentPage || owner instanceof ActionPage ) {
172                     // Updating overriden field
173
if ( !request.isUserInRole("core-contentField-updateOverriden") ) {
174                         response.sendError(HttpServletResponse.SC_FORBIDDEN);
175                         return null;
176                     }
177                     identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
178
179                     if ( !identifiers.contains(fieldIdentifier) ) {
180                         ActionMessages errors = new ActionMessages();
181                         errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier"));
182                         saveErrors(request, errors);
183                         saveToken(request);
184                         return mapping.getInputForward();
185                     }
186                 } else {
187                     // Updating indexed field
188
boolean canUpdateIndexed = request.isUserInRole("core-contentField-updateIndexed");
189                     boolean canUpdateOverriden = request.isUserInRole("core-contentField-updateOverriden");
190                     if ( !canUpdateIndexed && !canUpdateOverriden ) {
191                         response.sendError(HttpServletResponse.SC_FORBIDDEN);
192                         return null;
193                     }
194                     Layout ownerLayout = (Layout) owner;
195                     if (canUpdateIndexed) {
196                         identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
197                         if (identifiers == null) {
198                             identifiers = new TreeSet JavaDoc();
199                         }
200                     }
201                     if (canUpdateOverriden) {
202                         identifiers.addAll(WebappUtil.getOverridableFieldIdentifiers(ownerLayout, request));
203                     }
204                     if ( !identifiers.contains(fieldIdentifier) ) {
205                         ActionMessages errors = new ActionMessages();
206                         errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier"));
207                         saveErrors(request, errors);
208                         saveToken(request);
209                         return mapping.getInputForward();
210                     }
211 /*
212                     if ( GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
213                         ActionMessages errors = new ActionMessages();
214                         errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
215                         saveErrors(request, errors);
216                         saveToken(request);
217                         return mapping.getInputForward();
218                     }
219 */

220                 }
221             } else {
222                 // (it's AP or Layout and user can update fields) OR (its not CP,
223
// AP or Layout)
224
if ( !request.isUserInRole("core-contentField-update") ) {
225                     response.sendError(HttpServletResponse.SC_FORBIDDEN);
226                     return null;
227                 }
228             }
229
230             ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
231             ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
232
233             if ( contentField == null ) {
234                 // menu item not found. it might be deleted by someone else
235
ActionMessages errors = new ActionMessages();
236                 errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound"));
237                 saveErrors(request, errors);
238                 return mapping.findForward("listContentFields");
239             }
240
241             // save field's type
242
byte type = contentField.getType();
243
244             WebappUtil.copyProperties(contentField, contentFieldForm, request);
245             if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
246                 fieldIdentifier += "[" + contentFieldForm.getIndex() + "]";
247                 contentField.setIdentifier(fieldIdentifier);
248             }
249
250             // field's type cannot be changed, so set old value forcibly
251
contentField.setType(type);
252
253             try {
254
255                 contentFieldManager.updateContentField(contentField, ownerId);
256
257                 // put into search index
258
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
259
260                 //flush cache
261
CacheUtil cacheUtil = CacheUtil.getInstance(request);
262                 cacheUtil.flushFieldIndices();
263
264                 if ( owner instanceof Layout ) {
265                     Layout layout = (Layout) owner;
266
267                     //add into search index for every content page of the layout.
268
List JavaDoc contentPages = layout.getContentPages();
269                     for ( int i = 0; i < contentPages.size(); i++ ) {
270                         ContentPage contentPage = (ContentPage) contentPages.get(i);
271                         searchManager.reIndexPage(contentPage, request);
272                         cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri());
273                     }
274                     cacheUtil.flushLayoutFieldValueCache(layout.getDefinition());
275
276                 } else if ( owner instanceof Page ) {
277                     Page page = (Page) owner;
278                     searchManager.reIndexPage(page, request);
279                     cacheUtil.flushPageFieldValueCache(page.getUri());
280                     if ( page instanceof ContentPage ) {
281                         cacheUtil.updateContentPageLastModifiedInCache(page.getUri());
282                     }
283                 }
284
285             } catch ( BeanAlreadyExistsException e ) {
286                 // contentField already exists
287
ActionMessages errors = new ActionMessages();
288                 errors.add("contentFieldAlreadyExists", new ActionMessage("core.contentField.errors.alreadyExists"));
289                 saveErrors(request, errors);
290                 saveToken(request);
291                 return mapping.getInputForward();
292             } catch ( ObjectOptimisticLockingFailureException e ) {
293                 // content field was updated or deleted by another transaction
294
ActionMessages errors = new ActionMessages();
295                 errors.add("updateFailed", new ActionMessage("core.contentField.errors.updateFailed"));
296                 saveErrors(request, errors);
297                 return mapping.findForward("callUpdateContentField");
298             }
299             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, Long.valueOf(contentFieldForm.getId()));
300             return mapping.findForward("viewContentField");
301         } else {
302             // action was cancelled - return to page with list of content fields
303
String JavaDoc requestUrl = (String JavaDoc) request.getSession().getAttribute("beforeEditedFieldUrl");
304             if ( !GenericValidator.isBlankOrNull(requestUrl) ) {
305                 request.getSession().removeAttribute("beforeEditedFieldUrl");
306                 return new ActionForward(requestUrl, true);
307             } else {
308                 return mapping.findForward("listContentFields");
309             }
310         }
311
312     }
313 }
Popular Tags