source: trunk/www/common/import/index.jsp @ 3495

Last change on this file since 3495 was 3495, checked in by Nicklas Nordborg, 16 years ago

References #625. Major features are now in place. It's a bit tricky to update the isInContext
method on all plug-ins. I need more testing time to verify that all works before I close the
ticket.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 8.8 KB
Line 
1<%-- $Id: index.jsp 3495 2007-06-14 12:36:26Z 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.ItemContext"
32  import="net.sf.basedb.core.File"
33  import="net.sf.basedb.core.Job"
34  import="net.sf.basedb.core.Path"
35  import="net.sf.basedb.core.BaseException"
36  import="net.sf.basedb.core.PluginDefinition"
37  import="net.sf.basedb.core.PluginConfiguration"
38  import="net.sf.basedb.core.PluginConfigurationRequest"
39  import="net.sf.basedb.core.PluginExecutionRequest"
40  import="net.sf.basedb.core.PluginResponse"
41  import="net.sf.basedb.core.ItemQuery"
42  import="net.sf.basedb.core.ItemResultList"
43  import="net.sf.basedb.core.Include"
44  import="net.sf.basedb.core.query.Hql"
45  import="net.sf.basedb.core.query.Expressions"
46  import="net.sf.basedb.core.query.Restrictions"
47  import="net.sf.basedb.core.query.Orders"
48  import="net.sf.basedb.core.plugin.Response"
49  import="net.sf.basedb.core.plugin.GuiContext"
50  import="net.sf.basedb.core.plugin.Plugin"
51  import="net.sf.basedb.core.plugin.InteractivePlugin"
52  import="net.sf.basedb.util.AutoDetectFileFormat"
53  import="net.sf.basedb.clients.web.Base"
54  import="net.sf.basedb.clients.web.WebException"
55  import="net.sf.basedb.util.Values"
56  import="net.sf.basedb.util.ContextUtil"
57  import="net.sf.basedb.util.ContextUtil.ContextResult"
58  import="net.sf.basedb.clients.web.util.HTML"
59  import="java.util.List"
60  import="java.util.LinkedList"
61  import="java.util.Map"
62  import="java.util.HashMap"
63  import="java.util.Set"
64  import="java.util.HashSet"
65%>
66<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
67<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
68
69<%
70final String root = request.getContextPath()+"/";
71final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
72final String ID = sc.getId();
73final String cmd = request.getParameter("cmd");
74final Item itemType = Item.valueOf(request.getParameter("item_type"));
75final String subContext = Values.getString(request.getParameter("subcontext"), "");
76final GuiContext.Type contextType = GuiContext.Type.valueOf(request.getParameter("context_type"));
77final GuiContext context = new GuiContext(itemType, contextType, subContext);
78final ItemContext cc = sc.getCurrentContext(itemType, subContext);
79
80DbControl dc = sc.newDbControl();
81String forward = null;
82String message = null;
83String redirect = null;
84try
85{
86  if ("SelectPlugin".equals(cmd))
87  {
88    ItemQuery<PluginDefinition> pluginQuery = PluginDefinition.getQuery(context, null);
89    pluginQuery.restrict(
90      Restrictions.eq(
91        Hql.property("mainType"),
92        Expressions.integer(Plugin.MainType.IMPORT.getValue())
93      )
94    );
95    pluginQuery.order(Orders.asc(Hql.property("name")));
96    pluginQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS);
97   
98    ItemQuery<PluginConfiguration> configQuery = PluginConfiguration.getQuery();
99    configQuery.restrict(
100        Restrictions.eq(
101          Hql.property("pluginDefinition"),
102          Expressions.parameter("plugin")
103        )
104      );
105    configQuery.order(Orders.asc(Hql.property("name")));
106    configQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS);
107   
108    dc = sc.newDbControl();
109    Object currentItem = null;
110    if (contextType == GuiContext.Type.ITEM)
111    {
112      currentItem = itemType.getById(dc, cc.getId());
113    }
114    List<ContextResult> contextResult = ContextUtil.checkContext(dc, pluginQuery, configQuery, context, currentItem);
115    dc.close();
116
117    boolean showWarnings = Values.getBoolean(sc.getUserClientSetting("plugins.showWarnings"), false);
118    int totalPlugins = ContextUtil.countUsuablePlugins(contextResult);
119    Set<String> contextMessages = ContextUtil.getContextMessages(contextResult, showWarnings, true);
120   
121    if (totalPlugins == 0)
122    {
123      String msg = "No plugins can be used in this context, for the following reasons:<br><ul><li>";
124      if (contextMessages.size() == 0)
125      {
126        msg += "Could not find any plugins that you have permission to use</ul>";
127      }
128      else
129      {
130        msg += Values.getString(contextMessages, "<li>", true) + "</ul>";
131      }
132      throw new WebException("popup", "No usable plugins found", msg);
133    }
134    sc.setSessionSetting("IMPORTERS", contextResult);
135    sc.setSessionSetting("MESSAGES", contextMessages);
136    forward = "select_plugin.jsp";
137  }
138 
139  else if ("RunPlugin".equals(cmd))
140  {
141    int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"));
142    int pluginConfigurationId = Values.getInt(request.getParameter("pluginconfiguration_id"));
143   
144    if (pluginDefinitionId == -1 || pluginConfigurationId == -1)
145    {
146      // Auto-detect was selected
147      forward = "select_file.jsp";
148    }
149    else
150    {
151      // Specific plugin and configuration was selected
152      redirect = "../plugin/index.jsp?ID="+ID+
153        "&cmd=NewJob&pluginconfiguration_id="+pluginConfigurationId+
154        "&plugindefinition_id="+pluginDefinitionId+
155        "&title="+request.getParameter("title")+
156        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
157        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
158        "&item_type="+context.getItem().name()+
159        "&subcontext="+subContext+
160        "&context_type="+context.getType().name();
161    }
162  }
163 
164  else if ("AutoDetect".equals(cmd))
165  {
166    int fileId = Values.getInt(request.getParameter("file_id"));
167    String path = request.getParameter("path");
168    File f = fileId != 0 ? File.getById(dc, fileId) : File.getByPath(dc, new Path(path, Path.Type.FILE), false);
169    int maxRecent = Base.getMaxRecent(sc);
170    cc.setRecent(f, maxRecent);
171
172    int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"));
173    PluginDefinition plugin = null;
174    if (pluginDefinitionId != -1)
175    {
176      plugin = PluginDefinition.getById(dc, pluginDefinitionId);
177    }
178   
179    // Auto-detect file format
180    Map<PluginDefinition, List<PluginConfiguration>> plugins = 
181      AutoDetectFileFormat.findPlugins(dc, context, f, plugin);
182
183    sc.getCurrentContext(Item.FILE).setId(f.getId());
184    int matches = plugins.size();
185    if (matches == 0)
186    {
187      forward = "no_fileformat.jsp?ID="+ID+
188        "&file_id="+f.getId()+
189        "&title="+request.getParameter("title")+
190        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
191        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
192        "&item_type="+context.getItem().name()+
193        "&subcontext="+subContext+
194        "&context_type="+context.getType().name();
195      //throw new WebException("popup", "Import failed", "Couldn't find any file format matching file {1}", f);
196    }
197    else
198    {
199      if (matches == 1)
200      {
201        plugin = plugins.entrySet().iterator().next().getKey();
202        List<PluginConfiguration> configList = plugins.get(plugin);
203        if (configList != null && configList.size() > 1)
204        {
205          // Multiple configurations matches
206          matches = configList.size();
207        }
208        else
209        {
210          // 0 or 1 configuration matched
211          PluginConfiguration pc = null;
212          if (configList != null && configList.size() == 1)
213          {
214            pc = configList.get(0);
215          }
216          redirect = "../plugin/index.jsp?ID="+ID+
217            "&cmd=NewJob" + 
218            (pc == null ? "" : "&pluginconfiguration_id="+ pc.getId()) +
219            "&plugindefinition_id="+plugin.getId()+
220            "&title="+request.getParameter("title")+
221            "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
222            "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
223            "&item_type="+context.getItem().name()+
224            "&subcontext="+subContext+
225            "&context_type="+context.getType().name();
226        }
227      }
228      if (matches > 1)
229      {
230        sc.setSessionSetting("IMPORTERS", plugins);
231        forward = "select_plugin.jsp?noautodetect=1&message=Multiple+file+formats+found.+Please+select+one.";
232      }
233    }
234  }
235
236}
237finally
238{
239  if (dc != null) dc.close();
240}
241
242if (forward != null)
243{
244  pageContext.forward(forward);
245}
246else if (redirect != null)
247{
248  response.sendRedirect(redirect);
249}
250else if (message == null)
251{
252  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
253}
254else
255{
256  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
257}
258%>
259
Note: See TracBrowser for help on using the repository browser.