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

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

References #1015: Add reporter list from tables that list reporters/reporter info

It is now possible to create reporter lists from features, wells and raw data, eg. all DataQuery?-based item types. Creating the lists using queries from the dynamic database needs a different solution.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 15.7 KB
Line 
1<%-- $Id: index.jsp 4305 2008-05-21 11:11:20Z 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      dc.saveItem(rl);
142     
143      // Add individual reporters to the list
144      String which = request.getParameter("which");
145      if (which != null)
146      {
147        Item fromContext = Item.valueOf(request.getParameter("fromContext"));
148        String subContext = Values.getString(request.getParameter("subContext"), "");
149        DataQuery<ReporterData> query = 
150          (DataQuery<ReporterData>)sc.getCurrentContext(fromContext, subContext).getQuery();
151        if ("all".equals(which))
152        {
153          query.setFirstResult(0);
154          query.setMaxResults(-1);
155        }
156        else if ("selected".equals(which))
157        {
158          query.setFirstResult(0);
159          query.setMaxResults(-1);
160          Integer[] itemIds = Values.getInt(request.getParameter("items").split(","));
161          query.restrict(
162            Restrictions.in(
163              Hql.property("id"),
164              Expressions.parameter("_selected_")
165            )
166          );
167          query.setParameter("_selected_", Arrays.asList(itemIds), Type.INT);     
168        }
169        // else -- no modifications to the query mean that we only get the current page
170       
171        DataResultIterator<ReporterData> result = query.iterate(dc);
172        while (result.hasNext())
173        {
174          ReporterData reporter = result.next();
175          if (reporter != null) rl.addReporter(reporter, null);
176        }
177      }
178     
179      // Add reporters from other lists
180      String create = Values.getStringOrNull(request.getParameter("create"));
181      if (create != null)
182      {
183        List<ReporterList> lists = new ArrayList<ReporterList>(cc.getSelected().size());
184        for (int listId : cc.getSelected())
185        {
186          lists.add(ReporterList.getById(dc, listId));
187        }
188        if ("union".equals(create))
189        {
190          rl.addUnion(lists);
191        }
192        else if ("intersection".equals(create))
193        {
194          rl.addIntersection(lists);
195        }
196        else if ("count".equals(create))
197        {
198          int minCount = Values.getInt(request.getParameter("minCount"), 1);
199          rl.addIfPresentIn(minCount, lists.size(), lists);
200        }
201      }
202      message = "Reporter list created with " + rl.getSize() + " reporter(s)";
203    }
204    else
205    {
206      dc.reattachItem(rl);
207      message = "Reporter list updated";
208    }
209    rl.setName(Values.getStringOrNull(request.getParameter("name")));
210    rl.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
211    rl.setDescription(Values.getStringOrNull(request.getParameter("description")));
212   
213    dc.commit();
214    cc.removeObject("item");
215  }
216  else if ("DeleteItem".equals(cmd))
217  {
218    // Delete a single item and then return to the view page
219    dc = sc.newDbControl();
220    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
221    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
222    dc.commit();
223    redirect = viewPage;
224  }
225  else if ("DeleteItems".equals(cmd))
226  {
227    // Delete all selected items on the list page
228    dc = sc.newDbControl();
229    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
230    int numTotal = cc.getSelected().size();
231    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
232    dc.commit();
233    if (numTotal != numRemoved)
234    {
235      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
236    }
237    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
238  }
239  else if ("RestoreItem".equals(cmd))
240  {
241    // Restore a single item and then return to the view page
242    dc = sc.newDbControl();
243    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
244    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
245    dc.commit();
246    redirect = viewPage;
247  }
248  else if ("RestoreItems".equals(cmd))
249  {
250    // Restore all selected items on the list page
251    dc = sc.newDbControl();
252    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
253    int numTotal = cc.getSelected().size();
254    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
255    dc.commit();
256    if (numTotal != numRemoved)
257    {
258      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
259    }
260    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
261  }
262  else if ("ShareItem".equals(cmd))
263  {
264    // Display a popup window for sharing a single item
265    dc = sc.newDbControl();
266    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
267    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
268    dc.close();
269    cc.setObject("MultiPermissions", permissions);
270    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
271  }
272  else if ("ShareItems".equals(cmd))
273  {
274    // Display a popup window for sharing all selected items on the list page
275    dc = sc.newDbControl();
276    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
277    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
278    dc.close();
279    cc.setObject("MultiPermissions", permissions);
280    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
281  }
282  else if ("TakeOwnershipOfItem".equals(cmd))
283  {
284    // Take ownership a single item and then return to the view page
285    dc = sc.newDbControl();
286    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
287    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
288    dc.commit();
289    redirect = viewPage;
290  }
291  else if ("TakeOwnershipOfItems".equals(cmd))
292  {
293    // Take ownership all selected items on the list page
294    dc = sc.newDbControl();
295    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
296    int numTotal = cc.getSelected().size();
297    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
298    dc.commit();
299    if (numTotal != numOwned)
300    {
301      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
302    }
303    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
304  }
305  else if ("ExportItems".equals(cmd))
306  {
307    // Run an export plugin in a list context
308    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
309    final ItemQuery<ReporterList> query = ReporterList.getQuery();
310    cc.configureQuery(query, true);
311    cc.setQuery(query);
312    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+reporter+lists";
313  }
314  else if ("ExportItem".equals(cmd))
315  {
316    // Run an export plugin in single-item context
317    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
318    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+reporter+list";
319  }
320  else if ("ImportItems".equals(cmd))
321  {
322    // Run an import plugin in a list context
323    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
324    final ItemQuery<ReporterList> query = ReporterList.getQuery();
325    cc.configureQuery(query, true);
326    cc.setQuery(query);
327    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+reporter+lists";
328  }
329  else if ("ImportItem".equals(cmd))
330  {
331    // Run an import plugin in single-item context
332    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
333    // Set file type filter
334    ItemContext fileContext = sc.getCurrentContext(Item.FILE);
335    fileContext.setPropertyFilter(FileType.getPropertyFilter(FileType.REPORTER));
336    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+reporter+list";
337  }
338  else if ("RunListPlugin".equals(cmd))
339  {
340    // Run another plugin in a list context
341    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
342    final ItemQuery<ReporterList> query = ReporterList.getQuery();
343    cc.configureQuery(query, true);
344    cc.setQuery(query);
345    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
346  }
347  else if ("RunPlugin".equals(cmd))
348  {
349    // Run another plugin in single-item context
350    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
351    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
352  }
353  else if ("MergeItem".equals(cmd))
354  {
355    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
356    dc = sc.newDbControl();
357    ReporterList rl = (ReporterList)cc.getObject("item");
358    dc.reattachItem(rl);
359   
360    String mergeType = request.getParameter("mergeType");
361    String sourceMerge = request.getParameter("sourceMerge");
362   
363    String[] sourceListIds = Values.getString(request.getParameter("sourceLists")).split(",");
364    List<ReporterList> sourceLists = new ArrayList<ReporterList>(sourceListIds.length);
365    for (int i = 0; i < sourceListIds.length; ++i)
366    {
367      int listId = Values.getInt(sourceListIds[i], -1);
368      if (listId != -1) sourceLists.add(ReporterList.getById(dc, listId));
369    }
370   
371    int numAdded = 0;
372    int numRemoved = 0;
373    if ("union".equals(mergeType))
374    {
375      if ("union".equals(sourceMerge))
376      {
377        numAdded = rl.addUnion(sourceLists);
378      }
379      else if ("intersection".equals(sourceMerge))
380      {
381        numAdded = rl.addIntersection(sourceLists);
382      }
383      message = numAdded + " reporters added to this list";
384    }
385    else if ("intersection".equals(mergeType))
386    {
387      if ("union".equals(sourceMerge))
388      {
389        numRemoved = rl.retainUnion(sourceLists);
390      }
391      else if ("intersection".equals(sourceMerge))
392      {
393        numRemoved = rl.retainIntersection(sourceLists);
394      }
395      message = numRemoved + " reporters removed from this list";
396    }
397    else if ("complement".equals(mergeType))
398    {
399      if ("union".equals(sourceMerge))
400      {
401        numRemoved = rl.removeUnion(sourceLists);
402      }
403      else if ("intersection".equals(sourceMerge))
404      {
405        numRemoved = rl.removeIntersection(sourceLists);
406      }
407      message = numRemoved + " reporters removed from this list";
408    }
409    dc.commit();
410  }
411  else
412  {
413    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
414  }
415}
416finally
417{
418  if (dc != null) dc.close();
419}
420
421if (forward != null)
422{
423  pageContext.forward(forward);
424}
425else if (redirect != null)
426{
427  response.sendRedirect(redirect);
428}
429else if (message == null)
430{
431  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
432}
433else
434{
435  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
436}
437%>
438
Note: See TracBrowser for help on using the repository browser.