KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > actionPage > UpdateActionPageAction


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.actionPage;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.ConvertUtil;
20 import com.blandware.atleap.model.core.*;
21 import com.blandware.atleap.search.SearchManager;
22 import com.blandware.atleap.service.core.PageManager;
23 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ActionPageForm;
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.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.util.ModuleUtils;
37 import org.springframework.orm.ObjectOptimisticLockingFailureException;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.HashSet JavaDoc;
45
46 /**
47  * <p>Updates action page
48  * </p>
49  * <p><a HREF="UpdateActionPageAction.java.htm"><i>View Source</i></a></p>
50  * <p/>
51  *
52  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
53  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
54  * @version $Revision: 1.39 $ $Date: 2006/03/22 11:22:01 $
55  * @struts.action name="actionPageForm"
56  * path="/core/actionPage/update"
57  * scope="request"
58  * input="inputForward"
59  * validate="true"
60  * roles="core-actionPage-updateContent, core-actionPage-update"
61  * @struts.action-forward name="inputForward"
62  * path=".core.actionPage.update"
63  * @struts.action-forward name="viewActionPage"
64  * path="/core/actionPage/view.do"
65  * redirect="true"
66  * @struts.action-forward name="listActionPages"
67  * path="/core/actionPage/list.do"
68  * redirect="true"
69  * @struts.action-forward name="callUpdateActionPage"
70  * path="/core/actionPage/callUpdate.do"
71  * redirect="false"
72  * @struts.action-forward name="viewLinkedObjects"
73  * path="/core/linkedObjects/view.do"
74  * @struts.action-forward name="unsatisfiable"
75  * path="/core/actionPage/list.do"
76  */

