// RegistriesList.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.MobyException;

import org.apache.commons.discovery.tools.Service;
import java.util.Enumeration;

/**
 * A default implementation, providing a list of well-known BioMoby
 * registries. <p>
 *
 * Please edit this file if you want to have your BioMoby registry
 * visible to others (for example, in BioMoby Dashboard). <p>
 *
 * Or, you can also replace this implementation by your own, with your
 * own list of registries.
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: RegistriesList.java,v 1.6 2009/12/01 16:05:11 kawas Exp $
 */

public class RegistriesList
    implements Registries {

    Registry[] registries = new Registry[] {
	new Registry   // placeholder for a default registry
	(DEFAULT_REGISTRY_SYNONYM,
	 "", "", "", "", true, ""),

	new Registry
	("Calgary",                                                             // synonym
	 CentralImpl.DEFAULT_ENDPOINT,                                          // endpoint
	 CentralImpl.DEFAULT_NAMESPACE,                                         // namespace
	 "Sun Center of Excellence for Visual Genomics, University of Calgary", // full/long name
	 "Edward Kawas (edward.kawas@gmail.com)",                               // contact
	 true,                                                                  // isPublic
	 "A curated public registry hosted at the University of Calgary"),

	new Registry
	("IRRI",
	 "http://cropwiki.irri.org/cgi-bin/MOBY-Central.pl",
	 "http://cropwiki.irri.org/MOBY/Central",
	 "IRRI, Philippines",
	 "Mylah Rystie Anacleto (m.anacleto@cgiar.org)",
	 true,
	 "The MOBY registry at the International Rice Research Institute (IRRI) is intended mostly for Generation Challenge Program (GCP) developers. It allows the registration of experimental moby entities within GCP."),

	new Registry
	("INAB",
	 "http://moby-dev.inab.org/cgi-bin/MOBY-Central.pl",
	 "http://moby-dev.inab.org/MOBY/Central",
	 "INAB, Spain",
	 "",
	 true,
	 "The MOBY registry at the Instituto Nacional de Bioinform\u00E1tica, Spain."),

	new Registry
	("testing",
	 "http://mobytest.biordf.net/MOBY-Central.pl",
	 "http://mobytest.biordf.net/MOBY/Central",
	 "Testing BioMoby registry",
	 "Edward Kawas (edward.kawas@gmail.com)",
	 true,
	 "The MOBY test registry"),
    };

    /*********************************************************************
     * A default constructor. It initialises the registry list from a
     * here-hard-coded list.
     ********************************************************************/
    public RegistriesList() {

	// assign a default registry
	int fromIndex = 1;
	int toIndex = 0;
	registries [toIndex].setEndpoint (registries [fromIndex].getEndpoint());
	registries [toIndex].setNamespace (registries [fromIndex].getNamespace());
	registries [toIndex].setContact (registries [fromIndex].getContact());
	registries [toIndex].setDescription (registries [fromIndex].getDescription());
	registries [toIndex].setLongName (registries [fromIndex].getLongName());
    }

    /*********************************************************************
     * A constructor that adds registries to the default list.
     ********************************************************************/
    public RegistriesList (Registry[] additionalRegistries) {
	// first, make a default list
	this();

	// then, extend the default list
	int lenCurr = registries.length;
	int lenAdd = additionalRegistries.length;
	Registry[] extended = new Registry [lenCurr + lenAdd];
	System.arraycopy (registries, 0, extended, 0, lenCurr);
	System.arraycopy (additionalRegistries, 0, extended, lenCurr, lenAdd);
	registries = extended;
    }

    /*********************************************************************
     * Create a list of registries as dynamically found using the SPI
     * mechanim. If the SPI does not find any class, it creates a
     * default list. <p>
     *
     * Use this method if you want to add your own registry (or
     * registries) without changing the existing jMoby Java code. This
     * method is used by Dashboard - in its documenttaion is an
     * example how to do it. <p>
     *
     * @return an instance providing a list of known/available
     * registries
     ********************************************************************/
    public static Registries getInstance() {

	// check if there is around a non-default implementation
        Enumeration spe = Service.providers (Registries.class);
        while (spe.hasMoreElements()) {
            return (Registries)spe.nextElement();
        }

	// or, return a default list
	return new RegistriesList();
    }

    /*********************************************************************
     *
     ********************************************************************/
    public String[] list() {
	String[] results = new String [registries.length];
	for (int i = 0; i < registries.length; i++)
	    results[i] = registries[i].getSynonym();
	return results;
    }

    /*********************************************************************
     *
     ********************************************************************/
    public Registry get (String synonym)
	throws MobyException {
	if (synonym == null)
	    synonym = DEFAULT_REGISTRY_SYNONYM;
	for (int i = 0; i < registries.length; i++) {
	    String name = registries[i].getSynonym();
	    if (name == null) continue;
	    if (name.equalsIgnoreCase (synonym))
		return registries[i];
	}
	return null;
    }

    /*********************************************************************
     *
     ********************************************************************/
    public Registry[] getAll() {
	return registries;
    }

}
