/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.server;

import java.net.URI;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collection;

import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Localizable;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.server.framestore.RemoteServerFrameStore;
import edu.stanford.smi.protege.server.framestore.ServerFrameStore;
import edu.stanford.smi.protege.server.framestore.ServerSessionLost;
import edu.stanford.smi.protege.server.metaproject.ProjectInstance;
import edu.stanford.smi.protege.server.socket.RmiSocketFactory;
import edu.stanford.smi.protege.server.socket.SSLFactory;

public class ServerProject extends UnicastRemoteObject implements RemoteServerProject {
    private static final long serialVersionUID = 7320382402535936929L;
    
    private URI _uri;
    private Server _server;
    private Project _project;
    private ProjectInstance metaProjectInstance;
    private ServerFrameStore _domainKbFrameStore;
    private ServerFrameStore _projectKbFrameStore;
    
    public enum ProjectStatus implements Localizable {
        READY, SHUTTING_DOWN, CLOSED_FOR_MAINTENANCE;
        
        public void localize(KnowledgeBase kb) {

        }
    }



    public ServerProject(Server server, URI uri, ProjectInstance projectInstance, Project project) throws RemoteException {
        super(SSLFactory.getServerPort(SSLFactory.Context.ALWAYS),
              new RmiSocketFactory(SSLFactory.Context.ALWAYS),
              new RmiSocketFactory(SSLFactory.Context.ALWAYS));
        _server = server;
        _uri = uri;
        _project = project;
        metaProjectInstance = projectInstance;
        _domainKbFrameStore = createServerFrameStore(_project.getKnowledgeBase());
        _domainKbFrameStore.setMetaProjectInstance(projectInstance);
        _projectKbFrameStore = createServerFrameStore(_project.getInternalProjectKnowledgeBase());
    }
    
    private static ServerFrameStore createServerFrameStore(KnowledgeBase kb) throws RemoteException {
        ServerFrameStore sfs = new ServerFrameStore(kb);
        return sfs;
    }

    public ProjectInstance getMetaProjectInstance() {
        return metaProjectInstance;
    }
    
    public URI getURI(RemoteSession session) {
        return _uri;
    }

    public String getDomainKbFactoryClassName() {
        return _project.getKnowledgeBase().getKnowledgeBaseFactory().getClass().getName();
    }

    public String getProjectKbFactoryClassName() {
        return _project.getInternalProjectKnowledgeBase().getKnowledgeBaseFactory().getClass().getName();
    }


    public RemoteServerFrameStore getDomainKbFrameStore(RemoteSession session) {
        return _domainKbFrameStore;
    }
    

    public RemoteServerFrameStore getProjectKbFrameStore(RemoteSession session) {
        return _projectKbFrameStore;
    }

    public void register(RemoteSession session) throws ServerSessionLost {
        _domainKbFrameStore.register(session);
        _projectKbFrameStore.register(session);
    }
    
    public void deregister(RemoteSession session) throws ServerSessionLost {
      _domainKbFrameStore.deregister(session);
      _projectKbFrameStore.deregister(session);
  }

    public void close(RemoteSession session) throws ServerSessionLost{
        _server.disconnectFromProject(this, session);
    }

    public Collection getCurrentSessions(RemoteSession unusedSession) {
        return _server.getCurrentSessions(this);
    }

    public boolean isDirty() {
        return _domainKbFrameStore.isDirty();
    }

    public void setClean() {
        _domainKbFrameStore.markClean();
        _projectKbFrameStore.markClean();
    }
    
    public void setFrameCalculatorDisabled(boolean disabled) {
      _domainKbFrameStore.setFrameCalculatorDisabled(disabled);
    }
}