source: other-projects/trunk/greenstone3-extension/mat/src/java/org/greenstone/gsdl3_extension/mat/DescribeMessenger.java@ 17365

Last change on this file since 17365 was 17365, checked in by cc108, 16 years ago

Updating Mat Source Code

File size: 10.9 KB
Line 
1package org.greenstone.gsdl3_extension.mat;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.Iterator;
9
10
11import java.io.*;
12
13import org.w3c.dom.Node;
14import org.w3c.dom.Document;
15import org.w3c.dom.Element;
16
17import org.greenstone.gsdl3.core.MessageRouter;
18import org.greenstone.gsdl3.util.GlobalProperties;
19import org.greenstone.gsdl3.util.GSFile;
20import org.greenstone.gsdl3.util.GSXML;
21import org.greenstone.gsdl3.util.XMLConverter;
22import org.greenstone.gsdl3.util.GDBMWrapper;
23
24
25public class DescribeMessenger {
26
27 protected String site_name = "localsite";
28
29
30 /** message router object to pass requests messages to and which
31 * will process them.*/
32 protected MessageRouter mr = null;
33 private Node CollectionList = null;
34 /** container Document to create XML Nodes */
35 protected Document doc=null;
36 /** a converter class to parse XML and create Docs */
37 protected XMLConverter converter=null;
38
39 protected final static String mrSubsetOptions = //messageRouter
40 "collectionList serviceClusterList serviceList siteList";
41 protected final static String csSubsetOptions = //collections and serviceClusters
42 "metadataList serviceList displayItemList";
43 protected final static String serviceSubsetOptions = //services
44 "paramList displayItemList";
45 final int MESSAGEROUTER = 1, COLLECTION = 2,
46 SERVICECLUSTER = 3, SERVICE = 4;
47
48 private String collection_Name = null;
49 private String OAI_URL = null;
50 protected GDBMWrapper gdbm_src = null;
51 protected String site_name1 = "";
52 protected String coll_name1 = "";
53 protected Element coll_config_xml = null;
54 protected HashMap element_map = new HashMap();
55 protected MetadataStats ms;
56 String site_home = null;
57
58 public DescribeMessenger() {
59
60 String gsdl3_home = GlobalProperties.getGSDL3Home();
61
62 if (gsdl3_home == null || gsdl3_home.equals("")) {
63 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
64 return;
65 }
66
67
68 site_home = GSFile.siteHome(gsdl3_home, this.site_name);
69
70 File site_file = new File(site_home);
71 if (!site_file.isDirectory()) {
72 System.err.println("The site directory "+site_file.getPath()+" doesn't exist. Can't initialize the SOAP Server.");
73 return;
74 }
75 this.converter = new XMLConverter();
76 this.doc = this.converter.newDOM();
77
78 mr = new MessageRouter();
79
80 mr.setSiteName(this.site_name);
81 mr.setLibraryName(this.site_name);
82 mr.configure();
83 this.gdbm_src = new GDBMWrapper();
84
85
86
87 }
88
89 public DescribeMessenger(String collectionName, String URL) {
90
91 collection_Name = collectionName;
92 OAI_URL = URL;
93
94 String gsdl3_home = GlobalProperties.getGSDL3Home();
95 if (gsdl3_home == null || gsdl3_home.equals("")) {
96 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
97 return;
98 }
99
100 site_home = GSFile.siteHome(gsdl3_home, this.site_name);
101
102 File site_file = new File(site_home);
103 if (!site_file.isDirectory()) {
104 System.err.println("The site directory "+site_file.getPath()+" doesn't exist. Can't initialize the SOAP Server.");
105 return;
106 }
107 this.converter = new XMLConverter();
108 this.doc = this.converter.newDOM();
109
110 mr = new MessageRouter();
111 mr.setSiteName(this.site_name);
112 //mr.configure();
113 this.gdbm_src = new GDBMWrapper();
114 //describe();
115 }
116 /*
117 public void setURL(String arg){
118 OAI_URL = arg;
119 }
120 */
121
122 public void setCollectionName(String arg){
123 collection_Name = arg;
124 }
125
126 public String describe()
127 {
128 return describe("", "", "", mrSubsetOptions);
129 }
130
131 /*
132 public String getURL(){
133 return OAI_URL;
134 }
135 */
136
137 public String getCollectionName(){
138 return collection_Name;
139 }
140
141 /*
142 //Describe Messages sent to Services (or Collection/Services)
143 public String describeCollService(String toColl, String toService,
144 String lang, String subsetOption) {
145
146 return describe(toColl + "/" + toService,
147 lang, subsetOption, serviceSubsetOptions);
148 }
149 */
150 //If public, this method would give full access: a describe message that
151 //lets the user specify all the details of who the receiver is, and what
152 //details are requested.
153 protected String describe(String to, String lang,
154 String subsetOption, String validSubsetOptions)
155 {
156
157 //Create message element: <message></message>
158 try{
159 //DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
160 //Get the DocumentBuilder
161 //DocumentBuilder parser = factory.newDocumentBuilder();
162 //Create blank DOM Document
163 //Document doct = parser.newDocument();
164
165 Node message = doc.createElement(GSXML.MESSAGE_ELEM);
166
167 //<message><request lang="en" to="" type="describe" uid="" /></message>
168
169 Node request = GSXML.createBasicRequest(
170 this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
171
172 //Check if only a subset of this Module Interface's data is asked
173 //to be described
174
175 if(subsetOption != "") {
176 //Now deal with the value for subset param:
177 //only deal with valid params for subset of to-ModuleInterface
178 if(validSubsetOptions.indexOf(subsetOption) == -1)
179 return "Invalid List to be described. Choose one of:\n"
180 + validSubsetOptions;
181
182 //else, append <paramList />
183 Node param_list = this.doc.createElement(
184 GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
185 param_list.appendChild(GSXML.createParameter(
186 this.doc, GSXML.SUBSET_PARAM, subsetOption));
187 request.appendChild(param_list);
188
189 }
190 message.appendChild(request);
191 //System.out.println(this.converter.getPrettyString(message));
192 /*
193 Element request = GSXML.createBasicRequest(
194 this.doc, GSXML.REQUEST_TYPE_PROCESS,"nwgfril"+"/DocumentMetadataRetrieve","en", "");
195
196Element param_list = this.doc.createElement(
197 GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
198
199param_list.appendChild(GSXML.createParameter(
200 this.doc, "metadata", "all"));
201
202Element documentNode_list = this.doc.createElement(
203 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
204
205Element documentNode = this.doc.createElement(GSXML.DOC_NODE_ELEM);
206documentNode.setAttribute("nodeID","HASH01981d6a4a1502253e1a2283");
207documentNode_list.appendChild(documentNode);
208
209request.appendChild(param_list);
210request.appendChild(documentNode_list);
211message.appendChild(request);
212System.out.println(this.converter.getPrettyString(message));
213*/
214 // message.appendChild(request);
215 //System.out.println("\nRequest message");
216 //System.out.println(this.converter.getPrettyString(message));
217 //System.out.println(this.converter.getPrettyString(message));
218 //////////////////////////////////////////////////////////////////////////////////////////////
219 ////////////////////////////////////////////////////////////////////////////////////////////////
220 CollectionList = mr.process(message);
221 //System.out.println(this.converter.getPrettyString(CollectionList));
222 //Element response = mr.process(message);
223 //System.out.println(this.converter.getPrettyString(response));
224 //System.out.println("list"+CollectionList);
225 return ">>"+GSXML.MESSAGE_ELEM+"<<";
226 //return request.;
227 }catch(Exception ex){return "failure";}
228 //return this.converter.getPrettyString(message.toString());
229 //return this.converter.getPrettyString(CollectionList);
230
231 }
232
233 public Node getCollectionList(){
234 return CollectionList;
235 }
236
237
238 public void describeMatadata(String collectionName,String OAIURL){
239
240 ms = new MetadataStats(this.site_home,collectionName,OAIURL,"test");
241
242 if(ms.getStatus()){
243 //return;
244 printWebPage(ms);
245 }
246 // Servlet-------------------------------------------------
247 else{
248 System.out.print("<p> The metadataStats fails</p>");
249 }
250 //System.out.println("\nTotal Number of Documents: "+ms.getDocNum());
251 //System.out.println("\n system ends");
252 //System.out.println(ms.metadataNameList.size());
253
254 }
255
256 public void printWebPage(MetadataStats ms){
257 //long startTime=System.currentTimeMillis();
258 DataMaker dm = new DataMaker(ms);
259 String[] elements = new String [ms.metadataNameList.size()];
260
261 for(int i = 0; i<elements.length; i++){
262 elements[i] = (String)ms.metadataNameList.get(i);
263 }
264
265 Arrays.sort(elements);
266 String[] metaDataElementName = (String[])elements.clone();
267 String[] ids = dm.getDocumentIDs("archivedir");
268
269 ArrayList alist = new ArrayList();
270 for(int a = 0; a<elements.length; a++){
271 alist.add(dm.getMetadataRows(elements[a]));
272 }
273 //System.out.println(alist.size()+ " " +ids.length+" "+metaDataElementName.length);
274 PrintHTML phtml = new PrintHTML(ms);
275
276 //?????????????????????????????????????????????????????????????????????????/
277 HashMap hp = ms.getMetadataSetMap();
278 Collection c = hp.values();
279 Iterator i = c.iterator();
280
281 while(i.hasNext()){
282 MetadataSet mds = (MetadataSet)i.next();
283 //if(!mds.getName().equals("extracted")){
284 ArrayList elementList = mds.getIndexsList();
285 String[] mdsElement = new String[elementList.size()];
286 for(int x = 0; x<mdsElement.length; x++){
287 mdsElement[x]=(String)elementList.get(x);
288 }
289 Arrays.sort(mdsElement);
290
291 String[] metaDataElement = (String[])mdsElement.clone();
292 String[] idList = dm.getDocumentIDs("archivedir");
293
294 ArrayList datalist = new ArrayList();
295 for(int a = 0; a<mdsElement.length; a++){
296 datalist.add(dm.getMetadataRows(mdsElement[a]));
297 }
298 phtml.generateAllPossibleGraph(datalist,idList,metaDataElement,mds.getName());
299 //}
300 }
301
302 //??????????????????????????????????????????????????????????????????????????
303 //long startTime=System.currentTimeMillis();
304 //phtml.generateAllPossibleGraph(alist,ids,metaDataElementName,"both");
305 //////////////////////////////////
306 //Now generate chart for individaul mds
307 //????????????????????????????????????????????????????????//
308
309 //long endTime=System.currentTimeMillis();
310 //System.out.println("Total time taken for generating overall Statistics Page "+(endTime-startTime)+ " millisec");
311
312 phtml.generateOverallStatisticsPage(hp);
313
314 Collection cx = ms.getMetadataSetMap().values();
315 Iterator ix = cx.iterator();
316
317 while(ix.hasNext()){
318 MetadataSet temp = (MetadataSet)ix.next();
319 phtml.WriteMetadataSetDetailHTML(temp);
320 phtml.generateMetadataElementDetailPage(temp);
321 }
322 }
323
324
325 public MetadataStats getElementClass()
326 {
327 return ms;
328 }
329
330 /*
331 public static void main(String[] args) {
332 DescribeMessenger dm = new DescribeMessenger();
333 System.out.println("test");
334 dm.describe("","","","");
335 dm.describeMatadata("sampleco","http://researchcommons.waikato.ac.nz/cgi/oai2.cgi");
336 }
337 */
338}
339
Note: See TracBrowser for help on using the repository browser.