1 | /*
|
---|
2 | * GS2MGRetrieve.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 |
|
---|
22 | // Greenstone classes
|
---|
23 | import org.greenstone.mg.*;
|
---|
24 | import org.greenstone.gsdl3.util.*;
|
---|
25 |
|
---|
26 | // XML classes
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 |
|
---|
29 | // General Java classes
|
---|
30 | import java.io.File;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | *
|
---|
35 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
36 | * @version $Revision: 3799 $
|
---|
37 | */
|
---|
38 |
|
---|
39 | public class GS2MGRetrieve
|
---|
40 | extends GS2Retrieve {
|
---|
41 |
|
---|
42 | // Parameters used
|
---|
43 | private static final String INDEX_PARAM = "index";
|
---|
44 |
|
---|
45 | // Elements used in the config file that are specific to this class
|
---|
46 | private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
|
---|
47 |
|
---|
48 | private MGWrapper mg_src_ = null;
|
---|
49 |
|
---|
50 | private String default_index_ = null;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** constructor */
|
---|
54 | public GS2MGRetrieve()
|
---|
55 | {
|
---|
56 | System.out.println("Constructing GS2MGRetrieve...");
|
---|
57 | mg_src_ = new MGWrapper();
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /** configure this service */
|
---|
62 | public boolean configure(Element info)
|
---|
63 | {
|
---|
64 | // Do specific configuration
|
---|
65 | System.out.println("Configuring GS2MGRetrieve...");
|
---|
66 |
|
---|
67 | // Get the default index out of <defaultIndex> (buildConfig.xml)
|
---|
68 | Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM);
|
---|
69 | if (def != null) {
|
---|
70 | default_index_ = def.getAttribute(GSXML.NAME_ATT);
|
---|
71 | }
|
---|
72 | if (default_index_ == null || default_index_.equals("")) {
|
---|
73 | System.err.println("Error: default index not specified!");
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 | System.out.println("Default index: " + default_index_);
|
---|
77 |
|
---|
78 | // Do generic configuration
|
---|
79 | return super.configure(info);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /** Retrieve the structure of a document */
|
---|
84 | protected Element processDocumentStructureRetrieve(Element request)
|
---|
85 | {
|
---|
86 | return super.processDocumentStructureRetrieve(request);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /** Retrieve metadata associated with a document */
|
---|
91 | protected Element processDocumentMetadataRetrieve(Element request)
|
---|
92 | {
|
---|
93 | return super.processDocumentMetadataRetrieve(request);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /** Retrieve the content of a document */
|
---|
98 | protected Element processDocumentContentRetrieve(Element request)
|
---|
99 | {
|
---|
100 | // Create a new (empty) result message
|
---|
101 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
102 | String from = GSPath.appendLink(cluster_name_, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
|
---|
103 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
104 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
105 | Element result_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
106 | result.appendChild(result_content);
|
---|
107 |
|
---|
108 | // Get the parameters of the request
|
---|
109 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
110 | if (param_list == null) {
|
---|
111 | System.err.println("GS2Retrieve, DocumentContentRetrieve Error: missing paramList.\n");
|
---|
112 | return result; // Return the empty result
|
---|
113 | }
|
---|
114 |
|
---|
115 | String index = default_index_;
|
---|
116 |
|
---|
117 | // Process the request parameters
|
---|
118 | Element param = (Element) param_list.getFirstChild();
|
---|
119 | while (param != null) {
|
---|
120 | if (!param.getNodeName().equals(GSXML.PARAM_ELEM)) {
|
---|
121 | System.err.println("Warning: Non-param in paramList (ignored).");
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | //
|
---|
125 | if (param.getAttribute(GSXML.NAME_ATT) == INDEX_PARAM) {
|
---|
126 | index = GSXML.getValue(param);
|
---|
127 | System.out.println("Index: " + index);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | param = (Element) param.getNextSibling();
|
---|
132 | }
|
---|
133 |
|
---|
134 | // Get the request content
|
---|
135 | Element content = (Element) GSXML.getChildByTagName(request, GSXML.CONTENT_ELEM);
|
---|
136 | if (content == null) {
|
---|
137 | System.err.println("Error: DocumentContentRetrieve request had no content.\n");
|
---|
138 | return result;
|
---|
139 | }
|
---|
140 |
|
---|
141 | Element doc_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
142 | result_content.appendChild(doc_list);
|
---|
143 |
|
---|
144 | // The location of the MG index and text files
|
---|
145 | String basedir = GSFile.collectionBaseDir(site_home_, cluster_name_)
|
---|
146 | + File.separatorChar; // Needed by MG
|
---|
147 | String textdir = GSFile.collectionTextPath(cluster_name_);
|
---|
148 | String indexpath = GSFile.collectionIndexPath(cluster_name_, index);
|
---|
149 | System.out.println("Base directory: " + basedir);
|
---|
150 | System.out.println("Text directory: " + textdir);
|
---|
151 | System.out.println("Index path: " + indexpath);
|
---|
152 |
|
---|
153 | mg_src_.setIndex(indexpath);
|
---|
154 |
|
---|
155 | // Get the documents
|
---|
156 | String[] doc_ids = GSXML.getDocumentNameList(content);
|
---|
157 | for (int i = 0; i < doc_ids.length; i++) {
|
---|
158 | String doc_id = doc_ids[i];
|
---|
159 | System.out.println("Retrieving content of " + doc_id + "...");
|
---|
160 |
|
---|
161 | long doc_num = gdbm_src_.oid2Docnum(doc_id);
|
---|
162 | String doc_content = mg_src_.getDocument(basedir, textdir, doc_num);
|
---|
163 |
|
---|
164 | // For now, stick it in a text node - eventually should be parsed as xml??
|
---|
165 | Element doc = GSXML.createDocumentElement(doc_, doc_id);
|
---|
166 | GSXML.addDocText(doc_, doc, doc_content);
|
---|
167 | doc_list.appendChild(doc);
|
---|
168 | }
|
---|
169 |
|
---|
170 | return result;
|
---|
171 | }
|
---|
172 | }
|
---|