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

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

check for null coll_config_xml before trying to modify it for debug, otherwise get null pointer exception. this will happen if you have a folder in collect which is not actually a collection

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