//
// https://www.ramecera.it/myscrewdriver.html
// e-mail: screwdriver@ramecera.it
//
/* This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html */
package it.ramecera.screwdriver.component;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.screwdriver.CandyScrew;
import it.ramecera.screwdriver.Cst;
import it.ramecera.screwdriver.Markup;
import it.ramecera.screwdriver.Skill;
import it.ramecera.screwdriver.Train;
import it.ramecera.screwdriver.Wagon;
import it.ramecera.screwdriver.outfits.JspBeanElement;

/** @author M. Maglioni */
final public class JspPlain_java extends FileComponent {

	/** @param skill application main data
	 * @param snippetG name of the file containing the snippet */
	public JspPlain_java(Skill skill, String snippetG) {
		super();
		this.skill = skill;
		this.snippetG = snippetG;
	}

	/** @throws ExactException exception specified by a numeric code */
	@Override
	public void synthesis(HashMap<String, Markup> markupE,
			List<Train> trainE, CandyScrew candies)

			throws FileNotFoundException, IOException, ExactException {

		producedMaterial = "";
		producedMaterial += "package " + skill.pckg + ".web.model."
				+ jspKit.path + ";" + Candies.LF + Candies.LF;
		producedMaterial += "import java.text.DecimalFormat;"
				+ Candies.LF;
		producedMaterial += "import javax.servlet.http.HttpServletRequest;"
				+ Candies.LF;
		producedMaterial += "import " + skill.pckg
				+ ".web.model.GenericModel;" + Candies.LF;
		if (jspKit.arrayStringPropertyE.size() > 0) {
			producedMaterial += "import java.util.ArrayList;"
					+ Candies.LF;
		}
		for (Train t : markupE.get("hdefrail").recurrenceE) {
			producedMaterial += "import " + skill.networkDomain
					+ ".screwdriver." + t.wagonE.get("hjndi").text
							.toLowerCase() + "." + t.wagonE.get(
									"hdbschema").text + ".*;"
					+ Candies.LF;
		}
		if (jspKit.methodE.size() > 0) {
			producedMaterial += "import org.springframework.dao."
					+ "DataAccessException;" + Candies.LF;
		}
		if (jspKit.methodE.size() > 0) {
			for (Train t : markupE.get("hdefrail").recurrenceE) {
				producedMaterial += "import " + skill.networkDomain
						+ "." + skill.applicationNameLower + "."
						+ Cst.RAIL + t.locomotor.text + ".dao.*;"
						+ Candies.LF;
			}
		}
		if (skill.log4jVers != null) {
			producedMaterial += "import org.apache.log4j.Logger;"
					+ Candies.LF + Candies.LF;
		}
		if (jspKit.methodE.size() > 0) {
			producedMaterial += "abstract ";
		}
		producedMaterial += "public class " + jspKit.nomeFileU
				+ Cst.PLAINU + " extends GenericModel {" + Candies.LF
				+ Candies.LF;
		if (jspKit.stringPropertyE.size() > 0) {
			for (JspBeanElement x : jspKit.stringPropertyE) {
				producedMaterial += Candies.TABS
						+ "/** proprietà del bean */" + Candies.LF;
				producedMaterial += Candies.TABS + "private "
						+ x.datumType + " " + x.name + ";" + Candies.LF;
			}
		}
		if (jspKit.arrayStringPropertyE.size() > 0) {
			for (JspBeanElement x : jspKit.arrayStringPropertyE) {
				producedMaterial += Candies.TABS
						+ "/** proprietà del bean */" + Candies.LF;
				producedMaterial += Candies.TABS + "private "
						+ x.datumType + " " + x.name + ";" + Candies.LF;
			}
		}
		if (jspKit.integerPropertyE.size() > 0) {
			for (JspBeanElement x : jspKit.integerPropertyE) {
				producedMaterial += Candies.TABS
						+ "/** proprietà del bean */" + Candies.LF;
				producedMaterial += Candies.TABS + "private "
						+ x.datumType + " " + x.name + ";" + Candies.LF;
			}
		}
		if (jspKit.longPropertyE.size() > 0) {
			for (JspBeanElement x : jspKit.longPropertyE) {
				producedMaterial += Candies.TABS
						+ "/** proprietà del bean */" + Candies.LF;
				producedMaterial += Candies.TABS + "private "
						+ x.datumType + " " + x.name + ";" + Candies.LF;
			}
		}
		if (jspKit.booleanPropertyE.size() > 0) {
			for (JspBeanElement x : jspKit.booleanPropertyE) {
				producedMaterial += Candies.TABS
						+ "/** proprietà del bean */" + Candies.LF;
				producedMaterial += Candies.TABS + "private "
						+ x.datumType + " " + x.name + ";" + Candies.LF;
			}
		}
		if (skill.log4jVers != null) {

			producedMaterial += Candies.LF + Candies.TABS
					+ "/** ordinary logger */" + Candies.LF;
			producedMaterial += Candies.TABS
					+ "@SuppressWarnings(\"unused\")" + Candies.LF;
			producedMaterial += Candies.TABS
					+ "private static final Logger logger = Logger.getLogger("
					+ Candies.LF + Candies.TABS + Candies.TABS
					+ Candies.TABS + Mix.uppercaseFirstChar(
							jspKit.fileName) + Cst.PLAINU + ".class);"
					+ Candies.LF + Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.stringPropertyE) {
			producedMaterial += Candies.TABS
					+ "/** standard get method */" + Candies.LF;
			producedMaterial += Candies.TABS + "public " + jbe.datumType
					+ " get" + jbe.nameU + "() {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return this." + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument "
					+ jbe.datumType + " */" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(" + jbe.datumType + " s) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = s;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument Integer */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final Integer i) {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = " + jbe.datumType + ".valueOf(i);"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** set formattato, argomento Integer */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final Integer i, final String f) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = new DecimalFormat(f).format(i);"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument Short */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final Short i) {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = " + jbe.datumType + ".valueOf(i);"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** set formattato, argomento Short */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final Short i, final String f) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = new DecimalFormat(f).format(i);"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** set formattato, argomento Long */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final Long i, final String f) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = new DecimalFormat(f).format(i);"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.integerPropertyE) {
			producedMaterial += Candies.TABS
					+ "/** standard get method */" + Candies.LF;
			producedMaterial += Candies.TABS + "public " + jbe.datumType
					+ " get" + jbe.nameU + "() {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return this." + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument "
					+ jbe.datumType + " */" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(" + jbe.datumType + " s) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = s;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.longPropertyE) {
			producedMaterial += Candies.TABS
					+ "/** standard get method */" + Candies.LF;
			producedMaterial += Candies.TABS + "public " + jbe.datumType
					+ " get" + jbe.nameU + "() {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return this." + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument "
					+ jbe.datumType + " */" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(final " + jbe.datumType + " s) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = s;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.booleanPropertyE) {
			producedMaterial += Candies.TABS
					+ "/** standard get method */" + Candies.LF;
			producedMaterial += Candies.TABS + "public " + jbe.datumType
					+ " is" + jbe.nameU + "() {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return this." + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument "
					+ jbe.datumType + " */" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(" + jbe.datumType + " s) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = s;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.arrayStringPropertyE) {
			producedMaterial += Candies.TABS
					+ "/** standard get method */" + Candies.LF;
			producedMaterial += Candies.TABS + "public " + jbe.datumType
					+ " get" + jbe.nameU + "() {" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return this." + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS
					+ "/** standard set method, argument "
					+ jbe.datumType + " */" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(" + jbe.datumType + " s) {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = s;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
		}
		/* abstract method declaration */
		for (Wagon d : jspKit.methodE) {
			producedMaterial += Candies.LF + "/** additional method */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "abstract public void "
					+ d.text + "(";
			producedMaterial += "HttpServletRequest request";
			if (!skill.methodE.get(d.text).upperArguments.trim()
					.isEmpty()) {
				producedMaterial += ", ";
			}
			producedMaterial += skill.methodE.get(
					d.text).upperArguments;
			for (int cJ = 0; cJ < skill.methodE.get(d.text).classE
					.size(); cJ++) {
				producedMaterial += ", ";
				producedMaterial += skill.methodE.get(d.text).classE
						.get(cJ) + " other" + cJ;
			}
			producedMaterial += ");" + Candies.LF;
		}
		producedMaterial += Candies.TABS + "/* " + Cst.KNIFE + " */"
				+ Candies.LF;
		producedMaterial += "}" + Candies.LF;
	}

	/* written only to comply with the abstract class FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	/** @param markupE the list of markups
	 * @param skill together with the characteristics of the application
	 * @param trash file where the deleted files are moved
	 * @throws ExactException exception specified by a numeric code */
	public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

		skill.javaWebModelY = skill.javaWebY + "/model";
		skill.buildDirectory(skill.javaWebModelY);
		skill.javaWebModelBodyY = skill.javaWebModelY + "/"
				+ jspKit.path;
		skill.buildDirectory(skill.javaWebModelBodyY);
	}
}