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

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

References #108: Logging the change history of an item

  • Documented the change made in [5058]
  • Changed calls to DbControl?.reattachItem() from all core code and jsp scripts
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 18.6 KB
Line 
1<%-- $Id: index.jsp 5060 2009-08-19 07:02:11Z 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        if (unit != null) cc.setRecent(unit, maxRecent);
281      }
282     
283      String[] removeUnits = Values.getString(request.getParameter("removeUsableUnits")).split(",");
284      for (int i = 0; i < removeUnits.length; ++i)
285      {
286        int uId = Values.getInt(removeUnits[i], -1);
287        if (uId != -1) annotationType.removeUsableUnit(Unit.getById(dc, uId));
288      }
289     
290      String[] addUnits = Values.getString(request.getParameter("addUsableUnits")).split(",");
291      for (int i = 0; i < addUnits.length; ++i)
292      {
293        int uId = Values.getInt(addUnits[i], -1);
294        if (uId != -1) annotationType.addUsableUnit(Unit.getById(dc, uId));
295      }
296    }
297   
298    // Categories tab
299    String[] removeCategories = Values.getString(request.getParameter("removeCategories")).split(",");
300    for (int i = 0; i < removeCategories.length; ++i)
301    {
302      int categoryId = Values.getInt(removeCategories[i], -1);
303      if (categoryId != -1) annotationType.removeCategory(AnnotationTypeCategory.getById(dc, categoryId));
304    }
305   
306    String[] addCategories = Values.getString(request.getParameter("addCategories")).split(",");
307    for (int i = 0; i < addCategories.length; ++i)
308    {
309      int categoryId = Values.getInt(addCategories[i], -1);
310      if (categoryId != -1) annotationType.addCategory(AnnotationTypeCategory.getById(dc, categoryId));
311    }
312
313    dc.commit();
314    cc.removeObject("item");
315  }
316  else if ("DeleteItem".equals(cmd))
317  {
318    // Delete a single item and then return to the view page
319    dc = sc.newDbControl();
320    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
321    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
322    dc.commit();
323    redirect = viewPage;
324  }
325  else if ("DeleteItems".equals(cmd))
326  {
327    // Delete all selected items on the list page
328    dc = sc.newDbControl();
329    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
330    int numTotal = cc.getSelected().size();
331    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
332    dc.commit();
333    if (numTotal != numRemoved)
334    {
335      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
336    }
337    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
338  }
339  else if ("RestoreItem".equals(cmd))
340  {
341    // Restore a single item and then return to the view page
342    dc = sc.newDbControl();
343    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
344    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
345    dc.commit();
346    redirect = viewPage;
347  }
348  else if ("RestoreItems".equals(cmd))
349  {
350    // Restore all selected items on the list page
351    dc = sc.newDbControl();
352    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
353    int numTotal = cc.getSelected().size();
354    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
355    dc.commit();
356    if (numTotal != numRemoved)
357    {
358      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
359    }
360    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
361  }
362  else if ("ShareItem".equals(cmd))
363  {
364    // Display a popup window for sharing a single item
365    dc = sc.newDbControl();
366    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
367    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
368    dc.close();
369    cc.setObject("MultiPermissions", permissions);
370    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
371  }
372  else if ("ShareItems".equals(cmd))
373  {
374    // Display a popup window for sharing all selected items on the list page
375    dc = sc.newDbControl();
376    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
377    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
378    dc.close();
379    cc.setObject("MultiPermissions", permissions);
380    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
381  }
382  else if ("SetOwnerOfItem".equals(cmd))
383  {
384    // Change owner of items selected on a list page
385    dc = sc.newDbControl();
386    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
387    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
388    cc.setObject("OwnedItems", Collections.singleton(item));
389    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
390  }
391  else if ("SetOwnerOfItems".equals(cmd))
392  {
393    // Change owner of items selected on a list page
394    dc = sc.newDbControl();
395    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
396    Set<OwnedItem> items = new HashSet<OwnedItem>();
397    for (Integer id : cc.getSelected())
398    {
399      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
400    }
401    dc.close();
402    cc.setObject("OwnedItems", items);
403    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
404  }
405  else if ("ExportItems".equals(cmd))
406  {
407    // Run an export plugin in a list context
408    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
409    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
410    cc.configureQuery(query, true);
411    cc.setQuery(query);
412    registerExportUtils(cc);
413    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+annotation+types";
414  }
415  else if ("ExportItem".equals(cmd))
416  {
417    // Run an export plugin in single-item context
418    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
419    registerExportUtils(cc);
420    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+annotation+type";
421  }
422  else if ("ImportItems".equals(cmd))
423  {
424    // Run an import plugin in a list context
425    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
426    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
427    cc.configureQuery(query, true);
428    cc.setQuery(query);
429    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+annotation+types";
430  }
431  else if ("ImportItem".equals(cmd))
432  {
433    // Run an import plugin in single-item context
434    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
435    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+annotation+typey";
436  }
437  else if ("RunListPlugin".equals(cmd))
438  {
439    // Run another plugin in a list context
440    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
441    final ItemQuery<AnnotationType> query = AnnotationType.getQuery(null);
442    cc.configureQuery(query, true);
443    cc.setQuery(query);
444    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
445  }
446  else if ("RunPlugin".equals(cmd))
447  {
448    // Run another plugin in single-item context
449    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
450    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
451  }
452  else
453  {
454    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
455  }
456}
457finally
458{
459  if (dc != null) dc.close();
460}
461
462if (forward != null)
463{
464  pageContext.forward(forward);
465}
466else if (redirect != null)
467{
468  response.sendRedirect(redirect);
469}
470else if (message == null)
471{
472  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
473}
474else
475{
476  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
477}
478%>
479
Note: See TracBrowser for help on using the repository browser.