1 package uk.ac.ebi.intenz.tools.export;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.util.Collection;
6 import java.util.List;
7
8 import javax.json.Json;
9 import javax.json.JsonArrayBuilder;
10 import javax.json.JsonObject;
11 import javax.json.JsonObjectBuilder;
12 import javax.json.JsonWriter;
13
14 import uk.ac.ebi.intenz.domain.constants.View;
15 import uk.ac.ebi.intenz.domain.enzyme.EnzymeClass;
16 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
17 import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
18 import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubSubclass;
19 import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubclass;
20 import uk.ac.ebi.xchars.SpecialCharacters;
21
22
23
24
25
26
27
28 public class JsonExporter implements IntenzExporter {
29
30 public void export(Collection<EnzymeEntry> enzymes, OutputStream os)
31 throws IOException {
32 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
33 for (EnzymeEntry enzyme : enzymes) {
34 jsonArrayBuilder.add(jsonFromEnzyme(enzyme));
35 }
36 JsonWriter jsonWriter = Json.createWriter(os);
37 jsonWriter.writeArray(jsonArrayBuilder.build());
38 jsonWriter.close();
39 }
40
41 public void export(EnzymeEntry enzyme, OutputStream os) throws IOException {
42 JsonWriter jsonWriter = Json.createWriter(os);
43 jsonWriter.writeObject(jsonFromEnzyme(enzyme));
44 jsonWriter.close();
45 }
46
47
48
49
50
51
52
53 public void export(EnzymeSubSubclass essc, OutputStream os){
54 export(essc, os, true);
55 }
56
57
58
59
60
61
62
63
64 public void export(EnzymeSubSubclass essc, OutputStream os,
65 boolean onlyActive){
66 JsonWriter jsonWriter = Json.createWriter(os);
67 jsonWriter.writeObject(jsonFromSubsubClass(essc, onlyActive));
68 jsonWriter.close();
69 }
70
71
72
73
74
75
76 public void export(EnzymeSubclass esc, OutputStream os){
77 JsonWriter jsonWriter = Json.createWriter(os);
78 jsonWriter.writeObject(jsonFromSubclass(esc));
79 jsonWriter.close();
80 }
81
82
83
84
85
86
87 public void export(EnzymeClass ec, OutputStream os){
88 JsonWriter jsonWriter = Json.createWriter(os);
89 jsonWriter.writeObject(jsonFromClass(ec));
90 jsonWriter.close();
91 }
92
93
94
95
96
97
98 public void export(List<EnzymeClass> ecs, OutputStream os){
99 JsonWriter jsonWriter = Json.createWriter(os);
100 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
101 for (EnzymeClass ec : ecs) {
102 jsonArrayBuilder.add(jsonFromClass(ec));
103 }
104 jsonWriter.writeArray(jsonArrayBuilder.build());
105 jsonWriter.close();
106 }
107
108
109
110
111
112
113
114
115
116
117
118 private JsonObject jsonFromEnzyme(EnzymeEntry enzyme) {
119 JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
120 jsonObjectBuilder.add("ec", enzyme.getEc().toString());
121 jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
122 .xml2Display(enzyme.getCommonName().getName()));
123 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
124 for (EnzymeName synonym : enzyme.getSynonyms(View.INTENZ)){
125 jsonArrayBuilder.add(SpecialCharacters.getInstance(null)
126 .xml2Display(synonym.getName()));
127 }
128 jsonObjectBuilder.add("synonyms", jsonArrayBuilder);
129
130 return jsonObjectBuilder.build();
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 private JsonObject jsonFromSubsubClass(EnzymeSubSubclass essc,
147 boolean onlyActive){
148 JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
149 jsonObjectBuilder.add("ec", essc.getEc().toString());
150 jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
151 .xml2Display(essc.getClassName()));
152 jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
153 .xml2Display(essc.getDescription()));
154 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
155 for (EnzymeEntry e : essc.getEntries()) {
156 if (onlyActive && !e.isActive()) continue;
157 jsonArrayBuilder.add(jsonFromEnzyme(e));
158 }
159 jsonObjectBuilder.add("entries", jsonArrayBuilder.build());
160 return jsonObjectBuilder.build();
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175 private JsonObject jsonFromSubclass(EnzymeSubclass esc){
176 JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
177 jsonObjectBuilder.add("ec", esc.getEc().toString());
178 jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
179 .xml2Display(esc.getName()));
180 jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
181 .xml2Display(esc.getDescription()));
182 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
183 for (EnzymeSubSubclass essc : esc.getSubSubclasses()) {
184 jsonArrayBuilder.add(Json.createObjectBuilder()
185 .add("ec", essc.getEc().toString())
186 .add("name", SpecialCharacters.getInstance(null)
187 .xml2Display(essc.getName()))
188 .add("description", SpecialCharacters.getInstance(null)
189 .xml2Display(essc.getDescription())));
190 }
191 jsonObjectBuilder.add("subsubclasses", jsonArrayBuilder.build());
192 return jsonObjectBuilder.build();
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207 private JsonObject jsonFromClass(EnzymeClass ec){
208 JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
209 jsonObjectBuilder.add("ec", ec.getEc().toString());
210 jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
211 .xml2Display(ec.getName()));
212 jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
213 .xml2Display(ec.getDescription()));
214 JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
215 for (EnzymeSubclass esc : ec.getSubclasses()) {
216 jsonArrayBuilder.add(Json.createObjectBuilder()
217 .add("ec", esc.getEc().toString())
218 .add("name", SpecialCharacters.getInstance(null)
219 .xml2Display(esc.getName()))
220 .add("description", SpecialCharacters.getInstance(null)
221 .xml2Display(esc.getDescription())));
222 }
223 jsonObjectBuilder.add("subclasses", jsonArrayBuilder.build());
224 return jsonObjectBuilder.build();
225 }
226 }