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

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

made a new OAICollection class. Inherits from Collection. ALll OAI stuff is taken out of Collection and put in new class. OAICollection is a much simpler collection as it only loads in OAIPMH service rack, not any of the others. This will be used by new OAIMessageRouter instead of Collection

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