/*
 * 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.protegex.widget.scatterbox;

import java.util.*;

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

/**
 *  Description of the class
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public abstract class BasicQueryObject implements KBQueryUtils {

    public abstract Instance getDomainObject(Instance entry);

    // domain
    public Cls getDomainType(Cls functionCls) {
        Cls entryCls = getEntryCls(functionCls);
        return getDomainTypeFromEntryCls(entryCls);
    }

    public abstract Cls getDomainTypeFromEntryCls(Cls entryCls);

    public Collection getDomainValueSlots(Cls entryCls) {
        KnowledgeBase kb = entryCls.getKnowledgeBase();
        Slot domainValueSlots = kb.getSlot(DOMAIN_VALUE_SLOTS);
        return entryCls.getOwnSlotValues(domainValueSlots);
    }

    public Slot getEntriesSlot(Cls functionCls) {
        KnowledgeBase kb = functionCls.getKnowledgeBase();
        return kb.getSlot(ENTRY_SLOT_NAME);
    }

    public Cls getEntryCls(Cls functionCls) {
        Slot entriesSlot = getEntriesSlot(functionCls);
        Slot entrySlot = (Slot) functionCls.getOwnSlotValue(entriesSlot);
        if (null == entrySlot) {
            return null;
        }
        Collection allowedClasses = functionCls.getTemplateSlotAllowedClses(entrySlot);
        if ((null == allowedClasses) || (1 != allowedClasses.size())) {
            return null;
        }
        Cls entryCls = (Cls) CollectionUtilities.getSoleItem(allowedClasses);
        return entryCls;
    }

    public abstract Instance getOrCreateDomainObject(Instance entry);

    public abstract Instance getOrCreateRangeObject(Instance entry);

    public abstract Instance getRangeObject(Instance entry);

    // range
    public Cls getRangeType(Cls functionCls) {
        Cls entryCls = getEntryCls(functionCls);
        return getRangeTypeFromEntryCls(entryCls);
    }

    public abstract Cls getRangeTypeFromEntryCls(Cls entryCls);

    public Slot getRangeValueSlot(Cls entryCls) {
        Collection rangeValueSlots = getRangeValueSlots(entryCls);
        if (null == rangeValueSlots) {
            return null;
        }
        return (Slot) CollectionUtilities.getFirstItem(rangeValueSlots);
    }

    public Slot getRangeValueSlot(Instance entry) {
        Cls entryCls = entry.getDirectType();
        return getRangeValueSlot(entryCls);
    }

    public Collection getRangeValueSlots(Cls entryCls) {
        KnowledgeBase kb = entryCls.getKnowledgeBase();
        Slot rangeValueSlots = kb.getSlot(RANGE_VALUE_SLOTS);
        return entryCls.getOwnSlotValues(rangeValueSlots);
    }

    public Collection getRangeValueSlots(Instance entry) {
        Cls entryCls = entry.getDirectType();
        return getRangeValueSlots(entryCls);
    }

    public abstract void setDomainObject(Instance entry, Instance domainObject);

    public abstract void setRangeObject(Instance entry, Instance rangeObject);
}
