KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.core.ContentPage;
20 import com.blandware.atleap.model.core.Layout;
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.ContentPageForm;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
27 import com.blandware.atleap.webapp.util.core.WebappUtil;
28 import org.apache.commons.validator.GenericValidator;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34 import org.springframework.orm.ObjectOptimisticLockingFailureException;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 /**
40  * <p>Adds or removes content page to or from publishing
41  * </p>
42  * <p><a HREF="ChangeContentPageActivityAction.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.22 $ $Date: 2006/03/10 17:10:20 $
48  * @struts.action path="/core/contentPage/changeActivity"
49  * name="contentPageForm"
50  * scope="request"
51  * validate="false"
52  * roles="core-contentPage-changeActivity"
53  * @struts.action-forward name="listContentPages"
54  * path="/core/contentPage/list.do"
55  * redirect="true"
56  * @struts.action-forward name="unsatisfiable"
57  * path="/core/contentPage/list.do"
58  */

59 public final class ChangeContentPageActivityAction 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         ContentPageForm contentPageForm = (ContentPageForm) form;
73
74         Long JavaDoc contentPageId = null;
75         if ( !GenericValidator.isBlankOrNull(contentPageForm.getId()) ) {
76             contentPageId = Long.valueOf(contentPageForm.getId());
77         } else {
78             if ( log.isWarnEnabled() ) {
79                 log.warn("Missing content page ID. Returning to list.");
80             }
81             return mapping.findForward("listContentPages");
82         }
83
84         PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
85         ContentPage contentPage = pageManager.retrieveContentPage(contentPageId);
86
87         if ( contentPage == null ) {
88             // content page not found. it might be deleted by someone else
89
ActionMessages errors = new ActionMessages();
90             errors.add("contentPageNotFound", new ActionMessage("core.contentPage.errors.notFound"));
91             saveErrors(request, errors);
92             return mapping.findForward("listContentPages");
93         }
94
95         Layout layout = contentPage.getLayout();
96
97         String JavaDoc mode = request.getParameter("mode");
98         boolean alreadyActive = contentPage.getActive() != null && contentPage.getActive().booleanValue();
99         boolean active = true;
100         if ( !"active".equalsIgnoreCase(mode) ) {
101             active = false;
102         }
103
104         try {
105
106             if ( (alreadyActive && !active) || (!alreadyActive && active) ) {
107                 contentPage.setActive(Boolean.valueOf(active));
108                 pageManager.updateContentPage(contentPage, layout.getId());
109
110                 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
111                 searchManager.unIndexPage(contentPage.getUri(), request);
112
113                 CacheUtil cacheUtil = CacheUtil.getInstance(request);
114
115                 if ( active ) {
116                     searchManager.indexPage(contentPage, request);
117
118                     CacheUtil.ContentPageData cpd = new CacheUtil.ContentPageData(layout.getCpDefinition(), WebappUtil.rolesToString(contentPage.getRoles()), contentPage.getCacheMaxAge());
119                     cacheUtil.putContentPageInCache(cpd, contentPage.getUri());
120
121                 } else {
122                     cacheUtil.flushContentPageCache(contentPage.getUri());
123                 }
124             }
125
126
127         } catch ( BeanAlreadyExistsException e ) {
128             // contentPage already exists: will never be reached in our case
129
ActionMessages errors = new ActionMessages();
130             errors.add("contentPageAlreadyExists", new ActionMessage("core.contentPage.errors.alreadyExists"));
131             saveErrors(request, errors);
132             saveToken(request);
133             return mapping.getInputForward();
134         } catch ( ObjectOptimisticLockingFailureException e ) {
135             // content page was updated or deleted by another transaction
136
ActionMessages errors = new ActionMessages();
137             errors.add("updateFailed", new ActionMessage("core.contentPage.errors.updateFailed"));
138             saveErrors(request, errors);
139             return mapping.findForward("listContentPages");
140         }
141         return mapping.findForward("listContentPages");
142     }
143 }
Popular Tags