source: branches/2.14-stable/www/admin/annotationtypes/index.jsp @ 5152

Last change on this file since 5152 was 5152, checked in by Nicklas Nordborg, 12 years ago

Fixes #1412: The selected unit for an annotation type should not be remembered when creating a new annotation type

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 18.5 KB
Line 
1<%-- $Id: index.jsp 5152 2009-10-21 12:39:54Z 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 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="java.util.Enumeration"
60  import="java.util.Set"
61  import="java.util.HashSet"
62  import="java.util.List"
63  import="java.util.ArrayList"
64  import="java.util.Collections"
65  import="java.util.Collection"
66  import="java.util.Arrays"
67  import="java.util.Date"
68%>
69<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
70<%!
71  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,valueType,requiredForMiame,itemTypes,description");
72  private static final Item itemType = Item.ANNOTATIONTYPE;
73 
74  private static void registerExportUtils(ItemContext cc)
75  {
76    // Register formatter
77    cc.setObject("export.formatter.&categories(name)", new NameableFormatter());
78   
79    // Register dataloader
80    String typeParameter = "annotationType";
81    ItemQuery<AnnotationTypeCategory> categoryQuery = AnnotationTypeCategory.getQuery();
82    categoryQuery.include(cc.getInclude());
83    categoryQuery.join(Hql.innerJoin("annotationTypes", "atp"));
84    categoryQuery.restrict(Restrictions.eq(Hql.alias("atp"), Expressions.parameter(typeParameter)));
85    categoryQuery.order(Orders.asc(Hql.property("name")));
86    cc.setObject("export.dataloader.&categories(name)", new ItemQueryLoader(categoryQuery, typeParameter));
87  }
88%>
89<%
90final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
91final String ID = sc.getId();
92final String cmd = request.getParameter("cmd");
93final String root = request.getContextPath()+"/";
94final String mode = request.getParameter("mode");
95final String callback = request.getParameter("callback");
96final String itemId = request.getParameter("item_id");
97final String listPage = "list_annotationtypes.jsp?ID="+ID
98  +(mode == null ? "" : "&mode="+mode)
99  +(callback == null ? "" : "&callback="+callback)
100  +(itemId == null ? "" : "&item_id="+itemId);
101final String viewPage = "view_annotationtype.jsp?ID="+ID;
102final String editPage = "edit_annotationtype.jsp?ID="+ID;
103
104String forward = null;
105String redirect = null;
106String message = null;
107DbControl dc = null;
108
109try
110{
111  if (cmd == null || "List".equals(cmd))
112  {
113    // Display the list page without updatinging the current context
114    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
115    redirect = listPage;
116  }
117  else if ("UpdateContext".equals(cmd))
118  {
119    // Display the list page after updating the current context from the request parameters
120    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
121    redirect = listPage;
122  }
123  else if ("LoadContext".equals(cmd))
124  {
125    // Display the list page after loading a saved context
126    int contextId = Values.getInt(request.getParameter("context"));
127    Base.loadContext(sc, contextId, defaultContext);
128    redirect = listPage;
129  }
130
131  else if ("ViewItem".equals(cmd))
132  {
133    // Display the view page for a single item
134    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
135    forward = viewPage;
136  }
137  else if ("EditItem".equals(cmd))
138  {
139    // Display the edit page for a single item (should be opened in a popup)
140    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
141    redirect = editPage;
142  }
143  else if ("NewItem".equals(cmd))
144  {
145    // Display the edit page for a new item (should be opened in a popup)
146    if (!sc.hasPermission(Permission.CREATE, itemType))
147    {
148      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
149    }
150    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
151    cc.setId(0);
152    redirect = editPage+"&value_type="+request.getParameter("value_type");
153  }
154  else if ("UpdateItem".equals(cmd))
155  {
156    // Update the properties on an item (will close the popup)
157    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
158    final int maxRecent = Base.getMaxRecent(sc);
159    dc = sc.newDbControl();
160    AnnotationType annotationType = (AnnotationType)cc.getObject("item");
161    if (annotationType == null)
162    {
163      Type valueType = Type.valueOf(request.getParameter("value_type"));
164      annotationType = AnnotationType.getNew(dc, valueType);
165      message = "Annotation type created";
166      dc.saveItem(annotationType);
167    }
168    else
169    {
170      dc.reattachItem(annotationType, false);
171      message = "Annotation type updated";
172    }
173   
174    annotationType.setName(Values.getStringOrNull(request.getParameter("name")));
175    annotationType.setDescription(Values.getStringOrNull(request.getParameter("description")));
176    annotationType.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
177    annotationType.setMultiplicity(Values.getInt(request.getParameter("multiplicity"), 0));
178    annotationType.setDefaultValue(request.getParameter("defaultValue"));
179    annotationType.setRequiredForMiame(Values.getBoolean(request.getParameter("required_for_miame")));
180    annotationType.setProtocolParameter(Values.getBoolean(request.getParameter("is_protocol_parameter")));
181    if (annotationType.getValueType().canEnumerate())
182    {
183      annotationType.setEnumeration(!"box".equals(request.getParameter("interface")));
184    }
185   
186    // Options tab
187    Type valueType = annotationType.getValueType();
188    if (valueType == Type.INT || valueType == Type.LONG)
189    {
190      annotationType.setMinValueLong(Values.getLong(request.getParameter("minValue"), null));
191      annotationType.setMaxValueLong(Values.getLong(request.getParameter("maxValue"), null));
192      annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
193      if (annotationType.isEnumeration())
194      {
195        Object[] values = null;
196        if (valueType == Type.INT)
197        {
198          values = Values.getInt(request.getParameter("values").split("[\n\r]+"));
199        }
200        else
201        {
202          values = Values.getLong(request.getParameter("values").split("[\n\r]+"));
203        }
204        annotationType.setValues(Arrays.asList(values));
205        annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
206      }
207    }
208    else if (valueType == Type.FLOAT || valueType == Type.DOUBLE)
209    {
210      annotationType.setMinValueDouble(Values.getDouble(request.getParameter("minValue"), null));
211      annotationType.setMaxValueDouble(Values.getDouble(request.getParameter("maxValue"), null));
212      annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
213      if (annotationType.isEnumeration())
214      {
215        Object[] values = null;
216        if (valueType == Type.FLOAT)
217        {
218          values = Values.getFloat(request.getParameter("values").split("[\n\r]+"));
219        }
220        else
221        {
222          values = Values.getDouble(request.getParameter("values").split("[\n\r]+"));
223        }
224        annotationType.setValues(Arrays.asList(values));
225        annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
226      }
227    }
228    else if (valueType == Type.STRING)
229    {
230      annotationType.setMaxLength(Values.getInteger(request.getParameter("maxLength"), null));
231      annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
232      if (annotationType.isEnumeration())
233      {
234        String[] values = request.getParameter("values").split("[\n\r]+");
235        annotationType.setValues(Arrays.asList(values));
236        annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
237      }
238    }
239    else if (valueType == Type.TEXT)
240    {
241      annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
242      annotationType.setHeight(Values.getInt(request.getParameter("height"), 10));
243    }
244    else if (valueType == Type.DATE)
245    {
246      annotationType.setWidth(Values.getInt(request.getParameter("width"), 40));
247      if (annotationType.isEnumeration())
248      {
249        Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
250        Date[] values = Values.getDate(request.getParameter("values").split("[\n\r]+"), dateFormatter);
251        annotationType.setValues(Arrays.asList(values));
252        annotationType.setDisplayAsList("list".equals(request.getParameter("interface")));
253      }
254    }
255   
256    // Item types tab
257    Set<Item> allItems = Metadata.getAnnotatableItems();
258    String[] enabled = request.getParameterValues("enabled");
259    Collection<String> enabledItems = enabled != null ? Arrays.asList(enabled) : new ArrayList<String>();
260    for (Item item : allItems)
261    {
262      if (enabledItems.contains(item.name()))
263      {
264        annotationType.enableForItem(item);
265      }
266      else
267      {
268        annotationType.disableForItem(item);
269      }
270    }
271   
272    // Units tab
273    if (valueType.isNumerical())
274    {
275      int unitId = Values.getInt(request.getParameter("unit_id"), -1);
276      if (unitId >= 0) // < 0 = denied or unchanged
277      {
278        Unit unit = unitId == 0 ? null : Unit.getById(dc, unitId);
279        annotationType.setDefaultUnit(unit);
280      }
281     
282      String[] removeUnits = Values.getString(request.getParameter("removeUsableUnits")).split(",");
283      for (int i = 0; i < removeUnits.length; ++i)
284      {
285        int uId = Values.getInt(removeUnits[i], -1);
286        if (uId != -1) annotationType.removeUsableUnit(Unit.getById(dc, uId));
287      }
288     
289      String[] addUnits = Values.getString(request.getParameter("addUsableUnits")).split(",");
290      for (int i = 0; i < addUnits.length; ++i)
291      {
292        int uId = Values.getInt(addUnits[i], -1);
293        if (uId != -1) annotationType.addUsableUnit(Unit.getById(dc, uId));
294      }
295    }
296   
297    // Categories tab
298    String[] removeCategories = Values.getString(request.getParameter("removeCategories")).split(",");
299    for (int i = 0; i < removeCategories.length; ++i)
300    {
301      int categoryId = Values.getInt(removeCategories[i], -1);
302      if (categoryId != -1) annotationType.removeCategory(AnnotationTypeCategory.getById(dc, categoryId));
303    }
304   
305    String[] addCategories = Values.getString(request.getParameter("addCategories")).split(",");
306    for (int i = 0; i < addCategories.length; ++i)
307    {
308      int categoryId = Values.getInt(addCategories[i], -1);
309      if (categoryId != -1) annotationType.addCategory(AnnotationTypeCategory.getById(dc, categoryId));
310    }
311
312    dc.commit();
313    cc.removeObject("item");
314  }
315  else if ("DeleteItem".equals(cmd))
316  {
317    // Delete a single item and then return to the view page
318    dc = sc.newDbControl();
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
321    dc.commit();
322    redirect = viewPage;
323  }
324  else if ("DeleteItems".equals(cmd))
325  {
326    // Delete all selected items on the list page
327    dc = sc.newDbControl();
328    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
329    int numTotal = cc.getSelected().size();
330    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
331    dc.commit();
332    if (numTotal != numRemoved)
333    {
334      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
335    }
336    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
337  }
338  else if ("RestoreItem".equals(cmd))
339  {
340    // Restore a single item and then return to the view page
341    dc = sc.newDbControl();
342    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
343    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
344    dc.commit();
345    redirect = viewPage;
346  }
347  else if ("RestoreItems".equals(cmd))
348  {
349    // Restore all selected items on the list page
350    dc = sc.newDbControl();
351    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
352    int numTotal = cc.getSelected().size();
353    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
354    dc.commit();
355    if (numTotal != numRemoved)
356    {
357      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
358    }
359    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
360  }
361  else if ("ShareItem".equals(cmd))
362  {
363    // Display a popup window for sharing a single item
364    dc = sc.newDbControl();
365    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
366    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
367    dc.close();
368    cc.setObject("MultiPermissions", permissions);
369    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
370  }
371  else if ("ShareItems".equals(cmd))
372  {
373    // Display a popup window for sharing all selected items on the list page
374    dc = sc.newDbControl();
375    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
376    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
377    dc.close();
378    cc.setObject("MultiPermissions", permissions);
379    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
380  }
381  else if ("SetOwnerOfItem".equals(cmd))
382  {
383    // Change owner of items selected on a list page
384    dc = sc.newDbControl();
385    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
386    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
387    cc.setObject("OwnedItems", Collections.singleton(item));
388    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
389  }
390  else if ("SetOwnerOfItems".equals(cmd))
391  {
392    // Change owner of items selected on a list page
393    dc = sc.newDbControl();
394    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
395    Set<OwnedItem> items = new HashSet<OwnedItem>();
396    for (Integer id : cc.getSelected())
397    {
398      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
399    }
400    dc.close();
401    cc.setObject("OwnedItems", items);
402    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
403  }
404  else if ("ExportItems".equals(cmd))
405  {
406    // Run an export plugin in a list context
407    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
408    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
409    cc.configureQuery(query, true);
410    cc.setQuery(query);
411    registerExportUtils(cc);
412    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+annotation+types";
413  }
414  else if ("ExportItem".equals(cmd))
415  {
416    // Run an export plugin in single-item context
417    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
418    registerExportUtils(cc);
419    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+annotation+type";
420  }
421  else if ("ImportItems".equals(cmd))
422  {
423    // Run an import plugin in a list context
424    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
425    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
426    cc.configureQuery(query, true);
427    cc.setQuery(query);
428    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+annotation+types";
429  }
430  else if ("ImportItem".equals(cmd))
431  {
432    // Run an import plugin in single-item context
433    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
434    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+annotation+typey";
435  }
436  else if ("RunListPlugin".equals(cmd))
437  {
438    // Run another plugin in a list context
439    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
440    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
441    cc.configureQuery(query, true);
442    cc.setQuery(query);
443    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
444  }
445  else if ("RunPlugin".equals(cmd))
446  {
447    // Run another plugin in single-item context
448    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
449    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
450  }
451  else
452  {
453    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
454  }
455}
456finally
457{
458  if (dc != null) dc.close();
459}
460
461if (forward != null)
462{
463  pageContext.forward(forward);
464}
465else if (redirect != null)
466{
467  response.sendRedirect(redirect);
468}
469else if (message == null)
470{
471  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
472}
473else
474{
475  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
476}
477%>
478
Note: See TracBrowser for help on using the repository browser.