source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/model/Diagnostics.java @ 1540

Last change on this file since 1540 was 1540, checked in by gaba, 13 years ago

Utils.java in place of Helpers and Admin
Diagnostics changes in GenericAction?
code arrangement, comments

File size: 3.3 KB
Line 
1package 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
13import java.io.InputStream;
14import java.util.ArrayList;
15import java.util.Iterator;
16
17import javax.xml.parsers.DocumentBuilder;
18import javax.xml.parsers.DocumentBuilderFactory;
19import javax.xml.parsers.ParserConfigurationException;
20
21import org.w3c.dom.Document;
22import org.w3c.dom.Element;
23
24public class Diagnostics {     
25
26        private ArrayList<Diagnostic> list; 
27        private InputStream xmlstream;
28
29        public Diagnostics() {
30                list = new ArrayList<Diagnostic>();
31        }
32
33        public void Add(int number, String details, String message) {
34                list.add(new Diagnostic(number, details, message));
35        }
36       
37        public void Add(int number, String details) {
38                list.add(new Diagnostic(number, details));
39        }
40        public void Add(int number) {
41                list.add(new Diagnostic(number));
42        }
43       
44        public void Add(Diagnostic diagnostic){
45                list.add(diagnostic);
46        }
47       
48        public Document buildXMLDocument() throws ParserConfigurationException{
49                Document doc = null;
50                  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
51                  DocumentBuilder docBuilder;
52                        docBuilder = docFactory.newDocumentBuilder();
53                        //root elements
54                          doc = docBuilder.newDocument();
55                          Element response = doc.createElement("searchRetrieveResponse");
56                          doc.appendChild(response);
57                         
58                          Element version = doc.createElement("version");
59                          version.appendChild(doc.createTextNode("1.2"));
60                          response.appendChild(version);
61                 
62                          Element numberofrecords = doc.createElement("numberOfRecords");
63                          numberofrecords.appendChild(doc.createTextNode("0"));
64                          response.appendChild(numberofrecords);
65                         
66                          Element d = doc.createElement("diagnostics");
67                          Iterator<Diagnostic> itr = list.iterator();
68                                while(itr.hasNext())
69                                        d.appendChild(((Diagnostic)itr.next()).buildXMLNode(doc));
70                          response.appendChild(d);
71                         
72
73                return doc;
74        }
75       
76        //TODO to Utils.java
77        /*
78        public InputStream buildXMLStream() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, UnsupportedEncodingException{
79                if (xmlstream == null){
80                        Document doc = buildXMLDocument();
81                        DOMSource source = new DOMSource(doc);   
82                        StringWriter xmlAsWriter = new StringWriter();   
83                        StreamResult result = new StreamResult(xmlAsWriter);   
84                        TransformerFactory.newInstance().newTransformer().transform(source, result);
85                        xmlstream = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
86                }
87                return xmlstream;
88        }
89        */
90       
91        public boolean Accepted(){
92                boolean accepted = true;
93               
94                if (this.list.size() > 0){
95                         Iterator<Diagnostic> itr = list.iterator();
96                                while(itr.hasNext() && accepted) 
97                                {
98                                        Diagnostic d = (Diagnostic)itr.next();
99                                        if (d.Type().equals(Diagnostic.FATAL)){
100                                                accepted = false;       
101                                        }
102                                }
103                        //              accepted = accepted && !((Diagnostic)itr.next()).Type().equals(Diagnostic.FATAL);               
104                }
105                return accepted;
106        }
107       
108        public boolean Exists(){
109                return this.list.size() > 0;
110        }
111}
Note: See TracBrowser for help on using the repository browser.