/*
 * Created on 03-08-2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package net.sf.hibern8ide.node;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.metadata.ClassMetadata;
import net.sf.hibernate.metadata.CollectionMetadata;
import net.sf.hibernate.type.Type;

/**
 * @author MAX
 */
public class NodeFactory {

	SessionFactory sf;
	private Map classMetaData;
	private ArrayList classes;
	private Map collectionMetaData;

	/**
	 * @param sf2
	 */
	public NodeFactory(SessionFactory sf2) throws HibernateException {
		setSessionFactory(sf2);		
	}

	void setSessionFactory(SessionFactory sf) throws HibernateException {
		classMetaData = sf.getAllClassMetadata();
        collectionMetaData = sf.getAllCollectionMetadata();
		Set keyset = classMetaData.keySet();
		classes = new ArrayList();
		classes.addAll(keyset);
	}

    public BaseNode createRootNode() {
        return new RootNode(this, classes);        
    }

	public BaseNode createClassNode(BaseNode node, Class clazz) {
		return new ClassNode(this, node, clazz, getMetaData(clazz));
	}

	public ClassMetadata getMetaData(Class clazz) {
		Object o = classMetaData.get(clazz);
		return (ClassMetadata) o;
	}

    /**
     * @param string
     */
     public CollectionMetadata getCollectionMetaData(String role) {
        return (CollectionMetadata) collectionMetaData.get(role);        
     }
     
	public BaseNode createPropertyNode(BaseNode parent, int idx, ClassMetadata metadata) {
		return new PropertyNode(this, parent, idx, metadata);
	}

	/**
	 * @param node
	 * @param md
	 * @return
	 */
	public Object createIdentifierNode(BaseNode parent, ClassMetadata md) {
		return new IdentifierNode(this, parent, md);
	}

	public TypeNode createTypeNode(ClassNode node, Type type) {		
		return new TypeNode(this, node, type, getMetaData(type.getReturnedClass()));
	}

	

}
