source: trunk/www/admin/pluginconfigurations/index.jsp @ 3452

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

Fixes #558 and #568

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 14.5 KB
Line 
1<%-- $Id: index.jsp 3452 2007-06-05 11:58:36Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) Authors contributing to this file.
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.PluginDefinition"
33  import="net.sf.basedb.core.PluginConfiguration"
34  import="net.sf.basedb.core.ItemQuery"
35  import="net.sf.basedb.core.Permission"
36  import="net.sf.basedb.core.ItemContext"
37  import="net.sf.basedb.core.MultiPermissions"
38  import="net.sf.basedb.core.PermissionDeniedException"
39  import="net.sf.basedb.core.ItemAlreadyExistsException"
40  import="net.sf.basedb.core.query.Hql"
41  import="net.sf.basedb.util.RemovableUtil"
42  import="net.sf.basedb.util.ShareableUtil"
43  import="net.sf.basedb.util.OwnableUtil"
44  import="net.sf.basedb.util.JarClassLoader"
45  import="net.sf.basedb.clients.web.Base"
46  import="net.sf.basedb.clients.web.WebException"
47  import="net.sf.basedb.util.Values"
48  import="net.sf.basedb.clients.web.util.HTML"
49  import="java.util.Enumeration"
50  import="java.util.Set"
51  import="java.util.HashSet"
52  import="java.util.List"
53  import="java.util.ArrayList"
54  import="java.util.Collections"
55%>
56<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
57<%!
58  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,pluginDefinition,description,actions");
59  private static final Item itemType = Item.PLUGINCONFIGURATION;
60%>
61<%
62final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
63final String ID = sc.getId();
64final String cmd = request.getParameter("cmd");
65final String root = request.getContextPath()+"/";
66final String mode = request.getParameter("mode");
67final String callback = request.getParameter("callback");
68final String itemId = request.getParameter("item_id");
69final String listPage = "list_configurations.jsp?ID="+ID
70  +(mode == null ? "" : "&mode="+mode)
71  +(callback == null ? "" : "&callback="+callback)
72  +(itemId == null ? "" : "&item_id="+itemId);
73final String viewPage = "view_configuration.jsp?ID="+ID;
74final String editPage = "edit_configuration.jsp?ID="+ID;
75
76String forward = null;
77String redirect = null;
78String message = null;
79DbControl dc = null;
80
81try
82{
83  if (cmd == null || "List".equals(cmd))
84  {
85    // Display the list page without updatinging the current context
86    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
87    redirect = listPage;
88  }
89  else if ("UpdateContext".equals(cmd))
90  {
91    // Display the list page after updating the current context from the request parameters
92    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
93    redirect = listPage;
94  }
95  else if ("LoadContext".equals(cmd))
96  {
97    // Display the list page after loading a saved context
98    int contextId = Values.getInt(request.getParameter("context"));
99    Base.loadContext(sc, contextId, defaultContext);
100    redirect = listPage;
101  }
102
103  else if ("ViewItem".equals(cmd))
104  {
105    // Display the view page for a single item
106    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
107    forward = viewPage;
108  }
109  else if ("EditItem".equals(cmd))
110  {
111    // Display the edit page for a single item (should be opened in a popup)
112    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
113    redirect = editPage;
114  }
115  else if ("NewItem".equals(cmd))
116  {
117    // Display the edit page for a new item (should be opened in a popup)
118    if (!sc.hasPermission(Permission.CREATE, itemType))
119    {
120      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
121    }
122    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
123    cc.setId(0);
124    forward = editPage;
125  }
126  else if ("CopyItem".equals(cmd))
127  {
128    // Display the edit page for new item(copied from an existing item)
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    redirect = editPage + "&cmd="+ cmd;
135  }
136  else if ("UpdateItem".equals(cmd))
137  {
138    // Update the properties on an item (will close the popup)
139    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
140    final int maxRecent = Base.getMaxRecent(sc);
141    dc = sc.newDbControl();
142    PluginConfiguration configuration = (PluginConfiguration)cc.getObject("item");
143    boolean configure = Values.getBoolean(request.getParameter("configure"));
144    if (configuration == null)
145    {
146      int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"), -1);
147      PluginDefinition plugin = PluginDefinition.getById(dc, pluginDefinitionId);
148      if (!plugin.supportsConfigurations())
149      {
150        throw new WebException("popup", "Configurations are not supported",
151          "The plugin {1} doesn't support configurations.", HTML.encodeTags(plugin.getName()));
152      }
153      if (plugin != null) cc.setRecent(plugin, maxRecent);
154      configuration = PluginConfiguration.getNew(dc, plugin);
155      int copyFromId = Values.getInt(request.getParameter("copyfrom"), 0);
156      if (copyFromId > 0)
157      {
158        PluginConfiguration copyFrom = PluginConfiguration.getById(dc, copyFromId);
159        configuration.copyParametersFrom(copyFrom);
160      }
161      message = "Configuration created";
162      dc.saveItem(configuration);
163    }
164    else
165    {
166      dc.reattachItem(configuration);
167      message = "Configuration updated";
168    }
169    configuration.setName(Values.getStringOrNull(request.getParameter("name")));
170    configuration.setDescription(Values.getStringOrNull(request.getParameter("description")));
171    dc.commit();
172    cc.removeObject("item");
173    if (configure)
174    {
175      redirect = "../../common/plugin/index.jsp?ID=" + ID + 
176        "&cmd=ConfigurePlugin&pluginconfiguration_id=" + configuration.getId();
177    }
178  }
179  else if ("DeleteItem".equals(cmd))
180  {
181    // Delete a single item and then return to the view page
182    dc = sc.newDbControl();
183    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
184    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
185    dc.commit();
186    redirect = viewPage;
187  }
188  else if ("DeleteItems".equals(cmd))
189  {
190    // Delete all selected items on the list page
191    dc = sc.newDbControl();
192    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
193    int numTotal = cc.getSelected().size();
194    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
195    dc.commit();
196    if (numTotal != numRemoved)
197    {
198      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
199    }
200    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
201  }
202  else if ("RestoreItem".equals(cmd))
203  {
204    // Restore a single item and then return to the view page
205    dc = sc.newDbControl();
206    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
207    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
208    dc.commit();
209    redirect = viewPage;
210  }
211  else if ("RestoreItems".equals(cmd))
212  {
213    // Restore all selected items on the list page
214    dc = sc.newDbControl();
215    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
216    int numTotal = cc.getSelected().size();
217    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
218    dc.commit();
219    if (numTotal != numRemoved)
220    {
221      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
222    }
223    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
224  }
225  else if ("ShareItem".equals(cmd))
226  {
227    // Display a popup window for sharing a single item
228    dc = sc.newDbControl();
229    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
230    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
231    dc.close();
232    cc.setObject("MultiPermissions", permissions);
233    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
234  }
235  else if ("ShareItems".equals(cmd))
236  {
237    // Display a popup window for sharing all selected items on the list page
238    dc = sc.newDbControl();
239    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
240    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
241    dc.close();
242    cc.setObject("MultiPermissions", permissions);
243    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
244  }
245  else if ("TakeOwnershipOfItem".equals(cmd))
246  {
247    // Take ownership a single item and then return to the view page
248    dc = sc.newDbControl();
249    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
250    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
251    dc.commit();
252    redirect = viewPage;
253  }
254  else if ("TakeOwnershipOfItems".equals(cmd))
255  {
256    // Take ownership all selected items on the list page
257    dc = sc.newDbControl();
258    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
259    int numTotal = cc.getSelected().size();
260    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
261    dc.commit();
262    if (numTotal != numOwned)
263    {
264      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
265    }
266    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
267  }
268  else if ("ExportItems".equals(cmd))
269  {
270    // Run an export plugin in a list context
271    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
272    final ItemQuery<PluginConfiguration> query = PluginConfiguration.getQuery();
273    query.join(Hql.innerJoin("pluginDefinition", Item.PLUGINDEFINITION.getAlias()));
274    query.join(Hql.leftJoin(Item.PLUGINDEFINITION.getAlias(), "guiContexts", "ctx", null, false));
275    query.setDistinct(true);
276    cc.configureQuery(query, true);
277    cc.setQuery(query);
278    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+plugin+configurations";
279  }
280  else if ("ExportItem".equals(cmd))
281  {
282    // Run an export plugin in single-item context
283    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
284    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+plugin+configuration";
285  }
286  else if ("ImportItems".equals(cmd))
287  {
288    // Run an import plugin in a list context
289    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
290    final ItemQuery<PluginConfiguration> query = PluginConfiguration.getQuery();
291    query.join(Hql.innerJoin("pluginDefinition", Item.PLUGINDEFINITION.getAlias()));
292    query.join(Hql.leftJoin(Item.PLUGINDEFINITION.getAlias(), "guiContexts", "ctx", null, false));
293    query.setDistinct(true);
294    cc.configureQuery(query, true);
295    cc.setQuery(query);
296    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+plugin+configurations";
297  }
298  else if ("ImportItem".equals(cmd))
299  {
300    // Run an import plugin in single-item context
301    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
302    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+plugin+configuration";
303  }
304  else if ("RunListPlugin".equals(cmd))
305  {
306    // Run another plugin in a list context
307    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
308    final ItemQuery<PluginConfiguration> query = PluginConfiguration.getQuery();
309    query.join(Hql.innerJoin("pluginDefinition", Item.PLUGINDEFINITION.getAlias()));
310    query.join(Hql.leftJoin(Item.PLUGINDEFINITION.getAlias(), "guiContexts", "ctx", null, false));
311    query.setDistinct(true);
312    cc.configureQuery(query, true);
313    cc.setQuery(query);
314    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
315  }
316  else if ("RunPlugin".equals(cmd))
317  {
318    // Run another plugin in single-item context
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
321  }
322  else if ("UnloadItems".equals(cmd))
323  {
324    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
325    dc = sc.newDbControl();
326    for (int id : cc.getSelected())
327    {
328      PluginDefinition pd = PluginDefinition.getById(dc, id);
329      if (pd.getJarPath() != null)
330      {
331        JarClassLoader.unload(pd.getJarPath());
332      }
333    }
334    dc.close();
335    redirect = listPage;
336  }
337  else if ("UnloadItem".equals(cmd))
338  {
339    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
340    dc = sc.newDbControl();
341    PluginDefinition pd = PluginDefinition.getById(dc, cc.getId());
342    if (pd.getJarPath() != null)
343    {
344      JarClassLoader.unload(pd.getJarPath());
345    }
346    dc.close();
347    redirect = viewPage;
348  }
349  else
350  {
351    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
352  }
353}
354finally
355{
356  if (dc != null) dc.close();
357}
358
359if (forward != null)
360{
361  pageContext.forward(forward);
362}
363else if (redirect != null)
364{
365  response.sendRedirect(redirect);
366}
367else if (message == null)
368{
369  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
370}
371else
372{
373  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
374}
375%>
376
Note: See TracBrowser for help on using the repository browser.