5685 |
04 Aug 11 |
nicklas |
1 |
<%-- $Id $ |
5685 |
04 Aug 11 |
nicklas |
2 |
------------------------------------------------------------------ |
5685 |
04 Aug 11 |
nicklas |
Copyright (C) 2011 Nicklas Nordborg |
5685 |
04 Aug 11 |
nicklas |
4 |
|
5685 |
04 Aug 11 |
nicklas |
5 |
This file is part of BASE - BioArray Software Environment. |
5685 |
04 Aug 11 |
nicklas |
6 |
Available at http://base.thep.lu.se/ |
5685 |
04 Aug 11 |
nicklas |
7 |
|
5685 |
04 Aug 11 |
nicklas |
8 |
BASE is free software; you can redistribute it and/or |
5685 |
04 Aug 11 |
nicklas |
9 |
modify it under the terms of the GNU General Public License |
5685 |
04 Aug 11 |
nicklas |
10 |
as published by the Free Software Foundation; either version 3 |
5685 |
04 Aug 11 |
nicklas |
11 |
of the License, or (at your option) any later version. |
5685 |
04 Aug 11 |
nicklas |
12 |
|
5685 |
04 Aug 11 |
nicklas |
13 |
BASE is distributed in the hope that it will be useful, |
5685 |
04 Aug 11 |
nicklas |
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5685 |
04 Aug 11 |
nicklas |
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5685 |
04 Aug 11 |
nicklas |
16 |
GNU General Public License for more details. |
5685 |
04 Aug 11 |
nicklas |
17 |
|
5685 |
04 Aug 11 |
nicklas |
18 |
You should have received a copy of the GNU General Public License |
5685 |
04 Aug 11 |
nicklas |
19 |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5685 |
04 Aug 11 |
nicklas |
20 |
------------------------------------------------------------------ |
5685 |
04 Aug 11 |
nicklas |
21 |
|
5685 |
04 Aug 11 |
nicklas |
22 |
@author Nicklas |
5685 |
04 Aug 11 |
nicklas |
23 |
--%> |
5685 |
04 Aug 11 |
nicklas |
24 |
<%@ page pageEncoding="UTF-8" session="false" contentType="application/json" |
5685 |
04 Aug 11 |
nicklas |
25 |
import="net.sf.basedb.core.SessionControl" |
5685 |
04 Aug 11 |
nicklas |
26 |
import="net.sf.basedb.core.DbControl" |
5685 |
04 Aug 11 |
nicklas |
27 |
import="net.sf.basedb.core.PhysicalBioAssay" |
6095 |
30 Aug 12 |
nicklas |
28 |
import="net.sf.basedb.core.Extract" |
5685 |
04 Aug 11 |
nicklas |
29 |
import="net.sf.basedb.core.BioMaterial" |
5685 |
04 Aug 11 |
nicklas |
30 |
import="net.sf.basedb.core.BioMaterialEventSource" |
5685 |
04 Aug 11 |
nicklas |
31 |
import="net.sf.basedb.core.SpecialQuery" |
6095 |
30 Aug 12 |
nicklas |
32 |
import="net.sf.basedb.core.ItemQuery" |
6095 |
30 Aug 12 |
nicklas |
33 |
import="net.sf.basedb.core.Include" |
6095 |
30 Aug 12 |
nicklas |
34 |
import="net.sf.basedb.core.Type" |
5685 |
04 Aug 11 |
nicklas |
35 |
import="net.sf.basedb.core.PermissionDeniedException" |
6095 |
30 Aug 12 |
nicklas |
36 |
import="net.sf.basedb.core.query.Restrictions" |
6095 |
30 Aug 12 |
nicklas |
37 |
import="net.sf.basedb.core.query.Expressions" |
5685 |
04 Aug 11 |
nicklas |
38 |
import="net.sf.basedb.core.query.Hql" |
5685 |
04 Aug 11 |
nicklas |
39 |
import="net.sf.basedb.core.query.Orders" |
5685 |
04 Aug 11 |
nicklas |
40 |
import="net.sf.basedb.clients.web.Base" |
5685 |
04 Aug 11 |
nicklas |
41 |
import="net.sf.basedb.util.Values" |
5685 |
04 Aug 11 |
nicklas |
42 |
import="net.sf.basedb.util.error.ThrowableUtil" |
6799 |
25 Mar 15 |
nicklas |
43 |
import="net.sf.basedb.util.listable.TransformContext" |
6799 |
25 Mar 15 |
nicklas |
44 |
import="net.sf.basedb.util.listable.ExtractToParentExtractTransformer" |
6799 |
25 Mar 15 |
nicklas |
45 |
import="net.sf.basedb.util.listable.SourceItemTransformer" |
5685 |
04 Aug 11 |
nicklas |
46 |
import="net.sf.basedb.clients.web.WebException" |
5685 |
04 Aug 11 |
nicklas |
47 |
import="net.sf.basedb.clients.web.util.HTML" |
5685 |
04 Aug 11 |
nicklas |
48 |
import="org.json.simple.JSONObject" |
5685 |
04 Aug 11 |
nicklas |
49 |
import="org.json.simple.JSONArray" |
5685 |
04 Aug 11 |
nicklas |
50 |
import="java.util.Date" |
6095 |
30 Aug 12 |
nicklas |
51 |
import="java.util.Arrays" |
6095 |
30 Aug 12 |
nicklas |
52 |
import="java.util.List" |
6095 |
30 Aug 12 |
nicklas |
53 |
import="java.util.Set" |
6095 |
30 Aug 12 |
nicklas |
54 |
import="java.util.HashSet" |
5685 |
04 Aug 11 |
nicklas |
55 |
%> |
5685 |
04 Aug 11 |
nicklas |
56 |
<% |
6124 |
13 Sep 12 |
nicklas |
57 |
response.setHeader("Cache-Control", "no-cache, max-age=0"); |
5685 |
04 Aug 11 |
nicklas |
58 |
final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
5685 |
04 Aug 11 |
nicklas |
59 |
final String ID = sc.getId(); |
5685 |
04 Aug 11 |
nicklas |
60 |
final String cmd = request.getParameter("cmd"); |
5685 |
04 Aug 11 |
nicklas |
61 |
final String root = request.getContextPath()+"/"; |
5685 |
04 Aug 11 |
nicklas |
62 |
final int itemId = Values.getInt(request.getParameter("item_id")); |
5685 |
04 Aug 11 |
nicklas |
63 |
out.clear(); |
5685 |
04 Aug 11 |
nicklas |
64 |
JSONObject json = new JSONObject(); |
5685 |
04 Aug 11 |
nicklas |
65 |
json.put("status", "ok"); |
5685 |
04 Aug 11 |
nicklas |
66 |
DbControl dc = null; |
5685 |
04 Aug 11 |
nicklas |
67 |
try |
5685 |
04 Aug 11 |
nicklas |
68 |
{ |
5685 |
04 Aug 11 |
nicklas |
69 |
if ("GetSources".equals(cmd)) |
5685 |
04 Aug 11 |
nicklas |
70 |
{ |
7954 |
12 May 21 |
nicklas |
71 |
dc = sc.newDbControl(":Get biomaterial sources"); |
5685 |
04 Aug 11 |
nicklas |
72 |
PhysicalBioAssay pba = PhysicalBioAssay.getById(dc, itemId); |
5685 |
04 Aug 11 |
nicklas |
73 |
json.put("id", pba.getId()); |
5685 |
04 Aug 11 |
nicklas |
74 |
json.put("name", pba.getName()); |
5685 |
04 Aug 11 |
nicklas |
75 |
|
5685 |
04 Aug 11 |
nicklas |
76 |
SpecialQuery<BioMaterialEventSource> sourceQuery = pba.getCreationEvent().getEventSources(); |
5685 |
04 Aug 11 |
nicklas |
77 |
sourceQuery.order(Orders.asc(Hql.property("position"))); |
5685 |
04 Aug 11 |
nicklas |
78 |
sourceQuery.order(Orders.asc(Hql.property("bioMaterial.name"))); |
5685 |
04 Aug 11 |
nicklas |
79 |
JSONArray jsonSources = new JSONArray(); |
5685 |
04 Aug 11 |
nicklas |
80 |
for (BioMaterialEventSource src : sourceQuery.list(dc)) |
5685 |
04 Aug 11 |
nicklas |
81 |
{ |
5685 |
04 Aug 11 |
nicklas |
82 |
JSONObject jsonSrc = new JSONObject(); |
5685 |
04 Aug 11 |
nicklas |
83 |
try |
5685 |
04 Aug 11 |
nicklas |
84 |
{ |
5685 |
04 Aug 11 |
nicklas |
85 |
BioMaterial extract = src.getBioMaterial(); |
5685 |
04 Aug 11 |
nicklas |
86 |
jsonSrc.put("id", extract.getId()); |
5685 |
04 Aug 11 |
nicklas |
87 |
jsonSrc.put("name", extract.getName()); |
5685 |
04 Aug 11 |
nicklas |
88 |
jsonSrc.put("position", src.getPosition()); |
5685 |
04 Aug 11 |
nicklas |
89 |
jsonSources.add(jsonSrc); |
5685 |
04 Aug 11 |
nicklas |
90 |
} |
5685 |
04 Aug 11 |
nicklas |
91 |
catch (PermissionDeniedException ex) |
5685 |
04 Aug 11 |
nicklas |
92 |
{} |
5685 |
04 Aug 11 |
nicklas |
93 |
} |
5685 |
04 Aug 11 |
nicklas |
94 |
json.put("sources", jsonSources); |
5685 |
04 Aug 11 |
nicklas |
95 |
|
5685 |
04 Aug 11 |
nicklas |
96 |
dc.commit(); |
5685 |
04 Aug 11 |
nicklas |
97 |
} |
6095 |
30 Aug 12 |
nicklas |
98 |
else if ("GetAllExtractIds".equals(cmd)) |
6095 |
30 Aug 12 |
nicklas |
99 |
{ |
7954 |
12 May 21 |
nicklas |
100 |
dc = sc.newDbControl(":Get all extracts"); |
6095 |
30 Aug 12 |
nicklas |
101 |
|
6095 |
30 Aug 12 |
nicklas |
102 |
Integer[] bioAssayIds = Values.getInt(request.getParameter("bioassay_ids").split(",")); |
6095 |
30 Aug 12 |
nicklas |
103 |
|
6095 |
30 Aug 12 |
nicklas |
104 |
// Load all extracts that are directly linked with the given physical bioassays |
6095 |
30 Aug 12 |
nicklas |
105 |
ItemQuery<Extract> query = Extract.getQuery(); |
6095 |
30 Aug 12 |
nicklas |
106 |
query.include(Include.ALL); |
6095 |
30 Aug 12 |
nicklas |
107 |
query.join(Hql.innerJoin("childCreationEvents", "cce")); |
6095 |
30 Aug 12 |
nicklas |
108 |
query.join(Hql.innerJoin("cce", "event", "evt")); |
6095 |
30 Aug 12 |
nicklas |
109 |
query.restrict(Restrictions.in(Hql.property("evt", "physicalBioAssay"), Expressions.parameter("bioAssays"))); |
6095 |
30 Aug 12 |
nicklas |
110 |
query.setParameter("bioAssays", Arrays.asList(bioAssayIds), Type.INT); |
6799 |
25 Mar 15 |
nicklas |
111 |
Set<Integer> primaryExtracts = new HashSet<Integer>(query.idList(dc)); |
6095 |
30 Aug 12 |
nicklas |
112 |
|
6799 |
25 Mar 15 |
nicklas |
113 |
// Use the ExtractToParentExtractTransformer to load all parent extracts as well |
6799 |
25 Mar 15 |
nicklas |
114 |
SourceItemTransformer transformer = new ExtractToParentExtractTransformer(true); |
6799 |
25 Mar 15 |
nicklas |
115 |
Set<Integer> allExtracts = transformer.transform(new TransformContext(dc), primaryExtracts); |
6095 |
30 Aug 12 |
nicklas |
116 |
|
6095 |
30 Aug 12 |
nicklas |
117 |
JSONArray jsonExtracts = new JSONArray(); |
6799 |
25 Mar 15 |
nicklas |
118 |
for (Integer e : allExtracts) |
6095 |
30 Aug 12 |
nicklas |
119 |
{ |
6799 |
25 Mar 15 |
nicklas |
120 |
jsonExtracts.add(e); |
6095 |
30 Aug 12 |
nicklas |
121 |
} |
6095 |
30 Aug 12 |
nicklas |
122 |
|
6095 |
30 Aug 12 |
nicklas |
123 |
json.put("allExtractIds", jsonExtracts); |
6095 |
30 Aug 12 |
nicklas |
124 |
dc.commit(); |
6095 |
30 Aug 12 |
nicklas |
125 |
} |
5685 |
04 Aug 11 |
nicklas |
126 |
else |
5685 |
04 Aug 11 |
nicklas |
127 |
{ |
5685 |
04 Aug 11 |
nicklas |
128 |
throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd); |
5685 |
04 Aug 11 |
nicklas |
129 |
} |
5685 |
04 Aug 11 |
nicklas |
130 |
} |
5685 |
04 Aug 11 |
nicklas |
131 |
catch (Throwable t) |
5685 |
04 Aug 11 |
nicklas |
132 |
{ |
5685 |
04 Aug 11 |
nicklas |
133 |
t.printStackTrace(); |
5685 |
04 Aug 11 |
nicklas |
134 |
json.clear(); |
5685 |
04 Aug 11 |
nicklas |
135 |
json.put("status", "error"); |
5685 |
04 Aug 11 |
nicklas |
136 |
json.put("message", t.getMessage()); |
5685 |
04 Aug 11 |
nicklas |
137 |
json.put("stacktrace", ThrowableUtil.stackTraceToString(t)); |
5685 |
04 Aug 11 |
nicklas |
138 |
} |
5685 |
04 Aug 11 |
nicklas |
139 |
finally |
5685 |
04 Aug 11 |
nicklas |
140 |
{ |
5685 |
04 Aug 11 |
nicklas |
141 |
json.writeJSONString(out); |
5685 |
04 Aug 11 |
nicklas |
142 |
out.flush(); |
5685 |
04 Aug 11 |
nicklas |
143 |
if (dc != null) dc.close(); |
5685 |
04 Aug 11 |
nicklas |
144 |
} |
5685 |
04 Aug 11 |
nicklas |
145 |
%> |