// Registry.java
//
// Created: October 2006
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.registry.meta;

import org.biomoby.client.CentralImpl;
import org.biomoby.shared.MobyResourceRef;
import org.biomoby.shared.MobyException;

/**
 * A container for meta data about a BioMoby registry. <p>
 *
 * It includes properties allowing to access a BioMoby registry -
 * these properties must be always present: synonym, endpoint (URL)
 * and namespce (URI), and some additional properties (not always
 * present). <p>
 *
 * A usual way how to obtain these containers is through
 * implementation of the {@link Registries Registries}
 * interface. Therefore, if you wish to provide a richer container (with
 * more properties) you should have your own implementation of that
 * interface. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: Registry.java,v 1.4 2008/02/22 09:37:39 senger Exp $
 */

public class Registry {

    // mandatory members (set in a constructor)
    String synonym;
    String endpoint;
    String namespace;

    // optional members
    String longName;
    String contact;
    String description;
    boolean isPublic = true;

    /*********************************************************************
     * A minimal constructor
     ********************************************************************/
    public Registry (String synonym, String endpoint, String namespace) {
	setSynonym (synonym);
	setEndpoint (endpoint);
	setNamespace (namespace);
    }

    /*********************************************************************
     * A full constructor
     ********************************************************************/
    public Registry (String synonym, String endpoint, String namespace,
		     String longName,
		     String contact,
		     boolean isPublic,
		     String description
		     ) {
	this (synonym, endpoint, namespace);
	setLongName (longName);
	setContact (contact);
	setPublic (isPublic);
	setDescription (description);
    }

    /*********************************************************************
     *
     ********************************************************************/
    public String toString() {
	StringBuilder buf = new StringBuilder (200);
	String title = "Information about '" + getSynonym() + "' registry:";
	buf.append (title);
	buf.append ("\n");
	for (int i = 0; i < title.length(); i++) buf.append ("-");
	buf.append ("\n");
	buf.append ("Synonym:   " + getSynonym() + "\n");
	buf.append ("Full name: " + getLongName() + "\n");
	buf.append ("Endpoint:  " + getEndpoint() + "\n");
	buf.append ("Namespace: " + getNamespace() + "\n");
	buf.append ("Contact:   " + getContact() + "\n");
	buf.append ("Public:    " + new Boolean (isPublic()).toString() + "\n\n");
	buf.append ("Description:\n" + getDescription() + "\n");
	return buf.toString();
    }

    /*********************************************************************
     * Get a synonym (an abbreviation) of this BioMoby registry. It
     * (reasonably) uniquely identifis this registry (usually within a
     * given implementation).
     ********************************************************************/
    public String getSynonym() {
	return synonym;
    }

    public void setSynonym (String value) {
	synonym = value;
    }

    /*********************************************************************
     * Get an endpoint (a stringified URL) of this BioMoby registry.
     ********************************************************************/
    public String getEndpoint() {
	return endpoint;
    }

    public void setEndpoint (String value) {
	endpoint = value;
    }

    /*********************************************************************
     * Get a namespace (a URI) of this BioMoby registry.
     ********************************************************************/
    public String getNamespace() {
	return namespace;
    }

    public void setNamespace (String value) {
	namespace = value;
    }

    /*********************************************************************
     * Get a full name of this BioMoby registry. The name is often
     * accompanied by its geographical location. <p>
     *
     * @return full name or an empty string
     ********************************************************************/
    public String getLongName() {
	return (longName == null ? "" : longName);
    }

    public void setLongName (String value) {
	longName = value;
    }

    /*********************************************************************
     * Get a contact person who is in charge of this BioMoby
     * registry. It is often accompanied by an email address. <p>
     *
     * @return contact or an empty string
     ********************************************************************/
    public String getContact() {
	return (contact == null ? "" : contact);
    }

    public void setContact (String value) {
	contact = value;
    }

    /*********************************************************************
     * Get a human-readable description of this BioMoby registry. It
     * often contain also the registry policy. <p>
     *
     * @return description or an empty string
     ********************************************************************/
    public String getDescription() {
	return (description == null ? "" : description);
    }

    public void setDescription (String value) {
	description = value;
    }

    /*********************************************************************
     * Return true if this BioMoby registry is open without any access
     * restrictions. Otherwise return false.
     ********************************************************************/
    public boolean isPublic() {
	return isPublic;
    }

    public void setPublic (boolean value) {
	isPublic = value;
    }

    /**
     * Learn the RDF location for resources of the registry.
     *
     * @param resourceName one of the org.biomoby.shared.Central RESOURCE constants
     *
     * @return the URL of the resource, or null if the resource does not exist
     */
    public static java.net.URL findResourceURL(Registry reg, String resourceName) throws MobyException{
	CentralImpl central = new CentralImpl(reg.getEndpoint());
	MobyResourceRef[] resources = central.getResourceRefs();
	for(MobyResourceRef resource: resources){
	    if(resourceName.equals(resource.getResourceName())){
		return resource.getResourceLocation();
	    }
	}
	System.err.println("Error! Could not find the data type resource from the registry " + reg.getSynonym());
	return null;
    }

}
