1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.elasticsearch.index.objects.event;
23
24 import org.opencastproject.elasticsearch.index.objects.IndexObject;
25 import org.opencastproject.util.IoSupport;
26 import org.opencastproject.util.XmlSafeParser;
27
28 import org.codehaus.jettison.json.JSONException;
29 import org.codehaus.jettison.json.JSONObject;
30 import org.codehaus.jettison.mapped.Configuration;
31 import org.codehaus.jettison.mapped.MappedNamespaceConvention;
32 import org.codehaus.jettison.mapped.MappedXMLStreamReader;
33 import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.xml.sax.SAXException;
37
38 import java.io.BufferedReader;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.io.StringWriter;
43 import java.util.HashMap;
44 import java.util.Map;
45
46 import javax.xml.bind.JAXBContext;
47 import javax.xml.bind.JAXBException;
48 import javax.xml.bind.Marshaller;
49 import javax.xml.bind.Unmarshaller;
50 import javax.xml.bind.annotation.XmlAccessType;
51 import javax.xml.bind.annotation.XmlAccessorType;
52 import javax.xml.bind.annotation.XmlElement;
53 import javax.xml.bind.annotation.XmlRootElement;
54 import javax.xml.bind.annotation.XmlType;
55 import javax.xml.stream.XMLStreamException;
56 import javax.xml.stream.XMLStreamReader;
57 import javax.xml.stream.XMLStreamWriter;
58
59
60
61
62 @XmlType(
63 name = "comment",
64 namespace = IndexObject.INDEX_XML_NAMESPACE,
65 propOrder = {
66 "id", "reason", "text", "resolvedStatus"
67 }
68 )
69 @XmlRootElement(name = "comment", namespace = IndexObject.INDEX_XML_NAMESPACE)
70 @XmlAccessorType(XmlAccessType.NONE)
71 public class Comment implements IndexObject {
72
73
74 private static final Logger logger = LoggerFactory.getLogger(Comment.class);
75
76
77 public static final String DOCUMENT_TYPE = "comment";
78
79
80 public static final String XML_SURROUNDING_TAG = "comments";
81
82
83 @XmlElement(name = "id")
84 private String id = null;
85
86
87 @XmlElement(name = "reason")
88 private String reason = null;
89
90
91 @XmlElement(name = "text")
92 private String text = null;
93
94
95 @XmlElement(name = "resolvedStatus")
96 private Boolean resolvedStatus = null;
97
98
99 private static JAXBContext context = null;
100
101
102
103
104 public Comment() {
105
106 }
107
108
109
110
111
112
113
114
115
116 public Comment(String id, String reason, String text, boolean resolvedStatus) {
117 this.id = id;
118 this.reason = reason;
119 this.text = text;
120 this.resolvedStatus = resolvedStatus;
121 }
122
123 public String getId() {
124 return id;
125 }
126
127 public void setId(String id) {
128 this.id = id;
129 }
130
131 public String getReason() {
132 return reason;
133 }
134
135 public void setReason(String reason) {
136 this.reason = reason;
137 }
138
139 public String getText() {
140 return text;
141 }
142
143 public void setText(String text) {
144 this.text = text;
145 }
146
147 public Boolean isResolvedStatus() {
148 return resolvedStatus;
149 }
150
151 public void setResolvedStatus(Boolean resolvedStatus) {
152 this.resolvedStatus = resolvedStatus;
153 }
154
155
156
157
158
159
160
161
162
163
164 public static Comment valueOf(InputStream xml, Unmarshaller unmarshaller) throws IOException {
165 try {
166 if (context == null) {
167 createJAXBContext();
168 }
169 return unmarshaller.unmarshal(XmlSafeParser.parse(xml), Comment.class).getValue();
170 } catch (JAXBException e) {
171 throw new IOException(e.getLinkedException() != null ? e.getLinkedException() : e);
172 } catch (SAXException e) {
173 throw new IOException(e);
174 } finally {
175 IoSupport.closeQuietly(xml);
176 }
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public static Comment valueOfJson(InputStream json)
190 throws IOException, JSONException, XMLStreamException, JAXBException {
191
192 if (context == null) {
193 createJAXBContext();
194 }
195
196 BufferedReader streamReader = new BufferedReader(new InputStreamReader(json, "UTF-8"));
197 StringBuilder jsonStringBuilder = new StringBuilder();
198 String inputStr;
199 while ((inputStr = streamReader.readLine()) != null) {
200 jsonStringBuilder.append(inputStr);
201 }
202
203 JSONObject obj = new JSONObject(jsonStringBuilder.toString());
204 Configuration config = new Configuration();
205 config.setSupressAtAttributes(true);
206 Map<String, String> xmlToJsonNamespaces = new HashMap<String, String>(1);
207 xmlToJsonNamespaces.put(IndexObject.INDEX_XML_NAMESPACE, "");
208 config.setXmlToJsonNamespaces(xmlToJsonNamespaces);
209 MappedNamespaceConvention con = new MappedNamespaceConvention(config);
210 Unmarshaller unmarshaller = context.createUnmarshaller();
211
212
213 XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
214 Comment comment = (Comment) unmarshaller.unmarshal(xmlStreamReader);
215
216 return comment;
217 }
218
219
220
221
222 private static void createJAXBContext() throws JAXBException {
223 context = JAXBContext.newInstance(Comment.class);
224 }
225
226
227
228
229
230
231 public String toJSON() {
232 try {
233 if (context == null) {
234 createJAXBContext();
235 }
236 Marshaller marshaller = Comment.context.createMarshaller();
237
238 Configuration config = new Configuration();
239 config.setSupressAtAttributes(true);
240 MappedNamespaceConvention con = new MappedNamespaceConvention(config);
241 StringWriter writer = new StringWriter();
242 XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {
243 @Override
244 public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
245 super.writeStartElement("", local, "");
246 }
247
248 @Override
249 public void writeStartElement(String uri, String local) throws XMLStreamException {
250 super.writeStartElement("", local, "");
251 }
252
253 @Override
254 public void setPrefix(String pfx, String uri) throws XMLStreamException {
255 }
256
257 @Override
258 public void setDefaultNamespace(String uri) throws XMLStreamException {
259 }
260 };
261
262 marshaller.marshal(this, xmlStreamWriter);
263 return writer.toString();
264 } catch (JAXBException e) {
265 throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
266 }
267 }
268
269
270
271
272
273
274 public String toXML() {
275 try {
276 if (context == null) {
277 createJAXBContext();
278 }
279 StringWriter writer = new StringWriter();
280 Marshaller marshaller = Comment.context.createMarshaller();
281 marshaller.marshal(this, writer);
282 return writer.toString();
283 } catch (JAXBException e) {
284 throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
285 }
286 }
287
288
289
290
291
292
293 public static Unmarshaller createUnmarshaller() throws IOException {
294 try {
295 if (context == null) {
296 createJAXBContext();
297 }
298 return context.createUnmarshaller();
299 } catch (JAXBException e) {
300 throw new IOException(e.getLinkedException() != null ? e.getLinkedException() : e);
301 }
302 }
303
304 }