source: trunk/www/biomaterials/bioplates/wells/ajax.jsp @ 5557

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

References #1570: Use JSON for data transport in AJAX calls

Implemented this for various plate/well releated AJAX calls.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 4.3 KB
Line 
1<%-- $Id $
2  ------------------------------------------------------------------
3  Copyright (C) 2010 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  @author Nicklas
23--%>
24<%@ page pageEncoding="UTF-8" session="false" contentType="application/json"
25  import="net.sf.basedb.core.SessionControl"
26  import="net.sf.basedb.core.DbControl"
27  import="net.sf.basedb.core.Permission"
28  import="net.sf.basedb.core.BioWell"
29  import="net.sf.basedb.core.MeasuredBioMaterial"
30  import="net.sf.basedb.core.InvalidDataException"
31  import="net.sf.basedb.core.PermissionDeniedException"
32  import="net.sf.basedb.util.Values"
33  import="net.sf.basedb.util.error.ThrowableUtil"
34  import="net.sf.basedb.clients.web.Base"
35  import="net.sf.basedb.clients.web.WebException"
36  import="net.sf.basedb.clients.web.util.HTML"
37  import="org.json.simple.JSONObject"
38  import="org.json.simple.JSONArray"
39%>
40<%
41final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
42final String ID = sc.getId();
43final String cmd = request.getParameter("cmd");
44final String root = request.getContextPath()+"/";
45final int itemId = Values.getInt(request.getParameter("item_id"));
46DbControl dc = null;
47out.clear();
48JSONObject json = new JSONObject();
49json.put("status", "ok");
50try
51{
52  if ("WellInfo".equals(cmd))
53  {
54    dc = sc.newDbControl();
55    boolean encodeStrings = Values.getBoolean(request.getParameter("encodeStrings"));
56    BioWell well = BioWell.getById(dc, itemId);
57    boolean isEmpty = well.isEmpty();
58    boolean hasBeenUsed = well.hasBeenUsed();
59   
60    json.put("id", well.getId());
61    json.put("isEmpty", isEmpty);
62   
63    if (!isEmpty || hasBeenUsed)
64    {
65      JSONObject jsonBioMaterial = new JSONObject();
66      try
67      {
68        MeasuredBioMaterial bioMaterial = isEmpty ? well.getOriginalBioMaterial() : well.getBioMaterial();
69        jsonBioMaterial.put("id", bioMaterial.getId());
70        jsonBioMaterial.put("type", bioMaterial.getType().name());
71        jsonBioMaterial.put("name", encodeStrings ? HTML.encodeTags(bioMaterial.getName()) : bioMaterial.getName());
72        jsonBioMaterial.put("description", encodeStrings ? HTML.niceFormat(bioMaterial.getDescription()) : bioMaterial.getDescription());
73        jsonBioMaterial.put("editable", bioMaterial.hasPermission(Permission.WRITE));
74      }
75      catch (PermissionDeniedException ex)
76      {
77        jsonBioMaterial.put("denied", true);
78      }
79      json.put("bioMaterial", jsonBioMaterial);
80    }
81    /*
82    String status = "Ok";
83    MeasuredBioMaterial bioMaterial = null;
84    if (isEmpty && !hasBeenUsed)
85    {
86      status = "Empty";
87    }
88    else
89    {
90      try
91      {
92        bm = isEmpty ? w.getOriginalBioMaterial() : w.getBioMaterial();
93      }
94      catch (PermissionDeniedException ex)
95      {
96        status = "Denied";
97        bm = null;
98      }
99    }
100    aw.ajaxPrintEntry("status", status);
101    aw.ajaxPrintEntry("id", w.getId());
102    json.put("id", well.getId());
103   
104    if (bm != null)
105    {
106      aw.ajaxPrintEntry("bm.id", bm.getId());
107      aw.ajaxPrintEntry("bm.type", bm.getType().name());
108      aw.ajaxPrintEntry("bm.editable", bm.hasPermission(Permission.WRITE) ? "1" : "0");
109      aw.ajaxPrintEntry("bm.name", encodeStrings ? HTML.encodeTags(bm.getName()) : bm.getName());
110      aw.ajaxPrintEntry("bm.description", encodeStrings ? HTML.niceFormat(bm.getDescription()) : bm.getDescription());
111    }
112    aw.ajaxEndRecord();
113    */
114    dc.close();
115  }
116  else
117  {
118    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
119  }
120}
121catch (Throwable t)
122{
123  t.printStackTrace();
124  json.clear();
125  json.put("status", "error");
126  json.put("message", t.getMessage());
127  json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
128}
129finally
130{
131  json.writeJSONString(out);
132  out.flush();
133  if (dc != null) dc.close();
134}
135%>
Note: See TracBrowser for help on using the repository browser.