KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentPage > UpdateContentPageAction


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.contentPage;
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.LayoutManager;
23 import com.blandware.atleap.service.core.PageManager;
24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
25 import com.blandware.atleap.service.exception.OwnerNotFoundException;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.ContentPageForm;
28 import com.blandware.atleap.webapp.util.core.CacheUtil;
29 import com.blandware.atleap.webapp.util.core.WebappConstants;
30 import com.blandware.atleap.webapp.util.core.WebappUtil;
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 import org.apache.struts.config.ModuleConfig;
38 import org.apache.struts.util.ModuleUtils;
39 import org.springframework.orm.ObjectOptimisticLockingFailureException;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.util.*;
44
45 /**
46  * <p>Updates content page
47  * </p>
48  * <p><a HREF="UpdateContentPageAction.java.htm"><i>View Source</i></a></p>
49  * <p/>
50  *
51  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
52  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
53  * @version $Revision: 1.41 $ $Date: 2006/03/22 11:22:02 $
54  * @struts.action path="/core/contentPage/update"
55  * name="contentPageForm"
56  * scope="request"
57  * input="inputForward"
58  * validate="true"
59  * roles="core-contentPage-update"
60  * @struts.action-forward name="inputForward"
61  * path=".core.contentPage.update"
62  * @struts.action-forward name="listContentPages"
63  * path="/core/contentPage/list.do"
64  * redirect="true"
65  * @struts.action-forward name="callUpdateContentPage"
66  * path="/core/contentPage/callUpdate.do"
67  * redirect="false"
68  * @struts.action-forward name="viewLinkedObjects"
69  * path="/core/linkedObjects/view.do"
70  * @struts.action-forward name="unsatisfiable"
71  * path="/core/contentPage/list.do"
72  */

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

