KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.news.NewsManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.NewsItemForm;
23 import com.blandware.atleap.webapp.util.core.LocaleUtil;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
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
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * <p>Returns page with form to update news item body
40  * </p>
41  * <p><a HREF="CallUpdateNewsItemBodyAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.28 $ $Date: 2006/03/10 17:10:31 $
46  * @struts.action path="/news/callUpdateItemBody"
47  * input="inputForward"
48  * name="newsItemForm"
49  * validate="false"
50  * roles="news-item-update"
51  * scope="request"
52  * @struts.action-forward name="inputForward"
53  * path=".news.updateItem"
54  * @struts.action-forward name="updateNewsItemBody"
55  * path=".news.updateItemBody"
56  * @struts.action-forward name="listNewsItems"
57  * path="/news/listItems.do"
58  * redirect="true"
59  */

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

70     public ActionForward execute(ActionMapping mapping, ActionForm form,
71                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
72
73         request.getSession().removeAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY);
74         NewsItemForm newsItemForm = (NewsItemForm) form;
75         String JavaDoc localesMode = newsItemForm.getLocalesMode();
76
77         if ( isBackPressed(request) ) {
78             // First check, may be session has been invalidated and we need to
79
// fill content locales again
80
if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) {
81                 List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
82                 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
83             }
84
85             // check 'check all' ckeckbox if needed
86
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
87
88             return mapping.findForward("inputForward");
89         }
90
91         if (isCancelled(request)) {
92             if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) {
93                 String JavaDoc redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
94                 ActionForward redirect = new ActionForward(redirectUrl, true);
95                 return redirect;
96             }
97             return mapping.findForward("listNewsItems");
98         }
99
100         if ( request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null ) {
101             List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
102             request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
103         }
104
105         // Check whether user came here not from previous page (or he switched
106
// edit mode/language during editing news item)
107
if ( !"current".equalsIgnoreCase(localesMode) && !"all".equalsIgnoreCase(localesMode) && newsItemForm.getBodyCheckedBoxes().size() == 0 ) {
108             request.setAttribute("beginPage", mapping.findForward("listNewsItems").getPath());
109             return mapping.findForward("unsatisfiablePage");
110         }
111
112         // check news item existence, because it might be deleted by another transaction
113
Long JavaDoc newsItemId = null;
114         if ( !GenericValidator.isBlankOrNull(newsItemForm.getId()) ) {
115             newsItemId = Long.valueOf(newsItemForm.getId());
116         } else {
117             if ( log.isWarnEnabled() ) {
118                 log.warn("Missing news item ID. Returning to list");
119             }
120             return mapping.findForward("listNewsItems");
121         }
122
123         NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
124         NewsItem newsItem = newsManager.retrieveNewsItem(newsItemId);
125         if ( newsItem == null ) {
126             ActionMessages errors = new ActionMessages();
127             errors.add("newsItemNotFound", new ActionMessage("news.errors.notFound"));
128             saveErrors(request, errors);
129             return mapping.findForward("listNewsItems");
130         }
131
132         if ( request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_BODY_KEY) != null ) {
133             Map JavaDoc mergedBody = (Map JavaDoc) request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_BODY_KEY);
134             newsItemForm.setBodyMap(mergedBody);
135             request.getSession().removeAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_BODY_KEY);
136         }
137
138         saveToken(request);
139         return mapping.findForward("updateNewsItemBody");
140     }
141 }
Popular Tags