package de.snailshell.consensus.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import de.uka.ipd.consensus.foundation.schema.Topic;
import de.uka.ipd.consensus.foundation.schema.TypeInstance;
import de.uka.ipd.consensus.foundation.schema.User;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class TopicBean {
	
	private static final int MAX_LEVEL = 2;

	private Topic        topic;
	private TypeInstance typeInstance;
	private Collection   instances = Collections.EMPTY_SET;
	
	
	public TopicBean(User user, Topic topic) {
		this( user, topic, null, 0 );
	}


	private TopicBean(User user, Topic topic, TypeInstance typeInstance, int level) {
		this.topic        = topic;
		this.typeInstance = typeInstance;
		
		Collection c = topic.getInstances( user );

		if (level <= MAX_LEVEL) {
			if (!c.isEmpty()) {
				instances = new ArrayList(30);
				
				Iterator it = c.iterator();
				
				while (it.hasNext()) {
					TypeInstance ti = (TypeInstance)it.next();

					TopicBean tb = new TopicBean( user, ti.getInstance(), ti, level+1 );
					
					instances.add( tb );
				}
			}
		}
	}
	
	
	public Topic getTopic() {
		return this.topic;
	}


	public TypeInstance getTypeInstance() {
		return typeInstance;
	}


	/**
	 * @return a collection of instances of the topic as TopicBean objects (!)
	 */
	public Collection getInstances() {
		return this.instances;
	}

}
