source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS3Browse.java@ 8959

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

restructured the retrieval classes. split apart browsing and doc retrieval into two classes, inplemented an abstract base class for each which new services can inherit

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/*
2 * GS3Browse.java
3 * Copyright (C) 2005 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.util.GS3OID;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.GSFile;
25import org.greenstone.gsdl3.util.SQLQuery;
26
27// XML classes
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32// General Java classes
33import java.util.ArrayList;
34import java.util.StringTokenizer;
35import java.io.File;
36
37/** Greenstone 3 collection classifier service
38 *
39 * @author <a href="mailto:[email protected]">Katherine Don</a>
40 */
41public class GS3Browse
42 extends AbstractBrowse
43{
44
45 protected SQLQuery database = null;
46
47 /** constructor */
48 public GS3Browse()
49 {
50 this.database = new SQLQuery();
51
52 }
53
54 public boolean configure(Element info, Element extra_info)
55 {
56 System.err.println("Configuring GS3Browse...");
57
58 // open the database for querying
59 // the database name is a combination of site name and collection name
60
61 // check that site_home is set
62 if (this.site_home == null || this.site_home.equals("")) {
63 System.err.println("GS3Retrieve Error: site_home is not set, so cannot work out the site name and cannot determine the database name");
64 return false;
65 }
66 String site_name = this.site_home.substring(this.site_home.lastIndexOf(File.separator)+1);
67 if (site_name.equals("")) {
68 System.err.println("GS3Retrieve Error: Cannot extract the site name from site home: "+this.site_home);
69 return false;
70 }
71 if (!database.setDatabase(site_name+"_"+this.cluster_name)) {
72 System.err.println("GS3Retrieve Error: Could not open SQL database!");
73 return false;
74 }
75
76 // generic configure
77 return super.configure(info, extra_info);
78 }
79
80 /** if id ends in .fc, .pc etc, then translate it to the correct id */
81 protected String translateId(String node_id) {
82 // should really use a sql query
83 return GS3OID.translateOID(node_id);
84 }
85
86 /** returns the document type of the doc that the specified node
87 belongs to. should be one of
88 GSXML.DOC_TYPE_SIMPLE,
89 GSXML.DOC_TYPE_PAGED,
90 GSXML.DOC_TYPE_HIERARCHY
91 */
92 protected String getDocType(String node_id) {
93 boolean hierarchical = false;
94 if (!GS3OID.isDocTop(node_id) || database.isHierarchicalDocument(node_id) ) {
95 hierarchical = true;
96 }
97 // what about paged???
98 if (!hierarchical) {
99 // a simple document
100 return GSXML.DOC_TYPE_SIMPLE;
101 } else {
102 // a hierarchical doc
103 return GSXML.DOC_TYPE_HIERARCHY;
104 }
105 // don't have paged yet.
106 }
107
108 /** returns the id of the root node of the document containing node
109 * node_id. . may be the same as node_id */
110 protected String getRootId(String node_id) {
111 return GS3OID.getDocID(node_id);
112 }
113
114 /** returns a list of the child ids in order, null if no children */
115 protected ArrayList getChildrenIds(String node_id) {
116 ArrayList documents = database.getClassifierDocChildren(node_id);
117 if (documents == null) {
118 documents = new ArrayList();
119 }
120 ArrayList children = database.getClassifierChildren(node_id);
121 if (children != null) {
122 documents.addAll(children);
123 }
124 if (documents.size()==0) {
125 return null;
126 }
127 return documents;
128 }
129
130 /** returns the node id of the parent node, null if no parent */
131 protected String getParentId(String node_id){
132 String parent = GS3OID.getParent(node_id);
133 if (parent.equals(node_id)) {
134 return null;
135 }
136 return parent;
137 }
138
139 /** get the metadata for the classifier node node_id
140 * returns a metadataList element:
141 * <metadataList><metadata name="xxx">value</metadata></metadataList>
142 * if all_metadata is true, returns all available metadata, otherwise just
143 * returns requested metadata
144 */
145 // assumes only one value per metadata
146 protected Element getMetadataList(String node_id, boolean all_metadata, ArrayList metadata_names) {
147 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
148
149 if (all_metadata) {
150 // TODO
151
152
153 } else {
154 for (int m = 0; m < metadata_names.size(); m++) {
155 String metadata = (String) metadata_names.get(m);
156 String value = null;
157 value = database.getClassifierMetadata(node_id, metadata);
158 if (value != null) {
159 System.out.println("found value "+value);
160 GSXML.addMetadata(this.doc, metadata_list, metadata, value);
161 }
162 }
163 }
164
165 return metadata_list;
166 }
167
168 /** returns the structural information asked for.
169 * info_type may be one of
170 * INFO_NUM_SIBS, INFO_NUM_CHILDREN, INFO_SIB_POS
171 */
172 protected String getStructureInfo(String doc_id, String info_type) {
173 return null;
174 }
175
176 /** returns true if the id refers to a document (rather than
177 * a classifier node)
178 */
179 protected boolean isDocumentId(String node_id){
180 if (node_id.startsWith("CL")) {
181 return false;
182 }
183 return true;
184 }
185
186}
Note: See TracBrowser for help on using the repository browser.