/*
 * 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.metaproject.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Localizable;
import edu.stanford.smi.protege.server.metaproject.Group;
import edu.stanford.smi.protege.server.metaproject.GroupOperation;
import edu.stanford.smi.protege.server.metaproject.Operation;
import edu.stanford.smi.protege.server.metaproject.Policy;
import edu.stanford.smi.protege.server.metaproject.PolicyControlledObject;
import edu.stanford.smi.protege.server.metaproject.ProjectInstance;
import edu.stanford.smi.protege.server.metaproject.ServerInstance;
import edu.stanford.smi.protege.server.metaproject.User;
import edu.stanford.smi.protege.util.CollectionUtilities;

public class PolicyImpl implements Policy, Localizable, Serializable {
    private static final long serialVersionUID = 2209527990426609790L;
    
    
    MetaProjectImpl mp;

	public PolicyImpl(MetaProjectImpl mp) {
		this.mp = mp;
	}

	/*
	 * The project is ignored in this implementation.
	 */
	public boolean isOperationAuthorized(User user, 
	                                     Operation op, 
	                                     PolicyControlledObject project) {
		if (!getKnownOperations().contains(op)) {
			return true;
		}
		Set<Group> userGroups = user.getGroups();
		for (GroupOperation ga : project.getAllowedGroupOperations()) {
			if (userGroups.contains(ga.getAllowedGroup()) && ga.getAllowedOperations().contains(op)) {
				return true;
			}
		}
		return false;
	}

	/*
	 * The project is ignored in this implementation.
	 */
	public Set<Operation> getAllowedOperations(User user, ProjectInstance project) {
		Set<Operation> allowed = new HashSet<Operation>();
		Set<Group> userGroups = user.getGroups();
		for (GroupOperation ga : project.getAllowedGroupOperations()) {
			if (userGroups.contains(ga.getAllowedGroup())) {
				allowed.addAll(ga.getAllowedOperations());
			}
		}
		return allowed;
	}

	public Set<Operation> getKnownOperations() {
		return mp.getOperations();
	}
	
	public User getUserByName(String userName) {
	    for (User user : mp.getUsers()) {
	        if (userName.equals(user.getName())) {
	            return user;
	        }
	    }
	    return null;
	}
	
	public ProjectInstance getProjectInstanceByName(String projectName) {
	    for (ProjectInstance projectInstance : mp.getProjects()) {
	        if (projectInstance.getName().equals(projectName)) {
	            return projectInstance;
	        }
	    }
	    return null;
	}

	
	public ServerInstance getServerInstanceByName(String serverName) {
	    for (ServerInstance serverInstance : mp.getServers()) {
	        if (serverInstance.getName().equals(serverName)) {
	            return serverInstance;
	        }
	    }
	    return null;
	}
	
	public ServerInstance getFirstServerInstance() {		
		return CollectionUtilities.getFirstItem(mp.getServers());
	}

    public void localize(KnowledgeBase kb) {
        mp.localize(kb);
    }

}
