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