source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/AbstractGS3DocumentRetrieve.java@ 9815

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

some methods from documentretrieve classes now throw GSExceptions. am trying to make it so that no Exceptions get to the user interface. returning a lot more error elements too, in the hope that they may be useful for other people

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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
67 /** configure this service */
68 public boolean configure(Element info, Element extra_info)
69 {
70
71 System.out.println("Configuring AbstractGS3DocumentRetrieve...");
72
73 // the index stem is either specified in the config file or is "index"
74 Element index_stem_elem = (Element) GSXML.getChildByTagName(info, INDEX_STEM_ELEM);
75 if (index_stem_elem != null) {
76 this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
77 }
78 if (this.index_stem == null || this.index_stem.equals("")) {
79 System.err.println("GS3MGRetrieve.configure(): indexStem element not found, stem will default to 'index'");
80 this.index_stem = "index";
81 }
82
83 // open the database for querying
84 // the database name is a combination of site name and collection name
85
86 // check that site_home is set
87 if (this.site_home == null || this.site_home.equals("")) {
88 System.err.println("GS3Retrieve Error: site_home is not set, so cannot work out the site name and cannot determine the database name");
89 return false;
90 }
91 String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
92 if (site_name.equals("")) {
93 System.err.println("GS3Retrieve Error: Cannot extract the site name from site home: "+this.site_home);
94 return false;
95 }
96
97 if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
98 System.err.println("GS3Retrieve Error: Could not open SQL database!");
99 return false;
100 }
101
102 return super.configure(info, extra_info);
103
104 }
105
106 /** if id ends in .fc, .pc etc, then translate it to the correct id */
107 protected String translateId(String node_id) {
108 // should really use a sql query
109 return GS3OID.translateOID(node_id);
110 }
111
112 /** returns the id of the root node of the document containing node node_id. . may be the same as node_id */
113 protected String getRootId(String node_id) {
114 return GS3OID.getTop(node_id);
115 }
116
117 /** returns a list of the child ids in order, null if no children */
118 protected ArrayList getChildrenIds(String node_id) {
119 System.err.println("get children ids for "+node_id);
120 if (GS3OID.isDocTop(node_id)) { // hack
121 node_id = GS3OID.createOID(node_id, "1");
122 }
123 System.err.println("new node id="+node_id);
124 ArrayList documents = database.getDocumentChildren(node_id);
125 if (documents == null || documents.size()==0) {
126 return null;
127 }
128 return documents;
129 }
130
131 /** returns the node id of the parent node, null if no parent */
132 protected String getParentId(String node_id){
133 String parent = GS3OID.getParent(node_id);
134 if (parent.equals(node_id)) {
135 return null;
136 }
137 return parent;
138 }
139
140 /** get the metadata for the classifier node node_id
141 * returns a metadataList element:
142 * <metadataList><metadata name="xxx">value</metadata></metadataList>
143 */
144 // assumes only one value per metadata
145 protected Element getMetadataList(String node_id, boolean all_metadata,
146 ArrayList metadata_names)
147 throws GSException {
148 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
149
150 if (all_metadata) {
151 // TODO
152
153
154 } else {
155 for (int m = 0; m < metadata_names.size(); m++) {
156 String metadata = (String) metadata_names.get(m);
157 String value = null;
158 value = database.getDocumentMetadata(node_id, metadata);
159 if (value != null) {
160 System.out.println("found value "+value);
161 GSXML.addMetadata(this.doc, metadata_list, metadata, value);
162 }
163 }
164 }
165
166 return metadata_list;
167 }
168
169 /** returns the structural information asked for.
170 * info_type may be one of
171 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
172 */
173 protected String getStructureInfo(String doc_id, String info_type) {
174 return null; // not implemented yet
175 }
176
177
178 /** returns the document type of the doc that the specified node
179 belongs to. should be one of
180 GSXML.DOC_TYPE_SIMPLE,
181 GSXML.DOC_TYPE_PAGED,
182 GSXML.DOC_TYPE_HIERARCHY
183 */
184 protected String getDocType(String node_id) {
185 boolean hierarchical = false;
186 if (!GS3OID.isDocTop(node_id) || database.isHierarchicalDocument(node_id) ) {
187 hierarchical = true;
188 }
189 // what about paged???
190 if (!hierarchical) {
191 // a simple document
192 return GSXML.DOC_TYPE_SIMPLE;
193 } else {
194 // a hierarchical doc
195 return GSXML.DOC_TYPE_HIERARCHY;
196 }
197 // don't have paged yet.
198 }
199
200 protected String resolveRelativeLinks(String doc_content, String doc_id) {
201
202 String http_path = this.site_http_address + "/collect/"+this.cluster_name;
203 doc_content = doc_content.replaceAll("_httpcollection_", http_path);
204 return doc_content;
205
206 }
207
208 /** returns the content of a node
209 * should return a nodeContent element:
210 * <nodeContent>text content or other elements</nodeContent>
211 */
212 abstract protected Element getNodeContent(String doc_id) throws GSException;
213
214}
Note: See TracBrowser for help on using the repository browser.