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

Last change on this file since 32475 was 32475, checked in by kjdon, 6 years ago

set a no_archives att to the collection element if archives are not present - then we know not to show document editing. for now, this is just a basic test whether the folder exists or not. not doing any test on the contents of it.

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