KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.util.QueryInfo;
20 import com.blandware.atleap.model.news.NewsItem;
21 import com.blandware.atleap.service.news.NewsManager;
22 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
23 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
24 import com.blandware.atleap.webapp.taglib.core.grid.util.SortField;
25 import com.blandware.atleap.webapp.util.core.WebappConstants;
26 import com.blandware.atleap.webapp.util.core.WebappUtil;
27 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.ListIterator JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * <p>Forward to the page with list of news items
43  * </p>
44  * <p><a HREF="ListNewsItemsAction.java.htm"><i>View Source</i></a></p>
45  * <p/>
46  *
47  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
48  * @version $Revision: 1.10 $ $Date: 2006/03/10 17:10:31 $
49  * @struts.action path="/news/listItems"
50  * name="newsItemForm"
51  * validate="false"
52  * roles="news-item-list"
53  * @struts.action-forward name="listNewsItems"
54  * path=".news.listItems"
55  */

56 public final class ListNewsItemsAction extends BaseGridAction {
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
69
70         Grid newsGrid = getGridByName(NewsModuleWebConstants.NEWS_ITEMS_GRID, request.getSession());
71         if ( newsGrid == null ) {
72             newsGrid = new Grid(NewsModuleWebConstants.NEWS_ITEMS_GRID);
73         }
74
75         // catch parameters related to sorting
76
String JavaDoc sortFieldName = request.getParameter("sortField");
77         String JavaDoc orderByClause = new String JavaDoc();
78         if ( NewsModuleWebConstants.NEWS_ITEMS_GRID.equals(request.getParameter("gridName")) && sortFieldName != null && sortFieldName.length() > 0 ) {
79             SortField sortField = newsGrid.getSortFieldByFieldName(sortFieldName);
80             if ( sortField == null ) {
81                 sortField = new SortField(sortFieldName);
82                 newsGrid.addSortField(sortField);
83             } else {
84                 sortField.reverseOrder();
85                 newsGrid.addSortField(sortField);
86             }
87             orderByClause = newsGrid.getOrderByClause();
88             newsGrid.getSortFieldByFieldName(sortFieldName).reverseOrder();
89         }
90
91         saveGrid(newsGrid, request.getSession());
92
93         QueryInfo queryInfo = new QueryInfo();
94         queryInfo.setWhereClause(newsGrid.getWhereClause());
95         queryInfo.setOrderByClause(orderByClause);
96
97         Map JavaDoc queryParameters = new HashMap JavaDoc();
98         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
99         queryParameters.put("localeIdentifier", locale.getLanguage());
100         queryInfo.setQueryParameters(queryParameters);
101
102         NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
103         List JavaDoc newsItems = newsManager.listNewsItems(queryInfo).asList();
104
105         for ( ListIterator JavaDoc i = newsItems.listIterator(); i.hasNext(); ) {
106             NewsItem newsItem = (NewsItem) i.next();
107             NewsItem tmp = new NewsItem();
108             WebappUtil.copyProperties(tmp, newsItem, request);
109             tmp.setUri(WebappUtil.getActionMappingURL(newsItem.getUri(), null, request, WebappConstants.URL_TYPE_CONTEXT_RELATIVE));
110             i.set(tmp);
111         }
112
113         request.setAttribute(NewsModuleWebConstants.NEWS_ITEMS_COLLECTION_KEY, newsItems);
114
115         // save transaction token in request
116
saveToken(request);
117         return mapping.findForward("listNewsItems");
118     }
119 }
Popular Tags