/*
 * 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.List;

import javax.swing.tree.TreeNode;

import net.sf.hibernate.metadata.ClassMetadata;
import net.sf.hibernate.metadata.CollectionMetadata;
import net.sf.hibernate.type.AssociationType;
import net.sf.hibernate.type.PersistentCollectionType;
import net.sf.hibernate.type.Type;

/**
 * @author MAX
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ClassNode extends BaseNode {

	ClassMetadata md;
    Class clazz;
    
	public ClassNode(NodeFactory factory, BaseNode parent, Class clazz, ClassMetadata metadata) {
		super(factory, parent);
        name = clazz.getName();
    
		md = metadata;
		if (md != null) { // Don't have any hibernate related info about this one...
            if(metadata.getIdentifierPropertyName()!=null) children.add(null);
			String[] names = metadata.getPropertyNames();
			for (int i = 0; i < names.length; i++) {
				children.add(null);
			}
            icon = mappedClassIcon;
		} else {
            icon = unmappedClassIcon;      
        }
	}

	public TreeNode getChildAt(int childIndex) {
		if (children.get(childIndex) == null) {
			createChildren();
		}
		return super.getChildAt(childIndex);
	}

	private void createChildren() {
		System.out.println("Creating children for: " + this);
        int offset=0;
        // Identifier
        if(md.getIdentifierPropertyName()!=null) {
            children.set(0, factory.createIdentifierNode(this, md));
            offset++;
        }
        
        String[] names = md.getPropertyNames();
        for (int i = 0; i < names.length; i++) {
            Type type = md.getPropertyTypes()[i];
			
            if(type.isPersistentCollectionType()) {
                CollectionMetadata cm = factory.getCollectionMetaData(((PersistentCollectionType)type).getRole());
                TypeNode tn = factory.createTypeNode(this, cm.getElementType());
                tn.setName(names[i]);
                children.set(i+offset, tn);   
            } else {
                children.set(i+offset, factory.createPropertyNode(this, i, md));
            }
            
        }        		
	}	

	/* (non-Javadoc)
	 * @see net.sf.hibern8ide.BaseNode#getHQL()
	 */
	public String getHQL() {

		List parents = new ArrayList();
        
        BaseNode currentParent;
          currentParent = this;
          while (currentParent != null && !(currentParent instanceof RootNode)) {
                      parents.add(currentParent);
                      currentParent = currentParent.parent;
             }
		
        if(currentParent instanceof RootNode) {
            currentParent = (BaseNode) parents.get(parents.size()-1);
          }
        
        // currentParent is the root
        String cname = ((ClassNode)currentParent).md.getMappedClass().getName();
        
		if (cname.lastIndexOf(".") != -1) {
			cname = cname.substring(cname.lastIndexOf(".") + 1);
		}
		String alias = cname.toLowerCase();

		String path = "";
		for (int i = parents.size()-2; i >= 0; i--) {
			path += "." + ((BaseNode) parents.get(i)).getName();
		}

		return "select " + alias + path + " from " + cname + " as " + alias;
	}

}
