package de.uka.ipd.consensus.impl;

import de.uka.ipd.consensus.foundation.ontology.OntologyManagerException;
import de.uka.ipd.consensus.foundation.rights.DynamicRightsManager;
import de.uka.ipd.consensus.foundation.rights.DynamicRightsManagerException;
import de.uka.ipd.consensus.foundation.schema.Occurrence;
import de.uka.ipd.consensus.foundation.schema.Topic;
import de.uka.ipd.consensus.foundation.schema.User;

/**
 * 
 * TODO
 * @author Thomas Much
 * @version 2006-06-14
 */
public class OccurrenceImpl extends TM4JObjectWrapper implements Occurrence {

	
	protected OccurrenceImpl(OntologyManagerImpl omi, org.tm4j.topicmap.Occurrence tmOccurrence) {
		super(omi,tmOccurrence);
	}


	protected org.tm4j.topicmap.Occurrence getWrappedOccurrence() {
		return (org.tm4j.topicmap.Occurrence)this.getWrappedObject();
	}


	public String getId() {
		return this.getObjectId();
	}


	public Topic getTopic() {
		Object obj = TM4JObjectWrapper.wrap( this.getOntologyManager(), this.getWrappedOccurrence().getParent() );
		if ((obj != null) && (obj instanceof Topic)) {
			return (Topic)obj;
		}
		else {
			return null;
		}
	}


	public String getData() {
		String data = this.getWrappedOccurrence().getData();
		if (data == null) {
			return "";
		}
		else {
			return data;
		}
	}


	public void setData(User setter, String data) throws OntologyManagerException,DynamicRightsManagerException {
		DynamicRightsManager drm = this.getOntologyManager().getDynamicRightsManager();
		if (drm != null) {
			drm.checkChangeOccurrence( setter );
		}

		if (data == null) {
			data = "";
		}
		
		String oldData = this.getData();
		
		try {
			this.getWrappedOccurrence().setData( data );
			this.getOntologyManager().fireOccurrenceChanged( this, setter, this.getTopic(), oldData );
		}
		catch (Exception e) {
			throw new OntologyManagerException( e );
		}
	}

	
	public Topic getType() {
		Object obj = TM4JObjectWrapper.wrap( this.getOntologyManager(), this.getWrappedOccurrence().getType() );
		if ((obj != null) && (obj instanceof Topic)) {
			return (Topic)obj;
		}
		else {
			return null;
		}
	}


	public void setType(User setter, Topic type) throws OntologyManagerException,DynamicRightsManagerException {
		DynamicRightsManager drm = this.getOntologyManager().getDynamicRightsManager();
		if (drm != null) {
			drm.checkChangeOccurrence( setter );
		}

		org.tm4j.topicmap.Topic tmType;
		if (type == null) {
			tmType = null;
		}
		else {
			tmType = ((TopicImpl)type).getWrappedTopic();
		}
		
		try {
			this.getWrappedOccurrence().setType( tmType );
		}
		catch (Exception e) {
			throw new OntologyManagerException( e );
		}
	}


	public boolean isOfType(Topic type) {
		org.tm4j.topicmap.Topic tmType;
		if (type == null) {
			tmType = null;
		}
		else {
			tmType = ((TopicImpl)type).getWrappedTopic();
		}

		return this.getWrappedOccurrence().isOfType( tmType );
	}


	public int compareTo(Object other) {
		return this.getData().toLowerCase().compareTo( ((OccurrenceImpl)other).getData().toLowerCase() );
	}


	public String toString() {
		return "Occurrence \"" + this.getData() + "\", id=" + this.getId();
	}

}
