KickJava   Java API By Example, From Geeks To Geeks.

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


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.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * <p>Exposes bean to request to view its properties on JSP page (news item is
38  * to be viewed)
39  * </p>
40  * <p><a HREF="ViewNewsItemAction.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  * @version $Revision: 1.10 $ $Date: 2006/03/10 17:10:32 $
45  * @struts.action path="/news/viewItem"
46  * name="newsItemForm"
47  * scope="request"
48  * validate="false"
49  * roles="news-item-view"
50  * @struts.action-forward name="viewNewsItem"
51  * path=".news.viewItem"
52  * @struts.action-forward name="listNewsItems"
53  * path="/news/listItems.do"
54  * redirect="true"
55  */

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

66     public ActionForward execute(ActionMapping mapping, ActionForm form,
67                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
68         NewsItemForm newsItemForm = (NewsItemForm) form;
69         Long JavaDoc newsItemId = null;
70         if ( newsItemForm.getId() != null ) {
71             newsItemId = Long.valueOf(newsItemForm.getId());
72         } else if ( request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_ID_KEY) != null ) {
73             newsItemId = (Long JavaDoc) request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_ID_KEY);
74         } else {
75             if ( log.isWarnEnabled() ) {
76                 log.warn("Missing content page ID. Returning to list...");
77             }
78             return mapping.findForward("listNewsItems");
79         }
80
81         NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
82         NewsItem newsItem = newsManager.retrieveNewsItem(newsItemId);
83         if ( newsItem == null ) {
84             // newsItem not found. it might be deleted by someone else
85
ActionMessages errors = new ActionMessages();
86             errors.add("newsItemNotFound", new ActionMessage("news.errors.notFound"));
87             saveErrors(request, errors);
88             return mapping.findForward("listNewsItems");
89         }
90
91         List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
92
93         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
94
95         request.setAttribute("newsItem", newsItem);
96         return mapping.findForward("viewNewsItem");
97     }
98 }
Popular Tags