77 public final class UpdateActionPageAction extends BaseAction {
78     /**
79      * @param mapping The ActionMapping used to select this instance
80      * @param form The optional ActionForm bean for this request (if any)
81      * @param request The HTTP request we are proceeding
82      * @param response The HTTP response we are creating
83      * @return an ActionForward instance describing where and how
84      * control should be forwarded, or null if response
85      * has already been completed
86      */

87     public ActionForward execute(ActionMapping mapping, ActionForm form,
88                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
89
90         if ( !isCancelled(request) ) {
91             // flag shows whether to force changing of URI if there're some
92
// linked objects
93
boolean force = "true".equalsIgnoreCase(request.getParameter("force"));
94
95             ActionPageForm actionPageForm;
96             if (force) {
97                 actionPageForm = (ActionPageForm) request.getSession().getAttribute(WebappConstants.ACTION_PAGE_FORM_KEY);
98             } else {
99                 actionPageForm = (ActionPageForm) form;
100             }
101
102             if ( !WebappUtil.hasCorrectValues(actionPageForm.getTitleMap()) ) {
103                 // title must be specified
104
ActionMessages errors = new ActionMessages();
105                 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.page.form.title")));
106                 saveErrors(request, errors);
107                 saveToken(request);
108                 return mapping.getInputForward();
109             }
110
111             // check URI for existence
112
String JavaDoc uri = actionPageForm.getUri();
113             ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext());
114             String JavaDoc prefix = moduleConfig.getPrefix();
115             if ( log.isDebugEnabled() ) {
116                 log.debug("Prefix: " + prefix);
117             }
118
119             String JavaDoc actionPath = new String JavaDoc();
120             actionPath = prefix + uri;
121
122             if ( log.isDebugEnabled() ) {
123                 log.debug("Searching for action with path=" + actionPath);
124             }
125
126             if ( moduleConfig.findActionConfig(actionPath) == null ) {
127                 // action with this uri does not exist
128
if ( log.isDebugEnabled() ) {
129                     log.debug("Action with uri '" + actionPath + "' does not exist");
130                 }
131                 ActionMessages errors = new ActionMessages();
132                 errors.add("actionPageIncorrectUri", new ActionMessage("core.actionPage.errors.incorrectUri"));
133                 saveErrors(request, errors, false);
134                 saveToken(request);
135                 return mapping.getInputForward();
136             }
137
138             Long JavaDoc actionPageId = null;
139             if ( !GenericValidator.isBlankOrNull(actionPageForm.getId()) ) {
140                 actionPageId = Long.valueOf(actionPageForm.getId());
141             } else {
142                 if ( log.isWarnEnabled() ) {
143                     log.warn("Missing action page ID. Returning to list...");
144                 }
145                 return mapping.findForward("listActionPages");
146             }
147
148             request.getSession().setAttribute(WebappConstants.ACTION_PAGE_ID_KEY, actionPageId);
149
150             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
151             ActionPage actionPage = pageManager.retrieveActionPage(actionPageId);
152
153             if ( actionPage == null ) {
154                 // action page not found. it might be deleted by someone else
155
ActionMessages errors = new ActionMessages();
156                 errors.add("actionPageNotFound", new ActionMessage("core.actionPage.errors.notFound"));
157                 saveErrors(request, errors);
158                 return mapping.findForward("listActionPages");
159             }
160
161             String JavaDoc oldUri = actionPage.getUri();
162
163             // Check whether user tries to change fields that he cannot change
164
boolean canUpdateUriAndLinkable = request.isUserInRole("core-actionPage-update");
165             if (!canUpdateUriAndLinkable && (!oldUri.equals(uri) || !actionPageForm.getActive().equals(actionPage.getActive()))) {
166                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
167                 return null;
168             }
169
170             if (!oldUri.equals(uri)) {
171                 // check whether there is already another action page with this URI
172
ActionPage oldPage = pageManager.findActionPageByUri(uri);
173                 if (oldPage != null) {
174                     // action page already exists
175
ActionMessages errors = new ActionMessages();
176                     errors.add("actionPageAlreadyExists", new ActionMessage("core.actionPage.errors.alreadyExists"));
177                     saveErrors(request, errors);
178                     saveToken(request);
179                     return mapping.getInputForward();
180                 }
181             }
182
183             boolean uriChanged = !oldUri.equals(uri);
184             if (!force && actionPage.isInUse() && uriChanged ) {
185                 // trying to change URI of used page - display warning
186
String JavaDoc action = mapping.findForward("listActionPages").getPath();
187                 String JavaDoc redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
188                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl);
189                 request.getSession().setAttribute(WebappConstants.ACTION_PAGE_FORM_KEY, actionPageForm);
190                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECT_NEW_URI_KEY, uri);
191                 ActionMessages messages = new ActionMessages();
192                 messages.add("changeUriWarning", new ActionMessage("core.actionPage.messages.changeUriWarning"));
193                 if (actionPage.doUnmodifiableLinkedObjectsExist()) {
194                     messages.add("unmodifiableLinkedObjectsWarning", new ActionMessage("core.actionPage.messages.unmodifiableLinkedObjectsWarning"));
195                 }
196                 saveMessages(request, messages);
197                 request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY);
198                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY, "core/actionPage/update");
199                 request.setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, actionPage.getId());
200                 request.setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, "page");
201                 return mapping.findForward("viewLinkedObjects");
202             }
203
204             WebappUtil.copyProperties(actionPage, actionPageForm, request);
205             actionPage.setTitle(actionPageForm.getTitleMap());
206             actionPage.setDescription(actionPageForm.getDescriptionMap());
207             actionPage.setKeywords(actionPageForm.getKeywordsMap());
208
209             // remove links to this action page from unmodifiable menu items
210
if (uriChanged) {
211                 for (Iterator JavaDoc i = actionPage.getLinkedMenuItems().iterator(); i.hasNext();) {
212                     MenuItem menuItem = (MenuItem) i.next();
213                     if (!menuItem.isDynamic()) {
214                         i.remove();
215                     }
216                 }
217             }
218
219             try {
220                 pageManager.updateActionPage(actionPage);
221
222                 //reindex action page
223
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
224                 searchManager.unIndexPage(oldUri, request);
225
226                 if ( actionPage.getActive().booleanValue() ) {
227                     searchManager.indexPage(actionPage, request);
228                 }
229
230                 // update cache
231
CacheUtil cacheUtil = CacheUtil.getInstance(request);
232                 cacheUtil.flushFieldIndices();
233                 if ( !oldUri.equals(actionPage.getUri()) ) {
234
235                     // uri has been changed. flush all fields' cache
236
cacheUtil.flushPageFieldValueCache(oldUri);
237
238                     // recache all fields
239
List JavaDoc fields = actionPage.getContentFields();
240                     for ( Iterator JavaDoc i = fields.iterator(); i.hasNext(); ) {
241                         ContentField field = (ContentField) i.next();
242                         List JavaDoc cfvList = field.getContentFieldValues();
243                         for ( Iterator JavaDoc j = cfvList.iterator(); j.hasNext(); ) {
244                             ContentFieldValue fieldValue = (ContentFieldValue) j.next();
245                             String JavaDoc content = null;
246                             if ( field.getType() == ContentField.LINE_TYPE ) {
247                                 content = fieldValue.getSimpleValue();
248                             } else if ( field.getType() == ContentField.MULTILINE_TYPE ) {
249                                 content = ConvertUtil.convertToString(fieldValue.getValue());
250                             } else /* if HTML_TYPE */ {
251                                 content = ConvertUtil.convertToString(fieldValue.getValue());
252                             }
253                             CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, field.getId(), field.getType(), fieldValue.getId());
254                             cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), field.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
255                         }
256                     }
257                 } else {
258                     // uri has not been changed: recache internal fields only
259

260                     // title
261
ContentField titleField = (ContentField) actionPage.getContentFieldsMap().get("title");
262                     List JavaDoc titleValues = titleField.getContentFieldValues();
263                     for ( Iterator JavaDoc i = titleValues.iterator(); i.hasNext(); ) {
264                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
265                         // title field is single-lined
266
String JavaDoc content = fieldValue.getSimpleValue();
267                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, titleField.getId(), titleField.getType(), fieldValue.getId());
268                         cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), titleField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
269                     }
270
271                     // description
272
ContentField descriptionField = (ContentField) actionPage.getContentFieldsMap().get("description");
273                     List JavaDoc descriptionValues = descriptionField.getContentFieldValues();
274                     for ( Iterator JavaDoc i = descriptionValues.iterator(); i.hasNext(); ) {
275                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
276                         // description field is single-lined
277
String JavaDoc content = fieldValue.getSimpleValue();
278                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, descriptionField.getId(), descriptionField.getType(), fieldValue.getId());
279                         cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), descriptionField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
280                     }
281
282                     // keywords
283
ContentField keywordsField = (ContentField) actionPage.getContentFieldsMap().get("keywords");
284                     List JavaDoc keywordsValues = keywordsField.getContentFieldValues();
285                     for ( Iterator JavaDoc i = keywordsValues.iterator(); i.hasNext(); ) {
286                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
287                         // keywords field is single-lined
288
String JavaDoc content = fieldValue.getSimpleValue();
289                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, keywordsField.getId(), keywordsField.getType(), fieldValue.getId());
290                         cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), keywordsField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
291                     }
292
293                 }
294
295                 if (force) {
296                     // reindex and update cache for linked objects, because they
297
// were changed
298

299                     // flush cache related to menu items
300
cacheUtil.flushMenuCache();
301
302                     // put linked values in cache and into search index
303
List JavaDoc linkedContentFieldValues = actionPage.getLinkedContentFieldValues();
304                     Set JavaDoc owners = new HashSet JavaDoc();
305                     for (Iterator JavaDoc i = linkedContentFieldValues.iterator(); i.hasNext();) {
306                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
307                         ContentField field = fieldValue.getContentField();
308                         Localizable owner = field.getOwner();
309                         // put owner to set of owners to cache/reindex them later
310
owners.add(owner);
311
312                         String JavaDoc localeIdentifier = fieldValue.getContentLocale().getIdentifier();
313
314                         byte fieldType = field.getType();
315                         String JavaDoc content = null;
316
317                         if (fieldType == ContentField.LINE_TYPE) {
318                             content = fieldValue.getSimpleValue();
319                         } else {
320                             content = ConvertUtil.convertToString(fieldValue.getValue());
321                         }
322                         if ( owner instanceof Page ) {
323                             Page page = (Page) owner;
324                             CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, field.getId(), field.getType(), fieldValue.getId());
325                             cacheUtil.putPageFieldValueInCache(cfvData, page.getUri(), field.getIdentifier(), localeIdentifier);
326                         }
327                     }
328
329                     // process owners
330
for (Iterator JavaDoc i = owners.iterator(); i.hasNext();) {
331                         Localizable owner = (Localizable) i.next();
332                         if ( owner instanceof Layout ) {
333                             Layout layout = (Layout) owner;
334                             cacheUtil.flushLayoutFieldValueCache(layout.getDefinition());
335
336                             //TODO: add into search index for every content page of the layout.
337
//TODO: add into index action pages
338
List JavaDoc contentPages = layout.getContentPages();
339                             for ( int j = 0; j < contentPages.size(); j++ ) {
340                                 ContentPage cp = (ContentPage) contentPages.get(j);
341                                 searchManager.reIndexPage(cp, request);
342                                 cacheUtil.updateContentPageLastModifiedInCache(cp.getUri());
343                             }
344
345                         } else if ( owner instanceof Page ) {
346                             Page page = (Page) owner;
347                             if (!actionPage.getId().equals(page.getId())) {
348                                 searchManager.reIndexPage(page, request);
349                             }
350                             if ( page instanceof ContentPage ) {
351                                 cacheUtil.updateContentPageLastModifiedInCache(page.getUri());
352                             }
353                         }
354                     }
355                 }
356
357             } catch ( BeanAlreadyExistsException e ) {
358                 // actionPage already exists
359
ActionMessages errors = new ActionMessages();
360                 errors.add("actionPageAlreadyExists", new ActionMessage("core.actionPage.errors.alreadyExists"));
361                 saveErrors(request, errors);
362                 saveToken(request);
363                 return mapping.getInputForward();
364             } catch ( ObjectOptimisticLockingFailureException e ) {
365                 // action page was updated or deleted by another transaction
366
ActionMessages errors = new ActionMessages();
367                 errors.add("updateFailed", new ActionMessage("core.actionPage.errors.updateFailed"));
368                 saveErrors(request, errors);
369                 return mapping.findForward("callUpdateActionPage");
370             }
371             request.getSession().setAttribute(WebappConstants.ACTION_PAGE_ID_KEY, Long.valueOf(actionPageForm.getId()));
372         }
373         return mapping.findForward("listActionPages");
374     }
375 }
Popular Tags