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

Last change on this file since 7852 was 7852, checked in by Nicklas Nordborg, 16 months ago

References #2222: Add support for manually adding any-to-any link column in table lists

Implemented a function for remembering the most recently used item types when adding a column. The most recent types are then displayed first in the selection list.

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