/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.central.model;

import org.inb.biomoby.central.cache.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.inb.biomoby.central.MobyCentral;

/**
 * BioMoby registry model implementation class.
 * The class reads a list of BioMoby Registries from "META-INF/MobyCentralList.xml" configuration file
 * This model is to select a default (current) BioMoby Registry
 * the application is working at the moment.
 * @author Dmitry Repchevsky
 */

public class MobyCentralModel extends AbstractModel<MobyCentral>
{
    private static MobyCentralModel model;

    private List<MobyCentral> centrals;

    private MobyCentral selected;
    
    private MobyCentralModel()
    {
        InputStream in = CacheConfig.class.getClassLoader().getResourceAsStream("META-INF/MobyCentralList.xml");
        
        if (in != null)
        {
            try
            {
                centrals = CacheConfig.instance().load(new InputStreamReader(in));
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            finally
            {
                try { in.close(); }
                catch(IOException ex) {}
            }
        }
    }

    /**
     * Method to setup a BioMoby Registry that entire application is working with.
     * The latter should cascade an update in all the models as a consequence.
     *
     * @param selected - a BioMoby Registry to work with
     */
    public void setSelectedCentral(MobyCentral selected)
    {
        this.selected = selected;
        
        notifyCleared();
    }

    /**
     * Method returns a currently active BioMoby Registry
     *
     * @return - a currently active BioMoby Registry
     */
    public MobyCentral getSelectedCentral()
    {
        return selected;
    }

    /**
     * Method returns all known (stored in "META-INF/MobyCentralList.xml") BioMoby Registries
     *
     * @return - a list of BioMoby Registries
     */
    public List<MobyCentral> getElements()
    {
        return centrals;
    }

    /**
     * Method is not implemented
     *
     * @param element - a new BioMoby Registry to be added into the model
     * @return - true if a new BioMoby Registry has been added
     */
    public synchronized boolean addElement(MobyCentral element)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Method is not implemented
     *
     * @param element - a BioMoby Registry to be removed from the model
     * @return - true if a new BioMoby Registry has been removed
     */
    public synchronized boolean removeElement(MobyCentral element)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Method not implemented
     *
     * @param element1 - BioMoby Registry element to be updated
     * @param element2 - a new, updated, BioMoby Registry element
     * @return - true if a new BioMoby Registry has been updated
     */
    public synchronized boolean updateElement(MobyCentral element1, MobyCentral element2)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static synchronized MobyCentralModel instance()
    {
        if (model == null)
        {
            model = new MobyCentralModel();
        }
       
        return model;
    }
}
