KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > news > ChangeNewsItemActivityAction


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.news;
17
18 import com.blandware.atleap.common.NewsModuleConstants;
19 import com.blandware.atleap.model.news.NewsItem;
20 import com.blandware.atleap.search.SearchManager;
21 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
22 import com.blandware.atleap.service.news.NewsManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.NewsItemForm;
25 import com.blandware.atleap.webapp.util.core.CacheUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.springframework.orm.ObjectOptimisticLockingFailureException;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * <p>Adds or removes news item to or from publishing
39  * </p>
40  * <p><a HREF="ChangeNewsItemActivityAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
45  * @version $Revision: 1.18 $ $Date: 2006/03/10 17:10:31 $
46  * @struts.action path="/news/changeItemActivity"
47  * name="newsItemForm"
48  * scope="request"
49  * validate="false"
50  * roles="news-item-changeActivity"
51  * @struts.action-forward name="listNewsItems"
52  * path="/news/listItems.do"
53  * redirect="true"
54  * @struts.action-forward name="unsatisfiable"
55  * path="/news/listItems.do"
56  */

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

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         NewsItemForm newsItemForm = (NewsItemForm) form;
71
72         Long JavaDoc newsItemId = null;
73         if ( !GenericValidator.isBlankOrNull(newsItemForm.getId()) ) {
74             newsItemId = Long.valueOf(newsItemForm.getId());
75         } else {
76             if ( log.isWarnEnabled() ) {
77                 log.warn("Missing news item ID. Returning to list.");
78             }
79             return mapping.findForward("listNewsItems");
80         }
81
82         NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
83         NewsItem newsItem = newsManager.retrieveNewsItem(newsItemId);
84
85         if ( newsItem == null ) {
86             // newsItem not found. it might be deleted by someone else
87
ActionMessages errors = new ActionMessages();
88             errors.add("newsItemNotFound", new ActionMessage("news.errors.notFound"));
89             saveErrors(request, errors);
90             return mapping.findForward("listNewsItems");
91         }
92
93         String JavaDoc mode = request.getParameter("mode");
94         boolean alreadyActive = newsItem.getActive() != null && newsItem.getActive().booleanValue();
95         boolean active = true;
96         if ( !"active".equalsIgnoreCase(mode) ) {
97             active = false;
98         }
99
100         try {
101
102             if ( (alreadyActive && !active) || (!alreadyActive && active) ) {
103                 newsItem.setActive(Boolean.valueOf(active));
104                 newsManager.updateNewsItem(newsItem);
105
106                 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
107                 searchManager.unIndexPage(newsItem.getUri(), request);
108
109                 if ( active ) {
110                     searchManager.indexPage(newsItem, request);
111                 }
112
113                 CacheUtil cacheUtil = CacheUtil.getInstance(request);
114                 cacheUtil.flushLocalizableFieldValueCache(newsItemId);
115             }
116         } catch ( BeanAlreadyExistsException e ) {
117             // newsItem already exists: will never be reached in our case
118
ActionMessages errors = new ActionMessages();
119             errors.add("newsItemAlreadyExists", new ActionMessage("errors.newsItemAlreadyExists"));
120             saveErrors(request, errors);
121             return mapping.findForward("listNewsItems");
122         } catch ( ObjectOptimisticLockingFailureException e ) {
123             // news item was updated or deleted by another transaction
124
ActionMessages errors = new ActionMessages();
125             errors.add("updateFailed", new ActionMessage("news.errors.updateFailed"));
126             saveErrors(request, errors);
127             return mapping.findForward("listNewsItems");
128         }
129         return mapping.findForward("listNewsItems");
130     }
131 }
Popular Tags