source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/Action.java@ 24254

Last change on this file since 24254 was 24254, checked in by ak19, 13 years ago

Commits for ticket 770 concerning the display of multiple values for a metadata (like dc.Title) when classified by that metadata. So when the user browses by dc.Title, they no longer merely see a doc listed once for each dc.Title assigned but under the same (first retrieved) dc.Title, but they should now see the doc listed once for each dc.Title assigned to it with a different dc.Title value each time.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.core.ModuleInterface;
4import org.greenstone.gsdl3.util.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Element;
9import org.w3c.dom.Document;
10
11// other java stuff
12import java.io.File;
13import java.util.Vector;
14import java.util.HashMap;
15import java.util.HashSet;
16import java.util.Iterator;
17
18import org.apache.log4j.*;
19
20/** base class for Actions */
21abstract 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 } else if (all.equals("offset")) { // multiple is no longer boolean.
93 // Can be "true", "false" or it can be "offset" (when requested to use mdoffset)
94 metadata.append("offset");
95 metadata.append(GSConstants.META_RELATION_SEP);
96 } // if multiple=false, then get first value for the metadata
97 if (!select.equals("")) {
98 metadata.append(select);
99 metadata.append(GSConstants.META_RELATION_SEP);
100 }
101 if (!sep.equals("")) {
102 metadata.append(GSConstants.META_SEPARATOR_SEP);
103 metadata.append(sep);
104 metadata.append(GSConstants.META_SEPARATOR_SEP);
105 metadata.append(GSConstants.META_RELATION_SEP);
106 }
107
108 metadata.append(name);
109 meta_names.add(metadata.toString());
110 }
111
112 // The XSL tranform for
113 // gsf:link type="source"
114 // makes use of 'assocfilepath' so need to make sure it's asked for
115
116 NodeList link_nodes = format.getElementsByTagName("gsf:link");
117 for (int i=0; i<link_nodes.getLength(); i++) {
118 Element elem = (Element)link_nodes.item(i);
119 String type = elem.getAttribute("type");
120 if (type.equals("source")) {
121 meta_names.add("assocfilepath");
122 meta_names.add("srclinkFile");
123 }
124 }
125
126
127 // get all the metadata necessary for when the user has used "gsf:equivlink"
128 // so that we can build up the equivlink from the metadata components it needs
129 link_nodes = format.getElementsByTagName("gsf:equivlinkgs3");
130 if(link_nodes != null) {
131
132 String[] equivlink_metanames = {"equivDocIcon", "equivDocLink", "/equivDocLink"};
133
134 for(int i = 0; i < equivlink_metanames.length; i++) {
135 StringBuffer metadata = new StringBuffer();
136 metadata.append("all"); // this means the attr multiple = true;
137 metadata.append(GSConstants.META_RELATION_SEP);
138
139 metadata.append(GSConstants.META_SEPARATOR_SEP);
140 metadata.append(','); // attr separator = ","
141 metadata.append(GSConstants.META_SEPARATOR_SEP);
142 metadata.append(GSConstants.META_RELATION_SEP);
143
144 // the name of the metadata we're retrieving
145 metadata.append(equivlink_metanames[i]);
146 meta_names.add(metadata.toString());
147 }
148 }
149 }
150
151 protected Element createMetadataParamList(HashSet metadata_names) {
152 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
153
154 Element param = null;
155 Iterator i = metadata_names.iterator();
156 while (i.hasNext()) {
157 String name = (String)i.next();
158 param = this.doc.createElement(GSXML.PARAM_ELEM);
159 param_list.appendChild(param);
160 param.setAttribute(GSXML.NAME_ATT, "metadata");
161 param.setAttribute(GSXML.VALUE_ATT, name);
162
163 }
164 return param_list;
165 }
166
167 protected boolean processErrorElements(Element message, Element page) {
168 NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
169 if (error_nodes.getLength()==0) {
170 return false;
171 }
172 Document owner = page.getOwnerDocument();
173 for (int i=0; i<error_nodes.getLength(); i++) {
174 page.appendChild(owner.importNode(error_nodes.item(i), true));
175 }
176 return true;
177 }
178
179 /**
180 * Takes an XML element and adds the metadata of the current site to it.
181 * Useful for adding the current site's metadata to a response before sending it
182 *
183 * @param element the element to add site metadata to
184 * @param lang the current language
185 * @param uid the current user id
186 */
187 protected void addSiteMetadata( Element element, String lang, String uid ) {
188 //ADD SITE METADATA
189 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
190 //create a hashmap of params
191 HashMap subset_params = new HashMap(1);
192 subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
193 //create the element to put the params in
194 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
195 //put them in
196 GSXML.addParametersToList( this.doc, param_list, subset_params );
197 metadata_request.appendChild(param_list);
198 //create the message
199 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
200 metadata_message.appendChild(metadata_request);
201 //get response
202 Element metadata_response_message = (Element)this.mr.process(metadata_message);
203 //drill down to response
204 Element metadata_response = (Element)GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
205 //merge in metadata
206 GSXML.mergeMetadataLists(element,metadata_response);
207 }
208
209}
210
211
212
213
Note: See TracBrowser for help on using the repository browser.