source: trunk/www/admin/news/index.jsp @ 5478

Last change on this file since 5478 was 5478, checked in by Nicklas Nordborg, 12 years ago

References #1545: Publish news as a rss feed

Added servlet RssNewsFeed to serve rss document. It will cache the current news so it should not be too heavy even if clients update often.

The feed includes all public news and the broadcast message that the admin can set in the Administrate -> Broadcast message dialog.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1<%-- $Id: index.jsp 5478 2010-11-05 10:38:08Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23  @author Nicklas
24  @version 2.0
25--%>
26<%@ page pageEncoding="UTF-8" session="false"
27  import="net.sf.basedb.core.SessionControl"
28  import="net.sf.basedb.core.DbControl"
29  import="net.sf.basedb.core.Item"
30  import="net.sf.basedb.core.Include"
31  import="net.sf.basedb.core.News"
32  import="net.sf.basedb.core.ItemQuery"
33  import="net.sf.basedb.core.Permission"
34  import="net.sf.basedb.core.ItemContext"
35  import="net.sf.basedb.core.MultiPermissions"
36  import="net.sf.basedb.core.PermissionDeniedException"
37  import="net.sf.basedb.core.ItemAlreadyExistsException"
38  import="net.sf.basedb.util.RemovableUtil"
39  import="net.sf.basedb.clients.web.Base"
40  import="net.sf.basedb.clients.web.WebException"
41  import="net.sf.basedb.util.Values"
42  import="net.sf.basedb.clients.web.util.HTML"
43  import="net.sf.basedb.util.formatter.Formatter"
44  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
45  import="net.sf.basedb.clients.web.servlet.RssNewsFeed"
46  import="java.util.Date"
47  import="java.util.Set"
48  import="java.util.HashSet"
49  import="java.util.List"
50  import="java.util.ArrayList"
51  import="java.util.Collections"
52%>
53<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
54<%!
55  private static final ItemContext defaultContext = Base.createDefaultContext("newsDate,id", "all");
56  static
57  {
58    defaultContext.setSortDirection(ItemContext.SortDirection.DESC);
59  }
60  private static final Item itemType = Item.NEWS;
61%>
62<%
63final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
64final String ID = sc.getId();
65final String cmd = request.getParameter("cmd");
66final String root = request.getContextPath()+"/";
67final String mode = request.getParameter("mode");
68final String callback = request.getParameter("callback");
69final String itemId = request.getParameter("item_id");
70final String listPage = "list_news.jsp?ID="+ID
71  +(mode == null ? "" : "&mode="+mode)
72  +(callback == null ? "" : "&callback="+callback)
73  +(itemId == null ? "" : "&item_id="+itemId);
74final String viewPage = "view_news.jsp?ID="+ID;
75final String editPage = "edit_news.jsp?ID="+ID;
76
77String forward = null;
78String redirect = null;
79String message = null;
80DbControl dc = null;
81
82try
83{
84  if (cmd == null || "List".equals(cmd))
85  {
86    // Display the list page without updatinging the current context
87    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
88    redirect = listPage;
89  }
90  else if ("UpdateContext".equals(cmd))
91  {
92    // Display the list page after updating the current context from the request parameters
93    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
94    redirect = listPage;
95  }
96  else if ("LoadContext".equals(cmd))
97  {
98    // Display the list page after loading a saved context
99    int contextId = Values.getInt(request.getParameter("context"));
100    Base.loadContext(sc, contextId, defaultContext);
101    redirect = listPage;
102  }
103
104  else if ("ViewItem".equals(cmd))
105  {
106    // Display the view page for a single item
107    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
108    forward = viewPage;
109  }
110  else if ("EditItem".equals(cmd))
111  {
112    // Display the edit page for a single item (should be opened in a popup)
113    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
114    redirect = editPage;
115  }
116  else if ("NewItem".equals(cmd))
117  {
118    // Display the edit page for a new item (should be opened in a popup)
119    if (!sc.hasPermission(Permission.CREATE, itemType))
120    {
121      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
122    }
123    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
124    cc.setId(0);
125    redirect = editPage;
126  }
127  else if ("UpdateItem".equals(cmd))
128  {
129    // Update the properties on an item (will close the popup)
130    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
131    dc = sc.newDbControl();
132    News news = (News)cc.getObject("item");
133    Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
134    Date startDate = dateFormatter.parseString(Values.getStringOrNull(request.getParameter("start_date")));
135    Date newsDate = dateFormatter.parseString(Values.getStringOrNull(request.getParameter("news_date")));
136    if (news == null)
137    {
138      news = News.getNew(dc, startDate, newsDate);
139      message = "News created";
140      dc.saveItem(news);
141    }
142    else
143    {
144      news.setStartDate(startDate);
145      news.setNewsDate(newsDate);
146      dc.reattachItem(news, false);
147      message = "News updated";
148    }
149    news.setName(Values.getStringOrNull(request.getParameter("title")));
150    news.setEndDate(dateFormatter.parseString(Values.getStringOrNull(request.getParameter("end_date"))));
151    news.setDescription(Values.getStringOrNull(request.getParameter("description")));
152    dc.commit();
153    cc.removeObject("item");
154    RssNewsFeed.reset();
155  }
156  else if ("DeleteItem".equals(cmd))
157  {
158    // Delete a single item and then return to the view page
159    dc = sc.newDbControl();
160    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
161    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
162    dc.commit();
163    RssNewsFeed.reset();
164    redirect = viewPage;
165  }
166  else if ("DeleteItems".equals(cmd))
167  {
168    // Delete all selected items on the list page
169    dc = sc.newDbControl();
170    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
171    int numTotal = cc.getSelected().size();
172    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
173    dc.commit();
174    RssNewsFeed.reset();
175    if (numTotal != numRemoved)
176    {
177      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
178    }
179    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
180  }
181  else if ("RestoreItem".equals(cmd))
182  {
183    // Restore a single item and then return to the view page
184    dc = sc.newDbControl();
185    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
186    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
187    dc.commit();
188    RssNewsFeed.reset();
189    redirect = viewPage;
190  }
191  else if ("RestoreItems".equals(cmd))
192  {
193    // Restore all selected items on the list page
194    dc = sc.newDbControl();
195    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
196    int numTotal = cc.getSelected().size();
197    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
198    dc.commit();
199    RssNewsFeed.reset();
200    if (numTotal != numRemoved)
201    {
202      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
203    }
204    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
205  }
206  else if ("ExportItems".equals(cmd))
207  {
208    // Run an export plugin in a list context
209    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
210    final ItemQuery<News> query = News.getQuery();
211    cc.configureQuery(query, true);
212    cc.setQuery(query);
213    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+news";
214  }
215  else if ("ExportItem".equals(cmd))
216  {
217    // Run an export plugin in single-item context
218    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
219    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+news";
220  }
221  else if ("ImportItems".equals(cmd))
222  {
223    // Run an import plugin in a list context
224    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
225    final ItemQuery<News> query = News.getQuery();
226    cc.configureQuery(query, true);
227    cc.setQuery(query);
228    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+news";
229  }
230  else if ("ImportItem".equals(cmd))
231  {
232    // Run an import plugin in single-item context
233    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
234    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+news";
235  }
236  else if ("RunListPlugin".equals(cmd))
237  {
238    // Run another plugin in a list context
239    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
240    final ItemQuery<News> query = News.getQuery();
241    cc.configureQuery(query, true);
242    cc.setQuery(query);
243    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
244  }
245  else if ("RunPlugin".equals(cmd))
246  {
247    // Run another plugin in single-item context
248    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
249    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
250  }
251  else
252  {
253    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
254  }
255}
256finally
257{
258  if (dc != null) dc.close();
259}
260
261if (forward != null)
262{
263  pageContext.forward(forward);
264}
265else if (redirect != null)
266{
267  response.sendRedirect(redirect);
268}
269else if (message == null)
270{
271  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
272}
273else
274{
275  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
276}
277%>
278
Note: See TracBrowser for help on using the repository browser.