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

Last change on this file since 4111 was 4111, checked in by Nicklas Nordborg, 15 years ago

Fixes #910: Plug-ins ignore file set by the auto-detect file format function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 9.6 KB
Line 
1<%@page import="net.sf.basedb.util.ContextUtil.ContextResult"%>
2<%-- $Id: index.jsp 4111 2008-01-31 07:37:04Z nicklas $
3  ------------------------------------------------------------------
4  Copyright (C) 2005 Nicklas Nordborg
5  Copyright (C) 2006 Jari Hakkinen, Nicklas Nordborg
6  Copyright (C) 2007 Nicklas Nordborg
7
8  This file is part of BASE - BioArray Software Environment.
9  Available at http://base.thep.lu.se/
10
11  BASE is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License
13  as published by the Free Software Foundation; either version 2
14  of the License, or (at your option) any later version.
15
16  BASE is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330,
24  Boston, MA  02111-1307, USA.
25  ------------------------------------------------------------------
26
27  @author Nicklas
28  @version 2.0
29--%>
30<%@ page session="false"
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.ItemContext"
35  import="net.sf.basedb.core.File"
36  import="net.sf.basedb.core.Job"
37  import="net.sf.basedb.core.Path"
38  import="net.sf.basedb.core.BaseException"
39  import="net.sf.basedb.core.PluginDefinition"
40  import="net.sf.basedb.core.PluginConfiguration"
41  import="net.sf.basedb.core.PluginConfigurationRequest"
42  import="net.sf.basedb.core.PluginExecutionRequest"
43  import="net.sf.basedb.core.PluginResponse"
44  import="net.sf.basedb.core.ItemQuery"
45  import="net.sf.basedb.core.ItemResultList"
46  import="net.sf.basedb.core.Include"
47  import="net.sf.basedb.core.query.Hql"
48  import="net.sf.basedb.core.query.Expressions"
49  import="net.sf.basedb.core.query.Restrictions"
50  import="net.sf.basedb.core.query.Orders"
51  import="net.sf.basedb.core.plugin.Response"
52  import="net.sf.basedb.core.plugin.GuiContext"
53  import="net.sf.basedb.core.plugin.Plugin"
54  import="net.sf.basedb.core.plugin.InteractivePlugin"
55  import="net.sf.basedb.util.AutoDetectFileFormat"
56  import="net.sf.basedb.clients.web.Base"
57  import="net.sf.basedb.clients.web.WebException"
58  import="net.sf.basedb.util.Values"
59  import="net.sf.basedb.util.ContextUtil"
60  import="net.sf.basedb.util.ContextUtil.ContextResult"
61  import="net.sf.basedb.clients.web.util.HTML"
62  import="java.util.List"
63  import="java.util.LinkedList"
64  import="java.util.ArrayList"
65  import="java.util.Map"
66  import="java.util.HashMap"
67  import="java.util.Set"
68  import="java.util.HashSet"
69%>
70<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
71<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
72
73<%
74final String root = request.getContextPath()+"/";
75final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
76final String ID = sc.getId();
77final String cmd = request.getParameter("cmd");
78final Item itemType = Item.valueOf(request.getParameter("item_type"));
79final String subContext = Values.getString(request.getParameter("subcontext"), "");
80final GuiContext.Type contextType = GuiContext.Type.valueOf(request.getParameter("context_type"));
81final GuiContext context = new GuiContext(itemType, contextType, subContext);
82final ItemContext cc = sc.getCurrentContext(itemType, subContext);
83
84DbControl dc = null;;
85String forward = null;
86String message = null;
87String redirect = null;
88try
89{
90  if ("SelectPlugin".equals(cmd))
91  {
92    ItemQuery<PluginDefinition> pluginQuery = PluginDefinition.getQuery(context, null);
93    pluginQuery.restrict(
94      Restrictions.eq(
95        Hql.property("mainType"),
96        Expressions.integer(Plugin.MainType.IMPORT.getValue())
97      )
98    );
99    pluginQuery.order(Orders.asc(Hql.property("name")));
100    pluginQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS);
101   
102    ItemQuery<PluginConfiguration> configQuery = PluginConfiguration.getQuery();
103    configQuery.restrict(
104        Restrictions.eq(
105          Hql.property("pluginDefinition"),
106          Expressions.parameter("plugin")
107        )
108      );
109    configQuery.order(Orders.asc(Hql.property("name")));
110    configQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS);
111   
112    dc = sc.newDbControl();
113    Object currentItem = null;
114    if (contextType == GuiContext.Type.ITEM)
115    {
116      currentItem = itemType.getById(dc, cc.getId());
117    }
118    List<ContextResult> contextResult = ContextUtil.checkContext(dc, pluginQuery, configQuery, context, currentItem);
119    dc.close();
120
121    boolean showWarnings = Values.getBoolean(sc.getUserClientSetting("plugins.showWarnings"), false);
122    int totalPlugins = ContextUtil.countUsuablePlugins(contextResult);
123    Set<String> contextMessages = ContextUtil.getContextMessages(contextResult, showWarnings, true);
124   
125    if (totalPlugins == 0)
126    {
127      String msg = "No plugins can be used in this context, for the following reasons:<br><ul><li>";
128      if (contextMessages.size() == 0)
129      {
130        msg += "Could not find any plugins that you have permission to use</ul>";
131      }
132      else
133      {
134        msg += Values.getString(contextMessages, "<li>", true) + "</ul>";
135      }
136      throw new WebException("popup", "No usable plugins found", msg);
137    }
138    sc.setSessionSetting("IMPORTERS", contextResult);
139    sc.setSessionSetting("MESSAGES", contextMessages);
140    forward = "select_plugin.jsp";
141  }
142 
143  else if ("RunPlugin".equals(cmd))
144  {
145    int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"));
146    int pluginConfigurationId = Values.getInt(request.getParameter("pluginconfiguration_id"));
147   
148    if (pluginDefinitionId == -1 || pluginConfigurationId == -1)
149    {
150      // Auto-detect was selected
151      forward = "select_file.jsp";
152    }
153    else
154    {
155      // Specific plugin and configuration was selected
156      redirect = "../plugin/index.jsp?ID="+ID+
157        "&cmd=NewJob&pluginconfiguration_id="+pluginConfigurationId+
158        "&plugindefinition_id="+pluginDefinitionId+
159        "&title="+request.getParameter("title")+
160        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
161        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
162        "&item_type="+context.getItem().name()+
163        "&subcontext="+subContext+
164        "&context_type="+context.getType().name()+
165        "&warnIfOpen=0";
166    }
167  }
168 
169  else if ("AutoDetect".equals(cmd))
170  {
171    int fileId = Values.getInt(request.getParameter("file_id"));
172    String path = request.getParameter("path");
173    dc = sc.newDbControl();
174    File f = fileId != 0 ? File.getById(dc, fileId) : File.getByPath(dc, new Path(path, Path.Type.FILE), false);
175    int maxRecent = Base.getMaxRecent(sc);
176    cc.setRecent(f, maxRecent);
177
178    int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"));
179    PluginDefinition plugin = null;
180    if (pluginDefinitionId != -1)
181    {
182      plugin = PluginDefinition.getById(dc, pluginDefinitionId);
183    }
184   
185    // Auto-detect file format
186    Map<PluginDefinition, List<PluginConfiguration>> plugins = 
187      AutoDetectFileFormat.findPlugins(dc, context, f, plugin);
188
189    cc.setObject("auto-detected-file", f);
190    int matches = plugins.size();
191    if (matches == 0)
192    {
193      forward = "no_fileformat.jsp?ID="+ID+
194        "&file_id="+f.getId()+
195        "&title="+request.getParameter("title")+
196        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
197        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
198        "&item_type="+context.getItem().name()+
199        "&subcontext="+subContext+
200        "&context_type="+context.getType().name();
201      //throw new WebException("popup", "Import failed", "Couldn't find any file format matching file {1}", f);
202    }
203    else
204    {
205      if (matches == 1)
206      {
207        plugin = plugins.entrySet().iterator().next().getKey();
208        List<PluginConfiguration> configList = plugins.get(plugin);
209        if (configList != null && configList.size() > 1)
210        {
211          // Multiple configurations matches
212          matches = configList.size();
213        }
214        else
215        {
216          // 0 or 1 configuration matched
217          PluginConfiguration pc = null;
218          if (configList != null && configList.size() == 1)
219          {
220            pc = configList.get(0);
221          }
222          redirect = "../plugin/index.jsp?ID="+ID+
223            "&cmd=NewJob" + 
224            (pc == null ? "" : "&pluginconfiguration_id="+ pc.getId()) +
225            "&plugindefinition_id="+plugin.getId()+
226            "&title="+request.getParameter("title")+
227            "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
228            "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
229            "&item_type="+context.getItem().name()+
230            "&subcontext="+subContext+
231            "&context_type="+context.getType().name()+
232            "&warnIfOpen=0";
233        }
234      }
235      if (matches > 1)
236      {
237        List<ContextResult> importers = new ArrayList<ContextResult>(matches);
238        for (Map.Entry<PluginDefinition, List<PluginConfiguration>> entry : plugins.entrySet())
239        {
240          PluginDefinition pd = entry.getKey();
241          List<PluginConfiguration> configList = entry.getValue();
242          if (configList != null && configList.size() > 0)
243          {
244            for (PluginConfiguration pc : configList)
245            {
246              importers.add(new ContextResult(pd, pc, (String)null));
247            }
248          }
249          else
250          {
251            importers.add(new ContextResult(pd, null, (String)null));
252          }
253        }
254        sc.setSessionSetting("IMPORTERS", importers);
255        forward = "select_plugin.jsp?noautodetect=1&message=Multiple+file+formats+found.+Please+select+one.";
256      }
257    }
258  }
259
260}
261finally
262{
263  if (dc != null) dc.close();
264}
265
266if (forward != null)
267{
268  pageContext.forward(forward);
269}
270else if (redirect != null)
271{
272  response.sendRedirect(redirect);
273}
274else if (message == null)
275{
276  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
277}
278else
279{
280  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
281}
282%>
283
Note: See TracBrowser for help on using the repository browser.