source: trunk/www/common/datafiles/select_files.jsp @ 5698

Last change on this file since 5698 was 5698, checked in by Nicklas Nordborg, 12 years ago

References #597: Subtypes of items

Implemented linking of item subtypes and data file types so that we can show/hide the suitable file types when editing an item.

This should more or less complete the item subtype feature. I'll keep the ticket open a little longer in case something pops up.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 11.2 KB
Line 
1<%-- $Id:select_files.jsp 3820 2007-10-12 10:03:18Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, 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 3
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 BASE. If not, see <http://www.gnu.org/licenses/>.
22  ------------------------------------------------------------------
23 
24--%>
25<%@ page pageEncoding="UTF-8" session="false"
26  import="net.sf.basedb.core.SessionControl"
27  import="net.sf.basedb.core.DbControl"
28  import="net.sf.basedb.core.Item"
29  import="net.sf.basedb.core.ItemContext"
30  import="net.sf.basedb.core.Type"
31  import="net.sf.basedb.core.BasicItem"
32  import="net.sf.basedb.core.Permission"
33  import="net.sf.basedb.core.Nameable"
34  import="net.sf.basedb.core.FileStoreEnabled"
35  import="net.sf.basedb.core.FileSet"
36  import="net.sf.basedb.core.FileSetMember"
37  import="net.sf.basedb.core.File"
38  import="net.sf.basedb.core.Platform"
39  import="net.sf.basedb.core.PlatformVariant"
40  import="net.sf.basedb.core.PlatformFileType"
41  import="net.sf.basedb.core.DataFileType"
42  import="net.sf.basedb.core.Subtypable"
43  import="net.sf.basedb.core.ItemSubtype"
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 int itemSubtypeId = Values.getInt(request.getParameter("itemsubtype_id"), -1);
75final ItemContext cc = sc.getCurrentContext(itemType);
76
77final DbControl dc = sc.newDbControl();
78try
79{
80
81  // Get the current item; null if we are editing a new item
82  final FileStoreEnabled item = itemId == 0 ? null : (FileStoreEnabled)itemType.getById(dc, itemId);
83  FileSet fileSet = null;
84  if (item != null && item.hasFileSet())
85  {
86    fileSet = item.getFileSet();
87  }
88
89  // Get the current platform/variant/itemsubtype
90  // -- if not submitted in URL use values from current item
91  PlatformVariant variant = null;
92  Platform platform = null;
93  ItemSubtype itemSubtype = null;
94  boolean deniedPlatform = false;
95  try
96  {
97    if (itemSubtypeId > 0)
98    {
99      itemSubtype = ItemSubtype.getById(dc, itemSubtypeId);
100    }
101    else if (item instanceof Subtypable)
102    {
103      itemSubtype = ((Subtypable)item).getItemSubtype();
104    }
105    if (variantId > 0)
106    {
107      variant = PlatformVariant.getById(dc, variantId);
108      platform = variant.getPlatform();
109    }
110    else if (platformId > 0)
111    {
112      platform = Platform.getById(dc, platformId);
113    }
114    else if (item != null)
115    {
116      variant = item.getVariant();
117      platform = item.getPlatform();
118    }
119  }
120  catch (PermissionDeniedException ex)
121  {
122    deniedPlatform = true; 
123  }
124 
125  // Query to load data file types for specific itemType/platform/variant
126  final ItemQuery<DataFileType> fileTypeQuery = 
127    Base.getDataFileTypes(itemType, item, platform, variant, itemSubtype);
128  List<DataFileType> fileTypes = fileTypeQuery.list(dc);
129 
130 
131  String title = "Select data files for " + 
132    HTML.encodeTags((item instanceof Nameable ? ((Nameable)item).getName() : 
133      (item == null ? " new item" : item.toString())));
134
135  final String clazz = "class=\"text\"";
136  final String requiredClazz = "class=\"text required\"";
137  final StringBuilder sb = new StringBuilder();
138  List<File> recentFiles = null;
139 
140  %>
141<base:page type="popup" title="<%=title%>">
142  <base:head scripts="" styles="parameters.css">
143 
144  <script language="JavaScript">
145  function init()
146  {
147  }
148  var lastFileInputName;
149  var updateCheckboxes = false;
150  function browseOnClick(inputName, extension, updateCheck)
151  {
152    var frm = document.forms['datafiles'];
153    updateCheckboxes = updateCheck;
154    var url = '../../filemanager/index.jsp?ID=<%=ID%>&cmd=SelectOne&callback=setFileCallback';
155    if (extension)
156    {
157      url += '&resetTemporary=1&tmpfilter:STRING:name='+escape('%.' + extension);
158    }
159    else
160    {
161      url += '&resetTemporary=1&filter:STRING:name=';
162    }
163    lastFileInputName = inputName;
164    Main.openPopup(url, 'SelectFile', 1000, 700);
165  }
166  function setFileCallback(fileId, path)
167  {
168    var frm = document.forms['datafiles'];
169    frm[lastFileInputName].value = path;
170    fileOnChange(lastFileInputName, updateCheckboxes);
171  }
172  function fileOnChange(inputName, updateCheckbox)
173  {
174    var frm = document.forms['datafiles'];
175    var validateCheckbox = frm['datafiles.validate'];
176
177    // Check boxes should be updated if the data file type
178    //supports validation or metadata-extraction
179    if (updateCheckbox)
180    {
181      if (frm[inputName].value != '')
182      {
183        // Data filetype supports validation
184        if (validateCheckbox)
185        {       
186          if (validateCheckbox.disabled) validateCheckbox.disabled = false; 
187          validateCheckbox.checked = "1";
188        }
189      }
190    }   
191  }
192  function recentOnChange(inputName, updateCheck)
193  {
194    var frm = document.forms['datafiles'];
195    var recentInput = frm['recent.'+inputName];
196    if (recentInput.selectedIndex > 0)
197    {
198      var option = recentInput[recentInput.selectedIndex];
199      var path = option.value == '' ? '' : option.text;
200      if (frm[inputName].value != path)
201      {
202        frm[inputName].value = path;
203        fileOnChange(inputName, updateCheck);
204      }
205      recentInput.selectedIndex = 0;
206    }
207  }
208  </script>
209  </base:head>
210 
211  <base:body onload="init()" style="background: #E0E0E0;">
212
213    <form name="datafiles">
214    <%
215    if (fileTypes.size() == 0)
216    {
217      String what = "";
218      if (platform != null)
219      {
220        what = "'" + HTML.encodeTags(platform.getName()) + "' platform";
221      }
222      else if (itemSubtype != null)
223      {
224        what = "'" + HTML.encodeTags(itemSubtype.getName()) + "' subtype";
225      }
226      %>
227      <div class="error">
228        The <%=what%> doesn't define any file types for
229        <%=itemType.toString() %> items.
230      </div>
231      <%
232    }
233    else if (deniedPlatform)
234    {
235      %>
236      <div class="error">Denied</div>
237      <%
238    }
239    else
240    {
241      %>
242      <table class="form" cellspacing="2" border="0" cellpadding="0" width="100%">
243      <%
244      boolean hasNonPlatformFiles = false;     
245      boolean activateCheckBoxes = false;
246
247      boolean validationSupport = false;
248     
249      for (DataFileType dft : fileTypes)
250      {
251        PlatformFileType pft = platform == null ?
252          null : platform.getFileType(dft, variant);
253        // If file type is not registered with a variant, also check if it is inherited from platform
254        if (pft == null && variant != null) pft = platform.getFileType(dft, null);
255        boolean isPartOfPlatform = pft != null || (itemSubtype != null && itemSubtype.isAssociatedDataFileType(dft));
256        boolean isRequired = pft == null ? false : pft.isRequired();
257        FileSetMember member = fileSet == null || !fileSet.hasMember(dft) ?
258          null : fileSet.getMember(dft);
259        File file = null;
260        boolean deniedFile = false;
261        String extension = dft.getExtension();
262       
263        boolean hasValidator = dft.hasActiveValidator(dc);
264        boolean affectCheckboxes = hasValidator;
265        validationSupport |= hasValidator;
266        if (member != null)
267        {
268          try
269          {
270            file = member.getFile();           
271          }
272          catch (PermissionDeniedException ex)
273          {
274            deniedFile = true;
275          }
276        }
277       
278        String path = "";
279        if (file != null)
280        {
281          path = file.getPath().toString();
282          activateCheckBoxes |= hasValidator;
283        }
284        else if (deniedFile || deniedPlatform)
285        {
286          path = "- denied -";
287        }
288        String inputName= "datafile."+dft.getId();
289        recentFiles = (List<File>)cc.getRecent(dc, Item.FILE, dft.getExternalId());
290        %>
291        <tr>
292          <td class="prompt "><%=HTML.encodeTags(dft.getName())%>
293          <%
294          if (!isPartOfPlatform && !deniedPlatform)
295          {
296            hasNonPlatformFiles = true;
297            %>
298            <base:icon image="warning.gif" tooltip="This file is not part of the platform/subtype" />
299            <%
300          }
301          %>
302          </td>
303          <td>
304            <table border="0" cellspacing="0" cellpadding="0">
305            <tr>
306              <td>     
307                <input <%=isRequired ? requiredClazz : clazz%> type="text" 
308                name="<%=inputName%>" value="<%=HTML.encodeTags(path)%>"
309                size="50" title="<%=HTML.encodeTags(dft.getDescription())%>"
310                <%=deniedFile || deniedPlatform ? "disabled" : "" %>
311                onchange="fileOnChange('<%=inputName%>', <%=affectCheckboxes %>)">&nbsp;
312              </td>
313              <td><base:button 
314                  title="Browse&hellip;"
315                  onclick="<%="browseOnClick('"+inputName+"', '" + HTML.javaScriptEncode(extension) + "', "+ affectCheckboxes + ")"%>"
316                  disabled="<%=deniedFile || deniedPlatform %>"
317                  />
318              </td>
319            </tr>
320            </table>
321          </td>
322        </tr>
323        <tr>
324          <td align="right"></td>
325          <td>
326          <%
327          if (recentFiles != null && recentFiles.size() > 0)
328          {
329            %>
330            <select name="recent.<%=inputName%>" onchange="recentOnChange('<%=inputName%>', <%=affectCheckboxes%>)">
331            <option value="">- recently used -
332            <option value="">- clear -
333            <%
334            for (File recent : recentFiles)
335            {
336              %>
337              <option value="<%=recent.getId()%>"><%=HTML.encodeTags(recent.getPath().toString())%>
338              <%
339            }
340            %>
341            </select>
342            <%
343          }
344          %>
345          </td>
346        </tr>
347        <%
348      }
349      if (validationSupport)
350      {
351      %>
352      <tr>
353        <td class="prompt">Validate</td>
354        <td><input type="checkbox" value="1" <%=activateCheckBoxes ? "" : "disabled"%> name="datafiles.validate"></td>
355      </tr>
356      <%
357      }
358      %>
359      </table>
360      <%
361      if (hasNonPlatformFiles)
362      {
363        if (platform != null)
364        {
365          %>
366          <div align="right">
367          <base:icon image="warning.gif" /> 
368          = The file type is not part of the <i><%=HTML.encodeTags(platform.getName())%></i> platform
369          </div>
370          <%
371        }
372        else if (itemSubtype != null)
373        {
374          %>
375          <div align="right">
376          <base:icon image="warning.gif" /> 
377          = The file type is not part of the <i><%=HTML.encodeTags(itemSubtype.getName())%></i> subtype
378          </div>
379          <%
380        }
381      }
382    }
383    %>
384
385    </form>
386   
387  </base:body>
388  </base:page>
389  <%
390}
391finally
392{
393  if (dc != null) dc.close();
394}
395%>
Note: See TracBrowser for help on using the repository browser.