source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ServiceUtil.java-for-gs311@ 35179

Last change on this file since 35179 was 34644, checked in by davidb, 3 years ago

Version of file that is designed to work with planned changes in GS v3.11

  • Property svn:executable set to *
File size: 7.0 KB
Line 
1package org.greenstone.gsdl3.service;
2
3import java.io.File;
4import java.io.Serializable;
5import java.util.ArrayList;
6import java.util.HashMap;
7
8import org.apache.log4j.Logger;
9import org.greenstone.gsdl3.util.GSXML;
10import org.greenstone.gsdl3.util.XMLConverter;
11import org.greenstone.gsdl3.util.UserContext;
12
13import org.greenstone.gsdl3.core.MessageRouter;
14
15import org.w3c.dom.Document;
16import org.w3c.dom.Element;
17import org.w3c.dom.NodeList;
18
19
20public class ServiceUtil extends ServiceRack
21{
22 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ServiceUtil.class.getName());
23
24 protected static final String METADATA_RETRIEVAL_SERVICE = "DocumentMetadataRetrieve";
25
26 /**********************************************************
27 * The list of services the utility service rack supports *
28 *********************************************************/
29 protected static final String GET_ALL_IMAGES_IN_COLLECTION = "GetAllImagesInCollection";
30 /*********************************************************/
31
32 String[] services = { GET_ALL_IMAGES_IN_COLLECTION };
33
34 public boolean configure(Element info, Element extra_info)
35 {
36 if (!super.configure(info, extra_info))
37 {
38 return false;
39 }
40
41 logger.info("Configuring ServiceUtil...");
42 this.config_info = info;
43
44 for (int i = 0; i < services.length; i++)
45 {
46 Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
47 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
48 service.setAttribute(GSXML.NAME_ATT, services[i]);
49 this.short_service_info.appendChild(service);
50 }
51
52 return true;
53 }
54
55 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
56 {
57 for (int i = 0; i < services.length; i++)
58 {
59 if (service_id.equals(services[i]))
60 {
61 Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
62 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
63 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
64 return service_elem;
65 }
66 }
67
68 return null;
69 }
70
71 protected Element processGetAllImagesInCollection(Element request)
72 {
73 Document result_doc = XMLConverter.newDOM();
74 Element result = GSXML.createBasicResponse(result_doc, GET_ALL_IMAGES_IN_COLLECTION);
75
76 if (request == null)
77 {
78 GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
79 return result;
80 }
81
82 String lang = request.getAttribute(GSXML.LANG_ATT);
83 String uid = request.getAttribute(GSXML.USER_ID_ATT);
84
85 // Get the parameters of the request
86 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
87
88 if (param_list == null) {
89 GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
90 return result; // Return the empty result
91 }
92
93 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
94
95 String regex = (String)params.get("extregex");
96 if(regex == null)
97 {
98 GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No file name extensions specified", GSXML.ERROR_TYPE_SYNTAX);
99 return result;
100 }
101 regex = ".*" + regex + ".*";
102
103 String collection = (String)params.get("c");
104 if(collection == null)
105 {
106 GSXML.addError(result, GET_ALL_IMAGES_IN_COLLECTION + ": No collection specified", GSXML.ERROR_TYPE_SYNTAX);
107 return result;
108 }
109
110 String indexDirStr = this.site_home + File.separator + "collect" + File.separator + collection + File.separator + "index" + File.separator + "assoc";
111 File indexDir = new File(indexDirStr);
112 ArrayList<String> images = new ArrayList<String>();
113 getImagesRecursive(indexDir, regex, images);
114
115 Element imageListElem = result_doc.createElement("imageList");
116 result.appendChild(imageListElem);
117 for(String i : images)
118 {
119 System.err.println("*** image i = " + i);
120 String assoc_dir = getAssocDir(i,indexDirStr);
121 System.err.println("*** image i assoc dir = " + assoc_dir);
122
123 String doc_id = queryMRforDOCID(collection,assoc_dir);
124 System.err.println("*** doc id = " + doc_id);
125 System.err.println("======");
126
127 Element imageElem = result_doc.createElement("image");
128 imageElem.setAttribute("docid", doc_id);
129 imageElem.appendChild(result_doc.createTextNode(i));
130 imageListElem.appendChild(imageElem);
131 }
132 return result;
133 }
134
135 /********************
136 * Helper functions *
137 *******************/
138
139
140 private String getAssocDir(String full_filename, String remove_prefix)
141 {
142 String dir = null;
143
144 int remove_prefix_len = remove_prefix.length();
145
146 String dir_and_filetail = full_filename.substring(remove_prefix_len+1);
147
148 int dir_end_pos = dir_and_filetail.lastIndexOf("/");
149 if (dir_end_pos > 0) {
150 dir = dir_and_filetail.substring(0,dir_end_pos);
151 }
152 else {
153 dir = dir_and_filetail;
154 }
155
156 System.err.println("**** dir = " + dir);
157
158 return dir;
159 }
160
161
162
163 private String queryMRforDOCID(String collection, String assocfiledir) {
164 Document gsDoc = XMLConverter.newDOM();
165
166 Element metaMessage = gsDoc.createElement(GSXML.MESSAGE_ELEM);
167 Element metaRequest = GSXML.createBasicRequest(gsDoc, GSXML.REQUEST_TYPE_PROCESS, collection + "/" + METADATA_RETRIEVAL_SERVICE, new UserContext());
168 metaMessage.appendChild(metaRequest);
169
170 Element paramList = gsDoc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
171 metaRequest.appendChild(paramList);
172
173 Element param = gsDoc.createElement(GSXML.PARAM_ELEM);
174 param.setAttribute(GSXML.NAME_ATT, "metadata");
175 param.setAttribute(GSXML.VALUE_ATT, "contains");
176 paramList.appendChild(param);
177
178 Element docList = gsDoc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
179 metaRequest.appendChild(docList);
180
181 Element doc = gsDoc.createElement(GSXML.DOC_NODE_ELEM);
182 doc.setAttribute(GSXML.NODE_ID_ATT, assocfiledir);
183 docList.appendChild(doc);
184
185 Element metaResponse = (Element) router.process(metaMessage);
186
187 NodeList metadataList = metaResponse.getElementsByTagName(GSXML.METADATA_ELEM);
188 if (metadataList.getLength() == 0) {
189
190 logger.error("Could not find the document ID related to this url");
191 return null;
192 }
193
194 Element metadata = (Element) metadataList.item(0);
195 String document = metadata.getTextContent();
196 if (document != null && document.equals("")) {
197 document = null;
198 }
199 return document;
200
201
202 }
203
204
205
206 protected void getImagesRecursive(File current, String regex, ArrayList<String> images)
207 {
208 if(current.isDirectory())
209 {
210 File[] files = current.listFiles();
211 for(File f : files)
212 {
213 getImagesRecursive(f, regex, images);
214 }
215 }
216 else
217 {
218 String path = current.getAbsolutePath();
219 String filename = path.substring(path.lastIndexOf(File.separator) + File.separator.length());
220
221 if(filename.matches(regex))
222 {
223 images.add(path);
224 }
225 else
226 {
227
228 }
229 }
230 }
231}
Note: See TracBrowser for help on using the repository browser.