source: trunk/www/admin/annotationtypes/index.jsp @ 6728

Last change on this file since 6728 was 6728, checked in by Nicklas Nordborg, 7 years ago

References #1910: Disable annotation inheritance for annotation types

Added AnnotationType.getDisableInheritance() which is a flag that if set will disable the inheritance. Schema version is updated and all existing annotation types are set to FALSE.

A permission check when calling AnnotationSet.inheritAnnotation has been implemented and setting the flag with AnnotationType.setDisableInheritance() will trigger a removal of existing inherited annotations.

This should complete the core part of the functionality but we also need some supporting functionality (mainly in the gui).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 20.1 KB
Line 
1<%-- $Id: index.jsp 6728 2015-02-13 08:00:01Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23  @author Nicklas
24  @version 2.0
25--%>
26<%@ page pageEncoding="UTF-8" session="false"
27  import="net.sf.basedb.core.SessionControl"
28  import="net.sf.basedb.core.DbControl"
29  import="net.sf.basedb.core.Item"
30  import="net.sf.basedb.core.Include"
31  import="net.sf.basedb.core.AnnotationType"
32  import="net.sf.basedb.core.AnnotationTypeCategory"
33  import="net.sf.basedb.core.Quantity"
34  import="net.sf.basedb.core.Unit"
35  import="net.sf.basedb.core.ItemQuery"
36  import="net.sf.basedb.core.Permission"
37  import="net.sf.basedb.core.ItemContext"
38  import="net.sf.basedb.core.Metadata"
39  import="net.sf.basedb.core.Type"
40  import="net.sf.basedb.core.MultiPermissions"
41  import="net.sf.basedb.core.OwnedItem"
42  import="net.sf.basedb.core.PermissionDeniedException"
43  import="net.sf.basedb.core.ItemAlreadyExistsException"
44  import="net.sf.basedb.core.query.Expressions"
45  import="net.sf.basedb.core.query.Hql"
46  import="net.sf.basedb.core.query.Orders"
47  import="net.sf.basedb.core.query.Restrictions"
48  import="net.sf.basedb.util.RemovableUtil"
49  import="net.sf.basedb.util.ShareableUtil"
50  import="net.sf.basedb.util.OwnableUtil"
51  import="net.sf.basedb.clients.web.Base"
52  import="net.sf.basedb.clients.web.WebException"
53  import="net.sf.basedb.util.Values"
54  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
55  import="net.sf.basedb.clients.web.util.HTML"
56  import="net.sf.basedb.util.formatter.Formatter"
57  import="net.sf.basedb.util.formatter.NameableFormatter"
58  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
59  import="net.sf.basedb.core.plugin.GuiContext"
60  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
61  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
62  import="net.sf.basedb.clients.web.extensions.JspContext"
63  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
64  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
65  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
66  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
67  import="java.util.Enumeration"
68  import="java.util.Set"
69  import="java.util.HashSet"
70  import="java.util.List"
71  import="java.util.ArrayList"
72  import="java.util.Collections"
73  import="java.util.Collection"
74  import="java.util.Arrays"
75  import="java.util.Date"
76%>
77<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
78<%!
79  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,valueType,requiredForMiame,itemTypes,description");
80  private static final Item itemType = Item.ANNOTATIONTYPE;
81 
82  private static void registerExportUtils(ItemContext cc)
83  {
84    // Register formatter
85    cc.setObject("export.formatter.&categories(name)", new NameableFormatter());
86   
87    // Register dataloader
88    String typeParameter = "annotationType";
89    ItemQuery<AnnotationTypeCategory> categoryQuery = AnnotationTypeCategory.getQuery();
90    categoryQuery.include(cc.getInclude());
91    categoryQuery.join(Hql.innerJoin("annotationTypes", "atp"));
92    categoryQuery.restrict(Restrictions.eq(Hql.alias("atp"), Expressions.parameter(typeParameter)));
93    categoryQuery.order(Orders.asc(Hql.property("name")));
94    cc.setObject("export.dataloader.&categories(name)", new ItemQueryLoader(categoryQuery, typeParameter));
95  }
96%>
97<%
98final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
99final String ID = sc.getId();
100final String cmd = request.getParameter("cmd");
101final String root = request.getContextPath()+"/";
102final String mode = request.getParameter("mode");
103final String callback = request.getParameter("callback");
104final String itemId = request.getParameter("item_id");
105final String listPage = "list_annotationtypes.jsp?ID="+ID
106  +(mode == null ? "" : "&mode="+mode)
107  +(callback == null ? "" : "&callback="+callback)
108  +(itemId == null ? "" : "&item_id="+itemId);
109final String viewPage = "view_annotationtype.jsp?ID="+ID;
110final String editPage = "edit_annotationtype.jsp?ID="+ID;
111
112String forward = null;
113String redirect = null;
114String message = null;
115DbControl dc = null;
116
117try
118{
119  if (cmd == null || "List".equals(cmd))
120  {
121    // Display the list page without updatinging the current context
122    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
123    redirect = listPage;
124  }
125  else if ("UpdateContext".equals(cmd))
126  {
127    // Display the list page after updating the current context from the request parameters
128    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
129    redirect = listPage;
130  }
131  else if ("LoadContext".equals(cmd))
132  {
133    // Display the list page after loading a saved context
134    int contextId = Values.getInt(request.getParameter("context"));
135    Base.loadContext(sc, contextId, defaultContext);
136    redirect = listPage;
137  }
138
139  else if ("ViewItem".equals(cmd))
140  {
141    // Display the view page for a single item
142    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
143    forward = viewPage;
144  }
145  else if ("EditItem".equals(cmd))
146  {
147    // Display the edit page for a single item (should be opened in a popup)
148    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
149    redirect = editPage;
150  }
151  else if ("NewItem".equals(cmd))
152  {
153    // Display the edit page for a new item (should be opened in a popup)
154    if (!sc.hasPermission(Permission.CREATE, itemType))
155    {
156      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
157    }
158    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
159    cc.setId(0);
160    redirect = editPage+"&value_type="+request.getParameter("value_type");
161  }
162  else if ("UpdateItem".equals(cmd))
163  {
164    // Update the properties on an item (will close the popup)
165    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
166    final int maxRecent = Base.getMaxRecent(sc);
167    dc = sc.newDbControl();
168    AnnotationType annotationType = (AnnotationType)cc.getObject("item");
169    if (annotationType == null)
170    {
171      Type valueType = Type.valueOf(request.getParameter("value_type"));
172      annotationType = AnnotationType.getNew(dc, valueType);
173      message = "Annotation type created";
174      dc.saveItem(annotationType);
175    }
176    else
177    {
178      dc.reattachItem(annotationType, false);
179      message = "Annotation type updated";
180    }
181    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.ANNOTATIONTYPE), annotationType);
182    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
183    try
184    {
185      annotationType.setName(Values.getStringOrNull(request.getParameter("name")));
186      annotationType.setDescription(Values.getStringOrNull(request.getParameter("description")));
187      annotationType.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
188      annotationType.setMultiplicity(Values.getInt(request.getParameter("multiplicity"), 0));
189      annotationType.setDefaultValue(request.getParameter("defaultValue"));
190      annotationType.setRequiredForMiame(Values.getBoolean(request.getParameter("required_for_miame")));
191      annotationType.setProtocolParameter(Values.getBoolean(request.getParameter("is_protocol_parameter")));
192      annotationType.setDisableLogOfValues(Values.getBoolean(request.getParameter("disable_log")));
193      annotationType.setDisableInheritance(Values.getBoolean(request.getParameter("disable_inheritance")));
194      if (annotationType.getValueType().canEnumerate())
195      {
196        annotationType.setEnumeration(!"box".equals(request.getParameter("interface")));
197      }
198     
199      // Options tab
200      Type valueType = annotationType.getValueType();
201      if (valueType == Type.INT || valueType == Type.LONG)
202      {
203        annotationType.setMinValueLong(Values.getLong(request.getParameter("minValue"), null));
204        annotationType.setMaxValueLong(Values.getLong(request.getParameter("maxValue"), null));
205        annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
206        if (annotationType.isEnumeration())
207        {
208          Object[] values = null;
209          if (valueType == Type.INT)
210          {
211            values = Values.getInt(request.getParameter("values").split("[\n\r]+"));
212          }
213          else
214          {
215            values = Values.getLong(request.getParameter("values").split("[\n\r]+"));
216          }
217          annotationType.setValues(Arrays.asList(values));
218          annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
219        }
220      }
221      else if (valueType == Type.FLOAT || valueType == Type.DOUBLE)
222      {
223        annotationType.setMinValueDouble(Values.getDouble(request.getParameter("minValue"), null));
224        annotationType.setMaxValueDouble(Values.getDouble(request.getParameter("maxValue"), null));
225        annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
226        if (annotationType.isEnumeration())
227        {
228          Object[] values = null;
229          if (valueType == Type.FLOAT)
230          {
231            values = Values.getFloat(request.getParameter("values").split("[\n\r]+"));
232          }
233          else
234          {
235            values = Values.getDouble(request.getParameter("values").split("[\n\r]+"));
236          }
237          annotationType.setValues(Arrays.asList(values));
238          annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
239        }
240      }
241      else if (valueType == Type.STRING)
242      {
243        annotationType.setMaxLength(Values.getInteger(request.getParameter("maxLength"), null));
244        annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
245        if (annotationType.isEnumeration())
246        {
247          String[] values = request.getParameter("values").split("[\n\r]+");
248          annotationType.setValues(Arrays.asList(values));
249          annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
250        }
251      }
252      else if (valueType == Type.TEXT)
253      {
254        annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
255        annotationType.setHeight(Values.getInt(request.getParameter("height"), 10));
256      }
257      else if (valueType == Type.DATE)
258      {
259        annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
260        if (annotationType.isEnumeration())
261        {
262          Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
263          Date[] values = Values.getDate(request.getParameter("values").split("[\n\r]+"), dateFormatter);
264          annotationType.setValues(Arrays.asList(values));
265          annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
266        }
267      }
268     
269      // Item types tab
270      Set<Item> allItems = Metadata.getAnnotatableItems();
271      String[] enabled = request.getParameterValues("enabled");
272      Collection<String> enabledItems = enabled != null ? Arrays.asList(enabled) : new ArrayList<String>();
273      for (Item item : allItems)
274      {
275        if (enabledItems.contains(item.name()))
276        {
277          annotationType.enableForItem(item);
278        }
279        else
280        {
281          annotationType.disableForItem(item);
282        }
283      }
284     
285      // Units tab
286      if (valueType.isNumerical())
287      {
288        int unitId = Values.getInt(request.getParameter("unit_id"), -1);
289        if (unitId >= 0) // < 0 = denied or unchanged
290        {
291          Unit unit = unitId == 0 ? null : Unit.getById(dc, unitId);
292          annotationType.setDefaultUnit(unit);
293        }
294       
295        String[] removeUnits = Values.getString(request.getParameter("-UNIT")).split(",");
296        for (int i = 0; i < removeUnits.length; ++i)
297        {
298          int uId = Values.getInt(removeUnits[i], -1);
299          if (uId != -1) annotationType.removeUsableUnit(Unit.getById(dc, uId));
300        }
301       
302        String[] addUnits = Values.getString(request.getParameter("+UNIT")).split(",");
303        for (int i = 0; i < addUnits.length; ++i)
304        {
305          int uId = Values.getInt(addUnits[i], -1);
306          if (uId != -1) annotationType.addUsableUnit(Unit.getById(dc, uId));
307        }
308      }
309     
310      // Categories tab
311      String[] removeCategories = Values.getString(request.getParameter("-ANNOTATIONTYPECATEGORY")).split(",");
312      for (int i = 0; i < removeCategories.length; ++i)
313      {
314        int categoryId = Values.getInt(removeCategories[i], -1);
315        if (categoryId != -1) annotationType.removeCategory(AnnotationTypeCategory.getById(dc, categoryId));
316      }
317     
318      String[] addCategories = Values.getString(request.getParameter("+ANNOTATIONTYPECATEGORY")).split(",");
319      for (int i = 0; i < addCategories.length; ++i)
320      {
321        int categoryId = Values.getInt(addCategories[i], -1);
322        if (categoryId != -1) annotationType.addCategory(AnnotationTypeCategory.getById(dc, categoryId));
323      }
324     
325      // OnSave extensions
326      invoker.render(OnSaveRenderer.ON_SAVE);
327      dc.commit();
328      invoker.render(OnSaveRenderer.ON_COMMIT);
329    }
330    catch (Exception ex)
331    {
332      invoker.render(OnSaveRenderer.onRollback(ex));
333      throw ex;
334    }
335    finally
336    {
337      cc.removeObject("item");
338    }
339  }
340  else if ("DeleteItem".equals(cmd))
341  {
342    // Delete a single item and then return to the view page
343    dc = sc.newDbControl();
344    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
345    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
346    dc.commit();
347    redirect = viewPage;
348  }
349  else if ("DeleteItems".equals(cmd))
350  {
351    // Delete all selected items on the list page
352    dc = sc.newDbControl();
353    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
354    int numTotal = cc.getSelected().size();
355    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
356    dc.commit();
357    if (numTotal != numRemoved)
358    {
359      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
360    }
361    redirect = listPage;
362  }
363  else if ("RestoreItem".equals(cmd))
364  {
365    // Restore a single item and then return to the view page
366    dc = sc.newDbControl();
367    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
368    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
369    dc.commit();
370    redirect = viewPage;
371  }
372  else if ("RestoreItems".equals(cmd))
373  {
374    // Restore all selected items on the list page
375    dc = sc.newDbControl();
376    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
377    int numTotal = cc.getSelected().size();
378    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
379    dc.commit();
380    if (numTotal != numRemoved)
381    {
382      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
383    }
384    redirect = listPage;
385  }
386  else if ("ShareItem".equals(cmd))
387  {
388    // Display a popup window for sharing a single item
389    dc = sc.newDbControl();
390    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
391    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
392    dc.close();
393    cc.setObject("MultiPermissions", permissions);
394    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
395  }
396  else if ("ShareItems".equals(cmd))
397  {
398    // Display a popup window for sharing all selected items on the list page
399    dc = sc.newDbControl();
400    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
401    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
402    dc.close();
403    cc.setObject("MultiPermissions", permissions);
404    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
405  }
406  else if ("SetOwnerOfItem".equals(cmd))
407  {
408    // Change owner of items selected on a list page
409    dc = sc.newDbControl();
410    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
411    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
412    cc.setObject("OwnedItems", Collections.singleton(item));
413    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
414  }
415  else if ("SetOwnerOfItems".equals(cmd))
416  {
417    // Change owner of items selected on a list page
418    dc = sc.newDbControl();
419    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
420    Set<OwnedItem> items = new HashSet<OwnedItem>();
421    for (Integer id : cc.getSelected())
422    {
423      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
424    }
425    dc.close();
426    cc.setObject("OwnedItems", items);
427    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
428  }
429  else if ("ExportItems".equals(cmd))
430  {
431    // Run an export plugin in a list context
432    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
433    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
434    dc = sc.newDbControl();
435    cc.configureQuery(dc, query, true);
436    cc.setQuery(query);
437    registerExportUtils(cc);
438    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
439    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
440    listInvoker.render(new ListColumnExportRenderer(cc));
441    dc.close();
442    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+annotation+types";
443  }
444  else if ("ExportItem".equals(cmd))
445  {
446    // Run an export plugin in single-item context
447    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
448    registerExportUtils(cc);
449    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+annotation+type";
450  }
451  else if ("ImportItems".equals(cmd))
452  {
453    // Run an import plugin in a list context
454    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
455    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
456    dc = sc.newDbControl();
457    cc.configureQuery(dc, query, true);
458    dc.close();
459    cc.setQuery(query);
460    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+annotation+types";
461  }
462  else if ("ImportItem".equals(cmd))
463  {
464    // Run an import plugin in single-item context
465    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
466    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+annotation+typey";
467  }
468  else if ("RunListPlugin".equals(cmd))
469  {
470    // Run another plugin in a list context
471    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
472    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
473    dc = sc.newDbControl();
474    cc.configureQuery(dc, query, true);
475    dc.close();
476    cc.setQuery(query);
477    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
478  }
479  else if ("RunPlugin".equals(cmd))
480  {
481    // Run another plugin in single-item context
482    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
483    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
484  }
485  else
486  {
487    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
488  }
489}
490finally
491{
492  if (dc != null) dc.close();
493}
494
495if (forward != null)
496{
497  sc.setSessionSetting("alert-message", message);
498  pageContext.forward(forward);
499}
500else if (redirect != null)
501{
502  sc.setSessionSetting("alert-message", message);
503  response.sendRedirect(redirect);
504}
505else if (message == null)
506{
507  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
508}
509else
510{
511  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
512}
513%>
514
Note: See TracBrowser for help on using the repository browser.