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

Last change on this file since 6607 was 6607, checked in by Nicklas Nordborg, 8 years ago

References #1890: Improve skinnability of BASE

Minor adjustmen to most of the common utility pages.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 11.5 KB
Line 
1<%-- $Id: inherit.jsp 6607 2014-11-19 14:52:20Z 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.ItemSubtype"
45  import="net.sf.basedb.core.ItemQuery"
46  import="net.sf.basedb.core.Include"
47  import="net.sf.basedb.core.Nameable"
48  import="net.sf.basedb.core.ItemResultList"
49  import="net.sf.basedb.core.AnnotationType"
50  import="net.sf.basedb.core.PermissionDeniedException"
51  import="net.sf.basedb.util.AnnotationUtil"
52  import="net.sf.basedb.core.Subtypable"
53  import="net.sf.basedb.core.query.Orders"
54  import="net.sf.basedb.core.query.Hql"
55  import="net.sf.basedb.util.Tree"
56  import="net.sf.basedb.clients.web.Base"
57  import="net.sf.basedb.clients.web.WebException"
58  import="net.sf.basedb.clients.web.util.HTML"
59  import="net.sf.basedb.util.Values"
60  import="net.sf.basedb.util.formatter.Formatter"
61  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
62  import="java.util.List"
63  import="java.util.Set"
64  import="java.util.HashSet"
65  import="java.util.Map"
66  import="java.util.HashMap"
67  import="org.json.simple.JSONArray"
68  import="org.json.simple.JSONObject"
69%>
70<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
71<%!
72private void loadParents(DbControl dc, Set<AnnotationSet> parentAnnotations, Set<Annotatable> parentItems, Annotatable current)
73{
74  Set<Annotatable> parents = AnnotationUtil.getAllAnnotatableParentItems(dc, current, null);
75  for (Annotatable parent : parents)
76  {
77    if (parent != null && parentItems.add(parent))
78    {
79      if (parent.hasPermission(Permission.USE) && parent.isAnnotated())
80      {
81        parentAnnotations.add(parent.getAnnotationSet());
82      }
83    }
84  }
85}
86
87JSONArray generateJoustTree(Set<AnnotationSet> parentAnnotations, Set<AnnotationSet> nonParents, Set<Annotation> inheritedAnnotations, Set<AnnotationSet> inheritedSets)
88{
89  String itemIcon = "Annotatable";
90  String annotationIcon = "Annotation";
91  String parameterIcon = "Parameter";
92  String nonParentIcon = "NonParent";
93  JSONArray json = new JSONArray();
94 
95  for (AnnotationSet as : parentAnnotations)
96  {
97    Annotatable item = null;
98    String itemType = as.getItemType().toString();
99    try
100    {
101      item = as.getItem();
102    }
103    catch (PermissionDeniedException ex)
104    {}
105    if (item instanceof Subtypable)
106    {
107      try
108      {
109        ItemSubtype subtype = ((Subtypable)item).getItemSubtype();
110        if (subtype != null) itemType = subtype.getName();
111      }
112      catch (PermissionDeniedException ex)
113      {}
114    }
115   
116    String icon = nonParents.contains(as) ? nonParentIcon : itemIcon;
117   
118    boolean setInherited = inheritedSets != null && inheritedSets.contains(as);
119
120    String joustId = "as-"+as.getId();
121    String input = "<input type=\"checkbox\""+
122      " name=\""+joustId+"\""+
123      (setInherited ? " checked" : "")+
124      ">";
125    String name = item == null ? "<i>- denied -</i>" : 
126      HTML.encodeTags(((Nameable)item).getName()) + " <span class=\"itemsubtype\">(" + itemType + ")</span>";
127   
128    JSONObject jsonJoust = newJoustEntry(null, icon, input+name, joustId);
129    jsonJoust.put("isOpen", 1);
130    jsonJoust.put("type", "annotation-set");
131    jsonJoust.put("externalId", as.getId());
132    jsonJoust.put("inherited", setInherited ? 1 : 0);
133    json.add(jsonJoust);
134
135    List<Annotation> annotations = as.getAnnotations().list(as.getDbControl());
136    SessionControl sc = as.getSessionControl();
137    for (Annotation a : annotations)
138    {
139      Formatter formatter = FormatterFactory.getAnnotationFormatter(sc, a, null);
140      boolean inherited = inheritedAnnotations != null && inheritedAnnotations.contains(a);
141      AnnotationType at = a.getAnnotationType();
142      String annotationName = at.getName();
143      boolean isParameter = at.isProtocolParameter();
144
145      joustId = "a-"+a.getId();
146      String values = Values.getString(a.getValues(null), ", ", true, formatter);
147      name = HTML.encodeTags(annotationName +
148        " [" + Values.trimString(values, 20)+"]");
149      input = "<input type=\"checkbox\""+
150        " name=\""+joustId+"\""+
151        (inherited || setInherited ? " checked" : "")+
152        (setInherited ? " disabled" : "")+
153        ">";
154     
155      JSONObject jsonAnnotation = newJoustEntry(jsonJoust, isParameter ? parameterIcon : annotationIcon, input+name, joustId);
156      jsonAnnotation.put("type", "annotation");
157      jsonAnnotation.put("externalId", a.getId());
158      jsonAnnotation.put("annotationType", HTML.encodeTags(annotationName));
159      jsonAnnotation.put("values", HTML.encodeTags(values));
160      jsonAnnotation.put("inherited", inherited ? 1 : 0);
161    }
162  }
163  return json;
164}
165
166JSONObject newJoustEntry(JSONObject jsonParent, String icon, String text, String id)
167{
168  JSONObject jsonJoust = new JSONObject();
169  jsonJoust.put("icon", icon);
170  jsonJoust.put("text", text);
171  jsonJoust.put("id", id);
172  if (jsonParent != null)
173  {
174    JSONArray jsonChildren = (JSONArray)jsonParent.get("children");
175    if (jsonChildren == null)
176    {
177      jsonChildren = new JSONArray();
178      jsonParent.put("children", jsonChildren);
179    }
180    jsonChildren.add(jsonJoust);
181  }
182  return jsonJoust;
183}
184%>
185<%
186final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
187final String ID = sc.getId();
188final float scale = Base.getScale(sc);
189final Item itemType = Item.valueOf(request.getParameter("item_type"));
190final int itemId = Values.getInt(request.getParameter("item_id"));
191final boolean standalone = Values.getBoolean(request.getParameter("standalone"));
192
193final DbControl dc = sc.newDbControl();
194ItemResultList<AnnotationType> annotationTypes = null;
195try
196{
197  final Annotatable item = itemId == 0 ? null : (Annotatable)itemType.getById(dc, itemId);
198  Set<AnnotationSet> parentAnnotations = new java.util.LinkedHashSet<AnnotationSet>();
199  Set<Annotatable> parentItems = new HashSet<Annotatable>();
200  Set<AnnotationSet> nonParents = new HashSet<AnnotationSet>();
201
202  if (item != null)
203  {
204    // Get all annotated parents and their annotation sets
205    loadParents(dc, parentAnnotations, parentItems, item);
206  }
207  if (standalone)
208  {
209    sc.getCurrentContext(itemType).setObject("item", item); 
210  }
211  String title = "Inherit annotations to " + 
212    HTML.encodeTags((item instanceof Nameable ? ((Nameable)item).getName() : 
213    (item == null ? " new item" : item.toString())));
214
215  // Parent items may have been submitted by caller
216  String[] parents = request.getParameterValues("parents");
217  // Each parameter is ITEMTYPE:ID:ID:...
218  if (parents != null)
219  {
220    for (String parent : parents)
221    {
222      String[] temp = parent.split(":");
223      Item parentType = Item.valueOf(temp[0]);
224      for (int i = 1; i < temp.length; ++i)
225      {
226        int parentId = Values.getInt(temp[i], -1);
227        if (parentId != -1) 
228        {
229          Annotatable parentItem = (Annotatable)parentType.getById(dc, parentId);
230          if (parentItems.add(parentItem))
231          {
232            // This is a new parent
233            if (parentItem.hasPermission(Permission.USE) && parentItem.isAnnotated()) 
234            {
235              parentAnnotations.add(parentItem.getAnnotationSet());
236            }
237            loadParents(dc, parentAnnotations, parentItems, parentItem);
238          }
239        }
240      }
241    }
242  }
243 
244  // Get the currently inherited annotations
245  final AnnotationSet as = item != null && item.isAnnotated() ? item.getAnnotationSet() : null;
246  Set<Annotation> inheritedAnnotations = null;
247  Set<AnnotationSet> inheritedSets = null;
248  if (as != null)
249  {
250    ItemQuery<Annotation> inheritedQuery = as.getInheritedAnnotations();
251    inheritedQuery.order(Orders.asc(Hql.property("annotationSet")));
252    inheritedQuery.order(Orders.asc(Hql.property("annotationType.name")));
253    inheritedAnnotations = new HashSet<Annotation>(inheritedQuery.list(dc));
254    for (Annotation a : inheritedAnnotations)
255    {
256      AnnotationSet from = a.getAnnotationSet();
257      if (!parentAnnotations.contains(from) && from.hasPermission(Permission.USE))
258      {
259        parentAnnotations.add(from);
260        nonParents.add(from);
261      }
262    }
263   
264    ItemQuery<AnnotationSet> setQuery = as.getInheritedAnnotationSets();
265    inheritedSets = new HashSet<AnnotationSet>(setQuery.list(dc));
266    for (AnnotationSet ias : inheritedSets)
267    {
268      if (!parentAnnotations.contains(ias) && ias.hasPermission(Permission.USE))
269      {
270        parentAnnotations.add(ias);
271        nonParents.add(ias);
272      }
273    }
274  }
275  JSONArray json = generateJoustTree(parentAnnotations, nonParents, inheritedAnnotations, inheritedSets);
276  %>
277  <base:page type="popup" title="Inherit annotations">
278  <base:head  scripts="joust-2.js,~inherit.js" styles="joust-2.css">
279  <style>
280  .selected .itemsubtype
281  {
282    color: #E8E8E8;
283  }
284  </style>
285  </base:head>
286  <base:body>
287    <%
288    if (standalone)
289    {
290      %>
291      <h1><%=title%> <base:help helpid="annotations.inherit" /></h1>
292      <div class="content bottomborder">
293      <%
294    }
295    %>
296  <form name="annotations"> 
297  <div class="absolutefull">
298 
299    <div class="absolutefull bg-filled-100 rightborder" style="width: 28em;">
300   
301      <div class="absolutefull parameterlist" style="bottom: 3em;">
302        <div id="joust" class="joust absolutefull"
303          data-joust-tree="<%=HTML.encodeTags(json.toJSONString()) %>"
304          >
305          <%
306          if (parentAnnotations.size() == 0) 
307          {
308            %>
309            <div class="messagecontainer error">There are no parent items with annotations.</div>
310            <%
311          }
312          %>
313        </div>
314      </div>
315     
316      <div class="absolutefull topborder" style="top: auto; bottom: 0px; height: 3em;">
317        <table style="height: 100%; margin:auto;"><tr><td>
318        <base:icon image="annotation.png" style="vertical-align: text-bottom;"/> = annotation
319        <base:icon image="parameter.png" style="vertical-align: text-bottom;"/> = protocol parameter<br>
320        <base:icon image="item-error.png" style="vertical-align: text-bottom;" /> = not a parent item
321        </td></tr></table>
322      </div>
323   
324    </div>
325   
326    <div class="absolutefull" style="left: 28em; padding: 1em;">
327      <div id="annotationType" style="font-weight: bold;"></div>
328      <div id="annotationValues"></div>
329    </div>
330 
331  </div>
332  </form>
333
334    <%
335    if (standalone)
336    {
337      %>
338      </div>
339      <base:buttongroup subclass="dialogbuttons">
340        <base:button id="btnSave" title="Save" />
341        <base:button id="close" title="Cancel" />
342      </base:buttongroup>
343      <form name="modified" method="post" action="index.jsp?ID=<%=ID%>">
344        <input type="hidden" name="cmd" value="SaveAnnotations">
345        <input type="hidden" name="item_type" value="<%=itemType.name()%>">
346        <input type="hidden" name="item_id" value="<%=itemId%>">
347      </form>
348      <%
349    }
350    %>
351  </base:body>
352  </base:page>
353  <%
354}
355finally
356{
357  if (dc != null) dc.close();
358}
359%>
360
Note: See TracBrowser for help on using the repository browser.