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