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