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

Last change on this file since 27705 was 27705, checked in by sjm84, 11 years ago

Reformatting this file

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