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

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

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

It is now working from dynamic queries as well.

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