/*
 * 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.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Collection;

import edu.stanford.smi.protege.model.KnowledgeBaseFactory;
import edu.stanford.smi.protege.server.ServerProject.ProjectStatus;
import edu.stanford.smi.protege.server.metaproject.Operation;
import edu.stanford.smi.protege.util.ServerJob;

public interface RemoteServer extends Remote {
 
	/*
     * Server management
     */
    
    void reinitialize() throws RemoteException;
    
    boolean createUser(String userName, String password) throws RemoteException;
 
	
    /*
     * Session management
     */
    RemoteSession openSession(String username, String userMachine, String password) throws RemoteException;
    
    RemoteSession cloneSession(RemoteSession session) throws RemoteException;
    
    void closeSession(RemoteSession session) throws RemoteException;
    
    Collection<RemoteSession> getCurrentSessions(String projectName, RemoteSession session) throws RemoteException;
    
    void killOtherUserSession(RemoteSession sessionToKill, RemoteSession session) throws RemoteException;
    
    void killOtherUserSession(RemoteSession sessionToKill, RemoteSession session, int finalGracePeriod) throws RemoteException;
    
    
    /*
     * Project management
     */
    
    Collection<String> getAvailableProjectNames(RemoteSession session) throws RemoteException;
    
    RemoteServerProject openProject(String projectName, RemoteSession session) throws RemoteException;    
    
    RemoteServerProject createProject(String projectName, RemoteSession session, KnowledgeBaseFactory kbfactory, boolean saveToMetaProject) throws RemoteException;
    
    ProjectStatus getProjectStatus(String projectName) throws RemoteException;
    
    void setProjectStatus(String projectName, ProjectStatus status, RemoteSession session) throws RemoteException;
    
    void notifyProject(String projectName, String message, RemoteSession session) throws RemoteException;

    void shutdown() throws RemoteException;
    
    void shutdown(String projectName, RemoteSession session) throws RemoteException;
    
    /**
	 * Shuts down the remote project with the name projectName and sends shutdown notifications
	 * at time periods (in seconds) from the warningTimesInSeconds argument.
	 * A {@link SecurityException} is thrown if the user of the session does not
	 * have the right to shutdown the project.
	 * @param session - the session that tries to shutdown the project
	 * @param projectName - the remote project name
	 * @param warningTimesInSeconds - a list of {@link Integer} with the notify periods of time in seconds
	 * @param finalGracePeriodInSeconds - the time the server will wait until shutting down the project after the last notification message
	 */
    void shutdownProject(RemoteSession session, String projectName,
				Integer[] warningTimesInSeconds, int finalGracePeriodInSeconds) throws RemoteException;
    
    /**
     * Shuts down the remote project with the name projectName and sends shutdown notifications
	 * at certain time periods: it starts with the warningTimeInSeconds argument and divides it by two.
	 * A {@link SecurityException} is thrown if the user of the session does not
	 * have the right to shutdown the project.
	 * @param session - the session that tries to shutdown the project
	 * @param projectName - the remote project name	     
     * @param warningTimeInSeconds - the time of the first notification in seconds
     */
    void shutdownProject(RemoteSession session, String projectName, float warningTimeInSeconds) throws RemoteException;
    
    boolean cancelShutdownProject(RemoteSession session, String projectName) throws RemoteException;
        
    
    /*
     * Policy
     */
    
    boolean allowsCreateUsers() throws RemoteException;
    
    boolean isOperationAllowed(RemoteSession session, Operation op, String projectName) throws RemoteException;
    
    boolean isServerOperationAllowed(RemoteSession session, Operation op) throws RemoteException;
    
    boolean isServerOperationAllowed(RemoteSession session, Operation op, String serverName) throws RemoteException;    
    
    /* 
     * Misc
     */
    Object executeServerJob(ServerJob job, RemoteSession session) throws RemoteException;
}