source: trunk/www/common/columns/ajax.jsp @ 7773

Last change on this file since 7773 was 7773, checked in by Nicklas Nordborg, 2 years ago

References #2202: Include information from parent items in list pages

Implemented a simple dialog for selecting parent item columns. It currently allows selecting to display the name of a parent item or any annotation that is attached to it. It can probably be improved a bit more.

File size: 5.7 KB
Line 
1<%-- $Id $
2  ------------------------------------------------------------------
3  Copyright (C) 2015 Nicklas Nordborg
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 3
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 BASE. If not, see <http://www.gnu.org/licenses/>.
20  ------------------------------------------------------------------
21
22--%>
23<%@ page pageEncoding="UTF-8" session="false" contentType="application/json"
24  import="net.sf.basedb.core.SessionControl"
25  import="net.sf.basedb.core.DbControl"
26  import="net.sf.basedb.core.AnnotationType"
27  import="net.sf.basedb.core.Annotation"
28  import="net.sf.basedb.core.Item"
29  import="net.sf.basedb.core.Type"
30  import="net.sf.basedb.core.ItemQuery"
31  import="net.sf.basedb.core.ItemSubtype"
32  import="net.sf.basedb.core.plugin.GuiContext"
33  import="net.sf.basedb.util.Values"
34  import="net.sf.basedb.util.error.ThrowableUtil"
35  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
36  import="net.sf.basedb.core.query.Restrictions"
37  import="net.sf.basedb.core.query.Expressions"
38  import="net.sf.basedb.core.query.Hql"
39  import="net.sf.basedb.clients.web.AnnotationUtil"
40  import="net.sf.basedb.clients.web.Base"
41  import="net.sf.basedb.clients.web.WebException"
42  import="net.sf.basedb.clients.web.extensions.JspContext"
43  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
44  import="net.sf.basedb.clients.web.extensions.list.ListColumnAction"
45  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
46  import="org.json.simple.JSONObject"
47  import="org.json.simple.JSONArray"
48  import="java.util.Arrays"
49  import="java.util.List"
50  import="java.util.HashSet"
51%>
52<%
53response.setHeader("Cache-Control", "no-cache, max-age=0");
54final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
55final String ID = sc.getId();
56final String cmd = request.getParameter("cmd");
57final String root = request.getContextPath()+"/";
58final Item itemType = Item.valueOf(request.getParameter("itemType"));
59DbControl dc = null;
60out.clear();
61JSONObject json = new JSONObject();
62json.put("status", "ok");
63try
64{
65  if ("GetSubtypesAndAnnotationTypes".equals(cmd))
66  {
67    dc = sc.newDbControl();
68
69    ItemQuery<ItemSubtype> subtypeQuery = Base.getSubtypesQuery(itemType);
70    JSONArray jsonSubtypes = new JSONArray();
71    for (ItemSubtype st : subtypeQuery.list(dc))
72    {
73      JSONObject jsonSt = new JSONObject();
74      jsonSt.put("id", st.getId());
75      jsonSt.put("name", st.getName());
76      jsonSubtypes.add(jsonSt);
77    }
78    json.put("subtypes", jsonSubtypes);
79   
80    ItemQuery<AnnotationType> annotationTypeQuery = Base.getAnnotationTypesQuery(itemType);
81    JSONArray jsonAnnotationTypes = new JSONArray();
82    for (AnnotationType at : annotationTypeQuery.list(dc))
83    {
84      JSONObject jsonAt = new JSONObject();
85      jsonAt.put("id", at.getId());
86      jsonAt.put("name", at.getName());
87      jsonAnnotationTypes.add(jsonAt);
88    }
89    json.put("annotationTypes", jsonAnnotationTypes);
90   
91  }
92  else if ("GetAnnotationTypesForCategory".equals(cmd))
93  {
94    dc = sc.newDbControl();
95    String category = Values.getStringOrNull(request.getParameter("category"));
96    JSONArray jsonAnnotationTypes = new JSONArray();
97    if (category != null)
98    {
99      ItemQuery<AnnotationType> annotationTypeQuery = Base.getAnnotationTypesQuery(itemType);
100      annotationTypeQuery.join(Hql.innerJoin("categories", "c"));
101      annotationTypeQuery.restrict(Restrictions.eq(Hql.property("c", "name"), Expressions.string(category)));
102      for (AnnotationType at : annotationTypeQuery.list(dc))
103      {
104        JSONObject jsonAt = new JSONObject();
105        jsonAt.put("id", at.getId());
106        jsonAt.put("name", at.getName());
107        jsonAnnotationTypes.add(jsonAt);
108      }
109    }
110    json.put("annotationTypesCategory", jsonAnnotationTypes);
111  }
112  else if ("GetLazyParentColumns".equals(cmd))
113  {
114    String subContext = Values.getStringOrNull(request.getParameter("subcontext"));
115    GuiContext guiContext = GuiContext.list(itemType, subContext);
116    Integer[] items = Values.getInt(request.getParameter("items").split(","));
117    dc = sc.newDbControl();
118   
119    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, guiContext, null);
120    jspContext.setAttribute("lazy-loading", false);
121    ExtensionsInvoker<ListColumnAction<Object,?>> columnsInvoker = ListColumnUtil.useExtensions(jspContext);
122   
123    JSONArray jsonLazy = new JSONArray();
124    int index = 0;
125    for (Integer id : items)
126    {
127      Object item = itemType.getById(dc, id);
128      JSONObject jsonItem = new JSONObject();
129      JSONArray jsonCols = new JSONArray();
130      jsonItem.put("id", id);
131      jsonItem.put("data", jsonCols);
132     
133      for (ListColumnAction col : columnsInvoker)
134      {
135        if (col.getId().startsWith("/"))
136        {
137          jsonCols.add(col.getFormatter().format(col.getValue(dc, item)));
138        }
139      }
140      jsonLazy.add(jsonItem);
141    }
142    json.put("items", jsonLazy);
143  }
144  else
145  {
146    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
147  }
148}
149catch (Throwable t)
150{
151  t.printStackTrace();
152  json.clear();
153  json.put("status", "error");
154  json.put("message", t.getMessage());
155  json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
156}
157finally
158{
159  json.writeJSONString(out);
160  out.flush();
161  if (dc != null) dc.close();
162}
163%>
Note: See TracBrowser for help on using the repository browser.