package com.fp.hbm.bgenerator.xml;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.fp.hbm.bgenerator.AbstractColumn;
import com.fp.common.exception.APPException;

public class MapperColumn extends AbstractColumn {

	public MapperColumn() {
		super();
	}

	public String formatProperty() throws APPException {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		if (this.getComments() != null) {
			pw.println("<!-- "
					+ this.getComments().toLowerCase().replaceAll("á", "a").replaceAll("é", "e").replaceAll("í", "i").replaceAll("ó", "o")
							.replaceAll("ú", "u").replaceAll("ñ", "n").toUpperCase() + "-->");
		}
		String aditional = "";
		if(this.fiedBeanReference!=null){
			aditional+=" insert='"+(this.fiedBeanReference.getCaninsert().compareTo("1")==0)+"'";
			aditional+=" update='"+(this.fiedBeanReference.getCanupdate().compareTo("1")==0)+"'";
		}
		pw.println("\t<property name='" + this.name.toLowerCase() + "' column='" + this.name.toUpperCase() + "' type='" + getJavaDataType()
				+ "'  not-null='" + !this.isNullable() + "' " + aditional + "/>");

		String data = sw.toString();
		pw.close();
		return data;
	}

	public String formatJava() throws APPException {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		pw.println("/**");
		pw.println("* " + this.getComments());
		pw.println("*/");
		name = name.toLowerCase();
		pw.println("private " + getJavaSimple() + " " + this.name + ";");
		String data = sw.toString();
		pw.close();
		return data;
	}

	public String formatComposeKey() throws APPException {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		if (this.getComments() != null) {
			pw.println("<!-- "
					+ this.getComments().toLowerCase().replaceAll("á", "a").replaceAll("é", "e").replaceAll("á", "i").replaceAll("ó", "o")
							.replaceAll("ú", "u").replaceAll("ñ", "n").toUpperCase() + "-->");
		}
		pw.println("\t<key-property name='" + this.name.toLowerCase() + "' column='" + this.name.toUpperCase() + "' type='" + getJavaDataType()
				+ "'/>");

		String data = sw.toString();
		pw.close();
		return data;
	}

	public void formatSinglePK(PrintWriter pw) throws APPException {
		pw.println(" <id name='pk' column='" + this.name.toUpperCase() + "' type='" + this.getJavaDataType() + "'>");
		pw.println("     <generator class='assigned'/>");
		pw.println(" </id>");
	}

}
