source: branches/filedb/www/common/datafiles/select_files.jsp @ 3802

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

References #721: File validation is now in place. More robust code for users with not enough
permissions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 8.2 KB
Line 
1<%-- $Id: select_files.jsp 3802 2007-10-03 11:58:56Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Hakkinen, Nicklas Nordborg
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--%>
27<%@ page
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.Type"
33  import="net.sf.basedb.core.BasicItem"
34  import="net.sf.basedb.core.Permission"
35  import="net.sf.basedb.core.Nameable"
36  import="net.sf.basedb.core.FileStoreEnabled"
37  import="net.sf.basedb.core.FileSet"
38  import="net.sf.basedb.core.FileSetMember"
39  import="net.sf.basedb.core.File"
40  import="net.sf.basedb.core.Platform"
41  import="net.sf.basedb.core.PlatformVariant"
42  import="net.sf.basedb.core.PlatformFileType"
43  import="net.sf.basedb.core.DataFileType"
44  import="net.sf.basedb.core.ItemQuery"
45  import="net.sf.basedb.core.Include"
46  import="net.sf.basedb.core.ItemResultList"
47  import="net.sf.basedb.core.PermissionDeniedException"
48  import="net.sf.basedb.core.query.Expressions"
49  import="net.sf.basedb.core.query.Restrictions"
50  import="net.sf.basedb.core.query.Hql"
51  import="net.sf.basedb.core.query.Orders"
52  import="net.sf.basedb.clients.web.Base"
53  import="net.sf.basedb.clients.web.util.HTML"
54  import="net.sf.basedb.util.formatter.Formatter"
55  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
56  import="net.sf.basedb.clients.web.formatter.FormatterSettings"
57  import="net.sf.basedb.util.Values"
58  import="java.util.ArrayList"
59  import="java.util.List"
60  import="java.util.Date"
61  import="java.util.Set"
62  import="java.util.HashSet"
63  import="java.util.TreeSet"
64%>
65<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
66<%
67final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
68final String ID = sc.getId();
69final float scale = Base.getScale(sc);
70final Item itemType = Item.valueOf(request.getParameter("item_type"));
71final int itemId = Values.getInt(request.getParameter("item_id"));
72final int platformId = Values.getInt(request.getParameter("platform_id"), -1);
73final int variantId = Values.getInt(request.getParameter("variant_id"), -1);
74final ItemContext cc = sc.getCurrentContext(itemType);
75
76final DbControl dc = sc.newDbControl();
77try
78{
79
80  // Get the current item; null if we are editing a new item
81  final FileStoreEnabled item = itemId == 0 ? null : (FileStoreEnabled)itemType.getById(dc, itemId);
82  FileSet fileSet = null;
83  if (item != null && item.hasFileSet())
84  {
85    fileSet = item.getFileSet();
86  }
87
88  // Get the current platform/variant
89  // -- if not submitted in URL use values from current item
90  PlatformVariant variant = null;
91  Platform platform = null;
92  boolean deniedPlatform = false;
93  try
94  {
95    if (variantId > 0)
96    {
97      variant = PlatformVariant.getById(dc, variantId);
98      platform = variant.getPlatform();
99    }
100    else if (platformId > 0)
101    {
102      platform = Platform.getById(dc, platformId);
103    }
104    else if (item != null)
105    {
106      variant = item.getVariant();
107      platform = item.getPlatform();
108    }
109  }
110  catch (PermissionDeniedException ex)
111  {
112    deniedPlatform = true; 
113  }
114 
115  // Query to load data file types for specific itemType/platform/variant
116  final ItemQuery<DataFileType> fileTypeQuery = 
117    Base.getDataFileTypes(itemType, platform, variant);
118  List<DataFileType> fileTypes = fileTypeQuery.list(dc);
119 
120  String title = "Select data files for " + 
121    HTML.encodeTags((item instanceof Nameable ? ((Nameable)item).getName() : 
122      (item == null ? " new item" : item.toString())));
123
124  final String clazz = "class=\"text\"";
125  final String requiredClazz = "class=\"text required\"";
126  final StringBuilder sb = new StringBuilder();
127  List<File> recentFiles = null;
128 
129  %>
130<%@page import="net.sf.basedb.core.PlatformFileType"%>
131<base:page type="popup" title="<%=title%>">
132  <base:head scripts="" styles="parameters.css">
133 
134  <script language="JavaScript">
135  function init()
136  {
137  }
138  var lastFileInputName;
139  function browseOnClick(inputName)
140  {
141    var frm = document.forms['datafiles'];
142    var url = '../../filemanager/index.jsp?ID=<%=ID%>&cmd=SelectOne&callback=setFileCallback';
143    lastFileInputName = inputName;
144    Main.openPopup(url, 'SelectFile', 1000, 700);
145  }
146  function setFileCallback(fileId, path)
147  {
148    var frm = document.forms['datafiles'];
149    frm[lastFileInputName].value = path;
150    fileOnChange(lastFileInputName);
151  }
152  function fileOnChange(inputName)
153  {
154    var frm = document.forms['datafiles'];
155    if (frm[inputName].value != '')
156    {
157      frm['datafiles.validate'].checked = true;
158      frm['datafiles.metadata'].checked = true;
159    }
160  }
161  function recentOnChange(inputName)
162  {
163    var frm = document.forms['datafiles'];
164    var recentInput = frm['recent.'+inputName];
165    if (recentInput.selectedIndex > 0)
166    {
167      var path = recentInput[recentInput.selectedIndex].text;
168      if (frm[inputName].value != path)
169      {
170        frm[inputName].value = path;
171        fileOnChange(inputName);
172      }
173      recentInput.selectedIndex = 0;
174    }
175  }
176  </script>
177  </base:head>
178 
179  <base:body onload="init()" style="background: #E0E0E0;">
180
181    <form name="datafiles">
182    <table class="form" cellspacing="2" border="0" cellpadding="0" width="100%">
183    <%
184    if (deniedPlatform)
185    {
186      %>
187      <div class="error">Denied</div>
188      <%
189    }
190    %>
191    <%
192    for (DataFileType dft : fileTypes)
193    {
194      PlatformFileType pft = platform == null ?
195        null : platform.getFileType(dft, variant);
196      boolean isRequired = pft == null ? false : pft.isRequired();
197      FileSetMember member = fileSet == null || !fileSet.hasMember(dft) ?
198        null : fileSet.getMember(dft);
199      File file = null;
200      boolean deniedFile = false;
201      if (member != null)
202      {
203        try
204        {
205          file = member.getFile();
206        }
207        catch (PermissionDeniedException ex)
208        {
209          deniedFile = true;
210        }
211      }
212     
213      String path = "";
214      if (file != null)
215      {
216        path = file.getPath().toString();
217      }
218      else if (deniedFile || deniedPlatform)
219      {
220        path = "- denied -";
221      }
222      String inputName= "datafile."+dft.getId();
223      recentFiles = (List<File>)cc.getRecent(dc, Item.FILE, dft.getExternalId());
224      %>
225      <tr>
226        <td class="prompt"><%=HTML.encodeTags(dft.getName())%></td>
227        <td>
228          <table border="0" cellspacing="0" cellpadding="0">
229          <tr>
230            <td>     
231              <input <%=isRequired ? requiredClazz : clazz%> type="text" 
232              name="<%=inputName%>" value="<%=HTML.encodeTags(path)%>"
233              size="50" title="<%=HTML.encodeTags(dft.getDescription())%>"
234              <%=deniedFile || deniedPlatform ? "disabled" : "" %>
235              onchange="fileOnChange('<%=inputName%>')">&nbsp;
236            </td>
237            <td><base:button 
238                title="Browse&hellip;"
239                onclick="<%="browseOnClick('"+inputName+"')"%>"
240                disabled="<%=deniedFile || deniedPlatform %>"
241                />
242            </td>
243          </tr>
244          </table>
245        </td>
246      </tr>
247      <tr>
248        <td align="right"></td>
249        <td>
250        <%
251        if (recentFiles != null && recentFiles.size() > 0)
252        {
253          %>
254          <select name="recent.<%=inputName%>" onchange="recentOnChange('<%=inputName%>')">
255          <option value="">- recently used -
256          <%
257          for (File recent : recentFiles)
258          {
259            %>
260            <option value="<%=recent.getId()%>"><%=HTML.encodeTags(recent.getPath().toString())%>
261            <%
262          }
263          %>
264          </select>
265          <%
266        }
267        %>
268        </td>
269      </tr>
270      <%
271    }
272    %>
273    <tr>
274      <td class="prompt">Validate</td>
275      <td><input type="checkbox" value="1" name="datafiles.validate"></td>
276    </tr>
277    <tr>
278      <td class="prompt">Extract metadata</td>
279      <td><input type="checkbox" value="1" name="datafiles.metadata"></td>
280    </tr>
281    </table>
282
283    </form>
284   
285  </base:body>
286  </base:page>
287  <%
288}
289finally
290{
291  if (dc != null) dc.close();
292}
293%>
Note: See TracBrowser for help on using the repository browser.