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.IOException; |
---|
14 | import java.io.InputStream; |
---|
15 | import java.net.MalformedURLException; |
---|
16 | import java.net.URL; |
---|
17 | import java.util.HashMap; |
---|
18 | |
---|
19 | import javax.xml.parsers.DocumentBuilder; |
---|
20 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
21 | |
---|
22 | import org.apache.commons.io.IOUtils; |
---|
23 | import org.w3c.dom.Attr; |
---|
24 | import org.w3c.dom.Document; |
---|
25 | import org.w3c.dom.Element; |
---|
26 | import org.z3950.zing.cql.CQLNode; |
---|
27 | import org.z3950.zing.cql.CQLParseException; |
---|
28 | import org.z3950.zing.cql.CQLParser; |
---|
29 | |
---|
30 | import eu.clarin.cmdi.mdservice.internal.Admin; |
---|
31 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
32 | |
---|
33 | public class Diagnostic { |
---|
34 | public static int SYSTEM_ERROR = 1; |
---|
35 | public static int UNSUPPOERTED_OPERATION = 4; |
---|
36 | public static int UNSUPPORTED_VERSION = 5; |
---|
37 | public static int UNSUPPORTED_PARAMETERVALUE = 6; |
---|
38 | public static int MANDATORY_NOTSUPPLIED = 7; |
---|
39 | public static int UNSUPPORTED_PARAMETER = 8; |
---|
40 | public static int QUERYSYNTAXERROR = 10; |
---|
41 | |
---|
42 | // types ? |
---|
43 | public static String FATAL = "fatal"; |
---|
44 | public static String NONFATALSURROGATE = "surrogate"; |
---|
45 | public static String NONFATALNONSURORGATE = "nonsurrogate"; |
---|
46 | |
---|
47 | public static String NAMESPACE = "info:srw/diagnostic/1/"; |
---|
48 | |
---|
49 | private String type; |
---|
50 | |
---|
51 | private int number; |
---|
52 | private String details; |
---|
53 | private String message; |
---|
54 | |
---|
55 | public int getNumber(){ |
---|
56 | return this.number; |
---|
57 | } |
---|
58 | |
---|
59 | public void setNumber(int number){ |
---|
60 | this.number = number; |
---|
61 | } |
---|
62 | |
---|
63 | public String getType(){ |
---|
64 | return this.type; |
---|
65 | } |
---|
66 | |
---|
67 | public void setType(String type){ |
---|
68 | this.type = type; |
---|
69 | } |
---|
70 | |
---|
71 | public String Type(){ |
---|
72 | String t = this.type; |
---|
73 | |
---|
74 | if (t == null){ |
---|
75 | t = Diagnostic.diagnostictypes.get(this.number); |
---|
76 | if (t == null){ |
---|
77 | t = FATAL; |
---|
78 | } |
---|
79 | } |
---|
80 | return t; |
---|
81 | } |
---|
82 | public Diagnostic(int number) { |
---|
83 | this.number = number; |
---|
84 | this.message = diagnosticmessage.get(number); |
---|
85 | //this.type = null; |
---|
86 | } |
---|
87 | |
---|
88 | public Diagnostic(int number, String details) { |
---|
89 | this(number,details,null); |
---|
90 | } |
---|
91 | |
---|
92 | public Diagnostic(int number, String details, String type) { |
---|
93 | this(number); |
---|
94 | this.details = details; |
---|
95 | this.type = type; |
---|
96 | } |
---|
97 | |
---|
98 | public Diagnostic(int number, String details, String type,String message) { |
---|
99 | this.number = number; |
---|
100 | this.details = details; |
---|
101 | this.type = type; |
---|
102 | this.message = message; |
---|
103 | } |
---|
104 | |
---|
105 | private final static HashMap<Integer,String> diagnosticmessage = new HashMap<Integer,String>(); |
---|
106 | static |
---|
107 | { diagnosticmessage.put(SYSTEM_ERROR, "General system error."); |
---|
108 | diagnosticmessage.put(UNSUPPOERTED_OPERATION, "Unsupported operation."); |
---|
109 | diagnosticmessage.put(UNSUPPORTED_VERSION, "Unsupported version."); |
---|
110 | diagnosticmessage.put(UNSUPPORTED_PARAMETERVALUE, "Unsupported parameter value."); |
---|
111 | diagnosticmessage.put(MANDATORY_NOTSUPPLIED, "Mandatory parameter not supplied."); |
---|
112 | diagnosticmessage.put(UNSUPPORTED_PARAMETER, "Unsupported Parameter."); |
---|
113 | diagnosticmessage.put(QUERYSYNTAXERROR, "Query syntax error."); |
---|
114 | |
---|
115 | } |
---|
116 | public final static HashMap<Integer,String> diagnostictypes = new HashMap<Integer,String>(); |
---|
117 | static |
---|
118 | { diagnostictypes.put(SYSTEM_ERROR, FATAL); |
---|
119 | diagnostictypes.put(UNSUPPOERTED_OPERATION, FATAL); |
---|
120 | diagnostictypes.put(UNSUPPORTED_VERSION, FATAL); |
---|
121 | diagnostictypes.put(UNSUPPORTED_PARAMETERVALUE, FATAL); |
---|
122 | diagnostictypes.put(MANDATORY_NOTSUPPLIED, FATAL); |
---|
123 | diagnostictypes.put(UNSUPPORTED_PARAMETER, FATAL); |
---|
124 | diagnostictypes.put(QUERYSYNTAXERROR, FATAL); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | public Element buildXMLNode(Document doc){ |
---|
129 | |
---|
130 | Element d = doc.createElement("diagnostic"); |
---|
131 | Attr attr = doc.createAttribute("xmlns"); |
---|
132 | attr.setValue("http://www.loc.gov/zing/srw/diagnostic/"); |
---|
133 | d.setAttributeNode(attr); |
---|
134 | |
---|
135 | Element uri = doc.createElement("uri"); |
---|
136 | uri.appendChild(doc.createTextNode(NAMESPACE + Integer.toString(number))); |
---|
137 | d.appendChild(uri); |
---|
138 | |
---|
139 | if (this.details != null){ |
---|
140 | Element details = doc.createElement("details"); |
---|
141 | details.appendChild(doc.createTextNode(this.details)); |
---|
142 | d.appendChild(details); |
---|
143 | } |
---|
144 | |
---|
145 | if (this.message != null){ |
---|
146 | Element message = doc.createElement("message"); |
---|
147 | message.appendChild(doc.createTextNode(this.message)); |
---|
148 | d.appendChild(message); |
---|
149 | } |
---|
150 | return d; |
---|
151 | |
---|
152 | } |
---|
153 | } |
---|