/*
* Collection.java
* Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.greenstone.gsdl3.collection;
import org.greenstone.gsdl3.util.*;
import org.greenstone.gsdl3.core.*;
import org.greenstone.gsdl3.service.*;
// java XML classes we're using
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.File;
import java.util.HashMap;
import org.apache.log4j.*;
/**
* Represents a collection in Greenstone. A collection is an extension of
* a ServiceCluster - it has local data that the services use.
*
* @author Katherine Don
* @version $Revision: 13830 $
* @see ModuleInterface
*/
public class Collection
extends ServiceCluster {
static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
protected XMLTransformer transformer = null;
/** same as setClusterName */
public void setCollectionName(String name) {
setClusterName(name);
}
public Collection() {
super();
this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
}
/**
* Configures the collection.
*
* gsdlHome and collectionName must be set before configure is called.
*
* the file buildcfg.xml is located in gsdlHome/collect/collectionName
* collection metadata is obtained, and services loaded.
*
* @return true/false on success/fail
*/
public boolean configure() {
if (this.site_home == null || this.cluster_name== null) {
logger.error("Collection: site_home and collection_name must be set before configure called!");
return false;
}
Element coll_config_xml = loadCollConfigFile();
Element build_config_xml = loadBuildConfigFile();
if (coll_config_xml==null||build_config_xml==null) {
return false;
}
// process the metadata and display items
findAndLoadInfo(coll_config_xml, build_config_xml);
// now do the services
Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
configureServiceRack(service_list, coll_config_xml);
return true;
}
/**
* load in the collection config file into a DOM Element
*/
protected Element loadCollConfigFile() {
File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
if (!coll_config_file.exists()) {
logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
return null;
}
// get the xml for both files
Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
Element coll_config_elem = null;
if (coll_config_doc != null) {
coll_config_elem = coll_config_doc.getDocumentElement();
}
return coll_config_elem;
}
/**
* load in the collection build config file into a DOM Element
*/
protected Element loadBuildConfigFile() {
File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
if (!build_config_file.exists()) {
logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
return null;
}
Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
Element build_config_elem = null;
if (build_config_doc != null) {
build_config_elem = build_config_doc.getDocumentElement();
}
return build_config_elem;
}
/**
* find the metadata and display elems from the two config files and add it to the appropriate lists
*/
protected boolean findAndLoadInfo(Element coll_config_xml,
Element build_config_xml){
// metadata
Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
addMetadata(meta_list);
meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
addMetadata(meta_list);
meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
addMetadata(meta_list);
// display stuff
Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
if (display_list != null) {
resolveMacros(display_list);
addDisplayItems(display_list);
}
return true;
}
protected boolean resolveMacros(Element display_list) {
if (display_list==null) return false;
NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
if (displaynodes.getLength()>0) {
String http_site = this.site_http_address;
String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
for(int k=0; k