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

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

References #262 and #340. The overview and validation is now complete.
Still missing possibility to save validation options as presets.

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