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

Last change on this file since 6192 was 6192, checked in by Nicklas Nordborg, 10 years ago

References #1729 and #1730.

Refactored the code for displaying a popup message when loading a page. The message should now be stored as a session setting instead: sc.setSessionSetting("alert-message", message). The added benefit is that the message is only displayed once and is not re-displayed if the page is reloaded.

Removed onunload, onkeypress and attributes from the <base:body> tag. Added support for dynamic attributes instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 10.0 KB
Line 
1<%@page import="net.sf.basedb.util.ContextUtil.ContextResult"%>
2<%-- $Id: index.jsp 6192 2012-10-31 12:34:19Z 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="+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    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    int matches = plugins.size();
192    if (matches == 0)
193    {
194      forward = "no_fileformat.jsp?ID="+ID+
195        "&file_id="+f.getId()+
196        "&title="+request.getParameter("title")+
197        "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
198        "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
199        "&item_type="+context.getItem().name()+
200        "&subcontext="+subContext+
201        "&context_type="+context.getType().name();
202      //throw new WebException("popup", "Import failed", "Couldn't find any file format matching file {1}", f);
203    }
204    else
205    {
206      if (matches == 1)
207      {
208        plugin = plugins.entrySet().iterator().next().getKey();
209        List<PluginConfiguration> configList = plugins.get(plugin);
210        if (configList != null && configList.size() > 1)
211        {
212          // Multiple configurations matches
213          matches = configList.size();
214        }
215        else
216        {
217          // 0 or 1 configuration matched
218          PluginConfiguration pc = null;
219          if (configList != null && configList.size() == 1)
220          {
221            pc = configList.get(0);
222          }
223          redirect = "../plugin/index.jsp?ID="+ID+
224            "&cmd=NewJob" + 
225            (pc == null ? "" : "&pluginconfiguration_id="+ pc.getId()) +
226            "&plugindefinition_id="+plugin.getId()+
227            "&title="+request.getParameter("title")+
228            "&job_name="+HTML.urlEncode(request.getParameter("job_name"))+
229            "&job_description="+HTML.urlEncode(request.getParameter("job_description"))+
230            "&item_type="+context.getItem().name()+
231            "&subcontext="+subContext+
232            "&context_type="+context.getType().name()+
233            "&parameter:" + Parameters.CHARSET_PARAMETER + "=" + HTML.urlEncode(charset) +
234            "&warnIfOpen=0";
235        }
236      }
237      if (matches > 1)
238      {
239        List<ContextResult> importers = new ArrayList<ContextResult>(matches);
240        for (Map.Entry<PluginDefinition, List<PluginConfiguration>> entry : plugins.entrySet())
241        {
242          PluginDefinition pd = entry.getKey();
243          List<PluginConfiguration> configList = entry.getValue();
244          if (configList != null && configList.size() > 0)
245          {
246            for (PluginConfiguration pc : configList)
247            {
248              importers.add(new ContextResult(pd, pc, (String)null));
249            }
250          }
251          else
252          {
253            importers.add(new ContextResult(pd, null, (String)null));
254          }
255        }
256        sc.setSessionSetting("IMPORTERS", importers);
257        forward = "select_plugin.jsp?noautodetect=1&message=Multiple+file+formats+found.+Please+select+one.";
258      }
259    }
260  }
261
262}
263finally
264{
265  if (dc != null) dc.close();
266}
267
268if (forward != null)
269{
270  sc.setSessionSetting("alert-message", message);
271  pageContext.forward(forward);
272}
273else if (redirect != null)
274{
275  sc.setSessionSetting("alert-message", message);
276  response.sendRedirect(redirect);
277}
278else if (message == null)
279{
280  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
281}
282else
283{
284  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
285}
286%>
287
Note: See TracBrowser for help on using the repository browser.