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

import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.plugin.ExportPlugin;
import edu.stanford.smi.protege.plugin.ImportPlugin;
import edu.stanford.smi.protege.resource.LocalizedText;
import edu.stanford.smi.protege.resource.ResourceKey;
import edu.stanford.smi.protege.ui.ProjectManager;
import edu.stanford.smi.protege.util.FileUtilities;
import edu.stanford.smi.protege.util.Log;
import edu.stanford.smi.protege.util.ModalDialog;
import edu.stanford.smi.protege.util.WaitCursor;

/**
 * Import and Export plugins for the CLIPS file format.
 * 
 * Note that this is not a good example of a plugin. An import plugin for clips
 * should be able to read any clips file. This plugin can only read a subset of
 * clips files, in particular those generated by Protege. An export plugin for
 * clips should write out "clean" clips. This output should have none of the
 * "comments" and other Protege encoded information in it. This plugin does
 * neither of these jobs well because it just delegates the calls to the CLIPS
 * backend which has entirely different constraints. Nevertheless this plugin
 * does illustrate how to implement the required calls as well as demonstrating
 * a sample interaction with the USER.
 * 
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
public class ClipsImportExportPlugin implements ImportPlugin, ExportPlugin {
    private static  transient Logger log = Log.getLogger(ClipsImportExportPlugin.class);

    public String getName() {
        return "CLIPS";
    }

    public Project handleImportRequest() {
        Project project = Project.createNewProject(null, new ArrayList());
        handleImportRequest(project);
        return project;
    }

    private static void handleImportRequest(Project project) {
        ClipsFilePanel panel = new ClipsFilePanel();
        String title = LocalizedText.getText(ResourceKey.CLIPS_FILES_TO_IMPORT_DIALOG_TITLE);
        int rval = ModalDialog.showDialog(null, panel, title, ModalDialog.MODE_OK_CANCEL);
        if (rval == ModalDialog.OPTION_OK) {
            String classesFileName = panel.getClsesFileName();
            String instancesFileName = panel.getInstancesFileName();
            WaitCursor cursor = new WaitCursor(ProjectManager.getProjectManager().getMainPanel());
            try {
                importProject(project, classesFileName, instancesFileName);
            } finally {
                cursor.hide();
            }
        }
    }

    private static void importProject(Project project, String clsesFileName, String instancesFileName) {
        Collection errors = new ArrayList();
        KnowledgeBase kb = project.getKnowledgeBase();
        ClipsKnowledgeBaseFactory factory = new ClipsKnowledgeBaseFactory();
        Reader clsesReader = FileUtilities.getReader(clsesFileName);
        Reader instancesReader = FileUtilities.getReader(instancesFileName);
        factory.loadKnowledgeBase(kb, clsesReader, instancesReader, false, errors);
        Log.handleErrors(Log.getLogger(ClipsImportExportPlugin.class), Level.WARNING, errors);
    }

    public void handleExportRequest(Project project) {
        ClipsFilePanel panel = new ClipsFilePanel();
        String title = LocalizedText.getText(ResourceKey.CLIPS_FILES_TO_EXPORT_DIALOG_TITLE);
        int rval = ModalDialog.showDialog(null, panel, title, ModalDialog.MODE_OK_CANCEL);
        if (rval == ModalDialog.OPTION_OK) {
            String classesFileName = panel.getClsesFileName();
            String instancesFileName = panel.getInstancesFileName();
            WaitCursor cursor = new WaitCursor(ProjectManager.getProjectManager().getMainPanel());
            try {
                exportProject(project, classesFileName, instancesFileName);
            } finally {
                cursor.hide();
            }
        }
    }

    private static void exportProject(Project project, String clsesFileName, String instancesFileName) {
        Collection errors = new ArrayList();
        KnowledgeBase kb = project.getKnowledgeBase();
        ClipsKnowledgeBaseFactory factory = new ClipsKnowledgeBaseFactory();
        Writer clsesWriter = FileUtilities.getWriter(clsesFileName);
        Writer instancesWriter = FileUtilities.getWriter(instancesFileName);
        factory.saveKnowledgeBase(kb, clsesWriter, instancesWriter, errors);
        Log.handleErrors(log, Level.WARNING, errors);
    }

    public void dispose() {
        // do nothing
    }

}