1 | /*
|
---|
2 | * AbstractGS3DocumentRetrieve.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.core.GSException;
|
---|
23 | import org.greenstone.gsdl3.util.GSXML;
|
---|
24 | import org.greenstone.gsdl3.util.GSFile;
|
---|
25 | import org.greenstone.gsdl3.util.GS3OID;
|
---|
26 | import org.greenstone.gsdl3.util.MacroResolver;
|
---|
27 | import org.greenstone.gsdl3.util.GS2MacroResolver;
|
---|
28 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
29 | import org.greenstone.gsdl3.util.SQLQuery;
|
---|
30 |
|
---|
31 | // XML classes
|
---|
32 | import org.w3c.dom.Document;
|
---|
33 | import org.w3c.dom.Element;
|
---|
34 | import org.w3c.dom.NodeList;
|
---|
35 |
|
---|
36 | // General Java classes
|
---|
37 | import java.io.File;
|
---|
38 | import java.util.StringTokenizer;
|
---|
39 | import java.util.Vector;
|
---|
40 | import java.util.Set;
|
---|
41 | import java.util.Iterator;
|
---|
42 | import java.util.ArrayList;
|
---|
43 |
|
---|
44 | /** Implements the generic retrieval and classifier services for GS3
|
---|
45 | * collections.
|
---|
46 | *
|
---|
47 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
48 | * @author <a href="mailto:[email protected]">Michael Dewsnip</a>
|
---|
49 | */
|
---|
50 |
|
---|
51 | public abstract class AbstractGS3DocumentRetrieve
|
---|
52 | extends AbstractDocumentRetrieve {
|
---|
53 | protected static final String INDEX_STEM_ELEM = "indexStem";
|
---|
54 |
|
---|
55 | protected SQLQuery database = null;
|
---|
56 | protected String index_stem = null;
|
---|
57 |
|
---|
58 |
|
---|
59 | /** constructor */
|
---|
60 | protected AbstractGS3DocumentRetrieve()
|
---|
61 | {
|
---|
62 | this.database = new SQLQuery();
|
---|
63 | // set up a macro resolver
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void cleanUp() {
|
---|
67 | super.cleanUp();
|
---|
68 | this.database.closeConnection();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** configure this service */
|
---|
72 | public boolean configure(Element info, Element extra_info)
|
---|
73 | {
|
---|
74 | if (!super.configure(info, extra_info)){
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | System.out.println("Configuring AbstractGS3DocumentRetrieve...");
|
---|
79 |
|
---|
80 | // the index stem is either specified in the config file or is "index"
|
---|
81 | Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
|
---|
82 | if (index_stem_elem != null) {
|
---|
83 | this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
|
---|
84 | }
|
---|
85 | if (this.index_stem == null || this.index_stem.equals("")) {
|
---|
86 | System.err.println("GS3MGRetrieve.configure(): indexStem element not found, stem will default to 'index'");
|
---|
87 | this.index_stem = "index";
|
---|
88 | }
|
---|
89 |
|
---|
90 | // open the database for querying
|
---|
91 | // the database name is a combination of site name and collection name
|
---|
92 |
|
---|
93 | // check that site_home is set
|
---|
94 | if (this.site_home == null || this.site_home.equals("")) {
|
---|
95 | System.err.println("GS3Retrieve Error: site_home is not set, so cannot work out the site name and cannot determine the database name");
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
|
---|
99 | if (site_name.equals("")) {
|
---|
100 | System.err.println("GS3Retrieve Error: Cannot extract the site name from site home: "+this.site_home);
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
|
---|
105 | System.err.println("GS3Retrieve Error: Could not open SQL database!");
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 |
|
---|
109 | return true;
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** if id ends in .fc, .pc etc, then translate it to the correct id */
|
---|
114 | protected String translateId(String node_id) {
|
---|
115 | // should really use a sql query
|
---|
116 | return GS3OID.translateOID(node_id);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
|
---|
120 | protected String getRootId(String node_id) {
|
---|
121 | return GS3OID.getTop(node_id);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** returns a list of the child ids in order, null if no children */
|
---|
125 | protected ArrayList getChildrenIds(String node_id) {
|
---|
126 | if (GS3OID.isDocTop(node_id)) { // hack
|
---|
127 | node_id = GS3OID.createOID(node_id, "1");
|
---|
128 | }
|
---|
129 | ArrayList documents = database.getDocumentChildren(node_id);
|
---|
130 | if (documents == null || documents.size()==0) {
|
---|
131 | return null;
|
---|
132 | }
|
---|
133 | return documents;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** returns the node id of the parent node, null if no parent */
|
---|
137 | protected String getParentId(String node_id){
|
---|
138 | String parent = GS3OID.getParent(node_id);
|
---|
139 | if (parent.equals(node_id)) {
|
---|
140 | return null;
|
---|
141 | }
|
---|
142 | return parent;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** get the metadata for the classifier node node_id
|
---|
146 | * returns a metadataList element:
|
---|
147 | * <metadataList><metadata name="xxx">value</metadata></metadataList>
|
---|
148 | */
|
---|
149 | // assumes only one value per metadata
|
---|
150 | protected Element getMetadataList(String node_id, boolean all_metadata,
|
---|
151 | ArrayList metadata_names)
|
---|
152 | throws GSException {
|
---|
153 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
154 |
|
---|
155 | if (all_metadata) {
|
---|
156 | // TODO
|
---|
157 |
|
---|
158 |
|
---|
159 | } else {
|
---|
160 | for (int m = 0; m < metadata_names.size(); m++) {
|
---|
161 | String metadata = (String) metadata_names.get(m);
|
---|
162 | String value = null;
|
---|
163 | value = database.getDocumentMetadata(node_id, metadata);
|
---|
164 | if (value != null) {
|
---|
165 | GSXML.addMetadata(this.doc, metadata_list, metadata, value);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return metadata_list;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** returns the structural information asked for.
|
---|
174 | * info_type may be one of
|
---|
175 | * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
|
---|
176 | */
|
---|
177 | protected String getStructureInfo(String doc_id, String info_type) {
|
---|
178 | return null; // not implemented yet
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /** returns the document type of the doc that the specified node
|
---|
183 | belongs to. should be one of
|
---|
184 | GSXML.DOC_TYPE_SIMPLE,
|
---|
185 | GSXML.DOC_TYPE_PAGED,
|
---|
186 | GSXML.DOC_TYPE_HIERARCHY
|
---|
187 | */
|
---|
188 | protected String getDocType(String node_id) {
|
---|
189 | boolean hierarchical = false;
|
---|
190 | if (!GS3OID.isDocTop(node_id) || database.isHierarchicalDocument(node_id) ) {
|
---|
191 | hierarchical = true;
|
---|
192 | }
|
---|
193 | // what about paged???
|
---|
194 | if (!hierarchical) {
|
---|
195 | // a simple document
|
---|
196 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
197 | } else {
|
---|
198 | // a hierarchical doc
|
---|
199 | return GSXML.DOC_TYPE_HIERARCHY;
|
---|
200 | }
|
---|
201 | // don't have paged yet.
|
---|
202 | }
|
---|
203 |
|
---|
204 | protected String resolveRelativeLinks(String doc_content, String doc_id) {
|
---|
205 |
|
---|
206 | String http_path = this.site_http_address + "/collect/"+this.cluster_name;
|
---|
207 | doc_content = doc_content.replaceAll("_httpcollection_", http_path);
|
---|
208 | return doc_content;
|
---|
209 |
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** returns the content of a node
|
---|
213 | * should return a nodeContent element:
|
---|
214 | * <nodeContent>text content or other elements</nodeContent>
|
---|
215 | */
|
---|
216 | abstract protected Element getNodeContent(String doc_id) throws GSException;
|
---|
217 |
|
---|
218 | }
|
---|