source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/AbstractGS3DocumentRetrieve.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
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 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import org.greenstone.gsdl3.core.GSException;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.GS3OID;
26import org.greenstone.gsdl3.util.MacroResolver;
27import org.greenstone.gsdl3.util.GS2MacroResolver;
28import org.greenstone.gsdl3.util.GSConstants;
29import org.greenstone.gsdl3.util.SQLQuery;
30
31// XML classes
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.NodeList;
35
36// General Java classes
37import java.io.File;
38import java.util.StringTokenizer;
39import java.util.Vector;
40import java.util.Set;
41import java.util.Iterator;
42import 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
51public 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
75 System.out.println("Configuring AbstractGS3DocumentRetrieve...");
76
77 // the index stem is either specified in the config file or is "index"
78 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
79 if (index_stem_elem != null) {
80 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
81 }
82 if (this.index_stem == null || this.index_stem.equals("")) {
83 System.err.println("GS3MGRetrieve.configure(): indexStem element not found, stem will default to 'index'");
84 this.index_stem = "index";
85 }
86
87 // open the database for querying
88 // the database name is a combination of site name and collection name
89
90 // check that site_home is set
91 if (this.site_home == null || this.site_home.equals("")) {
92 System.err.println("GS3Retrieve Error: site_home is not set, so cannot work out the site name and cannot determine the database name");
93 return false;
94 }
95 String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
96 if (site_name.equals("")) {
97 System.err.println("GS3Retrieve Error: Cannot extract the site name from site home: "+this.site_home);
98 return false;
99 }
100
101 if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
102 System.err.println("GS3Retrieve Error: Could not open SQL database!");
103 return false;
104 }
105
106 return super.configure(info, extra_info);
107
108 }
109
110 /** if id ends in .fc, .pc etc, then translate it to the correct id */
111 protected String translateId(String node_id) {
112 // should really use a sql query
113 return GS3OID.translateOID(node_id);
114 }
115
116 /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
117 protected String getRootId(String node_id) {
118 return GS3OID.getTop(node_id);
119 }
120
121 /** returns a list of the child ids in order, null if no children */
122 protected ArrayList getChildrenIds(String node_id) {
123 System.err.println("get children ids for "+node_id);
124 if (GS3OID.isDocTop(node_id)) { // hack
125 node_id = GS3OID.createOID(node_id, "1");
126 }
127 System.err.println("new node id="+node_id);
128 ArrayList documents = database.getDocumentChildren(node_id);
129 if (documents == null || documents.size()==0) {
130 return null;
131 }
132 return documents;
133 }
134
135 /** returns the node id of the parent node, null if no parent */
136 protected String getParentId(String node_id){
137 String parent = GS3OID.getParent(node_id);
138 if (parent.equals(node_id)) {
139 return null;
140 }
141 return parent;
142 }
143
144 /** get the metadata for the classifier node node_id
145 * returns a metadataList element:
146 * <metadataList><metadata name="xxx">value</metadata></metadataList>
147 */
148 // assumes only one value per metadata
149 protected Element getMetadataList(String node_id, boolean all_metadata,
150 ArrayList metadata_names)
151 throws GSException {
152 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
153
154 if (all_metadata) {
155 // TODO
156
157
158 } else {
159 for (int m = 0; m < metadata_names.size(); m++) {
160 String metadata = (String) metadata_names.get(m);
161 String value = null;
162 value = database.getDocumentMetadata(node_id, metadata);
163 if (value != null) {
164 System.out.println("found value "+value);
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}
Note: See TracBrowser for help on using the repository browser.