source: trunk/www/biomaterials/samples/list_samples.jsp @ 2893

Last change on this file since 2893 was 2893, checked in by Martin Svensson, 17 years ago

Fixes #428 HTML tags in annotation values are not escaped.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 19.5 KB
Line 
1<%-- $Id: list_samples.jsp 2893 2006-11-10 09:35:34Z martin $
2  ------------------------------------------------------------------
3  Copyright (C) Authors contributing to this file.
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA  02111-1307, USA.
22  ------------------------------------------------------------------
23
24  @author Nicklas
25  @version 2.0
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.Sample"
32  import="net.sf.basedb.core.Extract"
33  import="net.sf.basedb.core.BioSource"
34  import="net.sf.basedb.core.BioMaterialEvent"
35  import="net.sf.basedb.core.AnnotationType"
36  import="net.sf.basedb.core.AnnotationSet"
37  import="net.sf.basedb.core.ItemQuery"
38  import="net.sf.basedb.core.Include"
39  import="net.sf.basedb.core.ItemResultIterator"
40  import="net.sf.basedb.core.ItemResultList"
41  import="net.sf.basedb.core.ItemContext"
42  import="net.sf.basedb.core.Permission"
43  import="net.sf.basedb.core.PermissionDeniedException"
44  import="net.sf.basedb.core.PluginDefinition"
45  import="net.sf.basedb.core.query.Hql"
46  import="net.sf.basedb.core.query.Restrictions"
47  import="net.sf.basedb.core.query.Expressions"
48  import="net.sf.basedb.core.query.Orders"
49  import="net.sf.basedb.core.plugin.GuiContext"
50  import="net.sf.basedb.core.plugin.Plugin"
51  import="net.sf.basedb.core.Type"
52  import="net.sf.basedb.util.Enumeration"
53  import="net.sf.basedb.clients.web.Base"
54  import="net.sf.basedb.clients.web.ModeInfo"
55  import="net.sf.basedb.clients.web.PermissionUtil"
56  import="net.sf.basedb.clients.web.util.HTML"
57  import="net.sf.basedb.util.Values"
58  import="java.util.List"
59  import="java.util.Map"
60%>
61<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
62<%@ taglib prefix="tbl" uri="/WEB-INF/table.tld" %>
63<%!
64  private static final Item itemType = Item.SAMPLE;
65  private static final GuiContext guiContext = new GuiContext(itemType, GuiContext.Type.LIST);
66%>
67<%
68final SessionControl sc = Base.getExistingSessionControl(pageContext, Permission.DENIED, itemType);
69final String ID = sc.getId();
70final boolean createPermission = sc.hasPermission(Permission.CREATE, itemType);
71final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null);
72
73final ModeInfo mode = ModeInfo.get(request.getParameter("mode"));
74final String callback = request.getParameter("callback");
75final String title = mode.generateTitle("sample", "samples");
76final DbControl dc = sc.newDbControl();
77ItemResultIterator<Sample> samples = null;
78ItemResultList<AnnotationType> annotationTypes = null;
79try
80{
81  final ItemQuery<AnnotationType> annotationTypeQuery = Base.getAnnotationTypesQuery(itemType);
82  final ItemQuery<Sample> query = Base.getConfiguredQuery(cc, true, Sample.getQuery(), mode);
83  final ItemQuery<Extract> extractQuery = Extract.getQuery();
84  extractQuery.include(cc.getInclude());
85  extractQuery.restrict(Restrictions.eq(Hql.property("parent"), Expressions.parameter("samples")));
86  extractQuery.order(Orders.asc(Hql.property("name")));
87  final boolean createExtractPermission = sc.hasPermission(Permission.CREATE, Item.EXTRACT);
88
89  Map<Plugin.MainType, Integer> pluginCount = PluginDefinition.countPlugins(dc, guiContext);
90  annotationTypes = annotationTypeQuery.list(dc);
91  try
92  {
93    samples = query.iterate(dc);
94  }
95  catch (Throwable t)
96  {
97    cc.setMessage(t.getMessage());
98  }
99  int numListed = 0;
100  %>
101  <base:page title="<%=title==null ? "Samples" : title%>" type="<%=mode.getPageType()%>">
102  <base:head scripts="menu.js,table.js" styles="menu.css,table.css">
103    <script language="JavaScript">
104    var submitPage = 'index.jsp';
105    var formId = 'samples';
106    function newItem()
107    {
108      Main.viewOrEditItem('<%=ID%>', '<%=itemType.name()%>', 0, true);
109    }
110    function newPooledItem()
111    {
112      Table.poolItems(submitPage, '<%=ID%>', formId, '<%=itemType.name()%>', 'NewPooledItem');
113    }
114    function editItem(itemId)
115    {
116      Main.viewOrEditItem('<%=ID%>', '<%=itemType.name()%>', itemId, true);
117    }
118    function viewItem(itemId)
119    {
120      Main.viewOrEditItem('<%=ID%>', '<%=itemType.name()%>', itemId, false);
121    }
122    function itemOnClick(evt, itemId)
123    {
124      Table.itemOnClick(formId, evt, itemId, '<%=mode.getName()%>', viewItem, editItem, returnSelected);
125    }
126    function newExtract(sampleId)
127    {
128      Main.viewOrEditItem('<%=ID%>', 'EXTRACT', 0, true, '&sample_id='+sampleId);
129    }
130    function deleteItems()
131    {
132      var frm = document.forms[formId];
133      if (Forms.numChecked(frm) == 0)
134      {
135        alert('Please select at least one item in the list');
136        return;
137      }
138      frm.action = submitPage;
139      frm.cmd.value = 'DeleteItems';
140      frm.submit();
141    }
142    function restoreItems()
143    {
144      var frm = document.forms[formId];
145      if (Forms.numChecked(frm) == 0)
146      {
147        alert('Please select at least one item in the list');
148        return;
149      }
150      frm.action = submitPage;
151      frm.cmd.value = 'RestoreItems';
152      frm.submit();
153    }
154    function shareItem(itemId)
155    {
156      Main.openPopup('index.jsp?ID=<%=ID%>&cmd=ShareItem&item_id='+itemId, 'ShareSamples', 500, 400);
157    }
158    function shareItems()
159    {
160      Table.shareItems(submitPage, '<%=ID%>', formId, '<%=itemType.name()%>', 'ShareItems');
161    }
162    function configureColumns()
163    {
164      Table.configureColumns('<%=ID%>', formId, '<%=itemType.name()%>', '<%=(String)cc.getObject("defaultColumns")%>');
165    }
166    function runPlugin(cmd)
167    {
168      Table.submitToPopup(formId, cmd, 740, 540);
169    }
170    function returnSelected()
171    {
172      Table.returnSelected(formId, <%=callback != null ? "window.opener."+callback : "null" %>);
173      window.close();
174    }
175    function presetOnChange()
176    {
177      Table.presetOnChange('<%=ID%>', formId, '<%=itemType.name()%>', '<%=(String)cc.getObject("defaultColumns")%>');
178    }
179    </script>
180  </base:head>
181 
182  <base:body>
183    <%
184    if (cc.getMessage() != null)
185    {
186      %>
187      <div class="error"><%=cc.getMessage()%></div>
188      <%
189      cc.setMessage(null);
190    }
191    %>
192    <tbl:table 
193      id="samples" 
194      clazz="itemlist" 
195      columns="<%=cc.getSetting("columns")%>"
196      sortby="<%=cc.getSortProperty()%>" 
197      direction="<%=cc.getSortDirection()%>"
198      title="<%=title%>"
199      action="index.jsp"
200      sc="<%=sc%>"
201      item="<%=itemType%>"
202      >
203      <tbl:hidden 
204        name="mode" 
205        value="<%=mode.getName()%>" 
206      />
207      <tbl:hidden 
208        name="callback" 
209        value="<%=callback%>" 
210        skip="<%=callback == null%>" 
211      />
212      <tbl:columndef 
213        id="name"
214        property="name"
215        datatype="string"
216        title="Name"
217        sortable="true" 
218        filterable="true"
219        exportable="true"
220        show="always" 
221      />
222      <tbl:columndef 
223        id="externalId"
224        property="externalId"
225        datatype="string"
226        title="External id"
227        sortable="true" 
228        filterable="true"
229        exportable="true"
230      />     
231      <tbl:columndef 
232        id="originalQuantity"
233        property="originalQuantity"
234        datatype="float"
235        title="Original quantity (µg)"
236        sortable="true" 
237        filterable="true"
238        exportable="true"
239      />
240      <tbl:columndef 
241        id="remainingQuantity"
242        property="remainingQuantity"
243        datatype="float"
244        title="Remaining quantity (µg)"
245        sortable="true" 
246        filterable="true"
247        exportable="true"
248      />
249      <tbl:columndef 
250        id="protocol"
251        property="creationEvent.protocol"
252        sortproperty="creationEvent.protocol.name"
253        filterproperty="creationEvent.protocol.name"
254        exportproperty="creationEvent.protocol.name"
255        datatype="string"
256        title="Protocol"
257        sortable="true" 
258        filterable="true"
259        exportable="true"
260      />
261      <tbl:columndef 
262        id="eventDate"
263        property="creationEvent.eventDate"
264        datatype="date"
265        title="Created"
266        sortable="true" 
267        filterable="true"
268        exportable="true"
269      />
270      <tbl:columndef 
271        id="entryDate"
272        property="creationEvent.entryDate"
273        datatype="date"
274        title="Registered"
275        sortable="true" 
276        filterable="true"
277        exportable="true"
278      />
279      <tbl:columndef 
280        id="pooled"
281        property="pooled"
282        datatype="boolean"
283        title="Pooled"
284        sortable="true" 
285        filterable="true"
286        exportable="true"
287      />
288      <tbl:columndef
289        id="parents"
290        title="Parents"
291      />
292      <tbl:columndef
293        id="extracts"
294        title="Extracts"
295      />     
296      <tbl:columndef 
297        id="owner"
298        property="owner.name"
299        datatype="string"
300        title="Owner"
301        sortable="true" 
302        filterable="true"
303        exportable="true"
304      />
305      <tbl:columndef 
306        id="description"
307        property="description"
308        datatype="string"
309        title="Description" 
310        sortable="true" 
311        filterable="true" 
312        exportable="true"
313      />     
314      <%
315      for (AnnotationType at : annotationTypes)
316      {
317        Enumeration<String, String> annotationEnum = null;
318        if (at.isEnumeration())
319        {
320          annotationEnum = new Enumeration<String, String>();
321          List<?> values = at.getValues();
322          for (Object value : values)
323          {
324            String encoded = HTML.encodeTags(value.toString());
325            annotationEnum.add(encoded, encoded);
326          }
327        }
328        %>
329        <tbl:columndef 
330          id="<%="at"+at.getId()%>"
331          title="<%=HTML.encodeTags(at.getName())+" [A]"%>" 
332          property="<%="#"+at.getId()%>"
333          annotation="true"
334          datatype="<%=at.getValueType().getStringValue()%>"
335          enumeration="<%=annotationEnum%>"
336          sortable="false" 
337          filterable="true" 
338          exportable="true"
339        />
340        <%
341      }
342      %>
343      <tbl:columndef
344        id="permission"
345        title="Permission"
346      />
347      <tbl:toolbar
348        visible="<%=mode.hasToolbar()%>"
349        >
350        <tbl:button 
351          disabled="<%=createPermission ? false : true%>" 
352          image="<%=createPermission ? "new.gif" : "new_disabled.gif"%>" 
353          onclick="newItem()" 
354          title="New&hellip;" 
355          tooltip="<%=createPermission ? "Create new sample" : "You do not have permission to create samples"%>" 
356        />
357        <tbl:button 
358          disabled="<%=createPermission ? false : true%>" 
359          image="<%=createPermission ? "new_pooled.gif" : "new_pooled_disabled.gif"%>" 
360          onclick="newPooledItem()" 
361          title="Pool&hellip;" 
362          tooltip="<%=createPermission ? "Create new pooled sample" : "You do not have permission to create samples"%>" 
363        />
364        <tbl:button 
365          image="delete.gif"
366          onclick="deleteItems()" 
367          title="Delete" 
368          tooltip="Delete the selected items" 
369        />
370        <tbl:button 
371          image="restore.gif"
372          onclick="restoreItems()" 
373          title="Restore" 
374          tooltip="Restore the selected (deleted) items"
375        />
376        <tbl:button 
377          image="share.gif"
378          onclick="shareItems()" 
379          title="Share&hellip;" 
380          tooltip="Share the selected items"
381        />
382        <tbl:button 
383          image="columns.gif" 
384          onclick="configureColumns()" 
385          title="Columns&hellip;" 
386          tooltip="Show, hide and re-order columns" 
387        />
388        <tbl:button 
389          image="import.gif" 
390          onclick="runPlugin('ImportItems')" 
391          title="Import&hellip;" 
392          tooltip="Import data" 
393          visible="<%=pluginCount.containsKey(Plugin.MainType.IMPORT)%>"
394        />
395        <tbl:button 
396          image="export.gif" 
397          onclick="runPlugin('ExportItems')" 
398          title="Export&hellip;" 
399          tooltip="Export data" 
400          visible="<%=pluginCount.containsKey(Plugin.MainType.EXPORT)%>"
401        />
402        <tbl:button 
403          image="runplugin.gif" 
404          onclick="runPlugin('RunListPlugin')" 
405          title="Run plugin&hellip;" 
406          tooltip="Run a plugin" 
407          visible="<%=pluginCount.containsKey(Plugin.MainType.OTHER)%>"
408        />
409      </tbl:toolbar>
410      <tbl:navigator
411        page="<%=cc.getPage()%>" 
412        rowsperpage="<%=cc.getRowsPerPage()%>" 
413        totalrows="<%=samples == null ? 0 : samples.getTotalCount()%>" 
414        visible="<%=mode.hasNavigator()%>"
415      />
416      <tbl:data>
417        <tbl:columns>
418        <tbl:presetselector 
419          clazz="columnheader"
420          colspan="3"
421          onchange="presetOnChange()"
422        />
423        </tbl:columns>
424
425        <tr>
426          <tbl:header 
427            clazz="index"
428            >&nbsp;</tbl:header>
429          <tbl:header 
430            clazz="check" 
431            visible="<%=mode.hasCheck()%>"
432            ><base:icon 
433              image="check_uncheck.gif" 
434              tooltip="Check/uncheck all" 
435              onclick="Forms.checkUncheck(document.forms[formId])" style="align: left;"
436            /></tbl:header>
437          <tbl:header 
438            clazz="check" 
439            visible="<%=mode.hasRadio()%>"
440            >&nbsp;</tbl:header>
441          <tbl:header 
442            clazz="icons" 
443            visible="<%=mode.hasIcons()%>"
444            >&nbsp;</tbl:header>
445          <tbl:propertyfilter />
446        </tr>
447         
448          <tbl:rows>
449          <%
450          int index = cc.getPage()*cc.getRowsPerPage();
451          int selectedItemId = cc.getId();
452          if (samples != null)
453          {           
454            while (samples.hasNext())
455            {
456              Sample item = samples.next();
457              BioMaterialEvent creationEvent = item.getCreationEvent();
458              int itemId = item.getId();
459              boolean usePermission = item.hasPermission(Permission.USE);
460              String openSharePopup = "shareItem("+itemId+")";
461              boolean sharePermission = item.hasPermission(Permission.SET_PERMISSION);
462              boolean writePermission = item.hasPermission(Permission.WRITE);
463              String tooltip = mode.isSelectionMode() ?
464                  "Select this item" : "View this item" + (writePermission ? " (use CTRL, ALT or SHIFT to edit)" : "");
465              String name = HTML.encodeTags(item.getName());
466              index++;
467              numListed++;
468              %>
469              <tbl:row>
470                <tbl:header 
471                  clazz="index"
472                  ><%=index%></tbl:header>
473                <tbl:header 
474                  clazz="check" 
475                  visible="<%=mode.hasCheck()%>"
476                  ><input 
477                    type="checkbox" 
478                    name="<%=itemId%>" 
479                    value="<%=itemId%>" 
480                    title="<%=name%>" 
481                    <%=cc.getSelected().contains(itemId) ? "checked" : ""%> 
482                  ></tbl:header>
483                <tbl:header 
484                  clazz="check" 
485                  visible="<%=mode.hasRadio()%>"
486                  ><input 
487                    type="radio" 
488                    name="item_id" 
489                    value="<%=itemId%>" 
490                    title="<%=name%>" 
491                    <%=selectedItemId == itemId ? "checked" : ""%>
492                  ></tbl:header>
493                <tbl:header 
494                  clazz="icons" 
495                  visible="<%=mode.hasIcons()%>"
496                  ><base:icon 
497                    image="deleted.gif" 
498                    tooltip="This item has been scheduled for deletion" 
499                    visible="<%=item.isRemoved()%>"
500                  /><base:icon 
501                    image="<%=sharePermission ? "shared.gif" : "shared_disabled.gif"%>" 
502                    onclick="<%=sharePermission ? openSharePopup : null%>"
503                    tooltip="This item is shared to other users, groups and/or projects" 
504                    visible="<%=item.isShared()%>"
505                  />&nbsp;</tbl:header>
506                <tbl:cell column="name"><div class="link" 
507                  onclick="itemOnClick(<%=writePermission ? "event" : null%>, <%=itemId%>)"
508                  title="<%=tooltip%>"><%=name%></div></tbl:cell>
509                <tbl:cell column="externalId"><%=HTML.encodeTags(item.getExternalId())%></tbl:cell>               
510                <tbl:cell column="originalQuantity"><%=Values.formatNumber(item.getOriginalQuantity(), 2)%></tbl:cell>
511                <tbl:cell column="remainingQuantity"><%=Values.formatNumber(item.getRemainingQuantity(), 2)%></tbl:cell>
512                <tbl:cell column="pooled"><%=item.isPooled()%></tbl:cell>
513                <tbl:cell column="protocol"
514                  ><base:propertyvalue 
515                    item="<%=creationEvent%>" 
516                    property="protocol"
517                    enableEditLink="<%=mode.hasEditLink()%>" 
518                    enablePropertyLink="<%=mode.hasPropertyLink()%>"
519                  /></tbl:cell>
520                <tbl:cell column="eventDate"><%=Values.formatDate(creationEvent.getEventDate())%></tbl:cell>
521                <tbl:cell column="entryDate"><%=Values.formatDate(creationEvent.getEntryDate())%></tbl:cell>
522                <tbl:cell column="parents">
523                  <%
524                  if (!item.isPooled())
525                  {
526                  %>
527                    <base:propertyvalue item="<%=item%>" property="parent"/>
528                  <%
529                  }
530                  else
531                  {
532                    String separator = "";
533                    ItemQuery<Sample> parentQuery = (ItemQuery<Sample>)item.getCreationEvent().getSources();
534                    parentQuery.include(Include.MINE, Include.SHARED, Include.IN_PROJECT, Include.OTHERS);
535                    parentQuery.order(Orders.asc(Hql.property("name")));                   
536                    for(Sample s : parentQuery.list(dc))
537                    {
538                      out.write(separator);
539                      if (mode.hasPropertyLink())
540                      {
541                        out.write(Base.getLinkedName(ID, s, false, mode.hasEditLink()));
542                      }
543                      else
544                      {
545                        out.write(HTML.encodeTags(s.getName()));
546                      }
547                      separator = ", ";
548                    }
549                   
550                  }
551                  %>
552                  </tbl:cell>
553                <tbl:cell column="extracts">
554                  <%
555                  extractQuery.setParameter("samples", itemId, Type.INT);
556                  try
557                  {
558                    String separator = "";
559                    for(Extract e : extractQuery.list(dc))
560                    {
561                      out.write(separator);
562                      if (mode.hasPropertyLink())
563                      {
564                        out.write(Base.getLinkedName(ID, e, false, mode.hasEditLink()));
565                      }
566                      else
567                      {
568                        out.write(HTML.encodeTags(e.getName()));
569                      }
570                      separator = ", ";
571                    }
572                  }
573                  catch (Throwable t)
574                  {
575                    %>
576                    <div class="error"><%=t.getMessage()%></div>
577                    <%
578                  }
579                  %>
580                  <base:icon
581                    image="add.png" 
582                    onclick="<%="newExtract("+itemId+")"%>" 
583                    tooltip="Create new sample" 
584                    visible="<%=mode.hasEditLink() && createExtractPermission && usePermission %>"
585                  />
586                </tbl:cell>
587                <tbl:cell column="owner"
588                  ><base:propertyvalue 
589                    item="<%=item%>" 
590                    property="owner"
591                    enableEditLink="<%=mode.hasEditLink()%>" 
592                    enablePropertyLink="<%=mode.hasPropertyLink()%>" 
593                  /></tbl:cell>
594                <tbl:cell column="description"><%=HTML.encodeTags(item.getDescription())%></tbl:cell>               
595                <%
596                AnnotationSet as = item.isAnnotated() ? item.getAnnotationSet() : null;
597                for (AnnotationType at : annotationTypes)
598                {
599                  %>
600                  <tbl:cell column="<%="at"+at.getId()%>">
601                    <%
602                    List<?> values = as == null || !as.hasAnnotation(at) ? null : as.getAnnotation(at).getValues();
603                    %>
604                    <%=values == null || values.size() == 0 ? "" : HTML.encodeTags(Values.getString(values, ", ", true))%>
605                  </tbl:cell>
606                  <%
607                }
608                %>
609                <tbl:cell column="permission"><%=PermissionUtil.getShortPermissions(item)%></tbl:cell>
610              </tbl:row>
611              <%
612              }
613            }
614          %>
615          </tbl:rows>
616      </tbl:data>
617      <%
618      if (numListed == 0)
619      {
620        %>
621        <tbl:panel><%=samples == null || samples.getTotalCount() == 0 ? "No samples where found" : "No samples on this page. Please select another page!" %></tbl:panel>
622        <%
623      }
624      else
625      {
626        %>
627        <tbl:navigator
628          page="<%=cc.getPage()%>" 
629          rowsperpage="<%=cc.getRowsPerPage()%>" 
630          totalrows="<%=samples == null ? 0 : samples.getTotalCount()%>" 
631          visible="<%=mode.hasNavigator()%>"
632          locked="true"
633        />
634        <%
635      }
636      %>
637    </tbl:table>
638    <base:buttongroup align="center" clazz="fixedatbottom">
639      <base:button onclick="returnSelected();" title="Ok" visible="<%=mode.hasOkButton()%>" />
640      <base:button onclick="window.close();" title="Cancel" visible="<%=mode.hasCancelButton()%>" />
641      <base:button onclick="window.close();" title="Close" visible="<%=mode.hasCloseButton()%>" />
642    </base:buttongroup>
643    <br><br><br>
644  </base:body>
645  </base:page>
646  <%
647}
648finally
649{
650  if (samples != null) samples.close();
651  if (dc != null) dc.close();
652}
653%>
Note: See TracBrowser for help on using the repository browser.