1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 |
|
---|
11 | // other java stuff
|
---|
12 | import java.io.File;
|
---|
13 | import java.util.Vector;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.HashSet;
|
---|
16 | import java.util.Iterator;
|
---|
17 |
|
---|
18 | import org.apache.log4j.*;
|
---|
19 |
|
---|
20 | /** base class for Actions */
|
---|
21 | abstract public class Action {
|
---|
22 |
|
---|
23 | /** the system set up variables */
|
---|
24 | protected HashMap config_params = null;
|
---|
25 | /** container Document to create XML Nodes */
|
---|
26 | protected Document doc=null;
|
---|
27 | /** a converter class to parse XML and create Docs */
|
---|
28 | protected XMLConverter converter=null;
|
---|
29 | /** a reference to the message router that it must talk to to
|
---|
30 | * get info. it may be a communicator acting as a proxy, but it
|
---|
31 | doesn't care about that */
|
---|
32 | protected ModuleInterface mr=null;
|
---|
33 |
|
---|
34 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
|
---|
35 |
|
---|
36 | public Action() {
|
---|
37 | this.converter = new XMLConverter();
|
---|
38 | this.doc = this.converter.newDOM();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** the config variables must be set before configure is called */
|
---|
42 | public void setConfigParams(HashMap params) {
|
---|
43 | this.config_params = params;
|
---|
44 | }
|
---|
45 | /** sets the message router */
|
---|
46 | public void setMessageRouter(ModuleInterface m) {
|
---|
47 | this.mr = m;
|
---|
48 | }
|
---|
49 | public boolean configure() {
|
---|
50 | // does nothing yet
|
---|
51 | return true;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** process takes an xml representation of cgi args
|
---|
55 | * and returns the page of results - may be in html/xml/other
|
---|
56 | * depending on the output att of the request */
|
---|
57 | public String process(String xml_in) {
|
---|
58 |
|
---|
59 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
60 | if (message_doc == null) {
|
---|
61 | logger.error("Couldn't parse request");
|
---|
62 | logger.error(xml_in);
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 | Node result = process(message_doc);
|
---|
66 | return this.converter.getString(result);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** the main process method - must be implemented in subclass */
|
---|
70 | abstract public Node process(Node xml_in);
|
---|
71 |
|
---|
72 | /** tell the param class what its arguments are
|
---|
73 | * if an action has its own arguments, this should add them to the params
|
---|
74 | * object - particularly important for args that should not be saved */
|
---|
75 | public boolean getActionParameters(GSParams params) {
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected void extractMetadataNames(Element format, HashSet meta_names) {
|
---|
80 | //NodeList nodes = format.getElementsByTagNameNS("metadata", "http://www.greenstone.org/configformat");
|
---|
81 | NodeList metadata_nodes = format.getElementsByTagName("gsf:metadata");
|
---|
82 | for (int i=0; i<metadata_nodes.getLength(); i++) {
|
---|
83 | Element elem = (Element)metadata_nodes.item(i);
|
---|
84 | StringBuffer metadata = new StringBuffer();
|
---|
85 | String all = elem.getAttribute("multiple");
|
---|
86 | String name = elem.getAttribute("name");
|
---|
87 | String select = elem.getAttribute("select");
|
---|
88 | String sep = elem.getAttribute("separator");
|
---|
89 | if (all.equals("true")) {
|
---|
90 | metadata.append("all");
|
---|
91 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
92 | }
|
---|
93 | if (!select.equals("")) {
|
---|
94 | metadata.append(select);
|
---|
95 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
96 | }
|
---|
97 | if (!sep.equals("")) {
|
---|
98 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
99 | metadata.append(sep);
|
---|
100 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
101 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
102 | }
|
---|
103 |
|
---|
104 | metadata.append(name);
|
---|
105 | meta_names.add(metadata.toString());
|
---|
106 | }
|
---|
107 |
|
---|
108 | // The XSL tranform for
|
---|
109 | // gsf:link type="source"
|
---|
110 | // makes use of 'assocfilepath' so need to make sure it's asked for
|
---|
111 |
|
---|
112 | NodeList link_nodes = format.getElementsByTagName("gsf:link");
|
---|
113 | for (int i=0; i<link_nodes.getLength(); i++) {
|
---|
114 | Element elem = (Element)link_nodes.item(i);
|
---|
115 | String type = elem.getAttribute("type");
|
---|
116 | if (type.equals("source") || type.equals("sourcelinkfile")) {
|
---|
117 | meta_names.add("assocfilepath");
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected Element createMetadataParamList(HashSet metadata_names) {
|
---|
124 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
125 |
|
---|
126 | Element param = null;
|
---|
127 | Iterator i = metadata_names.iterator();
|
---|
128 | while (i.hasNext()) {
|
---|
129 | String name = (String)i.next();
|
---|
130 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
131 | param_list.appendChild(param);
|
---|
132 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
133 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
134 |
|
---|
135 | }
|
---|
136 | return param_list;
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected boolean processErrorElements(Element message, Element page) {
|
---|
140 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
141 | if (error_nodes.getLength()==0) {
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | Document owner = page.getOwnerDocument();
|
---|
145 | for (int i=0; i<error_nodes.getLength(); i++) {
|
---|
146 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
147 | }
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Takes an XML element and adds the metadata of the current site to it.
|
---|
153 | * Useful for adding the current site's metadata to a response before sending it
|
---|
154 | *
|
---|
155 | * @param element the element to add site metadata to
|
---|
156 | * @param lang the current language
|
---|
157 | * @param uid the current user id
|
---|
158 | */
|
---|
159 | protected void addSiteMetadata( Element element, String lang, String uid ) {
|
---|
160 | //ADD SITE METADATA
|
---|
161 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
|
---|
162 | //create a hashmap of params
|
---|
163 | HashMap subset_params = new HashMap(1);
|
---|
164 | subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
165 | //create the element to put the params in
|
---|
166 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
167 | //put them in
|
---|
168 | GSXML.addParametersToList( this.doc, param_list, subset_params );
|
---|
169 | metadata_request.appendChild(param_list);
|
---|
170 | //create the message
|
---|
171 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
172 | metadata_message.appendChild(metadata_request);
|
---|
173 | //get response
|
---|
174 | Element metadata_response_message = (Element)this.mr.process(metadata_message);
|
---|
175 | //drill down to response
|
---|
176 | Element metadata_response = (Element)GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
|
---|
177 | //merge in metadata
|
---|
178 | GSXML.mergeMetadataLists(element,metadata_response);
|
---|
179 | }
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|