1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | * ResponseData.java - part of the demo-client for Greenstone 3, of the
|
---|
4 | * Greenstone digital library suite from the New Zealand Digital Library
|
---|
5 | * Project at the * University of Waikato, New Zealand.
|
---|
6 | * <BR><BR>
|
---|
7 | * Copyright (C) 2008 New Zealand Digital Library Project
|
---|
8 | * <BR><BR>
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | * <BR><BR>
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *########################################################################
|
---|
19 | */
|
---|
20 |
|
---|
21 | package org.greenstone.gs3client.data;
|
---|
22 |
|
---|
23 | import java.util.Vector;
|
---|
24 | import java.util.LinkedHashMap;
|
---|
25 | import java.util.Map;
|
---|
26 |
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 | import org.w3c.dom.NodeList;
|
---|
29 | import org.greenstone.gsdl3.util.GSXML;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Represents the data in a response XML message that is returned as
|
---|
33 | * either a Browse or Query (Search) request.
|
---|
34 | * Subclasses therefore are BrowseResponseData and QueryResponseData.
|
---|
35 | * This class maintains a Map of (nodeID, NodeData ref) pairs
|
---|
36 | * representing the resulting <classifierNode>s and <documentNode>s
|
---|
37 | * that can be returned when executing a SINGLE browse or query request.
|
---|
38 | * @author ak19
|
---|
39 | */
|
---|
40 | public abstract class ResponseData {
|
---|
41 | /** map of (nodeIDs, references to associated NodeData) pairs,
|
---|
42 | * stored in the order they are contained in the response XML messages
|
---|
43 | * (the order in which data objects are parsed and created for them) */
|
---|
44 | protected Map nodeIDsToNodes;
|
---|
45 |
|
---|
46 | /** Subclasses must implement this method to parse the
|
---|
47 | * <message><response></response></message>
|
---|
48 | * that is returned upon executing a browse or query request.
|
---|
49 | * At the end of this method, the Map nodeIDsToNodes will contain
|
---|
50 | * any DocumentNodeData or ClassifierNodeData objects described
|
---|
51 | * in the responseMsgTag.
|
---|
52 | * @param responseMsgTag the XMl response message Element whose data will
|
---|
53 | * be parsed and stored in the ResponseData subclass */
|
---|
54 | public abstract void setResponseData(Element responseMsgTag);
|
---|
55 |
|
---|
56 | /** Default constructor, creates the nodeIDsToNodes map */
|
---|
57 | public ResponseData() {
|
---|
58 | // A LinkedHashMap maintains documents in the order they were inserted
|
---|
59 | // This is what we want for the docIdsToDocNodes mapping, because the
|
---|
60 | // documents are returned in ranked order.
|
---|
61 | // TreeMap does not work, as it compares based on keys=docNodIDs here.
|
---|
62 | // And creating a Comparator for a TreeMap does not work either,
|
---|
63 | // because the compare() method of Comparator is given two keys and so
|
---|
64 | // needs to access the docIdsToDocNodes map to retrieve the
|
---|
65 | // documentNodeData obj for the parameter keys (nodeIDs) to access the
|
---|
66 | // rank member var of the documentNodeData objects in order to do the
|
---|
67 | // comparison. However, that is a problem when ordering!
|
---|
68 | nodeIDsToNodes = new LinkedHashMap();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Clears/resets this ResponseData object of all data, so that it can be
|
---|
72 | * reused to process future Browse and Query requests. In this case, it
|
---|
73 | * merely clears the internal Map nodeIDsToNodes. Subclasses may need to
|
---|
74 | * clear other variables. */
|
---|
75 | public void clear() { this.nodeIDsToNodes.clear(); }
|
---|
76 |
|
---|
77 |
|
---|
78 | /** @return the Map of mappings from nodeIDs to NodeData object references */
|
---|
79 | public Map getIDToNodeMapping() { return nodeIDsToNodes; }
|
---|
80 |
|
---|
81 | /** Given an nodeID, returns the NodeData object with that nodeID,
|
---|
82 | * if any. Otherwise, null is returned. Subclass might want to add
|
---|
83 | * a different method that converts this to DocumentNodeData type.
|
---|
84 | * @param ID is the nodeID whose NodeData object is requested */
|
---|
85 | public NodeData getNodeForID(String ID) {
|
---|
86 | return (NodeData)nodeIDsToNodes.get(ID);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** After a DocumentContentRetrieve request for one or more documents
|
---|
90 | * has returned a response, this method - when given the response XML -
|
---|
91 | * will set the nodeContents of each document in the response (as long
|
---|
92 | * as those documents already have an associated DocumentNodeData
|
---|
93 | * object instantiated, which should/would always be the case).
|
---|
94 | * @param messageTag is the DocumentContentRetrieve XML response message
|
---|
95 | * containing the document contents for one or more documentNodes */
|
---|
96 | public void setContentForDocs(Element messageTag) {
|
---|
97 | NodeList docNodeTags = messageTag.getElementsByTagName(
|
---|
98 | GSXML.DOC_NODE_ELEM);
|
---|
99 | int size = docNodeTags.getLength();
|
---|
100 | if(size == 0) // no <documentNode> in <message> to set the content for
|
---|
101 | return;
|
---|
102 |
|
---|
103 | for(int i = 0; i < size; i++) {
|
---|
104 | Element docNodeTag = (Element)docNodeTags.item(i);
|
---|
105 | String id = (String)docNodeTag.getAttribute(GSXML.NODE_ID_ATT);
|
---|
106 |
|
---|
107 | // Find the associated DocumentNodeData object
|
---|
108 | // in the map of documentNodeData objects
|
---|
109 | DocumentNodeData doc
|
---|
110 | = (DocumentNodeData)this.nodeIDsToNodes.get(id);
|
---|
111 | if(doc == null) //no such document: shouldn't happen
|
---|
112 | continue; // skip to look at next docNode
|
---|
113 |
|
---|
114 | doc.setNodeContent(docNodeTag);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** This method can be called after a DocumentMetadataRetrieve request
|
---|
119 | * has returned a response. Given the response message (XML) element,
|
---|
120 | * this method attempts to set the metadata for all the documentNodeData
|
---|
121 | * objects it has, using the metadata tags in the response-message-XML.
|
---|
122 | * This method returns false if the responseMessage XMl does not contain
|
---|
123 | * any <documentNodeList> elements.
|
---|
124 | * @param NODE_ELEM can be either GSXML defined constant for
|
---|
125 | * <ClassifierNode> or <DocumentNode>
|
---|
126 | * @param messageTag is the DocumentMetadataRetrieve XML response message
|
---|
127 | * containing the metadata for one or more documentNodes/classifierNodes
|
---|
128 | * as specified by NODE_ELEM */
|
---|
129 | protected boolean setMetadataForNodes(Element messageTag,
|
---|
130 | final String NODE_ELEM)
|
---|
131 | {
|
---|
132 | // get the <documentNodeList>
|
---|
133 | Element docList = ParseUtil.getFirstDescElementCalled(
|
---|
134 | messageTag, NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
135 | if(docList == null)
|
---|
136 | return false; // if no <documentNodeList>, then we can't process
|
---|
137 | // (shouldn't have got here)
|
---|
138 |
|
---|
139 | // for each <documentNode>'s nodeID attribute's value, get the
|
---|
140 | // documentNodeData reference, and set its metadataList using
|
---|
141 | // its <docNode> tag
|
---|
142 | Vector docNodesWithMetaInfo = ParseUtil.getAllChildElementsCalled(
|
---|
143 | docList, NODE_ELEM);
|
---|
144 | if(docNodesWithMetaInfo == null)
|
---|
145 | return false; // no <documentNode> children, so no metadata to set
|
---|
146 | for(int i = 0; i < docNodesWithMetaInfo.size(); i++) {
|
---|
147 | Element docNodeTag = (Element)docNodesWithMetaInfo.get(i);
|
---|
148 | if(!docNodeTag.hasAttribute(GSXML.NODE_ID_ATT))
|
---|
149 | continue; // shouldn't be the case
|
---|
150 | String nodeID = docNodeTag.getAttribute(GSXML.NODE_ID_ATT);
|
---|
151 | NodeData docNode
|
---|
152 | = (NodeData)nodeIDsToNodes.get(nodeID);
|
---|
153 | if(docNode != null) // can only set the meta if there is a
|
---|
154 | // docNode with this nodeID in our docIDsToDocNodes
|
---|
155 | docNode.setMetadataList(docNodeTag);
|
---|
156 | }
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** This method sets the metadata for one or even all <documentNode>
|
---|
161 | * data objects contained in the Map nodeIDsToNodes, using the <message>
|
---|
162 | * element parameter (which is returned upon a documentMetadataRetrieve
|
---|
163 | * request. Metadata for <classifierNodes> are not set with this method.
|
---|
164 | * It merely calls the setMetadataForDocuments(messageTag, tagName)
|
---|
165 | * method to parse out data for the tagName=<documentNode>.
|
---|
166 | * @param messageTag is the DocumentMetadataRetrieve XML response message
|
---|
167 | * containing the metadata for one or more documentNodes */
|
---|
168 | public boolean setMetadataForDocuments(Element messageTag) {
|
---|
169 | return setMetadataForNodes(messageTag, GSXML.DOC_NODE_ELEM);
|
---|
170 | }
|
---|
171 | }
|
---|