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

Last change on this file since 3679 was 3679, checked in by Jari Häkkinen, 14 years ago

Changing the pesky "a (ä) character to a.

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