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

Last change on this file since 30563 was 30563, checked in by Georgiy Litvinov, 8 years ago

Undoing solr cores initialization commit to move code in solr extension repo

  • Property svn:keywords set to Author Date Id Revision
File size: 27.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 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 disabled = securityBlock.getAttribute(GSXML.DISABLED_ATT);
306 if (!disabled.equals("")) {
307 // security block has been disabled.
308 logger.warn("Security block has been disabled. Not implementing any security for collection "+this.cluster_name);
309 return;
310 }
311 String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
312 String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
313
314 if (defaultAccess.toLowerCase().equals("public"))
315 {
316 _publicAccess = true;
317 }
318 else if (defaultAccess.toLowerCase().equals("private"))
319 {
320 _publicAccess = false;
321 }
322 else
323 {
324 logger.warn("Default access for collection " + this.cluster_name + " is neither public or private, assuming public");
325 }
326
327 if (scope.toLowerCase().equals("collection"))
328 {
329 _securityScopeCollection = true;
330 }
331 else if (scope.toLowerCase().equals("documents") || scope.toLowerCase().equals("document"))
332 {
333 _securityScopeCollection = false;
334 }
335 else
336 {
337 logger.warn("Security scope is neither collection or document, assuming collection");
338 }
339
340 NodeList exceptions = GSXML.getChildrenByTagName(securityBlock, GSXML.EXCEPTION_ELEM);
341
342 if (exceptions.getLength() > 0)
343 {
344 if (!_securityScopeCollection)
345 {
346 NodeList documentSetElems = GSXML.getChildrenByTagName(securityBlock, GSXML.DOCUMENT_SET_ELEM);
347 for (int i = 0; i < documentSetElems.getLength(); i++)
348 {
349 Element documentSet = (Element) documentSetElems.item(i);
350 String setName = documentSet.getAttribute(GSXML.NAME_ATT);
351 NodeList matchStatements = GSXML.getChildrenByTagName(documentSet, GSXML.MATCH_ELEM);
352 ArrayList<Element> matchStatementList = new ArrayList<Element>();
353 for (int j = 0; j < matchStatements.getLength(); j++)
354 {
355 matchStatementList.add((Element) matchStatements.item(j));
356 }
357 _documentSets.put(setName, matchStatementList);
358 }
359 }
360
361 for (int i = 0; i < exceptions.getLength(); i++)
362 {
363 HashMap<String, ArrayList<String>> securityException = new HashMap<String, ArrayList<String>>();
364 ArrayList<String> exceptionGroups = new ArrayList<String>();
365 ArrayList<String> exceptionSets = new ArrayList<String>();
366
367 Element exception = (Element) exceptions.item(i);
368 NodeList groups = GSXML.getChildrenByTagName(exception, GSXML.GROUP_ELEM);
369 for (int j = 0; j < groups.getLength(); j++)
370 {
371 Element group = (Element) groups.item(j);
372 String groupName = group.getAttribute(GSXML.NAME_ATT);
373 exceptionGroups.add(groupName);
374 }
375 NodeList docSets = GSXML.getChildrenByTagName(exception, GSXML.DOCUMENT_SET_ELEM);
376 for (int j = 0; j < docSets.getLength(); j++)
377 {
378 Element docSet = (Element) docSets.item(j);
379 String docSetName = docSet.getAttribute(GSXML.NAME_ATT);
380 exceptionSets.add(docSetName);
381 }
382 if (_securityScopeCollection) {
383 // we don't add in any exceptions that have document sets
384 if (!exceptionSets.isEmpty()) {
385 continue;
386 }
387 }
388 securityException.put("groups", exceptionGroups);
389 securityException.put("sets", exceptionSets);
390 _securityExceptions.add(securityException);
391 }
392 }
393 }
394
395 protected boolean configureServiceRacks(Element coll_config_xml, Element build_config_xml)
396 {
397 clearServices();
398 Element service_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
399 if (service_list != null)
400 {
401 configureServiceRackList(service_list, coll_config_xml);
402 }
403 // collection Config may also contain manually added service racks
404 service_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
405 if (service_list != null)
406 {
407 configureServiceRackList(service_list, build_config_xml);
408 }
409 return true;
410 }
411
412 /**
413 * do a configure on only part of the collection
414 */
415 protected boolean configureSubset(String subset)
416 {
417
418 // need the coll config files
419 Element coll_config_elem = loadCollConfigFile();
420 Element build_config_elem = loadBuildConfigFile();
421 if (coll_config_elem == null || build_config_elem == null)
422 {
423 // wont be able to do any of the requests
424 return false;
425 }
426
427 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
428 {
429 return configureServiceRacks(coll_config_elem, build_config_elem);
430 }
431
432 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))
433 {
434 configureLocalData(coll_config_elem);
435 configureLocalData(build_config_elem);
436 return findAndLoadInfo(coll_config_elem, build_config_elem);
437
438 }
439
440 logger.error("Collection: cant process system request, configure " + subset);
441 return false;
442 }
443
444 /**
445 * handles requests made to the ServiceCluster itself
446 *
447 * @param req
448 * - the request Element- <request>
449 * @return the result Element - should be <response>
450 */
451 protected Element processMessage(Document result_doc, Element request)
452 {
453 String type = request.getAttribute(GSXML.TYPE_ATT);
454 if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
455 {
456 return processFormatStringRequest(result_doc, request);
457 }
458 else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
459 {
460 return processSecurityRequest(result_doc, request);
461 }
462 else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
463 {
464
465 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
466 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
467 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
468 if (_globalFormat != null)
469 {
470 response.appendChild(result_doc.importNode(_globalFormat, true));
471 }
472 return response;
473 }
474 // unknown type
475 return super.processMessage(result_doc, request);
476
477 }
478
479 protected Element processSecurityRequest(Document result_doc, Element request)
480 {
481 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
482 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
483 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
484
485 String oid = request.getAttribute("oid");
486 if (oid.contains("."))
487 {
488 oid = oid.substring(0, oid.indexOf("."));
489 }
490
491 ArrayList<String> groups = getPermittedGroups(oid);
492
493 Element groupList = result_doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
494 response.appendChild(groupList);
495
496 for (String groupName : groups)
497 {
498 Element group = result_doc.createElement(GSXML.GROUP_ELEM);
499 groupList.appendChild(group);
500 group.setAttribute(GSXML.NAME_ATT, groupName);
501 }
502 return response;
503 }
504
505 protected ArrayList<String> getPermittedGroups(String oid)
506 {
507 ArrayList<String> groups = new ArrayList<String>();
508
509 if (_securityScopeCollection)
510 {
511 if (_publicAccess)
512 {
513 groups.add("");
514 }
515 else
516 {
517 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
518 {
519 for (String group : exception.get("groups"))
520 {
521 groups.add(group);
522 }
523 }
524 }
525 }
526 else
527 {
528 if (oid != null && !oid.equals(""))
529 {
530 boolean inSet = false;
531 for (HashMap<String, ArrayList<String>> exception : _securityExceptions) {
532
533 ArrayList<String> exceptionSets = exception.get("sets");
534 if (exceptionSets.size() == 0) {
535 inSet = true;
536 for (String group : exception.get("groups"))
537 {
538 groups.add(group);
539 }
540 }
541 else {
542 for (String setName : exception.get("sets"))
543 {
544 if (documentIsInSet(oid, setName))
545 {
546 inSet = true;
547 for (String group : exception.get("groups"))
548 {
549 groups.add(group);
550 }
551 break;
552 }
553 }
554 }
555 }
556
557
558
559 if (!inSet && _publicAccess)
560 {// our doc was not part of any exception, so it must be public
561 groups.add("");
562 }
563 }
564 else // if we are not doing a request with an oid, then free to access
565 {
566 groups.add("");
567 }
568 }
569
570 return groups;
571 }
572
573 protected boolean documentIsInSet(String oid, String setName)
574 {
575 ArrayList<Element> matchStatements = _documentSets.get(setName);
576 if (matchStatements == null || matchStatements.size() == 0)
577 {
578 return false;
579 }
580
581 for (Element currentMatchStatement : matchStatements)
582 {
583 String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
584 if (fieldName == null || fieldName.equals(""))
585 {
586 fieldName = "oid";
587 }
588
589 String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
590 if (type == null || type.equals(""))
591 {
592 type = "match";
593 }
594
595 String fieldValue = "";
596 if (!fieldName.equals("oid"))
597 {
598 fieldValue = getFieldValue(oid, fieldName);
599 if (fieldValue == null)
600 {
601 return false;
602 }
603 }
604 else
605 {
606 fieldValue = oid;
607 }
608
609 String matchValue = GSXML.getNodeText(currentMatchStatement);
610 if (type.equals("match"))
611 {
612 if (matchValue.equals(fieldValue))
613 {
614 return true;
615 }
616 }
617 else if (type.equals("regex"))
618 {
619 if (fieldValue.matches(matchValue))
620 {
621 return true;
622 }
623 }
624 else
625 {
626 logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
627 }
628 }
629
630 return false;
631 }
632
633 protected String getFieldValue(String oid, String fieldName)
634 {
635 Document msg_doc = XMLConverter.newDOM();
636 Element metadataMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
637 Element metadataRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
638 metadataMessage.appendChild(metadataRequest);
639
640 Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
641 metadataRequest.appendChild(paramList);
642
643 Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
644 paramList.appendChild(param);
645
646 param.setAttribute(GSXML.NAME_ATT, "metadata");
647 param.setAttribute(GSXML.VALUE_ATT, fieldName);
648
649 Element docList = msg_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
650 metadataRequest.appendChild(docList);
651
652 Element doc = msg_doc.createElement(GSXML.DOC_NODE_ELEM);
653 docList.appendChild(doc);
654
655 doc.setAttribute(GSXML.NODE_ID_ATT, oid);
656
657 Element response = (Element) this.router.process(metadataMessage);
658 NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
659
660 if (metadataElems.getLength() > 0)
661 {
662 Element metadata = (Element) metadataElems.item(0);
663 return GSXML.getNodeText(metadata);
664 }
665
666 return null;
667 }
668
669 protected Element processFormatStringRequest(Document result_doc, Element request)
670 {
671 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
672 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
673 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
674
675 String subaction = request.getAttribute("subaction");
676 String service = request.getAttribute("service");
677
678 String classifier = null;
679 if (service.equals("ClassifierBrowse"))
680 {
681 classifier = request.getAttribute("classifier");
682 }
683
684 // check for version file
685 String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
686
687 String version_filename = "";
688 if (service.equals("ClassifierBrowse"))
689 version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
690 else
691 version_filename = directory + "query_format_statement_version.txt";
692
693 File version_file = new File(version_filename);
694
695 if (subaction.equals("update"))
696 {
697 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
698 //String format_string = GSXML.getNodeText(format_element);
699 Element format_statement = (Element) format_element.getFirstChild();
700
701 String version_number = "1";
702 BufferedWriter writer;
703
704 try
705 {
706
707 if (version_file.exists())
708 {
709 // Read version
710 BufferedReader reader = new BufferedReader(new FileReader(version_filename));
711 version_number = reader.readLine();
712 int aInt = Integer.parseInt(version_number) + 1;
713 version_number = Integer.toString(aInt);
714 reader.close();
715 }
716 else
717 {
718 // Create
719 version_file.createNewFile();
720 writer = new BufferedWriter(new FileWriter(version_filename));
721 writer.write(version_number);
722 writer.close();
723 }
724
725 // Write version file
726 String format_statement_filename = "";
727
728 if (service.equals("ClassifierBrowse"))
729 format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
730 else
731 format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
732
733 // Write format statement
734 String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
735 writer = new BufferedWriter(new FileWriter(format_statement_filename));
736 writer.write(format_string);
737 writer.close();
738
739 // Update version number
740 writer = new BufferedWriter(new FileWriter(version_filename));
741 writer.write(version_number);
742 writer.close();
743
744 }
745 catch (IOException e)
746 {
747 logger.error("IO Exception " + e);
748 }
749 }
750
751 if (subaction.equals("saveDocument"))
752 {
753 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
754 //String format_string = GSXML.getNodeText(format_element);
755 // Get display tag
756 Element display_format = (Element) format_element.getFirstChild();
757
758 String collection_config = directory + "collectionConfig.xml";
759 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
760
761 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
762
763 // Get display child
764 if (GSXML.getChildByTagName(current_node, "display") == null)
765 {
766 // well then create a format tag
767 Element display_tag = config.createElement("display");
768 current_node = (Node) current_node.appendChild(display_tag);
769 }
770 else
771 {
772 current_node = GSXML.getChildByTagName(current_node, "display");
773 }
774
775 if (GSXML.getChildByTagName(current_node, "format") == null)
776 {
777 // well then create a format tag
778 Element format_tag = config.createElement("format");
779 current_node.appendChild(format_tag);
780 }
781
782 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
783
784 String new_config = this.converter.getString(config);
785
786 new_config = StringUtils.replace(new_config, "&lt;", "<");
787 new_config = StringUtils.replace(new_config, "&gt;", ">");
788 new_config = StringUtils.replace(new_config, "&quot;", "\"");
789
790 try
791 {
792 // Write to file (not original! for now)
793 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
794 writer.write(new_config);
795 writer.close();
796 }
797 catch (IOException e)
798 {
799 logger.error("IO Exception " + e);
800 }
801 }
802
803 if (subaction.equals("save"))
804 {
805 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
806 Element format_statement = (Element) format_element.getFirstChild();
807
808 try
809 {
810 // open collectionConfig.xml and read in to w3 Document
811 String collection_config = directory + "collectionConfig.xml";
812 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
813
814 //String tag_name = "";
815 int k;
816 int index;
817 Element elem;
818 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
819 NodeList current_node_list;
820
821 if (service.equals("ClassifierBrowse"))
822 {
823 //tag_name = "browse";
824 // if CLX then need to look in <classifier> X then <format>
825 // default is <browse><format>
826
827 current_node = GSXML.getChildByTagName(current_node, "browse");
828
829 // find CLX
830 if (classifier != null)
831 {
832 current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
833 index = Integer.parseInt(classifier.substring(2)) - 1;
834
835 // index should be given by X-1
836 current_node = current_node_list.item(index);
837 // what if classifier does not have a format tag?
838 if (GSXML.getChildByTagName(current_node, "format") == null)
839 {
840 // well then create a format tag
841 Element format_tag = config.createElement("format");
842 current_node.appendChild(format_tag);
843 }
844 }
845 else
846 {
847 // To support all classifiers, set classifier to null? There is the chance here that the format tag does not exist
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 }
856 else if (service.equals("AllClassifierBrowse"))
857 {
858 current_node = GSXML.getChildByTagName(current_node, "browse");
859 if (GSXML.getChildByTagName(current_node, "format") == null)
860 {
861 // well then create a format tag
862 Element format_tag = config.createElement("format");
863 current_node.appendChild(format_tag);
864 }
865 }
866 else
867 {
868 // look in <format> with no attributes
869 current_node_list = GSXML.getChildrenByTagName(current_node, "search");
870 for (k = 0; k < current_node_list.getLength(); k++)
871 {
872 current_node = current_node_list.item(k);
873 // if current_node has no attributes then break
874 elem = (Element) current_node;
875 if (elem.hasAttribute("name") == false)
876 break;
877 }
878 }
879
880 current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
881
882 // Now convert config document to string for writing to file
883 String new_config = this.converter.getString(config);
884
885 new_config = StringUtils.replace(new_config, "&lt;", "<");
886 new_config = StringUtils.replace(new_config, "&gt;", ">");
887 new_config = StringUtils.replace(new_config, "&quot;", "\"");
888
889 // Write to file (not original! for now)
890 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
891 writer.write(new_config);
892 writer.close();
893
894 }
895 catch (Exception ex)
896 {
897 logger.error("There was an exception " + ex);
898
899 StringWriter sw = new StringWriter();
900 PrintWriter pw = new PrintWriter(sw, true);
901 ex.printStackTrace(pw);
902 pw.flush();
903 sw.flush();
904 logger.error(sw.toString());
905 }
906
907 }
908
909 return response;
910 }
911}
Note: See TracBrowser for help on using the repository browser.