/*
 * 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.model.framestore.cleandispatch;

import java.util.*;

import edu.stanford.smi.protege.model.*;
import edu.stanford.smi.protege.model.framestore.*;
import edu.stanford.smi.protege.util.*;

/**
 * 
 * @author Ray Fergerson <fergerson@smi.stanford.edu>
 */
abstract class AbstractRedirectingDispatch implements Dispatch {
    public List getDirectOwnSlotValues(FrameStore delegate, Frame frame, Slot slot) {
        List values;
        if (isCorrectFrameType(frame)) {
            values = getCurrentValues(delegate, frame);
        } else {
            Log.getLogger().warning("Invalid frame type: " + frame);
            values = delegate.getDirectOwnSlotValues(frame, slot);
        }
        return values;
    }

    public void setDirectOwnSlotValues(FrameStore delegate, Frame frame, Slot slot, Collection newValues) {
        if (isCorrectFrameType(frame)) {
            Collection currentValues = new LinkedHashSet(getCurrentValues(delegate, frame));
            addValues(delegate, frame, currentValues, newValues);
            removeValues(delegate, frame, currentValues, newValues);
        } else {
            Log.getLogger().warning("Invalid frame type: " + frame);
            delegate.setDirectOwnSlotValues(frame, slot, newValues);
        }
    }

    private void addValues(FrameStore delegate, Frame frame, Collection currentValues, Collection newValues) {
        Collection valuesToAdd = new LinkedHashSet(newValues);
        valuesToAdd.removeAll(currentValues);
        Iterator i = valuesToAdd.iterator();
        while (i.hasNext()) {
            Object value = i.next();
            if (isCorrectValueType(value)) {
                addValue(delegate, frame, value);
            } else {
                Log.getLogger().warning("Ignoring invalid value: " + value);
            }
        }
    }

    private void removeValues(FrameStore delegate, Frame frame, Collection currentValues, Collection newValues) {
        Collection valuesToRemove = new LinkedHashSet(currentValues);
        valuesToRemove.removeAll(newValues);
        Iterator i = valuesToRemove.iterator();
        while (i.hasNext()) {
            Object value = i.next();
            if (isCorrectValueType(value)) {
                removeValue(delegate, frame, value);
            } else {
                Log.getLogger().warning("Ignoring invalid value: " + value);
            }
        }
    }

    abstract List getCurrentValues(FrameStore delegate, Frame frame);
    abstract void addValue(FrameStore delegate, Frame frame, Object value);
    abstract void removeValue(FrameStore delegate, Frame frame, Object value);
    abstract boolean isCorrectFrameType(Frame frame);
    abstract boolean isCorrectValueType(Object o);
}