83     public ActionForward execute(ActionMapping mapping, ActionForm form,
84                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
85
86         if ( !isCancelled(request) ) {
87             // flag shows whether to force changing of URI if there're some
88
// linked objects
89
boolean force = "true".equalsIgnoreCase(request.getParameter("force"));
90
91             ContentPageForm contentPageForm;
92             if (force) {
93                 contentPageForm = (ContentPageForm) request.getSession().getAttribute(WebappConstants.CONTENT_PAGE_FORM_KEY);
94             } else {
95                 contentPageForm = (ContentPageForm) form;
96             }
97
98             if ( !WebappUtil.hasCorrectValues(contentPageForm.getTitleMap()) ) {
99                 // title must be specified
100
ActionMessages errors = new ActionMessages();
101                 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.page.form.title")));
102                 saveErrors(request, errors, false);
103                 saveToken(request);
104                 return mapping.getInputForward();
105             }
106
107             // check URI. If there is an action with the same path, return error
108
String JavaDoc uri = WebappConstants.CONTENT_PAGES_URI_PREFIX + contentPageForm.getUri();
109             ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext());
110             String JavaDoc prefix = moduleConfig.getPrefix();
111             if ( log.isDebugEnabled() ) {
112                 log.debug("Prefix: " + prefix);
113             }
114
115             String JavaDoc actionPath = new String JavaDoc();
116             actionPath = prefix + uri;
117
118             if ( log.isDebugEnabled() ) {
119                 log.debug("Searching for action with path=" + actionPath);
120             }
121
122             if ( moduleConfig.findActionConfig(actionPath) != null ) {
123                 // action with same uri exists
124
if ( log.isDebugEnabled() ) {
125                     log.debug("Action was found - returning input");
126                 }
127                 ActionMessages errors = new ActionMessages();
128                 errors.add("contentPageIncorrectUri", new ActionMessage("core.contentPage.errors.incorrectUri"));
129                 saveErrors(request, errors);
130                 saveToken(request);
131                 return mapping.getInputForward();
132             }
133
134             Long JavaDoc contentPageId = null;
135             if ( !GenericValidator.isBlankOrNull(contentPageForm.getId()) ) {
136                 contentPageId = Long.valueOf(contentPageForm.getId());
137             } else {
138                 if ( log.isWarnEnabled() ) {
139                     log.warn("Missing content page ID. Returning to list...");
140                 }
141                 return mapping.findForward("listContentPages");
142             }
143
144             request.getSession().setAttribute(WebappConstants.CONTENT_PAGE_ID_KEY, contentPageId);
145
146             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
147             ContentPage contentPage = pageManager.retrieveContentPage(contentPageId);
148
149             if ( contentPage == null ) {
150                 // content page not found. it might be deleted by someone else
151
ActionMessages errors = new ActionMessages();
152                 errors.add("contentPageNotFound", new ActionMessage("core.contentPage.errors.notFound"));
153                 saveErrors(request, errors);
154                 return mapping.findForward("listContentPages");
155             }
156
157             String JavaDoc oldUri = contentPage.getUri();
158
159             if (!oldUri.equals(uri)) {
160                 // check whether there is already another content page with this URI
161
ContentPage oldPage = pageManager.findContentPageByUri(uri);
162                 if (oldPage != null) {
163                     // content page already exists
164
ActionMessages errors = new ActionMessages();
165                     errors.add("contentPageAlreadyExists", new ActionMessage("core.contentPage.errors.alreadyExists"));
166                     saveErrors(request, errors);
167                     saveToken(request);
168                     return mapping.getInputForward();
169                 }
170             }
171
172             Long JavaDoc layoutId = contentPage.getLayout().getId();
173             if ( !GenericValidator.isBlankOrNull(contentPageForm.getLayoutId()) ) {
174                 layoutId = Long.valueOf(contentPageForm.getLayoutId());
175             }
176
177             boolean uriChanged = !oldUri.equals(uri);
178             if (!force && contentPage.isInUse() && uriChanged ) {
179                 // trying to change URI of used page - display warning
180
String JavaDoc action = mapping.findForward("listContentPages").getPath();
181                 String JavaDoc redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
182                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl);
183                 request.getSession().setAttribute(WebappConstants.CONTENT_PAGE_FORM_KEY, contentPageForm);
184                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECT_NEW_URI_KEY, uri);
185                 ActionMessages messages = new ActionMessages();
186                 messages.add("changeUriWarning", new ActionMessage("core.contentPage.messages.changeUriWarning"));
187                 if (contentPage.doUnmodifiableLinkedObjectsExist()) {
188                     messages.add("unmodifiableLinkedObjectsWarning", new ActionMessage("core.contentPage.messages.unmodifiableLinkedObjectsWarning"));
189                 }
190                 saveMessages(request, messages);
191                 request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY);
192                 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY, "core/contentPage/update");
193                 request.setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, contentPage.getId());
194                 request.setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, "page");
195                 return mapping.findForward("viewLinkedObjects");
196             }
197
198             WebappUtil.copyProperties(contentPage, contentPageForm, request);
199             contentPage.setTitle(contentPageForm.getTitleMap());
200             contentPage.setDescription(contentPageForm.getDescriptionMap());
201             contentPage.setKeywords(contentPageForm.getKeywordsMap());
202             contentPage.setUri(uri);
203
204             // remove links to this content page from unmodifiable menu items
205
if (uriChanged) {
206                 for (Iterator i = contentPage.getLinkedMenuItems().iterator(); i.hasNext();) {
207                     MenuItem menuItem = (MenuItem) i.next();
208                     if (!menuItem.isDynamic()) {
209                         i.remove();
210                     }
211                 }
212             }
213
214             try {
215                 pageManager.updateContentPage(contentPage, layoutId);
216
217                 //reindex content page
218
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
219                 LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
220                 Layout layout = layoutManager.retrieveLayout(layoutId);
221                 searchManager.unIndexPage(oldUri, request);
222                 if ( contentPage.getActive().booleanValue() ) {
223                     searchManager.indexPage(contentPage, request);
224                 }
225
226                 // update cache
227
CacheUtil cacheUtil = CacheUtil.getInstance(request);
228                 cacheUtil.flushFieldIndices();
229                 if ( !oldUri.equals(contentPage.getUri()) ) {
230
231                     // uri has been changed. flush all fields' cache
232
cacheUtil.flushPageFieldValueCache(oldUri);
233                     cacheUtil.flushContentPageCache(oldUri);
234
235                     //put CP in cache
236
if ( contentPage.getActive().booleanValue() ) {
237                         CacheUtil.ContentPageData cpd = new CacheUtil.ContentPageData(layout.getCpDefinition(), WebappUtil.rolesToString(contentPage.getRoles()), contentPage.getCacheMaxAge());
238                         cacheUtil.putContentPageInCache(cpd, uri);
239                     }
240
241                     // recache all fields
242
List fields = contentPage.getContentFields();
243                     for ( Iterator i = fields.iterator(); i.hasNext(); ) {
244                         ContentField field = (ContentField) i.next();
245                         List cfvList = field.getContentFieldValues();
246                         for ( Iterator j = cfvList.iterator(); j.hasNext(); ) {
247                             ContentFieldValue fieldValue = (ContentFieldValue) j.next();
248                             String JavaDoc content = null;
249                             if ( field.getType() == ContentField.LINE_TYPE ) {
250                                 content = fieldValue.getSimpleValue();
251                             } else if ( field.getType() == ContentField.MULTILINE_TYPE ) {
252                                 content = ConvertUtil.convertToString(fieldValue.getValue());
253                             } else /* if HTML_TYPE */ {
254                                 content = ConvertUtil.convertToString(fieldValue.getValue());
255                             }
256                             CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, field.getId(), field.getType(), fieldValue.getId());
257                             cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), field.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
258                         }
259                     }
260                 } else {
261                     // uri has not been changed: recache internal fields only
262
if ( contentPage.getActive().booleanValue() ) {
263                         CacheUtil.ContentPageData cpd = new CacheUtil.ContentPageData(layout.getCpDefinition(), WebappUtil.rolesToString(contentPage.getRoles()), contentPage.getCacheMaxAge());
264                         cacheUtil.putContentPageInCache(cpd, contentPage.getUri());
265                     }
266
267                     // title
268
ContentField titleField = (ContentField) contentPage.getContentFieldsMap().get("title");
269                     List titleValues = titleField.getContentFieldValues();
270                     for ( Iterator i = titleValues.iterator(); i.hasNext(); ) {
271                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
272                         // title field is single-lined
273
String JavaDoc content = fieldValue.getSimpleValue();
274                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, titleField.getId(), titleField.getType(), fieldValue.getId());
275                         cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), titleField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
276                     }
277
278                     // description
279
ContentField descriptionField = (ContentField) contentPage.getContentFieldsMap().get("description");
280                     List descriptionValues = descriptionField.getContentFieldValues();
281                     for ( Iterator i = descriptionValues.iterator(); i.hasNext(); ) {
282                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
283                         // description field is single-lined
284
String JavaDoc content = fieldValue.getSimpleValue();
285                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, descriptionField.getId(), descriptionField.getType(), fieldValue.getId());
286                         cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), descriptionField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
287                     }
288
289                     // keywords
290
ContentField keywordsField = (ContentField) contentPage.getContentFieldsMap().get("keywords");
291                     List keywordsValues = keywordsField.getContentFieldValues();
292                     for ( Iterator i = keywordsValues.iterator(); i.hasNext(); ) {
293                         ContentFieldValue fieldValue = (ContentFieldValue) i.next();
294                         // keywords field is single-lined
295
String JavaDoc content = fieldValue.getSimpleValue();
296                         CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, keywordsField.getId(), keywordsField.getType(), fieldValue.getId());
297                         cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), keywordsField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
298                     }
299                 }
300
301                 if (force) {
302                     // reindex and update cache for linked objects, because they
303
// were changed
304

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