source: trunk/www/common/annotations/inherit.jsp @ 5919

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

References #1655: GUI improvements

Fixes image alignment in several places.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 14.0 KB
Line 
1<%-- $Id: inherit.jsp 5919 2012-01-09 13:51:06Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
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  This page is used to display and modify the attributes of
25  the of a news item
26
27  @author Nicklas
28  @version 2.0
29--%>
30<%@ page pageEncoding="UTF-8" session="false"
31  import="net.sf.basedb.core.SessionControl"
32  import="net.sf.basedb.core.DbControl"
33  import="net.sf.basedb.core.Item"
34  import="net.sf.basedb.core.Type"
35  import="net.sf.basedb.core.BasicItem"
36  import="net.sf.basedb.core.Permission"
37  import="net.sf.basedb.core.Annotatable"
38  import="net.sf.basedb.core.AnnotationSet"
39  import="net.sf.basedb.core.Annotation"
40  import="net.sf.basedb.core.RawBioAssay"
41  import="net.sf.basedb.core.PhysicalBioAssay"
42  import="net.sf.basedb.core.DerivedBioAssay"
43  import="net.sf.basedb.core.Extract"
44  import="net.sf.basedb.core.ItemQuery"
45  import="net.sf.basedb.core.Include"
46  import="net.sf.basedb.core.Nameable"
47  import="net.sf.basedb.core.ItemResultList"
48  import="net.sf.basedb.core.AnnotationType"
49  import="net.sf.basedb.core.PermissionDeniedException"
50  import="net.sf.basedb.core.query.Orders"
51  import="net.sf.basedb.core.query.Hql"
52  import="net.sf.basedb.util.Tree"
53  import="net.sf.basedb.clients.web.Base"
54  import="net.sf.basedb.clients.web.WebException"
55  import="net.sf.basedb.clients.web.util.HTML"
56  import="net.sf.basedb.util.Values"
57  import="net.sf.basedb.util.formatter.Formatter"
58  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
59  import="java.util.List"
60  import="java.util.Set"
61  import="java.util.HashSet"
62  import="java.util.Map"
63  import="java.util.HashMap"
64%>
65<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
66<%!
67private void loadParents(Set<AnnotationSet> parentAnnotations, Set<Annotatable> parentItems, Annotatable current, Annotatable root)
68{
69  Set<Annotatable> parents = null;
70  if (current instanceof PhysicalBioAssay)
71  {
72    Extract extract = null;
73    PhysicalBioAssay bioAssay = (PhysicalBioAssay)current;
74    try
75    {
76      if (root instanceof RawBioAssay)
77      {
78        extract = ((RawBioAssay)root).getParentExtract();
79      }
80      else if (root instanceof DerivedBioAssay)
81      {
82        extract = ((DerivedBioAssay)root).getExtract();
83      }
84    }
85    catch (PermissionDeniedException ex)
86    {}
87    parents = bioAssay.getAnnotatableParents(0, extract);
88  }
89  else
90  {
91    parents = current.getAnnotatableParents();
92  }
93  if (parents == null) return;
94  for (Annotatable parent : parents)
95  {
96    if (parent != null && parentItems.add(parent))
97    {
98      if (parent.hasPermission(Permission.USE) && parent.isAnnotated())
99      {
100        parentAnnotations.add(parent.getAnnotationSet());
101      }
102      loadParents(parentAnnotations, parentItems, parent, root);
103    }
104  }
105}
106
107private String generateJoustTree(Set<AnnotationSet> parentAnnotations, Set<AnnotationSet> nonParents, Set<Annotation> inheritedAnnotations, Set<AnnotationSet> inheritedSets)
108{
109  StringBuilder sb = new StringBuilder();
110  String itemIcon = "Annotatable";
111  String annotationIcon = "Annotation";
112  String parameterIcon = "Parameter";
113  String nonParentIcon = "NonParent";
114 
115  for (AnnotationSet as : parentAnnotations)
116  {
117    Annotatable item = null;
118    try
119    {
120      item = as.getItem();
121    }
122    catch (PermissionDeniedException ex)
123    {}
124   
125    String icon = nonParents.contains(as) ? nonParentIcon : itemIcon;
126   
127    boolean setInherited = inheritedSets != null && inheritedSets.contains(as);
128
129    String input = "<input type=\"checkbox\""+
130      " name=\"AS"+as.getId()+"\""+
131      (setInherited ? " checked" : "")+
132      " onclick=\"itemOnToggle(event, "+as.getId()+")\""+
133      ">";
134    String name = item == null ? "<i>- denied -</i>" : 
135      HTML.javaScriptEncode(((Nameable)item).getName()) + " (" + item.getType() + ")";
136    sb.append("var item").append(as.getId());
137    sb.append(" = JoustMenu.addMenuItem(-1"); 
138    sb.append(",'").append(icon).append("'");
139    sb.append(",'").append(input).append(name).append("'");
140    sb.append(", 'itemOnClick(event, ").append(as.getId()).append(")'");
141    sb.append(", '', 'AS").append(as.getId()).append("', true);\n");
142
143    List<Annotation> annotations = as.getAnnotations().list(as.getDbControl());
144    SessionControl sc = as.getSessionControl();
145    for (Annotation a : annotations)
146    {
147      Formatter formatter = FormatterFactory.getAnnotationFormatter(sc, a, null);
148      boolean inherited = inheritedAnnotations != null && inheritedAnnotations.contains(a);
149      AnnotationType at = null;
150      String annotationName = "<i>- denied -</i>";
151      boolean isParameter = false;
152      try
153      {
154        at = a.getAnnotationType();
155        annotationName = at.getName();
156        isParameter = at.isProtocolParameter();
157      }
158      catch (PermissionDeniedException ex)
159      {}
160      String values = Values.getString(a.getValues(null), ", ", true, formatter);
161      name = HTML.javaScriptEncode(annotationName +
162        " [" + Values.trimString(values, 20)+"]");
163      input = "<input type=\"checkbox\""+
164        " name=\"A"+a.getId()+"\""+
165        (inherited || setInherited ? " checked" : "")+
166        (setInherited ? " disabled" : "")+
167        " onclick=\"annotationOnToggle(event, "+a.getId()+")\""+
168        ">";
169      sb.append("JoustMenu.addChildItem(item" + as.getId());
170      sb.append(",'").append(isParameter ? parameterIcon : annotationIcon).append("'");
171      sb.append(",'").append(input).append(name).append("'");
172      sb.append(", 'annotationOnClick(event, ").append(a.getId()).append(")'");
173      sb.append(", '', 'A").append(a.getId()).append("');\n");
174      sb.append("annotationValues['A").append(a.getId()).append("'] = ");
175      sb.append("'").append(HTML.javaScriptEncode(values)).append("';\n");
176    }
177  }
178  return sb.toString();
179}
180%>
181<%
182final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
183final String ID = sc.getId();
184final float scale = Base.getScale(sc);
185final Item itemType = Item.valueOf(request.getParameter("item_type"));
186final int itemId = Values.getInt(request.getParameter("item_id"));
187final boolean standalone = Values.getBoolean(request.getParameter("standalone"));
188
189final DbControl dc = sc.newDbControl();
190ItemResultList<AnnotationType> annotationTypes = null;
191try
192{
193  final Annotatable item = itemId == 0 ? null : (Annotatable)itemType.getById(dc, itemId);
194  Set<AnnotationSet> parentAnnotations = new java.util.LinkedHashSet<AnnotationSet>();
195  Set<Annotatable> parentItems = new HashSet<Annotatable>();
196  Set<AnnotationSet> nonParents = new HashSet<AnnotationSet>();
197
198  if (item != null)
199  {
200    // Get all annotated parents and their annotation sets
201    loadParents(parentAnnotations, parentItems, item, item);
202  }
203  if (standalone)
204  {
205    sc.getCurrentContext(itemType).setObject("item", item); 
206  }
207  String title = "Inherit annotations to " + 
208    HTML.encodeTags((item instanceof Nameable ? ((Nameable)item).getName() : 
209    (item == null ? " new item" : item.toString())));
210
211  // Parent items may have been submitted by caller
212  String[] parents = request.getParameterValues("parents");
213  // Each parameter is ITEMTYPE:ID:ID:...
214  if (parents != null)
215  {
216    for (String parent : parents)
217    {
218      String[] temp = parent.split(":");
219      Item parentType = Item.valueOf(temp[0]);
220      for (int i = 1; i < temp.length; ++i)
221      {
222        int parentId = Values.getInt(temp[i], -1);
223        if (parentId != -1) 
224        {
225          Annotatable parentItem = (Annotatable)parentType.getById(dc, parentId);
226          if (parentItems.add(parentItem))
227          {
228            // This is a new parent
229            if (parentItem.hasPermission(Permission.USE) && parentItem.isAnnotated()) 
230            {
231              parentAnnotations.add(parentItem.getAnnotationSet());
232            }
233            loadParents(parentAnnotations, parentItems, parentItem, item);
234          }
235        }
236      }
237    }
238  }
239 
240  // Get the currently inherited annotations
241  final AnnotationSet as = item != null && item.isAnnotated() ? item.getAnnotationSet() : null;
242  Set<Annotation> inheritedAnnotations = null;
243  Set<AnnotationSet> inheritedSets = null;
244  if (as != null)
245  {
246    ItemQuery<Annotation> inheritedQuery = as.getInheritedAnnotations();
247    inheritedQuery.order(Orders.asc(Hql.property("annotationSet")));
248    inheritedQuery.order(Orders.asc(Hql.property("annotationType.name")));
249    inheritedAnnotations = new HashSet<Annotation>(inheritedQuery.list(dc));
250    for (Annotation a : inheritedAnnotations)
251    {
252      AnnotationSet from = a.getAnnotationSet();
253      if (!parentAnnotations.contains(from) && from.hasPermission(Permission.USE))
254      {
255        parentAnnotations.add(from);
256        nonParents.add(from);
257      }
258    }
259   
260    ItemQuery<AnnotationSet> setQuery = as.getInheritedAnnotationSets();
261    inheritedSets = new HashSet<AnnotationSet>(setQuery.list(dc));
262    for (AnnotationSet ias : inheritedSets)
263    {
264      if (!parentAnnotations.contains(ias) && ias.hasPermission(Permission.USE))
265      {
266        parentAnnotations.add(ias);
267        nonParents.add(ias);
268      }
269    }
270  }
271  %>
272  <base:page type="popup" title="Inherit annotations">
273  <base:head  scripts="annotations.js,parameters.js,newjoust.js,linkitems.js" styles="parameters.css,newjoust.css">
274  <script language="JavaScript">
275  var annotationValues = new Array();
276  function init()
277  {
278    <%
279    if (parentAnnotations.size() > 0)
280    {
281      %>
282      IconStore.init();
283      var path = getRoot() + 'images/joust/';
284      IconStore.addIcon('Annotatable', path + 'annotatable.gif', 18, 16);
285      IconStore.addIcon('Annotation', path + 'annotation.gif', 18, 16);
286      IconStore.addIcon('Parameter', path + 'parameter.gif', 18, 16);
287      IconStore.addIcon('NonParent', path + 'itemerror.gif', 18, 16);
288      <%=generateJoustTree(parentAnnotations, nonParents, inheritedAnnotations, inheritedSets)%>
289      JoustMenu.alwaysSendOnClickToSelected = true;
290      JoustMenu.draw('joust');
291      <%
292    }
293    %>
294  }
295  function itemOnClick(event, annotationSetId)
296  {
297    var frm = document.forms['annotations'];
298    frm['AS'+annotationSetId].checked = !frm['AS'+annotationSetId].checked;
299    itemOnToggle(event, annotationSetId);
300  }
301 
302  function itemOnToggle(event, annotationSetId)
303  {
304    //alert('itemOnClick: '+annotationSetId);
305    var frm = document.forms['annotations'];
306
307    var linkedItem = Link.getItem('AS', annotationSetId);
308    if (!linkedItem)
309    {
310      linkedItem = new Item('AS', annotationSetId);
311    }
312    var checked = frm['AS'+annotationSetId].checked;
313    linkedItem.action += checked ? 1 : -1;
314
315    frm['AS'+annotationSetId].checked = checked;
316    var menuItem = JoustMenu.menuItems['AS'+annotationSetId];
317    var childIndex = menuItem.firstChildIndex;
318    while (childIndex != -1)
319    {
320      var childMenuItem = JoustMenu.menuItems[childIndex];
321      frm[childMenuItem.externalId].checked = checked;
322      frm[childMenuItem.externalId].disabled = checked;
323      childIndex = childMenuItem.nextItemIndex;
324     
325      var annotationId = childMenuItem.externalId.substr(1);
326      annotationItem = Link.getItem('A', annotationId);
327      if (!annotationItem) annotationItem = new Item('A', annotationId);
328      annotationItem.action = -1;
329    }
330    event.cancelBubble = true;
331  }
332 
333  function annotationOnClick(event, annotationId)
334  {
335    var frm = document.forms['annotations'];
336    var annotation = frm['A'+annotationId];
337    if (!annotation.disabled)
338    {
339      annotation.checked = !annotation.checked;
340      annotationOnToggle(event, annotationId);
341    }
342    document.getElementById('annotationValues').innerHTML = annotationValues['A'+annotationId];
343  }
344 
345  function annotationOnToggle(event, annotationId)
346  {
347    //alert('itemOnClick: '+annotationSetId);
348    var frm = document.forms['annotations'];
349
350    var linkedItem = Link.getItem('A', annotationId);
351    if (!linkedItem)
352    {
353      linkedItem = new Item('A', annotationId);
354    }
355    var checked = frm['A'+annotationId].checked;
356    linkedItem.action = checked ? 1 : -1;
357   
358    frm['A'+annotationId].checked = checked;
359    event.cancelBubble = true;
360  }
361 
362  function saveAnnotations()
363  {
364    var frm = document.forms['modified'];
365    Annotations.addInheritedAnnotationsToForm(window, frm);
366    frm.submit();
367  }
368
369  </script>
370  </base:head>
371  <base:body onload="init()">
372    <%
373    if (standalone)
374    {
375      %>
376      <h1><%=title%> <base:help helpid="annotations.inherit" /></h1>
377      <div class="content">
378      <%
379    }
380    %>
381  <form name="annotations"> 
382  <div class="absolutefull">
383 
384    <div class="absolutefull filled rightborder" style="width: 28em;">
385   
386      <div class="absolutefull parameterlist" style="bottom: 3em;">
387        <div id="joust" class="joust">
388          <%
389          if (parentAnnotations.size() == 0) 
390          {
391            %>
392            <div class="messagecontainer error">There are no parent items with annotations.</div>
393            <%
394          }
395          %>
396        </div>
397      </div>
398     
399      <div class="absolutefull topborder" style="top: auto; bottom: 0px; height: 3em;">
400        <table style="height: 100%; margin:auto;"><tr><td>
401        <base:icon image="joust/annotation.gif" style="vertical-align: text-bottom;"/> = annotation
402        <base:icon image="joust/parameter.gif" style="vertical-align: text-bottom;"/> = protocol parameter<br>
403        <base:icon image="joust/itemerror.gif" style="vertical-align: text-bottom;" /> = not a parent item
404        </td></tr></table>
405      </div>
406   
407    </div>
408   
409    <div class="absolutefull" style="left: 28em; padding: 8px;" id="annotationValues">
410    </div>
411 
412  </div>
413  </form>
414
415    <%
416    if (standalone)
417    {
418      %>
419      </div>
420      <base:buttongroup subclass="dialogbuttons">
421        <base:button onclick="saveAnnotations()" title="Save" />
422        <base:button onclick="window.close()" title="Cancel" />
423      </base:buttongroup>
424      <form name="modified" method="post" action="index.jsp?ID=<%=ID%>">
425        <input type="hidden" name="cmd" value="SaveAnnotations">
426        <input type="hidden" name="item_type" value="<%=itemType.name()%>">
427        <input type="hidden" name="item_id" value="<%=itemId%>">
428      </form>
429      <%
430    }
431    %>
432  </base:body>
433  </base:page>
434  <%
435}
436finally
437{
438  if (dc != null) dc.close();
439}
440%>
441
Note: See TracBrowser for help on using the repository browser.