source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java@ 28859

Last change on this file since 28859 was 28859, checked in by kjdon, 10 years ago

I had removed some constants from OAIXML, so now need to use GSXML.NAME_ATT instead of OAIXML.NAME

  • Property svn:keywords set to Author Date Id Revision
File size: 28.2 KB
Line 
1/*
2 * Collection.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 */
19package org.greenstone.gsdl3.collection;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.util.ArrayList;
30import java.util.HashMap;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.core.ModuleInterface;
35import org.greenstone.gsdl3.util.GSFile;
36import org.greenstone.gsdl3.util.GSXML;
37import org.greenstone.gsdl3.util.GSXSLT;
38import org.greenstone.gsdl3.util.OAIXML;
39import org.greenstone.gsdl3.util.SimpleMacroResolver;
40import org.greenstone.gsdl3.util.UserContext;
41import org.greenstone.gsdl3.util.XMLTransformer;
42import org.w3c.dom.Document;
43import org.w3c.dom.Element;
44import org.w3c.dom.Node;
45import org.w3c.dom.NodeList;
46
47/**
48 * Represents a collection in Greenstone. A collection is an extension of a
49 * ServiceCluster - it has local data that the services use.
50 *
51 * @author Katherine Don
52 * @see ModuleInterface
53 */
54public class Collection extends ServiceCluster
55{
56
57 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
58
59 /** is this collection being tidied and therefore can support realistic book view? */
60 protected boolean useBook = false;
61 /**
62 * is this collection public or private - public collections will
63 * appear on the home page, whereas private collections won't
64 */
65 protected boolean is_public = true;
66 /** collection type : mg, mgpp or lucene */
67 protected String col_type = "";
68 /** database type : gdbm, jdbm or sqlite */
69 protected String db_type = "";
70
71 /** does this collection provide the OAI service */
72 protected boolean has_oai = false;
73 /** time when this collection was built */
74 protected long lastmodified = 0;
75 /** earliestDatestamp of this collection. Necessary for OAI */
76 protected long earliestDatestamp = 0;
77
78 /** Stores the default accessibility of guest users */
79 protected boolean _publicAccess = true;
80 /** Stores the scope of any security rules (either collection or document) */
81 protected boolean _securityScopeCollection = true;
82
83 protected HashMap<String, ArrayList<Element>> _documentSets = new HashMap<String, ArrayList<Element>>();
84 protected ArrayList<HashMap<String, ArrayList<String>>> _securityExceptions = new ArrayList<HashMap<String, ArrayList<String>>>();
85
86 protected XMLTransformer transformer = null;
87
88 /** same as setClusterName */
89 public void setCollectionName(String name)
90 {
91 setClusterName(name);
92 }
93
94 public Collection()
95 {
96 super();
97 this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
98 }
99
100 /**
101 * Configures the collection.
102 *
103 * gsdlHome and collectionName must be set before configure is called.
104 *
105 * the file buildcfg.xml is located in gsdlHome/collect/collectionName
106 * collection metadata is obtained, and services loaded.
107 *
108 * @return true/false on success/fail
109 */
110 public boolean configure()
111 {
112 if (this.site_home == null || this.cluster_name == null)
113 {
114 logger.error("Collection: site_home and collection_name must be set before configure called!");
115 return false;
116 }
117
118 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
119
120 Element coll_config_xml = loadCollConfigFile();
121 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
122 Element build_config_xml = loadBuildConfigFile();
123
124 if (coll_config_xml == null || build_config_xml == null)
125 {
126 return false;
127 }
128
129 // get the collection type attribute
130 Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
131 if (search != null)
132 {
133 col_type = search.getAttribute(GSXML.TYPE_ATT);
134 }
135
136 Element browse = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.INFODB_ELEM);
137 if (browse != null)
138 {
139 db_type = browse.getAttribute(GSXML.TYPE_ATT);
140 }
141 else
142 {
143 db_type = "gdbm"; //Default database type
144 }
145
146 this.description.setAttribute(GSXML.TYPE_ATT, col_type);
147 this.description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
148
149 _globalFormat = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.FORMAT_ELEM);
150 // process the metadata and display items and default library params
151 super.configureLocalData(coll_config_xml);
152 super.configureLocalData(build_config_xml);
153 // get extra collection specific stuff
154 findAndLoadInfo(coll_config_xml, build_config_xml);
155
156 loadSecurityInformation(coll_config_xml);
157
158 // now do the services
159 configureServiceRacks(coll_config_xml, build_config_xml);
160
161 return true;
162
163 }
164
165 public boolean useBook()
166 {
167 return useBook;
168 }
169
170 public boolean isPublic()
171 {
172 return is_public;
173 }
174
175 // Not used anymore by the OAIReceptionist to find out the earliest datestamp
176 // amongst all oai collections in the repository. May be useful generally.
177 public long getLastmodified()
178 {
179 return lastmodified;
180 }
181
182 //used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
183 public long getEarliestDatestamp()
184 {
185 return earliestDatestamp;
186 }
187
188 /**
189 * whether the service_map in ServiceCluster.java contains the service
190 * 'OAIPMH' 11/06/2007 xiao
191 */
192 public boolean hasOAI()
193 {
194 return has_oai;
195 }
196
197 /**
198 * load in the collection config file into a DOM Element
199 */
200 protected Element loadCollConfigFile()
201 {
202
203 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
204
205 if (!coll_config_file.exists())
206 {
207 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + coll_config_file + " does not exist");
208 return null;
209 }
210 // get the xml
211 Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
212 Element coll_config_elem = null;
213 if (coll_config_doc != null)
214 {
215 coll_config_elem = coll_config_doc.getDocumentElement();
216 }
217 return coll_config_elem;
218
219 }
220
221 /**
222 * load in the collection build config file into a DOM Element
223 */
224 protected Element loadBuildConfigFile()
225 {
226 File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
227 if (!build_config_file.exists())
228 {
229 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + build_config_file + " does not exist");
230 return null;
231 }
232 Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
233 Element build_config_elem = null;
234 if (build_config_doc != null)
235 {
236 build_config_elem = build_config_doc.getDocumentElement();
237 }
238
239 lastmodified = build_config_file.lastModified();
240
241 return build_config_elem;
242 }
243
244 /**
245 * find the metadata and display elems from the two config files and add it
246 * to the appropriate lists
247 */
248 protected boolean findAndLoadInfo(Element coll_config_xml, Element build_config_xml)
249 {
250 // Element meta_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
251 // addMetadata(meta_list);
252 // meta_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
253 // addMetadata(meta_list);
254 addMetadata("httpPath", this.site_http_address + "/collect/" + this.cluster_name);
255
256 // display stuff
257 // Element display_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
258 // if (display_list != null)
259 // {
260 // resolveMacros(display_list);
261 // addDisplayItems(display_list);
262 // }
263
264 //check whether the html are tidy or not
265 Element import_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
266 if (import_list != null)
267 {
268 Element plugin_list = (Element) GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
269 //addPlugins(plugin_list);
270 if (plugin_list != null)
271 {
272 Element plugin_elem = (Element) GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlugin");
273 if (plugin_elem != null)
274 {
275 //get the option
276 Element option_elem = (Element) GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-use_realistic_book");
277 if (option_elem != null)
278 {
279 useBook = true;
280 }
281 }
282 }
283 }
284 String tidy = (useBook == true ? "tidy" : "untidy");
285 addMetadata("tidyoption", tidy);
286
287 // check whether we are public or not
288 if (this.metadata_list != null)
289 {
290 Element meta_elem = (Element) GSXML.getNamedElement(this.metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
291 if (meta_elem != null)
292 {
293 String value = GSXML.getValue(meta_elem).toLowerCase().trim();
294 if (value.equals("false"))
295 {
296 is_public = false;
297 }
298 }
299 }
300 return true;
301 }
302
303 protected void loadSecurityInformation(Element coll_config_xml)
304 {
305 Element securityBlock = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SECURITY_ELEM);
306
307 if (securityBlock == null)
308 {
309 return;
310 }
311
312 String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
313 String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
314
315 if (defaultAccess.toLowerCase().equals("public"))
316 {
317 _publicAccess = true;
318 }
319 else if (defaultAccess.toLowerCase().equals("private"))
320 {
321 _publicAccess = false;
322 }
323 else
324 {
325 logger.warn("Default access for collection " + this.cluster_name + " is neither public or private, assuming public");
326 }
327
328 if (scope.toLowerCase().equals("collection"))
329 {
330 _securityScopeCollection = true;
331 }
332 else if (scope.toLowerCase().equals("documents") || scope.toLowerCase().equals("document"))
333 {
334 _securityScopeCollection = false;
335 }
336 else
337 {
338 logger.warn("Security scope is neither collection or document, assuming collection");
339 }
340
341 NodeList exceptions = GSXML.getChildrenByTagName(securityBlock, GSXML.EXCEPTION_ELEM);
342
343 if (exceptions.getLength() > 0)
344 {
345 if (!_securityScopeCollection)
346 {
347 NodeList documentSetElems = GSXML.getChildrenByTagName(securityBlock, GSXML.DOCUMENT_SET_ELEM);
348 for (int i = 0; i < documentSetElems.getLength(); i++)
349 {
350 Element documentSet = (Element) documentSetElems.item(i);
351 String setName = documentSet.getAttribute(GSXML.NAME_ATT);
352 NodeList matchStatements = GSXML.getChildrenByTagName(documentSet, GSXML.MATCH_ELEM);
353 ArrayList<Element> matchStatementList = new ArrayList<Element>();
354 for (int j = 0; j < matchStatements.getLength(); j++)
355 {
356 matchStatementList.add((Element) matchStatements.item(j));
357 }
358 _documentSets.put(setName, matchStatementList);
359 }
360 }
361
362 for (int i = 0; i < exceptions.getLength(); i++)
363 {
364 HashMap<String, ArrayList<String>> securityException = new HashMap<String, ArrayList<String>>();
365 ArrayList<String> exceptionGroups = new ArrayList<String>();
366 ArrayList<String> exceptionSets = new ArrayList<String>();
367
368 Element exception = (Element) exceptions.item(i);
369 NodeList groups = GSXML.getChildrenByTagName(exception, GSXML.GROUP_ELEM);
370 for (int j = 0; j < groups.getLength(); j++)
371 {
372 Element group = (Element) groups.item(j);
373 String groupName = group.getAttribute(GSXML.NAME_ATT);
374 exceptionGroups.add(groupName);
375 }
376 NodeList docSets = GSXML.getChildrenByTagName(exception, GSXML.DOCUMENT_SET_ELEM);
377 for (int j = 0; j < docSets.getLength(); j++)
378 {
379 Element docSet = (Element) docSets.item(j);
380 String docSetName = docSet.getAttribute(GSXML.NAME_ATT);
381 exceptionSets.add(docSetName);
382 }
383
384 securityException.put("groups", exceptionGroups);
385 securityException.put("sets", exceptionSets);
386 _securityExceptions.add(securityException);
387 }
388 }
389 }
390
391 protected boolean configureServiceRacks(Element coll_config_xml, Element build_config_xml)
392 {
393 clearServices();
394 Element service_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
395 Element oai_service_rack = null;
396 if (service_list != null)
397 {
398 configureServiceRackList(service_list, coll_config_xml);
399 oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, OAIXML.OAIPMH);
400 }
401 // collection Config may also contain manually added service racks
402 service_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
403 if (service_list != null)
404 {
405 configureServiceRackList(service_list, build_config_xml);
406 // this oai used in preference to one in buildConfig.xml
407 oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, OAIXML.OAIPMH);
408 }
409 // Check for oai
410
411 if (oai_service_rack != null)
412 {
413 has_oai = true;
414 logger.info(this.cluster_name + " has OAI services");
415 // extract earliestDatestamp from the buildconfig.xml for OAI
416 Element metadata_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
417
418 if (metadata_list != null)
419 {
420 NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
421 // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
422 for (int i = 0; i < children.getLength(); i++)
423 {
424 Element metadata = (Element) children.item(i);
425 if (metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP))
426 {
427 String earliestDatestampStr = GSXML.getValue(metadata);
428 if (!earliestDatestampStr.equals(""))
429 {
430 earliestDatestamp = Long.parseLong(earliestDatestampStr);
431 }
432 break; // found a metadata element with name=earliestDatestamp in buildconfig
433 }
434 }
435 }
436
437 // If at the end of this, there is no value for earliestDatestamp, print out a warning
438 logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
439
440 } // if oai_service_rack != null
441
442 return true;
443 }
444
445 /**
446 * do a configure on only part of the collection
447 */
448 protected boolean configureSubset(String subset)
449 {
450
451 // need the coll config files
452 Element coll_config_elem = loadCollConfigFile();
453 Element build_config_elem = loadBuildConfigFile();
454 if (coll_config_elem == null || build_config_elem == null)
455 {
456 // wont be able to do any of the requests
457 return false;
458 }
459
460 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
461 {
462 return configureServiceRacks(coll_config_elem, build_config_elem);
463 }
464
465 if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER) || subset.equals("libraryParamList"))
466 {
467 configureLocalData(coll_config_elem);
468 configureLocalData(build_config_elem);
469 return findAndLoadInfo(coll_config_elem, build_config_elem);
470
471 }
472
473 logger.error("Collection: cant process system request, configure " + subset);
474 return false;
475 }
476
477 /**
478 * handles requests made to the ServiceCluster itself
479 *
480 * @param req
481 * - the request Element- <request>
482 * @return the result Element - should be <response>
483 */
484 protected Element processMessage(Element request)
485 {
486 String type = request.getAttribute(GSXML.TYPE_ATT);
487 if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
488 {
489 return processFormatStringRequest(request);
490 }
491 else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
492 {
493 return processSecurityRequest(request);
494 }
495 else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
496 {
497 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
498 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
499 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
500 if (_globalFormat != null)
501 {
502 response.appendChild(this.doc.importNode(_globalFormat, true));
503 }
504 return response;
505 }
506 // unknown type
507 return super.processMessage(request);
508
509 }
510
511 protected Element processSecurityRequest(Element request)
512 {
513 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
514 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
515 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
516
517 String oid = request.getAttribute("oid");
518 if (oid.contains("."))
519 {
520 oid = oid.substring(0, oid.indexOf("."));
521 }
522
523 ArrayList<String> groups = getPermittedGroups(oid);
524
525 Element groupList = this.doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
526 response.appendChild(groupList);
527
528 for (String groupName : groups)
529 {
530 Element group = this.doc.createElement(GSXML.GROUP_ELEM);
531 groupList.appendChild(group);
532 group.setAttribute(GSXML.NAME_ATT, groupName);
533 }
534 return response;
535 }
536
537 protected ArrayList<String> getPermittedGroups(String oid)
538 {
539 ArrayList<String> groups = new ArrayList<String>();
540
541 if (_securityScopeCollection)
542 {
543 if (_publicAccess)
544 {
545 groups.add("");
546 }
547 else
548 {
549 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
550 {
551 for (String group : exception.get("groups"))
552 {
553 groups.add(group);
554 }
555 }
556 }
557 }
558 else
559 {
560 if (oid != null && !oid.equals(""))
561 {
562 boolean inSet = false;
563 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
564 {
565 for (String setName : exception.get("sets"))
566 {
567 if (documentIsInSet(oid, setName))
568 {
569 inSet = true;
570 for (String group : exception.get("groups"))
571 {
572 groups.add(group);
573 }
574 }
575 }
576 }
577
578 if (!inSet && _publicAccess)
579 {
580 groups.add("");
581 }
582 }
583 else
584 {
585 groups.add("");
586 }
587 }
588
589 return groups;
590 }
591
592 protected boolean documentIsInSet(String oid, String setName)
593 {
594 ArrayList<Element> matchStatements = _documentSets.get(setName);
595 if (matchStatements == null || matchStatements.size() == 0)
596 {
597 return false;
598 }
599
600 for (Element currentMatchStatement : matchStatements)
601 {
602 String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
603 if (fieldName == null || fieldName.equals(""))
604 {
605 fieldName = "oid";
606 }
607
608 String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
609 if (type == null || type.equals(""))
610 {
611 type = "match";
612 }
613
614 String fieldValue = "";
615 if (!fieldName.equals("oid"))
616 {
617 fieldValue = getFieldValue(oid, fieldName);
618 if (fieldValue == null)
619 {
620 return false;
621 }
622 }
623 else
624 {
625 fieldValue = oid;
626 }
627
628 String matchValue = GSXML.getNodeText(currentMatchStatement);
629 if (type.equals("match"))
630 {
631 if (matchValue.equals(fieldValue))
632 {
633 return true;
634 }
635 }
636 else if (type.equals("regex"))
637 {
638 if (fieldValue.matches(matchValue))
639 {
640 return true;
641 }
642 }
643 else
644 {
645 logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
646 }
647 }
648
649 return false;
650 }
651
652 protected String getFieldValue(String oid, String fieldName)
653 {
654 Element metadataMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
655 Element metadataRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
656 metadataMessage.appendChild(metadataRequest);
657
658 Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
659 metadataRequest.appendChild(paramList);
660
661 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
662 paramList.appendChild(param);
663
664 param.setAttribute(GSXML.NAME_ATT, "metadata");
665 param.setAttribute(GSXML.VALUE_ATT, fieldName);
666
667 Element docList = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
668 metadataRequest.appendChild(docList);
669
670 Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
671 docList.appendChild(doc);
672
673 doc.setAttribute(GSXML.NODE_ID_ATT, oid);
674
675 Element response = (Element) this.router.process(metadataMessage);
676 NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
677
678 if (metadataElems.getLength() > 0)
679 {
680 Element metadata = (Element) metadataElems.item(0);
681 return GSXML.getNodeText(metadata);
682 }
683
684 return null;
685 }
686
687 protected Element processFormatStringRequest(Element request)
688 {
689 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
690 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
691 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
692
693 String subaction = request.getAttribute("subaction");
694 String service = request.getAttribute("service");
695
696 String classifier = null;
697 if (service.equals("ClassifierBrowse"))
698 {
699 classifier = request.getAttribute("classifier");
700 }
701
702 // check for version file
703 String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
704
705 String version_filename = "";
706 if (service.equals("ClassifierBrowse"))
707 version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
708 else
709 version_filename = directory + "query_format_statement_version.txt";
710
711 File version_file = new File(version_filename);
712
713 if (subaction.equals("update"))
714 {
715 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
716 //String format_string = GSXML.getNodeText(format_element);
717 Element format_statement = (Element) format_element.getFirstChild();
718
719 String version_number = "1";
720 BufferedWriter writer;
721
722 try
723 {
724
725 if (version_file.exists())
726 {
727 // Read version
728 BufferedReader reader = new BufferedReader(new FileReader(version_filename));
729 version_number = reader.readLine();
730 int aInt = Integer.parseInt(version_number) + 1;
731 version_number = Integer.toString(aInt);
732 reader.close();
733 }
734 else
735 {
736 // Create
737 version_file.createNewFile();
738 writer = new BufferedWriter(new FileWriter(version_filename));
739 writer.write(version_number);
740 writer.close();
741 }
742
743 // Write version file
744 String format_statement_filename = "";
745
746 if (service.equals("ClassifierBrowse"))
747 format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
748 else
749 format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
750
751 // Write format statement
752 String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
753 writer = new BufferedWriter(new FileWriter(format_statement_filename));
754 writer.write(format_string);
755 writer.close();
756
757 // Update version number
758 writer = new BufferedWriter(new FileWriter(version_filename));
759 writer.write(version_number);
760 writer.close();
761
762 }
763 catch (IOException e)
764 {
765 logger.error("IO Exception " + e);
766 }
767 }
768
769 if (subaction.equals("saveDocument"))
770 {
771 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
772 //String format_string = GSXML.getNodeText(format_element);
773 // Get display tag
774 Element display_format = (Element) format_element.getFirstChild();
775
776 String collection_config = directory + "collectionConfig.xml";
777 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
778
779 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
780
781 // Get display child
782 if (GSXML.getChildByTagName(current_node, "display") == null)
783 {
784 // well then create a format tag
785 Element display_tag = config.createElement("display");
786 current_node = (Node) current_node.appendChild(display_tag);
787 }
788 else
789 {
790 current_node = GSXML.getChildByTagName(current_node, "display");
791 }
792
793 if (GSXML.getChildByTagName(current_node, "format") == null)
794 {
795 // well then create a format tag
796 Element format_tag = config.createElement("format");
797 current_node.appendChild(format_tag);
798 }
799
800 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
801
802 String new_config = this.converter.getString(config);
803
804 new_config = StringUtils.replace(new_config, "&lt;", "<");
805 new_config = StringUtils.replace(new_config, "&gt;", ">");
806 new_config = StringUtils.replace(new_config, "&quot;", "\"");
807
808 try
809 {
810 // Write to file (not original! for now)
811 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
812 writer.write(new_config);
813 writer.close();
814 }
815 catch (IOException e)
816 {
817 logger.error("IO Exception " + e);
818 }
819 }
820
821 if (subaction.equals("save"))
822 {
823 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
824 Element format_statement = (Element) format_element.getFirstChild();
825
826 try
827 {
828 // open collectionConfig.xml and read in to w3 Document
829 String collection_config = directory + "collectionConfig.xml";
830 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
831
832 //String tag_name = "";
833 int k;
834 int index;
835 Element elem;
836 // Try importing entire tree to this.doc so we can add and remove children at ease
837 //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
838 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
839 NodeList current_node_list;
840
841 if (service.equals("ClassifierBrowse"))
842 {
843 //tag_name = "browse";
844 // if CLX then need to look in <classifier> X then <format>
845 // default is <browse><format>
846
847 current_node = GSXML.getChildByTagName(current_node, "browse");
848
849 // find CLX
850 if (classifier != null)
851 {
852 current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
853 index = Integer.parseInt(classifier.substring(2)) - 1;
854
855 // index should be given by X-1
856 current_node = current_node_list.item(index);
857 // what if classifier does not have a format tag?
858 if (GSXML.getChildByTagName(current_node, "format") == null)
859 {
860 // well then create a format tag
861 Element format_tag = config.createElement("format");
862 current_node.appendChild(format_tag);
863 }
864 }
865 else
866 {
867 // To support all classifiers, set classifier to null? There is the chance here that the format tag does not exist
868 if (GSXML.getChildByTagName(current_node, "format") == null)
869 {
870 // well then create a format tag
871 Element format_tag = config.createElement("format");
872 current_node.appendChild(format_tag);
873 }
874 }
875 }
876 else if (service.equals("AllClassifierBrowse"))
877 {
878 current_node = GSXML.getChildByTagName(current_node, "browse");
879 if (GSXML.getChildByTagName(current_node, "format") == null)
880 {
881 // well then create a format tag
882 Element format_tag = config.createElement("format");
883 current_node.appendChild(format_tag);
884 }
885 }
886 else
887 {
888 // look in <format> with no attributes
889 current_node_list = GSXML.getChildrenByTagName(current_node, "search");
890 for (k = 0; k < current_node_list.getLength(); k++)
891 {
892 current_node = current_node_list.item(k);
893 // if current_node has no attributes then break
894 elem = (Element) current_node;
895 if (elem.hasAttribute("name") == false)
896 break;
897 }
898 }
899
900 current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
901
902 // Now convert config document to string for writing to file
903 String new_config = this.converter.getString(config);
904
905 new_config = StringUtils.replace(new_config, "&lt;", "<");
906 new_config = StringUtils.replace(new_config, "&gt;", ">");
907 new_config = StringUtils.replace(new_config, "&quot;", "\"");
908
909 // Write to file (not original! for now)
910 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
911 writer.write(new_config);
912 writer.close();
913
914 }
915 catch (Exception ex)
916 {
917 logger.error("There was an exception " + ex);
918
919 StringWriter sw = new StringWriter();
920 PrintWriter pw = new PrintWriter(sw, true);
921 ex.printStackTrace(pw);
922 pw.flush();
923 sw.flush();
924 logger.error(sw.toString());
925 }
926
927 }
928
929 return response;
930 }
931}
Note: See TracBrowser for help on using the repository browser.