source: trunk/www/views/reporterlists/index.jsp @ 4301

Last change on this file since 4301 was 4301, checked in by Nicklas Nordborg, 15 years ago

References #1019: Reporter list manager - Create reporter list using combinations of existing reporter lists

A lot of functionality has now been added. It should be tested by end-users.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 15.4 KB
Line 
1<%-- $Id: index.jsp 4301 2008-05-16 09:40:37Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2006 Jari Hakkinen, Nicklas Nordborg, Martin Svensson
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA  02111-1307, USA.
22  ------------------------------------------------------------------
23
24  @author Nicklas
25  @version 2.0
26--%>
27<%@ page session="false"
28  import="net.sf.basedb.core.SessionControl"
29  import="net.sf.basedb.core.DbControl"
30  import="net.sf.basedb.core.Item"
31  import="net.sf.basedb.core.Include"
32  import="net.sf.basedb.core.ReporterList"
33  import="net.sf.basedb.core.FileType"
34  import="net.sf.basedb.core.Permission"
35  import="net.sf.basedb.core.Type"
36  import="net.sf.basedb.core.ItemContext"
37  import="net.sf.basedb.core.DataQuery"
38  import="net.sf.basedb.core.ItemQuery"
39  import="net.sf.basedb.core.DataResultIterator"
40  import="net.sf.basedb.core.MultiPermissions"
41  import="net.sf.basedb.core.PermissionDeniedException"
42  import="net.sf.basedb.core.ItemAlreadyExistsException"
43  import="net.sf.basedb.core.data.ReporterData"
44  import="net.sf.basedb.core.query.Restrictions"
45  import="net.sf.basedb.core.query.Expressions"
46  import="net.sf.basedb.core.query.Hql"
47  import="net.sf.basedb.util.RemovableUtil"
48  import="net.sf.basedb.util.ShareableUtil"
49  import="net.sf.basedb.util.OwnableUtil"
50  import="net.sf.basedb.clients.web.Base"
51  import="net.sf.basedb.clients.web.WebException"
52  import="net.sf.basedb.util.Values"
53  import="net.sf.basedb.clients.web.util.HTML"
54  import="java.util.Enumeration"
55  import="java.util.Set"
56  import="java.util.HashSet"
57  import="java.util.List"
58  import="java.util.ArrayList"
59  import="java.util.Collections"
60  import="java.util.Arrays"
61%>
62<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
63<%!
64  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,externalId,description");
65  private static final Item itemType = Item.REPORTERLIST;
66%>
67<%
68final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
69final String ID = sc.getId();
70final String cmd = request.getParameter("cmd");
71final String root = request.getContextPath()+"/";
72final String mode = request.getParameter("mode");
73final String callback = request.getParameter("callback");
74final String itemId = request.getParameter("item_id");
75final String listPage = "list_reporterlists.jsp?ID="+ID
76  +(mode == null ? "" : "&mode="+mode)
77  +(callback == null ? "" : "&callback="+callback)
78  +(itemId == null ? "" : "&item_id="+itemId);
79final String viewPage = "view_reporterlist.jsp?ID="+ID;
80final String editPage = "edit_reporterlist.jsp?ID="+ID;
81
82String forward = null;
83String redirect = null;
84String message = null;
85DbControl dc = null;
86
87try
88{
89  if (cmd == null || "List".equals(cmd))
90  {
91    // Display the list page without updatinging the current context
92    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
93    redirect = listPage;
94  }
95  else if ("UpdateContext".equals(cmd))
96  {
97    // Display the list page after updating the current context from the request parameters
98    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
99    redirect = listPage;
100  }
101  else if ("LoadContext".equals(cmd))
102  {
103    // Display the list page after loading a saved context
104    int contextId = Values.getInt(request.getParameter("context"));
105    Base.loadContext(sc, contextId, defaultContext);
106    redirect = listPage;
107  }
108
109  else if ("ViewItem".equals(cmd))
110  {
111    // Display the view page for a single item
112    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
113    forward = viewPage;
114  }
115  else if ("EditItem".equals(cmd))
116  {
117    // Display the edit page for a single item (should be opened in a popup)
118    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
119    redirect = editPage;
120  }
121  else if ("NewItem".equals(cmd))
122  {
123    // Display the edit page for a new item (should be opened in a popup)
124    if (!sc.hasPermission(Permission.CREATE, itemType))
125    {
126      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
127    }
128    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
129    cc.setId(0);
130    forward = editPage;
131  }
132  else if ("UpdateItem".equals(cmd))
133  {
134    // Update the properties on an item (will close the popup)
135    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
136    dc = sc.newDbControl();
137    ReporterList rl = (ReporterList)cc.getObject("item");
138    if (rl == null)
139    {
140      rl = ReporterList.getNew(dc);
141      message = "Reporter list created";
142      dc.saveItem(rl);
143     
144      // Add individual reporters to the list
145      String which = request.getParameter("which");
146      if (which != null)
147      {
148        DataQuery<ReporterData> query = (DataQuery<ReporterData>)sc.getCurrentContext(Item.REPORTER).getQuery();
149        if ("all".equals(which))
150        {
151          query.setFirstResult(0);
152          query.setMaxResults(-1);
153        }
154        else if ("selected".equals(which))
155        {
156          query.setFirstResult(0);
157          query.setMaxResults(-1);
158          Integer[] itemIds = Values.getInt(request.getParameter("items").split(","));
159          query.restrict(
160            Restrictions.in(
161              Hql.property("id"),
162              Expressions.parameter("_selected_")
163            )
164          );
165          query.setParameter("_selected_", Arrays.asList(itemIds), Type.INT);     
166        }
167       
168        DataResultIterator<ReporterData> result = query.iterate(dc);
169        while (result.hasNext())
170        {
171          rl.addReporter(result.next(), null);
172        }
173      }
174     
175      // Add reporters from other lists
176      String create = Values.getStringOrNull(request.getParameter("create"));
177      if (create != null)
178      {
179        List<ReporterList> lists = new ArrayList<ReporterList>(cc.getSelected().size());
180        for (int listId : cc.getSelected())
181        {
182          lists.add(ReporterList.getById(dc, listId));
183        }
184        if ("union".equals(create))
185        {
186          rl.addUnion(lists);
187        }
188        else if ("intersection".equals(create))
189        {
190          rl.addIntersection(lists);
191        }
192        else if ("count".equals(create))
193        {
194          int minCount = Values.getInt(request.getParameter("minCount"), 1);
195          rl.addIfPresentIn(minCount, lists.size(), lists);
196        }
197      }
198    }
199    else
200    {
201      dc.reattachItem(rl);
202      message = "Reporter list updated";
203    }
204    rl.setName(Values.getStringOrNull(request.getParameter("name")));
205    rl.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
206    rl.setDescription(Values.getStringOrNull(request.getParameter("description")));
207   
208    dc.commit();
209    cc.removeObject("item");
210  }
211  else if ("DeleteItem".equals(cmd))
212  {
213    // Delete a single item and then return to the view page
214    dc = sc.newDbControl();
215    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
216    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
217    dc.commit();
218    redirect = viewPage;
219  }
220  else if ("DeleteItems".equals(cmd))
221  {
222    // Delete all selected items on the list page
223    dc = sc.newDbControl();
224    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
225    int numTotal = cc.getSelected().size();
226    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
227    dc.commit();
228    if (numTotal != numRemoved)
229    {
230      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
231    }
232    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
233  }
234  else if ("RestoreItem".equals(cmd))
235  {
236    // Restore a single item and then return to the view page
237    dc = sc.newDbControl();
238    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
239    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
240    dc.commit();
241    redirect = viewPage;
242  }
243  else if ("RestoreItems".equals(cmd))
244  {
245    // Restore all selected items on the list page
246    dc = sc.newDbControl();
247    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
248    int numTotal = cc.getSelected().size();
249    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
250    dc.commit();
251    if (numTotal != numRemoved)
252    {
253      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
254    }
255    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
256  }
257  else if ("ShareItem".equals(cmd))
258  {
259    // Display a popup window for sharing a single item
260    dc = sc.newDbControl();
261    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
262    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
263    dc.close();
264    cc.setObject("MultiPermissions", permissions);
265    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
266  }
267  else if ("ShareItems".equals(cmd))
268  {
269    // Display a popup window for sharing all selected items on the list page
270    dc = sc.newDbControl();
271    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
272    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
273    dc.close();
274    cc.setObject("MultiPermissions", permissions);
275    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
276  }
277  else if ("TakeOwnershipOfItem".equals(cmd))
278  {
279    // Take ownership a single item and then return to the view page
280    dc = sc.newDbControl();
281    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
282    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
283    dc.commit();
284    redirect = viewPage;
285  }
286  else if ("TakeOwnershipOfItems".equals(cmd))
287  {
288    // Take ownership all selected items on the list page
289    dc = sc.newDbControl();
290    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
291    int numTotal = cc.getSelected().size();
292    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
293    dc.commit();
294    if (numTotal != numOwned)
295    {
296      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
297    }
298    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
299  }
300  else if ("ExportItems".equals(cmd))
301  {
302    // Run an export plugin in a list context
303    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
304    final ItemQuery<ReporterList> query = ReporterList.getQuery();
305    cc.configureQuery(query, true);
306    cc.setQuery(query);
307    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+reporter+lists";
308  }
309  else if ("ExportItem".equals(cmd))
310  {
311    // Run an export plugin in single-item context
312    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
313    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+reporter+list";
314  }
315  else if ("ImportItems".equals(cmd))
316  {
317    // Run an import plugin in a list context
318    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
319    final ItemQuery<ReporterList> query = ReporterList.getQuery();
320    cc.configureQuery(query, true);
321    cc.setQuery(query);
322    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+reporter+lists";
323  }
324  else if ("ImportItem".equals(cmd))
325  {
326    // Run an import plugin in single-item context
327    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
328    // Set file type filter
329    ItemContext fileContext = sc.getCurrentContext(Item.FILE);
330    fileContext.setPropertyFilter(FileType.getPropertyFilter(FileType.REPORTER));
331    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+reporter+list";
332  }
333  else if ("RunListPlugin".equals(cmd))
334  {
335    // Run another plugin in a list context
336    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
337    final ItemQuery<ReporterList> query = ReporterList.getQuery();
338    cc.configureQuery(query, true);
339    cc.setQuery(query);
340    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
341  }
342  else if ("RunPlugin".equals(cmd))
343  {
344    // Run another plugin in single-item context
345    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
346    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
347  }
348  else if ("MergeItem".equals(cmd))
349  {
350    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
351    dc = sc.newDbControl();
352    ReporterList rl = (ReporterList)cc.getObject("item");
353    dc.reattachItem(rl);
354   
355    String mergeType = request.getParameter("mergeType");
356    String sourceMerge = request.getParameter("sourceMerge");
357   
358    String[] sourceListIds = Values.getString(request.getParameter("sourceLists")).split(",");
359    List<ReporterList> sourceLists = new ArrayList<ReporterList>(sourceListIds.length);
360    for (int i = 0; i < sourceListIds.length; ++i)
361    {
362      int listId = Values.getInt(sourceListIds[i], -1);
363      if (listId != -1) sourceLists.add(ReporterList.getById(dc, listId));
364    }
365   
366    int numAdded = 0;
367    int numRemoved = 0;
368    if ("union".equals(mergeType))
369    {
370      if ("union".equals(sourceMerge))
371      {
372        numAdded = rl.addUnion(sourceLists);
373      }
374      else if ("intersection".equals(sourceMerge))
375      {
376        numAdded = rl.addIntersection(sourceLists);
377      }
378      message = numAdded + " reporters added to this list";
379    }
380    else if ("intersection".equals(mergeType))
381    {
382      if ("union".equals(sourceMerge))
383      {
384        numRemoved = rl.retainUnion(sourceLists);
385      }
386      else if ("intersection".equals(sourceMerge))
387      {
388        numRemoved = rl.retainIntersection(sourceLists);
389      }
390      message = numRemoved + " reporters removed from this list";
391    }
392    else if ("complement".equals(mergeType))
393    {
394      if ("union".equals(sourceMerge))
395      {
396        numRemoved = rl.removeUnion(sourceLists);
397      }
398      else if ("intersection".equals(sourceMerge))
399      {
400        numRemoved = rl.removeIntersection(sourceLists);
401      }
402      message = numRemoved + " reporters removed from this list";
403    }
404    dc.commit();
405  }
406  else
407  {
408    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
409  }
410}
411finally
412{
413  if (dc != null) dc.close();
414}
415
416if (forward != null)
417{
418  pageContext.forward(forward);
419}
420else if (redirect != null)
421{
422  response.sendRedirect(redirect);
423}
424else if (message == null)
425{
426  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
427}
428else
429{
430  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
431}
432%>
433
Note: See TracBrowser for help on using the repository browser.