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

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

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

Implemented lazy-loading of data from the parent item colums. The column definition will generate a proxy html tag (ParentItemColumn.getProxyTag()) that a JavaScript (lazy-parent-items.js) can detect and initiate a lazy loading action via AJAX.

File size: 4.8 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.clients.web.AnnotationUtil"
37  import="net.sf.basedb.clients.web.Base"
38  import="net.sf.basedb.clients.web.WebException"
39  import="net.sf.basedb.clients.web.extensions.JspContext"
40  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
41  import="net.sf.basedb.clients.web.extensions.list.ListColumnAction"
42  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
43  import="org.json.simple.JSONObject"
44  import="org.json.simple.JSONArray"
45  import="java.util.Arrays"
46  import="java.util.List"
47  import="java.util.HashSet"
48%>
49<%
50response.setHeader("Cache-Control", "no-cache, max-age=0");
51final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
52final String ID = sc.getId();
53final String cmd = request.getParameter("cmd");
54final String root = request.getContextPath()+"/";
55final Item itemType = Item.valueOf(request.getParameter("itemType"));
56DbControl dc = null;
57out.clear();
58JSONObject json = new JSONObject();
59json.put("status", "ok");
60try
61{
62  if ("GetSubtypesAndAnnotationTypes".equals(cmd))
63  {
64    dc = sc.newDbControl();
65
66    ItemQuery<ItemSubtype> subtypeQuery = Base.getSubtypesQuery(itemType);
67    JSONArray jsonSubtypes = new JSONArray();
68    for (ItemSubtype st : subtypeQuery.list(dc))
69    {
70      JSONObject jsonSt = new JSONObject();
71      jsonSt.put("id", st.getId());
72      jsonSt.put("name", st.getName());
73      jsonSubtypes.add(jsonSt);
74    }
75    json.put("subtypes", jsonSubtypes);
76   
77    ItemQuery<AnnotationType> annotationTypeQuery = Base.getAnnotationTypesQuery(itemType);
78    JSONArray jsonAnnotationTypes = new JSONArray();
79    for (AnnotationType at : annotationTypeQuery.list(dc))
80    {
81      JSONObject jsonAt = new JSONObject();
82      jsonAt.put("id", at.getId());
83      jsonAt.put("name", at.getName());
84      jsonAnnotationTypes.add(jsonAt);
85    }
86    json.put("annotationTypes", jsonAnnotationTypes);
87   
88  }
89  else if ("GetLazyParentColumns".equals(cmd))
90  {
91    String subContext = Values.getStringOrNull(request.getParameter("subcontext"));
92    GuiContext guiContext = GuiContext.list(itemType, subContext);
93    Integer[] items = Values.getInt(request.getParameter("items").split(","));
94    dc = sc.newDbControl();
95   
96    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, guiContext, null);
97    jspContext.setAttribute("lazy-loading", false);
98    ExtensionsInvoker<ListColumnAction<Object,?>> columnsInvoker = ListColumnUtil.useExtensions(jspContext);
99   
100    JSONArray jsonLazy = new JSONArray();
101    int index = 0;
102    for (Integer id : items)
103    {
104      Object item = itemType.getById(dc, id);
105      JSONObject jsonItem = new JSONObject();
106      JSONArray jsonCols = new JSONArray();
107      jsonItem.put("id", id);
108      jsonItem.put("data", jsonCols);
109     
110      for (ListColumnAction col : columnsInvoker)
111      {
112        if (col.getId().startsWith("/"))
113        {
114          jsonCols.add(col.getFormatter().format(col.getValue(dc, item)));
115        }
116      }
117      jsonLazy.add(jsonItem);
118    }
119    json.put("items", jsonLazy);
120  }
121  else
122  {
123    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
124  }
125}
126catch (Throwable t)
127{
128  t.printStackTrace();
129  json.clear();
130  json.put("status", "error");
131  json.put("message", t.getMessage());
132  json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
133}
134finally
135{
136  json.writeJSONString(out);
137  out.flush();
138  if (dc != null) dc.close();
139}
140%>
Note: See TracBrowser for help on using the repository browser.