package com.fp.hbm.bgenerator;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;


import com.fp.persistence.commondb.exception.CommondbException;
import com.fp.hbm.helper.PersistenceHelperHQL;
import com.fp.hbm.persistence.TgeneEntity;
import com.fp.hbm.persistence.TgeneEntityKey;
import com.fp.hbm.persistence.TgeneFields;

public class Reference {
	private TgeneFields fieldReference;
	private TgeneEntity reference;
	private String basePac;
    private List<String> cols=new ArrayList<String>();
	public Reference(TgeneFields pFields,String pBasePac) throws HibernateException, CommondbException {
		this.basePac=pBasePac;
		this.fieldReference = pFields;
		this.reference=(TgeneEntity)PersistenceHelperHQL.get(TgeneEntity.class, new TgeneEntityKey(fieldReference.getFktname(),fieldReference.getFkentity()));
		this.fillCols();
	}
	@SuppressWarnings("unchecked")
	private void fillCols() throws HibernateException, CommondbException{
		String sql="";
		if(this.fieldReference.getType().compareTo("S")==0){
		sql="select t.column_name as col from user_cons_columns  t " +
				"where t.constraint_name = (select constraint_name " +
				"from user_constraints t2 " +
				"where t2.table_name=:table and t2.constraint_type='R' " +
				"and t2.r_constraint_name =( " +
				"select constraint_name from user_constraints t1 where t1.table_name=:fk and t1.constraint_type='P')) order by t.position";
		 SQLQuery qry=PersistenceHelperHQL.getSession().createSQLQuery(sql);
	        qry.setString("fk", fieldReference.getFktname());
	        qry.setString("table", fieldReference.getPk().getTname());
	        cols=(List<String>)qry.list();
		}else{
			if(fieldReference.getFkconstraint()==null){
				throw new CommondbException("MP002","Se debe definir el nombre del constraint a utilizar "+fieldReference.getPk().getField());
			}
			sql="select t.column_name as col from user_cons_columns  t  where t.constraint_name =:cons   order by t.position";
			 SQLQuery qry=PersistenceHelperHQL.getSession().createSQLQuery(sql);
		        qry.setString("cons", fieldReference.getFkconstraint());
		        cols=(List<String>)qry.list();
		}

	}

	public TgeneFields getFieldReference() {
		return fieldReference;
	}
	public List<String> getCols() {
		return cols;
	}
	public List<String> getImport(){
		List<String> data=new ArrayList<String>();
		data.add("import "+this.basePac+"."+this.reference.getPac()+"."+this.reference.getPk().getEntity()+";");
		if(this.fieldReference.getType().compareTo("M")==0){
			data.add("import java.util.Set;");
		}
		return data;
	}
	public String getJava(){
		String data="";
		String name=this.fieldReference.getPk().getField();
		name=name.substring(0,1).toUpperCase()+name.substring(1);
		if(this.fieldReference.getType().compareTo("S")==0){
			data+="private "+this.reference.getPk().getEntity()+" "+this.fieldReference.getPk().getField()+";\n";
			data+="public "+this.reference.getPk().getEntity()+" get"+name+"(){\n";
			data+="  return "+this.fieldReference.getPk().getField()+";\n";
			data+="}\n";
			data+="public void set"+name+"("+this.reference.getPk().getEntity()+" p"+name+"){\n";
			data+="  "+this.fieldReference.getPk().getField()+"=p"+name+";\n";
			data+="}\n";
		}else{
			data+="private Set<"+this.reference.getPk().getEntity()+"> "+this.fieldReference.getPk().getField()+";\n";
			data+="public Set<"+this.reference.getPk().getEntity()+"> get"+name+"(){\n";
			data+="  return "+this.fieldReference.getPk().getField()+";\n";
			data+="}\n";
			data+="public void set"+name+"(Set<"+this.reference.getPk().getEntity()+"> p"+name+"){\n";
			data+="  "+this.fieldReference.getPk().getField()+"=p"+name+";\n";
			data+="}\n";
		}
		return data;
	}
	public String getXML(){
		String data="";
		String name=this.fieldReference.getPk().getField();
		name=name.substring(0,1).toUpperCase()+name.substring(1);
		if(this.fieldReference.getType().compareTo("S")==0){
			data+="<many-to-one name='"+this.fieldReference.getPk().getField()+"' entity-name='"+this.basePac+"."+this.reference.getPac()+"."+this.reference.getPk().getEntity()+"' insert='false' update='false' >\n";
			for (String f : this.cols) {
				data+=   " <column name='"+f+"' />\n";
			}
			data+=   "</many-to-one>\n";
		}else{
			data+="<set name='"+this.fieldReference.getPk().getField()+"' lazy='true' >\n";
			data+="<key>\n";
			for (String f : this.cols) {
				data+=   " <column name='"+f+"' />\n";
			}
			data+="</key>\n";
			data+="<one-to-many class='"+this.basePac+"."+this.reference.getPac()+"."+this.reference.getPk().getEntity()+"'/>\n";
			data+="</set>";
		}
		return data;
	}
}
