source: trunk/www/admin/plugindefinitions/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: 15.3 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.JobAgent"
34  import="net.sf.basedb.core.JobAgentSettings"
35  import="net.sf.basedb.core.ItemQuery"
36  import="net.sf.basedb.core.ItemResultList"
37  import="net.sf.basedb.core.Permission"
38  import="net.sf.basedb.core.ItemContext"
39  import="net.sf.basedb.core.MultiPermissions"
40  import="net.sf.basedb.core.PermissionDeniedException"
41  import="net.sf.basedb.core.ItemAlreadyExistsException"
42  import="net.sf.basedb.core.RoleKey"
43  import="net.sf.basedb.core.plugin.Plugin"
44  import="net.sf.basedb.core.query.Hql"
45  import="net.sf.basedb.util.RemovableUtil"
46  import="net.sf.basedb.util.ShareableUtil"
47  import="net.sf.basedb.util.OwnableUtil"
48  import="net.sf.basedb.util.JarClassLoader"
49  import="net.sf.basedb.clients.web.Base"
50  import="net.sf.basedb.clients.web.PermissionUtil"
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%>
61<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
62<%!
63  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,className,description");
64  private static final Item itemType = Item.PLUGINDEFINITION;
65%>
66<%
67final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
68final String ID = sc.getId();
69final String cmd = request.getParameter("cmd");
70final String root = request.getContextPath()+"/";
71final String mode = request.getParameter("mode");
72final String callback = request.getParameter("callback");
73final String itemId = request.getParameter("item_id");
74final String listPage = "list_plugins.jsp?ID="+ID
75  +(mode == null ? "" : "&mode="+mode)
76  +(callback == null ? "" : "&callback="+callback)
77  +(itemId == null ? "" : "&item_id="+itemId);
78final String viewPage = "view_plugin.jsp?ID="+ID;
79final String editPage = "edit_plugin.jsp?ID="+ID;
80
81String forward = null;
82String redirect = null;
83String message = null;
84DbControl dc = null;
85
86try
87{
88  if (cmd == null || "List".equals(cmd))
89  {
90    // Display the list page without updatinging the current context
91    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
92    redirect = listPage;
93  }
94  else if ("UpdateContext".equals(cmd))
95  {
96    // Display the list page after updating the current context from the request parameters
97    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
98    redirect = listPage;
99  }
100  else if ("LoadContext".equals(cmd))
101  {
102    // Display the list page after loading a saved context
103    int contextId = Values.getInt(request.getParameter("context"));
104    Base.loadContext(sc, contextId, defaultContext);
105    redirect = listPage;
106  }
107
108  else if ("ViewItem".equals(cmd))
109  {
110    // Display the view page for a single item
111    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
112    forward = viewPage;
113  }
114  else if ("EditItem".equals(cmd))
115  {
116    // Display the edit page for a single item (should be opened in a popup)
117    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
118    forward = editPage;
119  }
120  else if ("NewItem".equals(cmd))
121  {
122    // Display the edit page for a new item (should be opened in a popup)
123    if (!sc.hasPermission(Permission.CREATE, itemType))
124    {
125      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
126    }
127    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
128    cc.setId(0);
129    redirect = editPage;
130  }
131  else if ("UpdateItem".equals(cmd))
132  {
133    // Update the properties on an item (will close the popup)
134    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
135    dc = sc.newDbControl();
136    PluginDefinition plugin = (PluginDefinition)cc.getObject("item");
137    String className = Values.getStringOrNull(request.getParameter("className"));
138    String jarPath = Values.getStringOrNull(request.getParameter("jarPath"));
139    int usePermissions = Values.getInt(request.getParameter("use_permissions"));
140    if (plugin == null)
141    {
142      plugin = PluginDefinition.getNew(dc, className, jarPath, usePermissions == 2);
143      message = "Plugin created";
144      dc.saveItem(plugin);
145    }
146    else
147    {
148      dc.reattachItem(plugin);
149      plugin.loadPluginInformation(jarPath, className, usePermissions == 2);
150      message = "Plugin updated";
151    }
152    plugin.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory"), null));
153    plugin.setTrusted(Values.getBoolean(request.getParameter("trusted")));
154    String aie = Values.getStringOrNull(request.getParameter("allow_immediate_execution"));
155    plugin.setAllowImmediateExecution(aie == null ? 
156      plugin.getMainType() == Plugin.MainType.EXPORT : Values.getBoolean(aie));
157
158    //Update permissions
159    ItemQuery<RoleKey> q = RoleKey.getQuery();
160    q.setCacheResult(true);
161    ItemResultList<RoleKey> roleKeys = q.list(dc);
162    for (RoleKey key : roleKeys)
163    {
164      String name = key.getItemType().name();
165      int granted = Values.getInt(request.getParameter(name+"_granted"));
166      int denied = Values.getInt(request.getParameter(name+"_denied"));
167      plugin.setPermissions(key, PermissionUtil.getPermissions(granted), PermissionUtil.getPermissions(denied));
168    }
169    plugin.setUsePermissions(usePermissions != 0);
170   
171    // Installed on job agents
172    String[] removeAgents = Values.getString(request.getParameter("removeAgents")).split(",");
173    for (int i = 0; i < removeAgents.length; ++i)
174    {
175      int agentId = Values.getInt(removeAgents[i], -1);
176      if (agentId != -1) 
177      {
178        JobAgent agent = JobAgent.getById(dc, agentId);
179        JobAgentSettings settings = agent.getSettings(plugin, false);
180        if (settings != null && settings.isInDatabase()) dc.deleteItem(settings);
181      }
182    }
183
184    String[] allAgents = Values.getString(request.getParameter("allAgents")).split(",");
185    for (int i = 0; i < allAgents.length; ++i)
186    {
187      int agentId = Values.getInt(allAgents[i], -1);
188      if (agentId != -1) 
189      {
190        JobAgent agent = JobAgent.getById(dc, agentId);
191        JobAgentSettings settings = agent.getSettings(plugin, true);
192        if (!settings.isInDatabase()) 
193        {
194          dc.saveItem(settings);
195        }
196        else
197        {
198          dc.reattachItem(settings);
199        }
200        settings.setJarPath(Values.getStringOrNull(request.getParameter("jarPath."+agentId)));
201        settings.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory."+agentId), null));
202        settings.setPriorityBoost(Values.getInt(request.getParameter("priorityBoost."+agentId)));
203        String trusted = request.getParameter("trusted."+agentId);
204        settings.setTrusted(trusted == null ? null : Values.getBoolean(trusted));
205      }
206    }
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<PluginDefinition> query = PluginDefinition.getQuery();
305    query.join(Hql.leftJoin("guiContexts", "ctx"));
306    query.setDistinct(true);
307    cc.configureQuery(query, true);
308    cc.setQuery(query);
309    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+plugins";
310  }
311  else if ("ExportItem".equals(cmd))
312  {
313    // Run an export plugin in single-item context
314    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
315    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+plugin";
316  }
317  else if ("ImportItems".equals(cmd))
318  {
319    // Run an import plugin in a list context
320    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
321    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
322    query.join(Hql.leftJoin("guiContexts", "ctx"));
323    query.setDistinct(true);
324    cc.configureQuery(query, true);
325    cc.setQuery(query);
326    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+plugins";
327  }
328  else if ("ImportItem".equals(cmd))
329  {
330    // Run an import plugin in single-item context
331    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
332    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+plugin";
333  }
334  else if ("RunListPlugin".equals(cmd))
335  {
336    // Run another plugin in a list context
337    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
338    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
339    query.join(Hql.leftJoin("guiContexts", "ctx"));
340    query.setDistinct(true);
341    cc.configureQuery(query, true);
342    cc.setQuery(query);
343    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
344  }
345  else if ("RunPlugin".equals(cmd))
346  {
347    // Run another plugin in single-item context
348    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
349    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
350  }
351  else if ("UnloadItems".equals(cmd))
352  {
353    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
354    dc = sc.newDbControl();
355    for (int id : cc.getSelected())
356    {
357      PluginDefinition pd = PluginDefinition.getById(dc, id);
358      if (pd.getJarPath() != null)
359      {
360        JarClassLoader.unload(pd.getJarPath());
361      }
362    }
363    dc.close();
364    redirect = listPage;
365  }
366  else if ("UnloadItem".equals(cmd))
367  {
368    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
369    dc = sc.newDbControl();
370    PluginDefinition pd = PluginDefinition.getById(dc, cc.getId());
371    if (pd.getJarPath() != null)
372    {
373      JarClassLoader.unload(pd.getJarPath());
374    }
375    dc.close();
376    redirect = viewPage;
377  }
378  else
379  {
380    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
381  }
382}
383finally
384{
385  if (dc != null) dc.close();
386}
387
388if (forward != null)
389{
390  pageContext.forward(forward);
391}
392else if (redirect != null)
393{
394  response.sendRedirect(redirect);
395}
396else if (message == null)
397{
398  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
399}
400else
401{
402  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
403}
404%>
405
Note: See TracBrowser for help on using the repository browser.