source: trunk/www/admin/plugindefinitions/index.jsp @ 2978

Last change on this file since 2978 was 2978, checked in by Nicklas Nordborg, 17 years ago

Added session="false" to all jsp pages so we no longer generate unneeded cookies

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 15.0 KB
Line 
1<%-- $Id: index.jsp 2978 2006-11-30 07:27:42Z 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.util.RemovableUtil"
45  import="net.sf.basedb.util.ShareableUtil"
46  import="net.sf.basedb.util.OwnableUtil"
47  import="net.sf.basedb.util.JarClassLoader"
48  import="net.sf.basedb.clients.web.Base"
49  import="net.sf.basedb.clients.web.PermissionUtil"
50  import="net.sf.basedb.clients.web.WebException"
51  import="net.sf.basedb.util.Values"
52  import="net.sf.basedb.clients.web.util.HTML"
53  import="java.util.Enumeration"
54  import="java.util.Set"
55  import="java.util.HashSet"
56  import="java.util.List"
57  import="java.util.ArrayList"
58  import="java.util.Collections"
59%>
60<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
61<%!
62  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,className,description");
63  private static final Item itemType = Item.PLUGINDEFINITION;
64%>
65<%
66final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
67final String ID = sc.getId();
68final String cmd = request.getParameter("cmd");
69final String root = request.getContextPath()+"/";
70final String mode = request.getParameter("mode");
71final String callback = request.getParameter("callback");
72final String itemId = request.getParameter("item_id");
73final String listPage = "list_plugins.jsp?ID="+ID
74  +(mode == null ? "" : "&mode="+mode)
75  +(callback == null ? "" : "&callback="+callback)
76  +(itemId == null ? "" : "&item_id="+itemId);
77final String viewPage = "view_plugin.jsp?ID="+ID;
78final String editPage = "edit_plugin.jsp?ID="+ID;
79
80String forward = null;
81String redirect = null;
82String message = null;
83DbControl dc = null;
84
85try
86{
87  if (cmd == null || "List".equals(cmd))
88  {
89    // Display the list page without updatinging the current context
90    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
91    redirect = listPage;
92  }
93  else if ("UpdateContext".equals(cmd))
94  {
95    // Display the list page after updating the current context from the request parameters
96    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
97    redirect = listPage;
98  }
99  else if ("LoadContext".equals(cmd))
100  {
101    // Display the list page after loading a saved context
102    int contextId = Values.getInt(request.getParameter("context"));
103    Base.loadContext(sc, contextId, defaultContext);
104    redirect = listPage;
105  }
106
107  else if ("ViewItem".equals(cmd))
108  {
109    // Display the view page for a single item
110    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
111    forward = viewPage;
112  }
113  else if ("EditItem".equals(cmd))
114  {
115    // Display the edit page for a single item (should be opened in a popup)
116    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
117    forward = editPage;
118  }
119  else if ("NewItem".equals(cmd))
120  {
121    // Display the edit page for a new item (should be opened in a popup)
122    if (!sc.hasPermission(Permission.CREATE, itemType))
123    {
124      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
125    }
126    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
127    cc.setId(0);
128    redirect = editPage;
129  }
130  else if ("UpdateItem".equals(cmd))
131  {
132    // Update the properties on an item (will close the popup)
133    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
134    dc = sc.newDbControl();
135    PluginDefinition plugin = (PluginDefinition)cc.getObject("item");
136    String className = Values.getStringOrNull(request.getParameter("className"));
137    String jarPath = Values.getStringOrNull(request.getParameter("jarPath"));
138    int usePermissions = Values.getInt(request.getParameter("use_permissions"));
139    if (plugin == null)
140    {
141      plugin = PluginDefinition.getNew(dc, className, jarPath, usePermissions == 2);
142      message = "Plugin created";
143      dc.saveItem(plugin);
144    }
145    else
146    {
147      dc.reattachItem(plugin);
148      plugin.loadPluginInformation(jarPath, className, usePermissions == 2);
149      message = "Plugin updated";
150    }
151    plugin.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory"), null));
152    plugin.setTrusted(Values.getBoolean(request.getParameter("trusted")));
153    String aie = Values.getStringOrNull(request.getParameter("allow_immediate_execution"));
154    plugin.setAllowImmediateExecution(aie == null ? 
155      plugin.getMainType() == Plugin.MainType.EXPORT : Values.getBoolean(aie));
156
157    //Update permissions
158    ItemQuery<RoleKey> q = RoleKey.getQuery();
159    q.setCacheResult(true);
160    ItemResultList<RoleKey> roleKeys = q.list(dc);
161    for (RoleKey key : roleKeys)
162    {
163      String name = key.getItemType().name();
164      int granted = Values.getInt(request.getParameter(name+"_granted"));
165      int denied = Values.getInt(request.getParameter(name+"_denied"));
166      plugin.setPermissions(key, PermissionUtil.getPermissions(granted), PermissionUtil.getPermissions(denied));
167    }
168    plugin.setUsePermissions(usePermissions != 0);
169   
170    // Installed on job agents
171    String[] removeAgents = Values.getString(request.getParameter("removeAgents")).split(",");
172    for (int i = 0; i < removeAgents.length; ++i)
173    {
174      int agentId = Values.getInt(removeAgents[i], -1);
175      if (agentId != -1) 
176      {
177        JobAgent agent = JobAgent.getById(dc, agentId);
178        JobAgentSettings settings = agent.getSettings(plugin, false);
179        if (settings != null && settings.isInDatabase()) dc.deleteItem(settings);
180      }
181    }
182
183    String[] allAgents = Values.getString(request.getParameter("allAgents")).split(",");
184    for (int i = 0; i < allAgents.length; ++i)
185    {
186      int agentId = Values.getInt(allAgents[i], -1);
187      if (agentId != -1) 
188      {
189        JobAgent agent = JobAgent.getById(dc, agentId);
190        JobAgentSettings settings = agent.getSettings(plugin, true);
191        if (!settings.isInDatabase()) 
192        {
193          dc.saveItem(settings);
194        }
195        else
196        {
197          dc.reattachItem(settings);
198        }
199        settings.setJarPath(Values.getStringOrNull(request.getParameter("jarPath."+agentId)));
200        settings.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory."+agentId), null));
201        settings.setPriorityBoost(Values.getInt(request.getParameter("priorityBoost."+agentId)));
202        String trusted = request.getParameter("trusted."+agentId);
203        settings.setTrusted(trusted == null ? null : Values.getBoolean(trusted));
204      }
205    }
206   
207    dc.commit();
208    cc.removeObject("item");
209  }
210  else if ("DeleteItem".equals(cmd))
211  {
212    // Delete a single item and then return to the view page
213    dc = sc.newDbControl();
214    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
215    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
216    dc.commit();
217    redirect = viewPage;
218  }
219  else if ("DeleteItems".equals(cmd))
220  {
221    // Delete all selected items on the list page
222    dc = sc.newDbControl();
223    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
224    int numTotal = cc.getSelected().size();
225    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
226    dc.commit();
227    if (numTotal != numRemoved)
228    {
229      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
230    }
231    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
232  }
233  else if ("RestoreItem".equals(cmd))
234  {
235    // Restore a single item and then return to the view page
236    dc = sc.newDbControl();
237    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
238    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
239    dc.commit();
240    redirect = viewPage;
241  }
242  else if ("RestoreItems".equals(cmd))
243  {
244    // Restore all selected items on the list page
245    dc = sc.newDbControl();
246    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
247    int numTotal = cc.getSelected().size();
248    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
249    dc.commit();
250    if (numTotal != numRemoved)
251    {
252      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
253    }
254    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
255  }
256  else if ("ShareItem".equals(cmd))
257  {
258    // Display a popup window for sharing a single item
259    dc = sc.newDbControl();
260    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
261    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
262    dc.close();
263    cc.setObject("MultiPermissions", permissions);
264    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
265  }
266  else if ("ShareItems".equals(cmd))
267  {
268    // Display a popup window for sharing all selected items on the list page
269    dc = sc.newDbControl();
270    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
271    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
272    dc.close();
273    cc.setObject("MultiPermissions", permissions);
274    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
275  }
276  else if ("TakeOwnershipOfItem".equals(cmd))
277  {
278    // Take ownership a single item and then return to the view page
279    dc = sc.newDbControl();
280    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
281    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
282    dc.commit();
283    redirect = viewPage;
284  }
285  else if ("TakeOwnershipOfItems".equals(cmd))
286  {
287    // Take ownership all selected items on the list page
288    dc = sc.newDbControl();
289    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
290    int numTotal = cc.getSelected().size();
291    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
292    dc.commit();
293    if (numTotal != numOwned)
294    {
295      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
296    }
297    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
298  }
299  else if ("ExportItems".equals(cmd))
300  {
301    // Run an export plugin in a list context
302    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
303    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
304    cc.configureQuery(query, true);
305    cc.setQuery(query);
306    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+plugins";
307  }
308  else if ("ExportItem".equals(cmd))
309  {
310    // Run an export plugin in single-item context
311    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
312    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+plugin";
313  }
314  else if ("ImportItems".equals(cmd))
315  {
316    // Run an import plugin in a list context
317    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
318    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
319    cc.configureQuery(query, true);
320    cc.setQuery(query);
321    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+plugins";
322  }
323  else if ("ImportItem".equals(cmd))
324  {
325    // Run an import plugin in single-item context
326    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
327    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+plugin";
328  }
329  else if ("RunListPlugin".equals(cmd))
330  {
331    // Run another plugin in a list context
332    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
333    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
334    cc.configureQuery(query, true);
335    cc.setQuery(query);
336    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
337  }
338  else if ("RunPlugin".equals(cmd))
339  {
340    // Run another plugin in single-item context
341    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
342    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
343  }
344  else if ("UnloadItems".equals(cmd))
345  {
346    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
347    dc = sc.newDbControl();
348    for (int id : cc.getSelected())
349    {
350      PluginDefinition pd = PluginDefinition.getById(dc, id);
351      if (pd.getJarPath() != null)
352      {
353        JarClassLoader.unload(pd.getJarPath());
354      }
355    }
356    dc.close();
357    redirect = listPage;
358  }
359  else if ("UnloadItem".equals(cmd))
360  {
361    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
362    dc = sc.newDbControl();
363    PluginDefinition pd = PluginDefinition.getById(dc, cc.getId());
364    if (pd.getJarPath() != null)
365    {
366      JarClassLoader.unload(pd.getJarPath());
367    }
368    dc.close();
369    redirect = viewPage;
370  }
371  else
372  {
373    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
374  }
375}
376finally
377{
378  if (dc != null) dc.close();
379}
380
381if (forward != null)
382{
383  pageContext.forward(forward);
384}
385else if (redirect != null)
386{
387  response.sendRedirect(redirect);
388}
389else if (message == null)
390{
391  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
392}
393else
394{
395  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
396}
397%>
398
Note: See TracBrowser for help on using the repository browser.