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

Last change on this file since 3708 was 3708, checked in by Martin Svensson, 14 years ago

References #364 Configuring the installation is allmost done.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 16.2 KB
Line 
1<%-- $Id: index.jsp 3708 2007-09-05 15:48:53Z martin $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Hakkinen, Nicklas Nordborg, Martin Svensson
5  Copyright (C) 2007 Nicklas Nordborg
6
7  This file is part of BASE - BioArray Software Environment.
8  Available at http://base.thep.lu.se/
9
10  BASE is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14
15  BASE is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330,
23  Boston, MA  02111-1307, USA.
24  ------------------------------------------------------------------
25
26  @author Nicklas
27  @version 2.0
28--%>
29<%@ page session="false"
30  import="net.sf.basedb.core.Application"
31  import="net.sf.basedb.core.SessionControl"
32  import="net.sf.basedb.core.DbControl"
33  import="net.sf.basedb.core.Item"
34  import="net.sf.basedb.core.Include"
35  import="net.sf.basedb.core.PluginDefinition"
36  import="net.sf.basedb.core.JobAgent"
37  import="net.sf.basedb.core.JobAgentSettings"
38  import="net.sf.basedb.core.ItemQuery"
39  import="net.sf.basedb.core.ItemResultList"
40  import="net.sf.basedb.core.Permission"
41  import="net.sf.basedb.core.ItemContext"
42  import="net.sf.basedb.core.MultiPermissions"
43  import="net.sf.basedb.core.PermissionDeniedException"
44  import="net.sf.basedb.core.ItemAlreadyExistsException"
45  import="net.sf.basedb.core.RoleKey"
46  import="net.sf.basedb.core.plugin.Plugin"
47  import="net.sf.basedb.core.query.Hql"
48  import="net.sf.basedb.util.RemovableUtil"
49  import="net.sf.basedb.util.ShareableUtil"
50  import="net.sf.basedb.util.OwnableUtil"
51  import="net.sf.basedb.util.JarClassLoader"
52  import="net.sf.basedb.clients.web.Base"
53  import="net.sf.basedb.clients.web.PermissionUtil"
54  import="net.sf.basedb.clients.web.WebException"
55  import="net.sf.basedb.util.Values"
56  import="net.sf.basedb.clients.web.util.HTML"
57  import="java.util.Enumeration"
58  import="java.util.Set"
59  import="java.util.HashSet"
60  import="java.util.List"
61  import="java.util.ArrayList"
62  import="java.util.Collections"
63%>
64<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
65<%!
66  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,className,description");
67  private static final Item itemType = Item.PLUGINDEFINITION;
68%>
69<%
70final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
71final String ID = sc.getId();
72final String cmd = request.getParameter("cmd");
73final String root = request.getContextPath()+"/";
74final String mode = request.getParameter("mode");
75final String callback = request.getParameter("callback");
76final String itemId = request.getParameter("item_id");
77final String listPage = "list_plugins.jsp?ID="+ID
78  +(mode == null ? "" : "&mode="+mode)
79  +(callback == null ? "" : "&callback="+callback)
80  +(itemId == null ? "" : "&item_id="+itemId);
81final String viewPage = "view_plugin.jsp?ID="+ID;
82final String editPage = "edit_plugin.jsp?ID="+ID;
83
84String forward = null;
85String redirect = null;
86String message = null;
87DbControl dc = null;
88
89try
90{
91  if (cmd == null || "List".equals(cmd))
92  {
93    // Display the list page without updatinging the current context
94    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
95    redirect = listPage;
96  }
97  else if ("UpdateContext".equals(cmd))
98  {
99    // Display the list page after updating the current context from the request parameters
100    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
101    redirect = listPage;
102  }
103  else if ("LoadContext".equals(cmd))
104  {
105    // Display the list page after loading a saved context
106    int contextId = Values.getInt(request.getParameter("context"));
107    Base.loadContext(sc, contextId, defaultContext);
108    redirect = listPage;
109  }
110
111  else if ("ViewItem".equals(cmd))
112  {
113    // Display the view page for a single item
114    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
115    forward = viewPage;
116  }
117  else if ("EditItem".equals(cmd))
118  {
119    // Display the edit page for a single item (should be opened in a popup)
120    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
121    forward = editPage;
122  }
123  else if ("NewItem".equals(cmd))
124  {
125    // Display the edit page for a new item (should be opened in a popup)
126    if (!sc.hasPermission(Permission.CREATE, itemType))
127    {
128      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
129    }
130    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
131    cc.setId(0);
132    if (Application.getNonCorePluginDirectory() == null)
133    {
134      redirect = editPage;
135    }
136    else
137    {
138      String pluginPath = Application.getNonCorePluginDirectory().getPath();
139      redirect = "select_installation_type.jsp?ID="+ID+"&plugin_path="+pluginPath;
140    }
141  }
142  else if("InstallationType".equals(cmd))
143  {
144    //Either manually or auto installation is selected
145    if (request.getParameter("installationtypes").equals("autoinstall"))
146    {
147      String pluginPath = request.getParameter("plugin_path");
148      redirect = "auto_install.jsp?ID="+ID+"&plugin_path="+pluginPath;
149    }
150    else if (request.getParameter("installationtypes").equals("manuellinstall"))
151    {
152      redirect = editPage;
153    }
154   
155  }
156  else if("LoadPlugins".equals(cmd))
157  {
158    //Loads plugins from jars that have been selected in auto_installer.jsp
159    dc = sc.newDbControl();
160  }
161  else if ("UpdateItem".equals(cmd))
162  {
163    // Update the properties on an item (will close the popup)
164    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
165    dc = sc.newDbControl();
166    PluginDefinition plugin = (PluginDefinition)cc.getObject("item");
167    String className = Values.getStringOrNull(request.getParameter("className"));
168    String jarPath = Values.getStringOrNull(request.getParameter("jarPath"));
169    int usePermissions = Values.getInt(request.getParameter("use_permissions"));
170    if (plugin == null)
171    {
172      plugin = PluginDefinition.getNew(dc, className, jarPath, usePermissions == 2);
173      message = "Plugin created";
174      dc.saveItem(plugin);
175    }
176    else
177    {
178      dc.reattachItem(plugin);
179      plugin.loadPluginInformation(jarPath, className, usePermissions == 2);
180      message = "Plugin updated";
181    }
182    plugin.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory"), null));
183    plugin.setTrusted(Values.getBoolean(request.getParameter("trusted")));
184    String aie = Values.getStringOrNull(request.getParameter("allow_immediate_execution"));
185    plugin.setAllowImmediateExecution(aie == null ? 
186      plugin.getMainType() == Plugin.MainType.EXPORT : Values.getBoolean(aie));
187
188    //Update permissions
189    ItemQuery<RoleKey> q = RoleKey.getQuery();
190    q.setCacheResult(true);
191    ItemResultList<RoleKey> roleKeys = q.list(dc);
192    for (RoleKey key : roleKeys)
193    {
194      String name = key.getItemType().name();
195      int granted = Values.getInt(request.getParameter(name+"_granted"));
196      int denied = Values.getInt(request.getParameter(name+"_denied"));
197      plugin.setPermissions(key, PermissionUtil.getPermissions(granted), PermissionUtil.getPermissions(denied));
198    }
199    plugin.setUsePermissions(usePermissions != 0);
200   
201    // Installed on job agents
202    String[] removeAgents = Values.getString(request.getParameter("removeAgents")).split(",");
203    for (int i = 0; i < removeAgents.length; ++i)
204    {
205      int agentId = Values.getInt(removeAgents[i], -1);
206      if (agentId != -1) 
207      {
208        JobAgent agent = JobAgent.getById(dc, agentId);
209        JobAgentSettings settings = agent.getSettings(plugin, false);
210        if (settings != null && settings.isInDatabase()) dc.deleteItem(settings);
211      }
212    }
213
214    String[] allAgents = Values.getString(request.getParameter("allAgents")).split(",");
215    for (int i = 0; i < allAgents.length; ++i)
216    {
217      int agentId = Values.getInt(allAgents[i], -1);
218      if (agentId != -1) 
219      {
220        JobAgent agent = JobAgent.getById(dc, agentId);
221        JobAgentSettings settings = agent.getSettings(plugin, true);
222        if (!settings.isInDatabase()) 
223        {
224          dc.saveItem(settings);
225        }
226        else
227        {
228          dc.reattachItem(settings);
229        }
230        settings.setJarPath(Values.getStringOrNull(request.getParameter("jarPath."+agentId)));
231        settings.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory."+agentId), null));
232        settings.setPriorityBoost(Values.getInt(request.getParameter("priorityBoost."+agentId)));
233        String trusted = request.getParameter("trusted."+agentId);
234        settings.setTrusted(trusted == null ? null : Values.getBoolean(trusted));
235      }
236    }
237   
238    dc.commit();
239    cc.removeObject("item");
240  }
241  else if ("DeleteItem".equals(cmd))
242  {
243    // Delete a single item and then return to the view page
244    dc = sc.newDbControl();
245    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
246    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
247    dc.commit();
248    redirect = viewPage;
249  }
250  else if ("DeleteItems".equals(cmd))
251  {
252    // Delete all selected items on the list page
253    dc = sc.newDbControl();
254    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
255    int numTotal = cc.getSelected().size();
256    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
257    dc.commit();
258    if (numTotal != numRemoved)
259    {
260      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
261    }
262    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
263  }
264  else if ("RestoreItem".equals(cmd))
265  {
266    // Restore a single item and then return to the view page
267    dc = sc.newDbControl();
268    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
269    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
270    dc.commit();
271    redirect = viewPage;
272  }
273  else if ("RestoreItems".equals(cmd))
274  {
275    // Restore all selected items on the list page
276    dc = sc.newDbControl();
277    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
278    int numTotal = cc.getSelected().size();
279    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
280    dc.commit();
281    if (numTotal != numRemoved)
282    {
283      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
284    }
285    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
286  }
287  else if ("ShareItem".equals(cmd))
288  {
289    // Display a popup window for sharing a single item
290    dc = sc.newDbControl();
291    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
292    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
293    dc.close();
294    cc.setObject("MultiPermissions", permissions);
295    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
296  }
297  else if ("ShareItems".equals(cmd))
298  {
299    // Display a popup window for sharing all selected items on the list page
300    dc = sc.newDbControl();
301    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
302    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
303    dc.close();
304    cc.setObject("MultiPermissions", permissions);
305    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
306  }
307  else if ("TakeOwnershipOfItem".equals(cmd))
308  {
309    // Take ownership a single item and then return to the view page
310    dc = sc.newDbControl();
311    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
312    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
313    dc.commit();
314    redirect = viewPage;
315  }
316  else if ("TakeOwnershipOfItems".equals(cmd))
317  {
318    // Take ownership all selected items on the list page
319    dc = sc.newDbControl();
320    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
321    int numTotal = cc.getSelected().size();
322    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
323    dc.commit();
324    if (numTotal != numOwned)
325    {
326      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
327    }
328    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
329  }
330  else if ("ExportItems".equals(cmd))
331  {
332    // Run an export plugin in a list context
333    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
334    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
335    query.join(Hql.leftJoin("guiContexts", "ctx"));
336    query.setDistinct(true);
337    cc.configureQuery(query, true);
338    cc.setQuery(query);
339    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+plugins";
340  }
341  else if ("ExportItem".equals(cmd))
342  {
343    // Run an export plugin in single-item context
344    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
345    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+plugin";
346  }
347  else if ("ImportItems".equals(cmd))
348  {
349    // Run an import plugin in a list context
350    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
351    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
352    query.join(Hql.leftJoin("guiContexts", "ctx"));
353    query.setDistinct(true);
354    cc.configureQuery(query, true);
355    cc.setQuery(query);
356    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+plugins";
357  }
358  else if ("ImportItem".equals(cmd))
359  {
360    // Run an import plugin in single-item context
361    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
362    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+plugin";
363  }
364  else if ("RunListPlugin".equals(cmd))
365  {
366    // Run another plugin in a list context
367    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
368    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
369    query.join(Hql.leftJoin("guiContexts", "ctx"));
370    query.setDistinct(true);
371    cc.configureQuery(query, true);
372    cc.setQuery(query);
373    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
374  }
375  else if ("RunPlugin".equals(cmd))
376  {
377    // Run another plugin in single-item context
378    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
379    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
380  }
381  else if ("UnloadItems".equals(cmd))
382  {
383    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
384    dc = sc.newDbControl();
385    for (int id : cc.getSelected())
386    {
387      PluginDefinition pd = PluginDefinition.getById(dc, id);
388      if (pd.getJarPath() != null)
389      {
390        JarClassLoader.unload(pd.getJarPath());
391      }
392    }
393    dc.close();
394    redirect = listPage;
395  }
396  else if ("UnloadItem".equals(cmd))
397  {
398    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
399    dc = sc.newDbControl();
400    PluginDefinition pd = PluginDefinition.getById(dc, cc.getId());
401    if (pd.getJarPath() != null)
402    {
403      JarClassLoader.unload(pd.getJarPath());
404    }
405    dc.close();
406    redirect = viewPage;
407  }
408  else
409  {
410    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
411  }
412}
413finally
414{
415  if (dc != null) dc.close();
416}
417
418if (forward != null)
419{
420  pageContext.forward(forward);
421}
422else if (redirect != null)
423{
424  response.sendRedirect(redirect);
425}
426else if (message == null)
427{
428  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
429}
430else
431{
432  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
433}
434%>
435
Note: See TracBrowser for help on using the repository browser.