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

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.stanford.smi.protege.model.KnowledgeBaseFactory;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.ui.ProjectManager;
import edu.stanford.smi.protege.util.Log;
import edu.stanford.smi.protege.util.PropertyList;

/**
 * 
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
public abstract class AbstractCreateProjectPlugin implements CreateProjectPlugin {

    private static final transient Logger log = Log.getLogger(AbstractCreateProjectPlugin.class);
    private String name;
    private KnowledgeBaseFactory knowledgeBaseFactory;
    private boolean useExistingSources;

    protected AbstractCreateProjectPlugin(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void dispose() {
        // do nothing
    }

    //ESCA-JAVA0130 
    protected void handleErrors(Collection errors) {
        if (!errors.isEmpty()) {
            ProjectManager.getProjectManager().displayErrors("Create Project Errors", errors);
            Log.handleErrors(log,  Level.SEVERE, errors);
        }
    }

    public void setKnowledgeBaseFactory(KnowledgeBaseFactory factory) {
        this.knowledgeBaseFactory = factory;
    }

    public void setUseExistingSources(boolean b) {
        this.useExistingSources = b;
    }

    public Project createProject() {
        Project project = null;
        if (useExistingSources) {
            project = buildNewProject(knowledgeBaseFactory);
        } else {
            project = createNewProject(knowledgeBaseFactory);
        }
        return project;
    }

    protected Project createNewProject(KnowledgeBaseFactory factory) {
        Collection errors = new ArrayList();
        Project project = Project.createNewProject(factory, errors);
        handleErrors(errors);
        return project;
    }

    protected void initialize(Project project) {
        initializeSources(project.getSources());
    }

    protected Project buildNewProject(KnowledgeBaseFactory factory) {
        Collection errors = new ArrayList();
        Project project = Project.createBuildProject(factory, errors);
        initialize(project);
        URI uri = getBuildProjectURI();
        if (uri != null) {
            project.setProjectURI(uri);
        }
        project.createDomainKnowledgeBase(factory, errors, true);
        handleErrors(errors);
        return project;
    }

    //ESCA-JAVA0130 
    protected URI getBuildProjectURI() {
        return null;
    }

    //ESCA-JAVA0130 
    protected void initializeSources(PropertyList sources) {
        throw new UnsupportedOperationException();
    }

}