source: other-projects/trunk/greenstone3-extension/mat/Greenstone3Project/src/Mat/DescribeMessager.java@ 17156

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

Adding the project Metadata Quality for Digital Libraries into the repository

File size: 11.3 KB
Line 
1package Mat;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.StringTokenizer;
6
7import org.w3c.dom.Node;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11import org.greenstone.gsdl3.core.MessageRouter;
12import org.greenstone.gsdl3.util.DBInfo;
13import org.greenstone.gsdl3.util.GDBMWrapper;
14import org.greenstone.gsdl3.util.GlobalProperties;
15import org.greenstone.gsdl3.util.GSFile;
16import org.greenstone.gsdl3.util.GSXML;
17import org.greenstone.gsdl3.util.XMLConverter;
18
19
20
21public class DescribeMessager {
22 protected String site_name = "localsite";
23 private GDBMWrapper gdbm_src = new GDBMWrapper();
24 /** message router object to pass requests messages to and which
25 * will process them.*/
26 protected MessageRouter mr = null;
27
28 /** container Document to create XML Nodes */
29 protected Document doc=null;
30 /** a converter class to parse XML and create Docs */
31 protected XMLConverter converter=null;
32
33 protected final static String mrSubsetOptions = //messageRouter
34 "collectionList serviceClusterList serviceList siteList";
35 protected final static String csSubsetOptions = //collections and serviceClusters
36 "metadataList serviceList displayItemList";
37 protected final static String serviceSubsetOptions = //services
38 "paramList displayItemList";
39 final int MESSAGEROUTER = 1, COLLECTION = 2,
40 SERVICECLUSTER = 3, SERVICE = 4;
41
42
43 public DescribeMessager() {
44 String gsdl3_home = GlobalProperties.getGSDL3Home();
45 if (gsdl3_home == null || gsdl3_home.equals("")) {
46 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
47 return;
48 }
49
50 String site_home = GSFile.siteHome(gsdl3_home, this.site_name);
51
52 File site_file = new File(site_home);
53 if (!site_file.isDirectory()) {
54 System.err.println("The site directory "+site_file.getPath()+" doesn't exist. Can't initialize the SOAP Server.");
55 return;
56 }
57 this.converter = new XMLConverter();
58 this.doc = this.converter.newDOM();
59
60 mr = new MessageRouter();
61 mr.setSiteName(this.site_name);
62 mr.configure();
63 }
64
65 //Describe messages sent to MESSAGEROUTER
66 //By default: sends a describe request message to the MessageRouter, with
67 //lang=en (and all MessageRouter subsetOptions set).
68 public String describe()
69 {
70 //describe(to=MR, lang=en, no subsetOptions, from valid mrSubsetOptions)
71 return describe("", "", "", mrSubsetOptions);
72 }
73
74 public String describe(String subsetOption)
75 {
76 return describe("", "", subsetOption, mrSubsetOptions);
77 }
78
79 public String describe(String lang, String subsetOption)
80 {
81 return describe("", lang, subsetOption, mrSubsetOptions);
82 }
83
84
85 //Describe messages sent to COLLECTIONS and SERVICERACKS
86 public String describeServiceRack(String toSC, String lang,
87 String subsetOption)
88 {
89 return describe(toSC, lang, subsetOption, csSubsetOptions);
90 }
91
92
93 public String describeCollection(String toColl, String lang,
94 String subsetOption)
95 {
96 return describe(toColl, lang, subsetOption, csSubsetOptions);
97 }
98
99 //Describe Messages sent to Services (or Collection/Services)
100 public String describeCollService(String toColl, String toService,
101 String lang, String subsetOption) {
102 /*String message =
103 "<message><request lang=\"en\" to=\"gs2mgppdemo/FieldQuery\" type=\"describe\" /></message>";
104
105 String response = mr.process(message);
106 System.out.println("Response to: " + message + ":\n");
107 System.out.println(response);
108 */
109 return describe(toColl + "/" + toService,
110 lang, subsetOption, serviceSubsetOptions);
111 }
112
113 public String describeService(
114 String toService, String lang, String subsetOption)
115 {
116 return describe(toService, lang, subsetOption, serviceSubsetOptions);
117 }
118
119 //GET POSSIBLE SUBSET OPTIONS FOR THE VARIOUS RECEIVERS (MessageRouter,
120 //Services, ServiceRack/Collections
121
122 //Subset options that can be specified when asking the messageRouter to
123 //describe itself
124 public String getMessageRouterSubsetOptions() {
125 return mrSubsetOptions;
126 }
127
128 //Possible subset options for ServiceClusters and Collections
129 public String getCSSubsetOptions() {
130 return csSubsetOptions;
131 }
132
133 //Possible subset options for services
134 public String getServiceSubsetOptions() {
135 return serviceSubsetOptions;
136 }
137
138 //If public, this method would give full access: a describe message that
139 //lets the user specify all the details of who the receiver is, and what
140 //details are requested.
141
142 /*
143 protected String describe(String to, String lang,
144 String subsetOption, String validSubsetOptions)
145 {
146 //Create message element: <message></message>
147 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
148 //<message><request lang="en" to="" type="describe" uid="" /></message>
149 Element request = GSXML.createBasicRequest(
150 this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
151
152 //Check if only a subset of this Module Interface's data is asked
153 //to be described
154 if(subsetOption != "") {
155 //Now deal with the value for subset param:
156 //only deal with valid params for subset of to-ModuleInterface
157 if(validSubsetOptions.indexOf(subsetOption) == -1)
158 return "Invalid List to be described. Choose one of:\n"
159 + validSubsetOptions;
160
161 //else, append <paramList />
162 Element param_list = this.doc.createElement(
163 GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
164 //append <param name="subset" value=paramValue />
165 //createParam(Document, name, value);
166 //Name needs to be "subset", but this can be either GSXML.SUBSET_PARAM
167 //or GSXML.SYSTEM_SUBSET_ATT. It's the first one probably.
168 param_list.appendChild(GSXML.createParameter(
169 this.doc, GSXML.SUBSET_PARAM, subsetOption));
170 request.appendChild(param_list);
171 }
172 message.appendChild(request);
173
174 //System.out.println(this.converter.getPrettyString(message));
175
176 //Let the messagerouter process the request message and get the response
177 Element response = mr.process(message.getFirstChild());
178 //String response = mr.process(this.converter.getPrettyString(message));
179 //this.converter.getPrettyString(arg0);
180 //mr.p
181 //Return it as a String formatted for display
182 System.out.println(this.converter.getPrettyString(response));
183
184 return null;
185 //return this.converter.getPrettyString(response);
186 //return this.converter.getString(response);
187 //return mr.process(this.converter.getString(message));
188 }
189 */
190
191
192
193 protected String describe(String to, String lang,
194 String subsetOption, String validSubsetOptions)
195 {
196 //Create message element: <message></message>
197 Node message = this.doc.createElement(GSXML.MESSAGE_ELEM);
198 //<message><request lang="en" to="" type="describe" uid="" /></message>
199 Node request = GSXML.createBasicRequest(
200 this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, "");
201
202 //Check if only a subset of this Module Interface's data is asked
203 //to be described
204 if(subsetOption != "") {
205 //Now deal with the value for subset param:
206 //only deal with valid params for subset of to-ModuleInterface
207 if(validSubsetOptions.indexOf(subsetOption) == -1)
208 return "Invalid List to be described. Choose one of:\n"
209 + validSubsetOptions;
210
211 //else, append <paramList />
212 Node param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
213 //append <param name="subset" value=paramValue />
214 //createParam(Document, name, value);
215 //Name needs to be "subset", but this can be either GSXML.SUBSET_PARAM
216 //or GSXML.SYSTEM_SUBSET_ATT. It's the first one probably.
217 param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, subsetOption));
218 request.appendChild(param_list);
219 }
220 message.appendChild(request);
221
222 //System.out.println(this.converter.getPrettyString(message));
223
224 //Let the messagerouter process the request message and get the response
225 Node response = mr.process(message);
226 //String response = mr.process(this.converter.getPrettyString(message));
227 //this.converter.getPrettyString(arg0);
228 //mr.p
229 //Return it as a String formatted for display
230 Element res = (Element) response;
231 System.out.println(this.converter.getPrettyString(res));
232 System.out.println("break1");
233
234 String gsdl3_home = GlobalProperties.getGSDL3Home();
235 if (gsdl3_home == null || gsdl3_home.equals("")) {
236 System.err.println("Couldn't access GSDL3Home from GlobalProperties.getGSDL3HOME, can't initialize the SOAP Server.");
237 }
238
239 String site_home = GSFile.siteHome(gsdl3_home, this.site_name);
240 String gdbm = GSFile.collectionDatabaseFile(site_home,"sampleco","sampleco","gdbm");
241
242 /*
243 try{
244 System.out.println("break2.1");
245 //System.out.println(this.gdbm_src.openDatabase("gs2mgppdemo",GDBMWrapper.READ));
246 }catch (Exception ex) {ex.printStackTrace();}
247 */
248 //System.out.println(gdbm);
249 System.out.println("break2.2");
250 if (!this.gdbm_src.openDatabase(gdbm,GDBMWrapper.READ)) {
251 //if(false){
252 System.out.println("break31");
253 System.out.println("Could not open GDBM database!");
254
255 }
256
257 else{
258 System.out.println("break32");
259
260
261 String info = this.gdbm_src.getValue("browselist");
262
263 if (info == null) {
264 System.out.println("cannot locate the list");
265 }
266 else{
267
268 if (info == null) {
269 System.out.println("the db does not contain any info");
270 }
271
272
273 ArrayList children = new ArrayList();
274 StringTokenizer st = new StringTokenizer(info, ";");
275 //StringTokenizer st = new StringTokenizer(st2, ";");
276 //System.out.println(info);
277
278 while (st.hasMoreTokens()) {
279 String part = st.nextToken(";");
280 //System.out.println("..."+part+".....1");
281
282
283 if(part.contains("<contains>")){
284 part = part.replace("<contains>", "");
285 }
286 else if(part.contains("<thistype>")){
287 //System.out.println("here");
288 int location = part.indexOf("<thistype>");
289 part = part.substring(0,location-1);
290 }
291 System.out.println("..."+part+".....2");
292 //String child_id = st.nextToken().replaceAll("\"", node_id);
293 //children.add(child_id);
294 }
295
296 gdbm_src.closeDatabase();
297 System.out.println("start..................");
298 //System.out.println(info);
299 System.out.println("end..................");
300 //parseMetadata(children,"all");
301 //TotalDoc = children.size();
302 //System.out.println("Total Doc:"+TotalDoc);
303 //status = true;
304 //Display();
305 //}
306 }
307 }
308
309 return "";
310 //return this.converter.getPrettyString(response);
311 //return this.converter.getString(response);
312 //return mr.process(this.converter.getString(message));
313 }
314 //UNUSED:
315 public static String describeExampleResponse() {
316 XMLConverter converter = new XMLConverter();
317 Document doc = converter.getDOM("SampleServiceResponse.xml");
318 return converter.getPrettyString(doc);
319 }
320
321
322
323 public static void main(String[] args) {
324 DescribeMessager dm = new DescribeMessager();
325 String response[] = new String[3];
326
327 response[0] = dm.describe();
328 //response[1] = dm.describeMessageRouter("", "serviceList"); //""->lang=en
329 response[1] = dm.describeService("TextQuery", "en", "");
330 response[2] = dm.describeCollection("gs2mgdemo", "en", "metadataList");
331 //response[3] = dm.describeCollection("gberg", "en", "serviceList");
332 //response[4] = dm.describeService("DocumentMetadataRetrieve", "", "");
333 //response[3] = dm.describeService("DocumentContentRetrieve", "en", "");
334
335 //French doesn't seem to have any effect
336 //response[3] = dm.sendDescribe("", "fr");
337 /*
338 for(int i = 0; i < response.length; i++)
339 System.out.println(
340 (response[i]==null) ? "no response" : response[i]);
341 System.exit(0);
342 */
343 }
344}
Note: See TracBrowser for help on using the repository browser.