1 | package eu.clarin.cmdi.mdservice.model; |
---|
2 | |
---|
3 | /** |
---|
4 | * The main model class, carrying users_query |
---|
5 | * able to parse and transform the user-string |
---|
6 | * and execute the query, ie issue appropriate request on the MDRepository |
---|
7 | * getting the url-wording right from MDRepositoryProxy |
---|
8 | * |
---|
9 | * At the moment just responsible for querying MDRepository |
---|
10 | * perhaps unify approach for other proxies - subtyping this Class |
---|
11 | */ |
---|
12 | |
---|
13 | import java.io.ByteArrayInputStream; |
---|
14 | import java.io.IOException; |
---|
15 | import java.io.InputStream; |
---|
16 | import java.io.StringWriter; |
---|
17 | import java.io.UnsupportedEncodingException; |
---|
18 | import java.net.MalformedURLException; |
---|
19 | import java.net.URL; |
---|
20 | import java.util.ArrayList; |
---|
21 | import java.util.Iterator; |
---|
22 | import java.util.List; |
---|
23 | |
---|
24 | import javax.xml.parsers.DocumentBuilder; |
---|
25 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
26 | import javax.xml.parsers.ParserConfigurationException; |
---|
27 | import javax.xml.transform.OutputKeys; |
---|
28 | import javax.xml.transform.Transformer; |
---|
29 | import javax.xml.transform.TransformerConfigurationException; |
---|
30 | import javax.xml.transform.TransformerException; |
---|
31 | import javax.xml.transform.TransformerFactory; |
---|
32 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
33 | import javax.xml.transform.dom.DOMSource; |
---|
34 | import javax.xml.transform.stream.StreamResult; |
---|
35 | |
---|
36 | import org.apache.commons.io.IOUtils; |
---|
37 | import org.w3c.dom.Document; |
---|
38 | import org.w3c.dom.Element; |
---|
39 | import org.z3950.zing.cql.CQLNode; |
---|
40 | import org.z3950.zing.cql.CQLParseException; |
---|
41 | import org.z3950.zing.cql.CQLParser; |
---|
42 | |
---|
43 | import eu.clarin.cmdi.mdservice.action.Admin; |
---|
44 | import eu.clarin.cmdi.mdservice.action.MDTransformer; |
---|
45 | |
---|
46 | public class Diagnostics { |
---|
47 | |
---|
48 | private ArrayList<Diagnostic> list; |
---|
49 | private InputStream xmlstream; |
---|
50 | |
---|
51 | public Diagnostics() { |
---|
52 | list = new ArrayList<Diagnostic>(); |
---|
53 | } |
---|
54 | |
---|
55 | public void Add(int number, String details, String message) { |
---|
56 | list.add(new Diagnostic(number, details, message)); |
---|
57 | } |
---|
58 | |
---|
59 | public void Add(int number, String details) { |
---|
60 | list.add(new Diagnostic(number, details)); |
---|
61 | } |
---|
62 | public void Add(int number) { |
---|
63 | list.add(new Diagnostic(number)); |
---|
64 | } |
---|
65 | |
---|
66 | public void Add(Diagnostic diagnostic){ |
---|
67 | list.add(diagnostic); |
---|
68 | } |
---|
69 | |
---|
70 | public Document buildXMLDocument() throws ParserConfigurationException{ |
---|
71 | Document doc = null; |
---|
72 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
73 | DocumentBuilder docBuilder; |
---|
74 | docBuilder = docFactory.newDocumentBuilder(); |
---|
75 | //root elements |
---|
76 | doc = docBuilder.newDocument(); |
---|
77 | Element response = doc.createElement("searchRetrieveResponse"); |
---|
78 | doc.appendChild(response); |
---|
79 | |
---|
80 | Element version = doc.createElement("version"); |
---|
81 | version.appendChild(doc.createTextNode("1.2")); |
---|
82 | response.appendChild(version); |
---|
83 | |
---|
84 | Element numberofrecords = doc.createElement("numberOfRecords"); |
---|
85 | numberofrecords.appendChild(doc.createTextNode("0")); |
---|
86 | response.appendChild(numberofrecords); |
---|
87 | |
---|
88 | Element d = doc.createElement("diagnostics"); |
---|
89 | Iterator itr = list.iterator(); |
---|
90 | while(itr.hasNext()) |
---|
91 | d.appendChild(((Diagnostic)itr.next()).buildXMLNode(doc)); |
---|
92 | response.appendChild(d); |
---|
93 | |
---|
94 | |
---|
95 | return doc; |
---|
96 | } |
---|
97 | @SuppressWarnings("unchecked") |
---|
98 | public InputStream buildXMLStream() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, UnsupportedEncodingException{ |
---|
99 | if (xmlstream == null){ |
---|
100 | Document doc = buildXMLDocument(); |
---|
101 | DOMSource source = new DOMSource(doc); |
---|
102 | StringWriter xmlAsWriter = new StringWriter(); |
---|
103 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
104 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
105 | xmlstream = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); |
---|
106 | } |
---|
107 | return xmlstream; |
---|
108 | } |
---|
109 | |
---|
110 | public boolean Accepted(){ |
---|
111 | boolean accepted = true; |
---|
112 | |
---|
113 | if (this.list.size() > 0){ |
---|
114 | Iterator itr = list.iterator(); |
---|
115 | while(itr.hasNext() && accepted) |
---|
116 | { |
---|
117 | Diagnostic d = (Diagnostic)itr.next(); |
---|
118 | if (d.Type().equals(Diagnostic.FATAL)){ |
---|
119 | accepted = false; |
---|
120 | } |
---|
121 | } |
---|
122 | // accepted = accepted && !((Diagnostic)itr.next()).Type().equals(Diagnostic.FATAL); |
---|
123 | } |
---|
124 | return accepted; |
---|
125 | } |
---|
126 | |
---|
127 | public boolean Exists(){ |
---|
128 | return this.list.size() > 0; |
---|
129 | } |
---|
130 | } |
---|