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

Last change on this file since 7494 was 7494, checked in by Nicklas Nordborg, 5 years ago

Merged patch release 3.12.3 to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 10.1 KB
Line 
1<%@page import="net.sf.basedb.util.ContextUtil.ContextResult"%>
2<%-- $Id: index.jsp 7494 2018-06-04 06:42:46Z nicklas $
3  ------------------------------------------------------------------
4  Copyright (C) 2005 Nicklas Nordborg
5  Copyright (C) 2006 Jari Häkkinen, 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 3
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 BASE. If not, see <http://www.gnu.org/licenses/>.
23  ------------------------------------------------------------------
24
25  @author Nicklas
26  @version 2.0
27--%>
28<%@ page pageEncoding="UTF-8" session="false"
29  import="net.sf.basedb.core.SessionControl"
30  import="net.sf.basedb.core.DbControl"
31  import="net.sf.basedb.core.Item"
32  import="net.sf.basedb.core.ItemContext"
33  import="net.sf.basedb.core.File"
34  import="net.sf.basedb.core.Job"
35  import="net.sf.basedb.core.Path"
36  import="net.sf.basedb.core.BaseException"
37  import="net.sf.basedb.core.PluginDefinition"
38  import="net.sf.basedb.core.PluginConfiguration"
39  import="net.sf.basedb.core.PluginConfigurationRequest"
40  import="net.sf.basedb.core.PluginExecutionRequest"
41  import="net.sf.basedb.core.PluginResponse"
42  import="net.sf.basedb.core.ItemQuery"
43  import="net.sf.basedb.core.ItemResultList"
44  import="net.sf.basedb.core.Include"
45  import="net.sf.basedb.core.query.Hql"
46  import="net.sf.basedb.core.query.Expressions"
47  import="net.sf.basedb.core.query.Restrictions"
48  import="net.sf.basedb.core.query.Orders"
49  import="net.sf.basedb.core.plugin.Response"
50  import="net.sf.basedb.core.plugin.GuiContext"
51  import="net.sf.basedb.core.plugin.Plugin"
52  import="net.sf.basedb.core.plugin.InteractivePlugin"
53  import="net.sf.basedb.plugins.util.Parameters"
54  import="net.sf.basedb.util.AutoDetectFileFormat"
55  import="net.sf.basedb.clients.web.Base"
56  import="net.sf.basedb.clients.web.WebException"
57  import="net.sf.basedb.util.Values"
58  import="net.sf.basedb.util.ContextUtil"
59  import="net.sf.basedb.util.ContextUtil.ContextResult"
60  import="net.sf.basedb.clients.web.util.HTML"
61  import="java.util.List"
62  import="java.util.LinkedList"
63  import="java.util.ArrayList"
64  import="java.util.Map"
65  import="java.util.HashMap"
66  import="java.util.Set"
67  import="java.util.HashSet"
68%>
69<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
70<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
71
72<%
73final String root = request.getContextPath()+"/";
74final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
75final String ID = sc.getId();
76final String cmd = request.getParameter("cmd");
77final Item itemType = Item.valueOf(request.getParameter("item_type"));
78final String subContext = Values.getString(request.getParameter("subcontext"), "");
79final GuiContext.Type contextType = GuiContext.Type.valueOf(request.getParameter("context_type"));
80final GuiContext context = new GuiContext(itemType, contextType, subContext);
81final ItemContext cc = sc.getCurrentContext(itemType, subContext);
82
83DbControl dc = null;;
84String forward = null;
85String message = null;
86String redirect = null;
87try
88{
89  if ("SelectPlugin".equals(cmd))
90  {
91    ItemQuery<PluginDefinition> pluginQuery = PluginDefinition.getQuery(context, null);
92    pluginQuery.restrict(
93      Restrictions.eq(
94        Hql.property("mainType"),
95        Expressions.integer(Plugin.MainType.IMPORT.getValue())
96      )
97    );
98    pluginQuery.restrict(Restrictions.eq(Hql.property("disabled"), Expressions.parameter("isDisabled", false)));
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 = "<b>No plugins can be used in this context, for the following reasons:</b><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="+HTML.urlEncode(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="+HTML.urlEncode(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    String charset = request.getParameter("charset");
174    dc = sc.newDbControl();
175    File f = fileId != 0 ? File.getById(dc, fileId) : File.getByPath(dc, new Path(path, Path.Type.FILE), false);
176    int maxRecent = Base.getMaxRecent(sc);
177    cc.setRecent(f, maxRecent);
178
179    int pluginDefinitionId = Values.getInt(request.getParameter("plugindefinition_id"));
180    PluginDefinition plugin = null;
181    if (pluginDefinitionId != -1)
182    {
183      plugin = PluginDefinition.getById(dc, pluginDefinitionId);
184    }
185   
186    // Auto-detect file format
187    Map<PluginDefinition, List<PluginConfiguration>> plugins = 
188      AutoDetectFileFormat.findPlugins(dc, context, f, charset, plugin);
189
190    cc.setObject("auto-detected-file", f);
191    cc.setObject("last-file-charset", charset);
192    int matches = plugins.size();
193    if (matches == 0)
194    {
195      forward = "no_fileformat.jsp?ID="+ID+
196        "&file_id="+f.getId()+
197        "&title="+HTML.urlEncode(request.getParameter("title"))+
198        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
199        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
200        "&item_type="+context.getItem().name()+
201        "&subcontext="+HTML.urlEncode(subContext)+
202        "&context_type="+context.getType().name();
203      //throw new WebException("popup", "Import failed", "Couldn't find any file format matching file {1}", f);
204    }
205    else
206    {
207      if (matches == 1)
208      {
209        plugin = plugins.entrySet().iterator().next().getKey();
210        List<PluginConfiguration> configList = plugins.get(plugin);
211        if (configList != null && configList.size() > 1)
212        {
213          // Multiple configurations matches
214          matches = configList.size();
215        }
216        else
217        {
218          // 0 or 1 configuration matched
219          PluginConfiguration pc = null;
220          if (configList != null && configList.size() == 1)
221          {
222            pc = configList.get(0);
223          }
224          redirect = "../plugin/index.jsp?ID="+ID+
225            "&cmd=NewJob" + 
226            (pc == null ? "" : "&pluginconfiguration_id="+ pc.getId()) +
227            "&plugindefinition_id="+plugin.getId()+
228            "&title="+HTML.urlEncode(request.getParameter("title"))+
229            "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
230            "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
231            "&item_type="+context.getItem().name()+
232            "&subcontext="+HTML.urlEncode(subContext)+
233            "&context_type="+context.getType().name()+
234            "&parameter:" + Parameters.CHARSET_PARAMETER + "=" + HTML.urlEncode(charset) +
235            "&warnIfOpen=0";
236        }
237      }
238      if (matches > 1)
239      {
240        List<ContextResult> importers = new ArrayList<ContextResult>(matches);
241        for (Map.Entry<PluginDefinition, List<PluginConfiguration>> entry : plugins.entrySet())
242        {
243          PluginDefinition pd = entry.getKey();
244          List<PluginConfiguration> configList = entry.getValue();
245          if (configList != null && configList.size() > 0)
246          {
247            for (PluginConfiguration pc : configList)
248            {
249              importers.add(new ContextResult(pd, pc, (String)null));
250            }
251          }
252          else
253          {
254            importers.add(new ContextResult(pd, null, (String)null));
255          }
256        }
257        sc.setSessionSetting("IMPORTERS", importers);
258        forward = "select_plugin.jsp?noautodetect=1&message=Multiple+file+formats+found.+Please+select+one.";
259      }
260    }
261  }
262
263}
264finally
265{
266  if (dc != null) dc.close();
267}
268
269if (forward != null)
270{
271  sc.setSessionSetting("alert-message", message);
272  pageContext.forward(forward);
273}
274else if (redirect != null)
275{
276  sc.setSessionSetting("alert-message", message);
277  response.sendRedirect(redirect);
278}
279else if (message == null)
280{
281  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
282}
283else
284{
285  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
286}
287%>
288
Note: See TracBrowser for help on using the repository browser.