//
// 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.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;
import it.ramecera.screwdriver.utilities.ValidConstScrew;

/**
 * <p>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-06-20<br>
 * Company: Ramecera™
 * </p>
 * 
 * @author Massimo Maglioni
 *         <p>
 *         Description:
 *         </p>
 */
final public class JspForm_java extends FileComponent {

	/** @param skill application main data
	 * @param snippetG name of the file containing the snippet */
	public JspForm_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 candyScrew)

			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;
		if (jspKit.arrayStringPropertyE.size() > 0) {
			producedMaterial += "import java.util.List;" + Candies.LF;
			producedMaterial += "import java.util.ArrayList;"
					+ Candies.LF;
		}
		producedMaterial += "import  " + skill.pckg
				+ ".web.model.GenericModel;" + Candies.LF;
		if (jspKit.controlE.size() > 0) {
			producedMaterial += "import java.util.ArrayList;"
					+ Candies.LF;
			producedMaterial += "import java.util.List;" + 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 (skill.log4jVers != null) {
			producedMaterial += "import org.apache.log4j.Logger;"
					+ Candies.LF + 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 (jspKit.methodE.size() > 0) {
			producedMaterial += "abstract ";
		}
		producedMaterial += "public class " + jspKit.nomeFileU
				+ Cst.FORMU + " extends GenericModel {" + Candies.LF
				+ Candies.LF;

		if (skill.log4jVers != null) {
			producedMaterial += 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 + jspKit.nomeFileU + Cst.FORMU
					+ ".class);" + Candies.LF + Candies.LF;
		}
		for (JspBeanElement jbe : jspKit.integerPropertyE) {
			producedMaterial += Candies.TABS + "/** bean property */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ jbe.datumType + " " + jbe.name + ";" + Candies.LF
					+ Candies.LF;
			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;
		}
		for (JspBeanElement jbe : jspKit.longPropertyE) {
			producedMaterial += Candies.TABS + "/** bean property */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ jbe.datumType + " " + jbe.name + ";" + Candies.LF
					+ Candies.LF;
			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;

			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;
		}
		for (JspBeanElement jbe : jspKit.booleanPropertyE) {
			producedMaterial += Candies.TABS + "/** bean property */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ jbe.datumType + " " + jbe.name + ";" + Candies.LF
					+ Candies.LF;
			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;
		}
		for (JspBeanElement jbe : jspKit.stringPropertyE) {
			producedMaterial += Candies.TABS + "/** bean property */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ jbe.datumType + " " + jbe.name + ";" + Candies.LF
					+ Candies.LF;
			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;
		}
		for (JspBeanElement jbe : jspKit.arrayStringPropertyE) {
			producedMaterial += Candies.TABS + "/** bean property */"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ jbe.datumType + " " + jbe.name + ";" + Candies.LF
					+ Candies.LF;
			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;
		}
		for (int j = 0; j < jspKit.controlE.size(); j++) {
			JspBeanElement jbe = jspKit.controlE.get(j);
			if (!jbe.controlType.equals(Cst.TEXT_BOX)) {
				continue;
			}
			producedMaterial += Candies.LF;
			for (ValidConstScrew vc : jbe.validConstE) {
				producedMaterial += Candies.TABS + vc.valcons.text
						+ "(";
				if (vc.valparams != null) {
					producedMaterial += vc.valparams.text + ", ";
				}
				producedMaterial += "message = \"{" + vc.valtag.text
						+ "}\")" + Candies.LF;
			}
			stringProperty(jbe.name, jbe.nameU);
		}
		for (int j = 0; j < jspKit.controlE.size(); j++) {
			JspBeanElement jbe = jspKit.controlE.get(j);
			if (!jbe.controlType.equals(Cst.TEXT_AREA)) {
				continue;
			}
			producedMaterial += Candies.LF;
			for (ValidConstScrew vc : jbe.validConstE) {
				producedMaterial += Candies.TABS + vc.valcons.text
						+ "(";
				if (vc.valparams != null) {
					producedMaterial += vc.valparams.text + ", ";
				}
				producedMaterial += "message = \"{" + vc.valtag.text
						+ "}\")" + Candies.LF;
			}
			stringProperty(jbe.name, jbe.nameU);
		}
		for (int j = 0; j < jspKit.controlE.size(); j++) {
			JspBeanElement jbe = jspKit.controlE.get(j);
			if (!jbe.controlType.equals(Cst.LIST_BOX)) {
				continue;
			}
			producedMaterial += Candies.LF;
			if (jbe.ftokens != null) {
				producedMaterial += Candies.TABS
						+ "private List<Object> arr_" + jbe.name + ";"
						+ Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS
						+ "public List<Object> getArr_" + jbe.name
						+ "() {" + Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "return arr_" + jbe.name + ";" + Candies.LF;
				producedMaterial += Candies.TABS + "}" + Candies.LF;
				producedMaterial += Candies.TABS + "public void setArr_"
						+ jbe.name + "(List<Object> arr_" + jbe.name
						+ ") {" + Candies.LF;
			} else if (jbe.hsqln.valutazioneColonne == 1) {
				producedMaterial += Candies.TABS
						+ "private List<Object> arr_" + jbe.name + ";"
						+ Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS
						+ "public List<Object> getArr_" + jbe.name
						+ "() {" + Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "return arr_" + jbe.name + ";" + Candies.LF;
				producedMaterial += Candies.TABS + "}" + Candies.LF;
				producedMaterial += Candies.TABS + "public void setArr_"
						+ jbe.name + "(List<Object> arr_" + jbe.name
						+ ") {" + Candies.LF;
			} else {
				producedMaterial += Candies.TABS
						+ "private List<Object[]> arr_" + jbe.name + ";"
						+ Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS
						+ "public List<Object[]> getArr_" + jbe.name
						+ "() {" + Candies.LF + Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "return arr_" + jbe.name + ";" + Candies.LF;
				producedMaterial += Candies.TABS + "}" + Candies.LF;
				producedMaterial += Candies.TABS + "public void setArr_"
						+ jbe.name + "(List<Object[]> arr_" + jbe.name
						+ ") {" + Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ "this.arr_" + jbe.name + " = arr_" + jbe.name
					+ ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
			stringProperty("selected", "Selected");
		}
		for (int j = 0; j < jspKit.controlE.size(); j++) {
			JspBeanElement jbe = jspKit.controlE.get(j);
			if (!jbe.controlType.equals(Cst.CHECK_BOX)) {
				continue;
			}
			producedMaterial += Candies.LF;
			for (ValidConstScrew vc : jbe.validConstE) {
				producedMaterial += Candies.TABS + vc.valcons.text
						+ "(";
				if (vc.valparams != null) {
					producedMaterial += vc.valparams.text + ", ";
				}
				producedMaterial += "message = \"{" + vc.valtag.text
						+ "}\")" + Candies.LF;
			}
			producedMaterial += Candies.TABS + "private boolean "
					+ jbe.name + ";" + Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + "public boolean is"
					+ jbe.nameU + "() {" + Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "return "
					+ jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(boolean " + jbe.name + ") {"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = " + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;
		}
		for (int j = 0; j < jspKit.controlE.size(); j++) {
			JspBeanElement jbe = jspKit.controlE.get(j);
			if (!jbe.controlType.equals(Cst.RADIO_BUTTON)) {
				continue;
			}
			producedMaterial += Candies.LF;
			producedMaterial += Candies.TABS + "private Integer "
					+ jbe.name + ";" + Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + "public Integer get"
					+ jbe.nameU + "() {" + Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "return "
					+ jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;
			producedMaterial += Candies.TABS + "public void set"
					+ jbe.nameU + "(Integer " + jbe.name + ") {"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "this."
					+ jbe.name + " = " + jbe.name + ";" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + 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;
	}

	private void stringProperty(final String name, final String nameU) {
		producedMaterial += Candies.TABS + "private String " + name
				+ ";" + Candies.LF + Candies.LF;
		producedMaterial += Candies.TABS + "public String get" + nameU
				+ "() {" + Candies.LF + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "return "
				+ name + ";" + Candies.LF;
		producedMaterial += Candies.TABS + "}" + Candies.LF;
		producedMaterial += Candies.TABS + "public void set" + nameU
				+ "(String " + name + ") {" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "this." + name
				+ " = " + name + ";" + Candies.LF;
		producedMaterial += Candies.TABS + "}" + 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);
	}
}
