1 | package org.greenstone.gsdl3.core;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.util.*;
|
---|
4 | import org.greenstone.gsdl3.action.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | // other java classes
|
---|
12 | import java.io.File;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.Enumeration;
|
---|
15 |
|
---|
16 | /** The default greenstone receptionist - needs some extra info for each page
|
---|
17 | */
|
---|
18 | public class DefaultReceptionist extends TransformingReceptionist {
|
---|
19 |
|
---|
20 | protected void addExtraInfo(Element page) {
|
---|
21 | // we want to add in the collection description for each page - cos
|
---|
22 | // our default xslt needs this.
|
---|
23 | super.addExtraInfo(page);
|
---|
24 |
|
---|
25 | Element page_request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
|
---|
26 | // if it is a system request, then we don't bother with this.
|
---|
27 | String action = page_request.getAttribute(GSXML.ACTION_ATT);
|
---|
28 | if (action.equals("s")) {
|
---|
29 | System.err.println("HACK: don't ask for coll info if system action");
|
---|
30 | return;
|
---|
31 | }
|
---|
32 | ///ystem.out.println("add extra info, page request="+this.converter.getString(page_request));
|
---|
33 | // is a collection defined?
|
---|
34 | Element param_list = (Element)GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
35 | if (param_list==null) { // must be the original home page
|
---|
36 | ///ystem.out.println("DefaultReceptionist: no param list, assuming home page");
|
---|
37 | return;
|
---|
38 | }
|
---|
39 | Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSParams.COLLECTION);
|
---|
40 | if (coll_param == null) {
|
---|
41 | ///ystem.out.println("DefaultReceptionist: coll param is null, returning");
|
---|
42 | return;
|
---|
43 | }
|
---|
44 |
|
---|
45 | // see if the collection/cluster element is already there
|
---|
46 | String coll_name = coll_param.getAttribute(GSXML.VALUE_ATT);
|
---|
47 | String lang = page_request.getAttribute(GSXML.LANG_ATT);
|
---|
48 | String uid = page_request.getAttribute(GSXML.USER_ID_ATT);
|
---|
49 |
|
---|
50 | boolean get_service_description = false;
|
---|
51 | Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
|
---|
52 | if (this.language_list != null) {
|
---|
53 | page_response.appendChild(this.language_list);
|
---|
54 | }
|
---|
55 | Element coll_description = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
|
---|
56 | if (coll_description == null) {
|
---|
57 | // try cluster
|
---|
58 | coll_description = (Element)GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
|
---|
59 | }
|
---|
60 | if (coll_description == null) {
|
---|
61 | // we dont have one yet - get it
|
---|
62 | Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
63 | Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
|
---|
64 | coll_about_message.appendChild(coll_about_request);
|
---|
65 |
|
---|
66 | Element coll_about_response_message = this.mr.process(coll_about_message);
|
---|
67 | Element coll_about_response = (Element)GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
|
---|
68 | if (coll_about_response == null) {
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | coll_description = (Element)GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
|
---|
72 | if (coll_description==null) { // may be a cluster
|
---|
73 | coll_description = (Element)GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (coll_description == null) {
|
---|
77 | System.err.println("DefaultReceptionist: no collection description, returning");
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | // have found one, append it to the page response
|
---|
81 | coll_description = (Element)this.doc.importNode(coll_description, true);
|
---|
82 | page_response.appendChild(coll_description);
|
---|
83 | get_service_description = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // have got a coll description
|
---|
87 |
|
---|
88 | // now get the dispay info for the services
|
---|
89 | Element service_list = (Element)GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
|
---|
90 | if (service_list == null) {
|
---|
91 | System.err.println("DefaultReceptionist: no service list, returning");
|
---|
92 | // something weird has gone wrong
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
97 | if (services.getLength()==0) {
|
---|
98 | System.err.println("DefaultReceoptionist: no services found for colllection/cluster "+ coll_name);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | // check one service for display items
|
---|
102 | if (!get_service_description) {
|
---|
103 | // we dont know yet if we need to get these
|
---|
104 | int i=1;
|
---|
105 | Element test_s = (Element)services.item(0);
|
---|
106 | while (i<services.getLength() && test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_RETRIEVE)) {
|
---|
107 | test_s = (Element)services.item(i); i++;
|
---|
108 | }
|
---|
109 | if (i==services.getLength()) {
|
---|
110 | // we have only found retrieve services, so dont need descripitons anyway
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) !=null) {
|
---|
114 | // have got descriptions already,
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | // if get here, we need to get the service descriptions
|
---|
120 | ///ystem.out.println("getting services description");
|
---|
121 |
|
---|
122 | // we will send all the requests in a single message
|
---|
123 | Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
124 | for (int i=0; i<services.getLength(); i++) {
|
---|
125 | Element c = (Element)services.item(i);
|
---|
126 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
127 | String address = GSPath.appendLink(coll_name, name);
|
---|
128 | Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, address, lang, uid);
|
---|
129 | Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
130 | req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
|
---|
131 | info_request.appendChild(req_param_list);
|
---|
132 | info_message.appendChild(info_request);
|
---|
133 |
|
---|
134 | }
|
---|
135 |
|
---|
136 | Element info_response = (Element)this.mr.process(info_message);
|
---|
137 |
|
---|
138 | NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
139 | // check that have same number of responses as collections
|
---|
140 | if (services.getLength() != service_responses.getLength()) {
|
---|
141 | System.err.println("DefaultReceptionist Error: didn't get a response for each service - somethings gone wrong!");
|
---|
142 | // for now, dont use the metadata
|
---|
143 | } else {
|
---|
144 | for (int i=0; i<services.getLength(); i++) {
|
---|
145 | Element c1 = (Element)services.item(i);
|
---|
146 | Element c2 = (Element)GSXML.getChildByTagName((Element)service_responses.item(i), GSXML.SERVICE_ELEM);
|
---|
147 | if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT))) {
|
---|
148 | //add the service data into the original response
|
---|
149 | GSXML.mergeElements(c1, c2);
|
---|
150 | } else {
|
---|
151 | System.err.println("DefaultReceptionist Error: response does not correspond to request!");
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|