//
// 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.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.salad.everyday.Strings;
import it.ramecera.salad.file.Equipment;
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.outfits.JspKit;
import it.ramecera.screwdriver.outfits.RequestMap;
import it.ramecera.screwdriver.outfits.TableOutfit;

/**
 * <p>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-09-03<br>
 * Company: Ramecera™<br>
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * <p>
 * History: 2020-08-03 implementation of N13 (see
 * martoraProject.ods)<br>
 * 2020-09-03 implementation of N3 (see martoraProject.ods)
 * </p>
 * 
 * @author Massimo Maglioni */
final public class ControllerBelong_java extends FileComponent {

	/* stored materials */
	private String impForm = "";
	private String s = "";
	private String aws = "";

	/** ordinary logger */
	@SuppressWarnings("unused")
	private static final Logger logger = Logger.getLogger(
			ControllerBelong_java.class);

	/** @param skill together with the descriptive data of the
	 * @param snippetG application */
	public ControllerBelong_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 {

		String plainControllerG = skill.candies.snippetsYS + snippetG;
		producedMaterial = Equipment.oneString(plainControllerG)
				+ Candies.LF;
		producedMaterial += "import org.springframework.web.servlet.ModelAndView;"
				+ Candies.LF;
		producedMaterial += "import java.util.Map;" + Candies.LF;
		producedMaterial += "import java.util.List;" + Candies.LF;
		producedMaterial += "import java.util.ArrayList;" + Candies.LF;
		producedMaterial += "import org.hibernate.HibernateException;"
				+ Candies.LF;
		producedMaterial += "import javax.servlet.http.HttpServletRequest;"
				+ Candies.LF;
		producedMaterial += "+importModel+";
		producedMaterial += "+importForm+";
		producedMaterial += "+importPlain+";
		String impMod = "";
		impMod += "import org.springframework.ui.Model;" + Candies.LF;
		impMod += "import org.springframework.web.bind.annotation.ModelAttribute;"
				+ Candies.LF;
		impForm += "import org.springframework.web.bind.annotation"
				+ ".RequestMethod;" + Candies.LF;
		impForm += "import org.springframework.web.servlet.mvc.support"
				+ ".RedirectAttributes;" + Candies.LF;
		impForm += "import org.springframework.dao.DataAccessException;"
				+ Candies.LF;
		impForm += "import org.springframework.validation.BindingResult;"
				+ Candies.LF;
		if (skill.hiberValid | skill.springValid) { // v 1.10.2
			impForm += "import javax.validation.Valid;" + Candies.LF;
		}
		impForm += Candies.LF;
		impForm += "import java.sql.Types;" + Candies.LF;
		impForm += "import java.sql.Timestamp;" + Candies.LF;
		impForm += "import java.util.Date;" + Candies.LF;
		impForm += "import java.text.SimpleDateFormat;" + Candies.LF;
		for (Train t : markupE.get("hdefrail").recurrenceE) {
			impForm += "import " + skill.networkDomain + ".screwdriver."
					+ t.wagonE.get("hjndi").text.toLowerCase() + "."
					+ t.wagonE.get("hdbschema").text + ".*;"
					+ Candies.LF;
		}
		producedMaterial += "+importAutowired+";
		String impAuto = "";
		if (skill.hasUseridPassword) {
			impAuto += "import " + skill.networkDomain + "."
					+ skill.applicationNameLower
					+ ".web.authentication.UserContext;" + Candies.LF;
			if (skill.authenticationMode.equals("trivial")) {
				impAuto += "import " + skill.networkDomain + "."
						+ skill.applicationNameLower
						+ ".web.authentication.KingDao" + ";"
						+ Candies.LF;
				impAuto += "import org.springframework.beans.factory"
						+ ".annotation.Autowired;" + Candies.LF;
			} else {
				impAuto += "import " + skill.networkDomain + "."
						+ skill.applicationNameLower + "." + Cst.RAIL
						+ skill.tabUser[1] + ".service.I" + Cst.RAILU
						+ skill.tabUser[1] + skill.tabUser[0]
						+ "Service" + ";" + Candies.LF;
			}
		}
		String impPlain = "";
		RequestMap zW = null;
		/* the 5 elements of the request mapping: z [0] =
		 * requestMap.map; z [1] = requestMap.pathJsp; z [2] =
		 * requestMap.jsp; z [3] = requestMap.type; z [4] =
		 * requestMap.path; */
		/* JSP type: 0 = plain; 1 = form; 2 = model */
		boolean elencoImport = false;
		for (int i = 0; i < skill.correlationE.get(skill.cntK).rmE
				.size(); i++) {
			RequestMap z = skill.correlationE.get(skill.cntK).rmE.get(
					i);
			if (z.tipo == Cst.MODEL) {
				if (markupE.get("xprovider").recurrenceE.get(
						0).locomotor.text.equalsIgnoreCase("ldap")
						& !elencoImport) {
					producedMaterial += Equipment.oneString(
							candyScrew.snippetsYS
									+ "java/ControllerBelongLDAP.java");
					elencoImport = true;
				}
			} else if (zW == null || !equivalente(z, zW)) {
				String f = Mix.uppercaseFirstChar(z.jsp)
						+ Cst.TIPO_JSPE[z.tipo];
				producedMaterial += "import +zoccolo+.web.model."
						+ z.path + "." + f + ";" + Candies.LF;
				producedMaterial += "import +zoccolo+.web.model."
						+ z.path + "." + f + "Impl;" + Candies.LF;
			}
			zW = z.clone();
		}
		if (skill.ldapService) {
			producedMaterial += "import org.springframework.security.core."
					+ "userdetails.UserDetailsService;" + Candies.LF;
			producedMaterial += "import org.springframework.beans.factory."
					+ "annotation.Autowired;" + Candies.LF;
		}
		if (skill.log4jVers != null) {
			producedMaterial += "import org.apache.log4j.Logger;"
					+ Candies.LF + Candies.LF;
		}

		producedMaterial += Candies.LF + "@Controller" + Candies.LF;
		producedMaterial += "@RequestMapping(\"/" + skill.correlationE
				.get(skill.cntK).gruppo + "\")" + Candies.LF;
		producedMaterial += "public class " + skill.groupControllerF
				+ " {" + Candies.LF + Candies.LF;
		producedMaterial += "+autowireds+" + Candies.LF;
		if (skill.hasUseridPassword) {
			producedMaterial += Candies.TABS
					+ "private final UserContext userContext;"
					+ Candies.LF;
			if (skill.authenticationMode.equals("trivial")) {
				producedMaterial += Candies.TABS
						+ "private final KingDao holder;" + Candies.LF
						+ Candies.LF;
			} else {
				producedMaterial += Candies.TABS + "private final I"
						+ Cst.RAILU + skill.tabUser[1]
						+ skill.tabUser[0] + "Service holder;"
						+ 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 + skill.groupControllerF + ".class);"
					+ Candies.LF + Candies.LF;
		}
		/* for the next for I first accumulate all the material in s,
		 * then only at the end I transfer s */
		boolean accountController = false;
		boolean autowired = false;
		if (skill.hasUseridPassword) {

			// constructor

			s += Candies.TABS + "@Autowired" + Candies.LF;
			if (skill.authenticationMode.equals("trivial")) {
				s += Candies.TABS + "public " + skill.groupControllerF
						+ "(UserContext userContext," + Candies.LF
						+ Candies.TABS + Candies.TABS
						+ "KingDao holder) {" + Candies.LF;
			} else {
				s += Candies.TABS + "public " + skill.groupControllerF
						+ "(UserContext userContext," + Candies.LF
						+ Candies.TABS + Candies.TABS + "I" + Cst.RAILU
						+ skill.tabUser[1] + skill.tabUser[0]
						+ "Service holder) {" + Candies.LF;
			}
			s += Candies.TABS + Candies.TABS
					+ "if (userContext == null) {" + Candies.LF;
			s += Candies.TABS + Candies.TABS + Candies.TABS
					+ "throw new IllegalArgumentException(" + Candies.LF
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "\"userContext cannot be null\");"
					+ Candies.LF;
			s += Candies.TABS + Candies.TABS + "}" + Candies.LF;
			s += Candies.TABS + Candies.TABS + "if (holder == null) {"
					+ Candies.LF;
			s += Candies.TABS + Candies.TABS + Candies.TABS
					+ "throw new IllegalArgumentException(" + Candies.LF
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "\"holder cannot be null\");"
					+ Candies.LF;
			s += Candies.TABS + Candies.TABS + "}" + Candies.LF;

			s += Candies.TABS + Candies.TABS
					+ "this.userContext = userContext;" + Candies.LF;
			s += Candies.TABS + Candies.TABS + "this.holder = holder;"
					+ Candies.LF;
			s += Candies.TABS + "}" + Candies.LF;
		}
		for (int i = 0; i < skill.correlationE.get(skill.cntK).rmE
				.size(); i++) {
			RequestMap rm = skill.correlationE.get(skill.cntK).rmE.get(
					i);
			String idJsp = rm.pathJsp + ".jsp";
			int z = 0;
			List<Train> tE = markupE.get("jsp").recurrenceE;
			for (; z < tE.size(); z++) {
				if (tE.get(z).locomotor.text.equals(idJsp)) {
					break;
				}
			}
			if (z == tE.size()) {
				ExactException ne = new ExactException();
				ne.code = 2100;
				ne.datum = "I've not fount the JSP " + Mix.dress_A(
						idJsp) + ".";
				ne.className = this.getClass().getName();
				throw ne;
			}
			JspKit jspKit = (JspKit) tE.get(z).locomotor.substance;
			if (jspKit.crud & impForm.indexOf(".Notice") == -1) {
				impForm += "import " + skill.networkDomain + "."
						+ skill.applicationNameLower
						+ ".web.model.utility.Notice;" + Candies.LF;
			}
			s += Candies.LF;
			if (rm.tipo == Cst.PLAIN | rm.tipo == Cst.FORM) {
				s += Candies.TABS + "@RequestMapping(value = { ";
				for (int k = 0; k < rm.map.length; k++) {
					s += "\"" + Strings.replaceFirst(rm.map[k],
							skill.correlationE.get(skill.cntK).gruppo,
							"") + "\", ";
				}
				/* I remove the last comma-space pair */
				s = s.substring(0, s.length() - 2);
				s += " })" + Candies.LF;
				s += Candies.TABS + "public ModelAndView "
						+ skill.correlationE.get(skill.cntK).gruppoMinus
						+ "_" + i + "(Map<String, Object> model,"
						+ Candies.LF + Candies.TABS + Candies.TABS
						+ Candies.TABS + "HttpServletRequest request) {"
						+ Candies.LF + Candies.LF;
				s += Candies.TABS + Candies.TABS + rm.jspU
						+ Cst.JU[rm.tipo] + " " + rm.jsp
						+ Cst.JU[rm.tipo] + " = new " + rm.jspU
						+ Cst.JU[rm.tipo] + "Impl();" + Candies.LF;
				if (rm.tipo == Cst.FORM) {
					s += Candies.TABS + Candies.TABS + "model.put(\""
							+ rm.jsp + Cst.JU[rm.tipo] + "\", " + rm.jsp
							+ Cst.JU[rm.tipo] + ");" + Candies.LF;
					/* list-boxes process */
					for (int k = 0; k < jspKit.controlE.size(); k++) {
						JspBeanElement jbe = jspKit.controlE.get(k);
						/* I reject the others */
						if (!jbe.controlType.equals(Cst.LIST_BOX)) {
							continue;
						}
						if (jbe.dao != null & jbe.hsqln != null) {
							tableReading(jbe, rm);
						} else if (jbe.ftokens != null) {
							tokensReading(jbe, rm);
						}
					}
				}
				for (Wagon metodo : jspKit.methodE) {
					s += Candies.TABS + Candies.TABS + rm.jsp
							+ Cst.JU[rm.tipo] + "." + metodo.text
							+ "(request";
					if (!skill.methodE.get(metodo.text).lowerArguments
							.isEmpty()) {
						s += ", " + skill.methodE.get(
								metodo.text).lowerArguments;
					}
					for (int cJ = 0; cJ < skill.methodE.get(
							metodo.text).classE.size(); cJ++) {
						s += ", other" + cJ;
					}
					s += ");" + Candies.LF;
					for (TableOutfit tab : skill.methodE.get(
							metodo.text).tableE) {
						String interfaccia = "private I"
								+ tab.railUppercase + tab.entityNameU
								+ "Dao" + " " + tab.railLowercase
								+ tab.entityName + "Dao;";
						/* I have to avoid repetition */
						if (aws.indexOf(interfaccia) == -1) {
							aws += Candies.TABS + "@Autowired"
									+ Candies.LF;
							aws += Candies.TABS + interfaccia
									+ Candies.LF;
						}
					}
				}
				if (!aws.isEmpty() & !autowired) {
					for (Train train : markupE.get(
							"hdefrail").recurrenceE) {
						impAuto += "import " + skill.networkDomain + "."
								+ skill.applicationNameLower + "."
								+ Cst.RAIL + train.locomotor.text
								+ ".dao.*;" + Candies.LF;
					}
					if (impAuto.indexOf(".Autowired;") == -1) {
						impAuto += "import org.springframework.beans."
								+ "factory.annotation.Autowired;"
								+ Candies.LF;
					}
					autowired = true;
				}
				s += Candies.TABS + Candies.TABS
						+ "return new ModelAndView(\"" + rm.pathJsp
						+ "\", \"belong\"," + Candies.LF + Candies.TABS
						+ Candies.TABS + rm.jsp + Cst.JU[rm.tipo] + ");"
						+ Candies.LF;
			} else { // type model
				if (skill.ldapService & !accountController) {
					if (skill.ldapService) {
						s += Candies.TABS
								+ "private final UserDetailsService"
								+ " userDetailsService;" + Candies.LF;
					}
					s += Candies.TABS + "@Autowired" + Candies.LF;
					s += Candies.TABS + "public "
							+ skill.groupControllerF
							+ "(UserDetailsService"
							+ " userDetailsService) {" + Candies.LF;
					s += Candies.TABS + Candies.TABS
							+ "if (userDetailsService == null) {"
							+ Candies.LF;
					s += Candies.TABS
							+ "throw new IllegalArgumentException("
							+ "\"userDetailsService cannot be null\");"
							+ Candies.LF;
					s += Candies.TABS + Candies.TABS + "}" + Candies.LF;
					s += Candies.TABS + Candies.TABS
							+ "this.userDetailsService = userDetailsService;"
							+ Candies.LF;
					s += Candies.TABS + "}" + Candies.LF;
					accountController = true;
				}
				s += Candies.TABS + "@RequestMapping(value = { ";
				for (int k = 0; k < rm.map.length; k++) {
					s += "\"" + Strings.replaceFirst(rm.map[k],
							skill.correlationE.get(skill.cntK).gruppo,
							" ") + "\", ";
				}
				/* I remove the last comma-space pair */
				s = s.substring(0, s.length() - 2);
				s += " })" + Candies.LF;
				s += Candies.TABS + "public String view_" + i
						+ "(Model model) {" + Candies.LF + Candies.LF;
				if (skill.ldapService) {
					s += Equipment.oneString(candyScrew.snippetsYS
							+ "java/ldapAuthenticationModelService.java");
				} else {
					s += Equipment.oneString(candyScrew.snippetsYS
							+ "java/ldapAuthenticationModel.java");
				}
				s += Candies.TABS + Candies.TABS + "return \""
						+ rm.pathJsp + "\";" + Candies.LF;
				// producedMaterial = Strings.replaceFirst(
				// producedMaterial, "+importModel+", impMod);
			}
			s += Candies.TABS + "}" + Candies.LF;
			if (rm.tipo == Cst.FORM & jspKit.crud) {
				// producedMaterial = Strings.replaceFirst(
				// producedMaterial, "+importForm+", impForm);
				for (int crudJ = 0; crudJ < 4; crudJ++) {
					s += Candies.LF + Candies.TABS
							+ "@RequestMapping(value = \"/screw"
							+ rm.jsp + Candies.CRUD[crudJ]
							+ "\", method = RequestMethod.POST)"
							+ Candies.LF;
					s += Candies.TABS + "public ModelAndView compile"
							+ rm.jsp + Candies.CRUD[crudJ] + "(";
					if (skill.hiberValid | skill.springValid) {
						s += "@ModelAttribute(\"" + rm.jsp
								+ Cst.JU[rm.tipo] + "\") ";
						s += "@Valid "; // v 1.10.2
					}
					s += rm.jspU + Cst.JU[rm.tipo] + "Impl " + rm.jsp
							+ Cst.JU[rm.tipo] + "," + Candies.LF;
					s += Candies.TABS + Candies.TABS
							+ "BindingResult result," + Candies.LF;
					s += Candies.TABS + Candies.TABS
							+ "RedirectAttributes redirectAttributes) {"
							+ Candies.LF;
					if (skill.hiberValid | skill.springValid) {
						s += Candies.TABS + Candies.TABS
								+ "if (result.hasErrors()) {"
								+ Candies.LF;
						s += Candies.TABS + Candies.TABS + Candies.TABS
								+ "return new ModelAndView(\""
								+ rm.pathJsp + "\", \"belong\","
								+ Candies.LF + Candies.TABS
								+ Candies.TABS + rm.jsp
								+ Cst.JU[rm.tipo] + ");" + Candies.LF;
						s += Candies.TABS + Candies.TABS + "}"
								+ Candies.LF;
					}

					for (int j = 0; j < jspKit.controlE.size(); j++) {
						JspBeanElement jbe = jspKit.controlE.get(j);
						/* I reject the others */
						if (!jbe.controlType.equals(Cst.LIST_BOX)) {
							continue;
						}
						if (crudJ == 1 & jbe.dao != null
								& jbe.hsqln != null) {
							tableReading(jbe, rm);
						} else if (crudJ == 1 & jbe.ftokens != null) {
							tokensReading(jbe, rm);
						}
					}

					s += Candies.TABS + Candies.TABS
							+ "/* here you can use " + rm.jsp
							+ Cst.JU[rm.tipo] + " */" + Candies.LF;
					for (int j = 0; j < jspKit.controlE.size(); j++) {
						JspBeanElement jbe = jspKit.controlE.get(j);
						if (!jbe.controlType.equals(Cst.CHECK_BOX)) {
							continue;
						}
						s += Candies.TABS + Candies.TABS
								+ "redirectAttributes.addAttribute(\""
								+ jbe.name + "\", " + rm.jsp + ""
								+ Cst.JU[rm.tipo] + ".is" + jbe.nameU
								+ "());" + Candies.LF;
					}
					for (int j = 0; j < jspKit.controlE.size(); j++) {
						JspBeanElement jbe = jspKit.controlE.get(j);
						if (!jbe.controlType.equals(Cst.TEXT_AREA)) {
							continue;
						}
						s += Candies.TABS + Candies.TABS
								+ "redirectAttributes.addAttribute(\""
								+ jspKit.controlE.get(j).name + "\", "
								+ rm.jsp + "" + Cst.JU[rm.tipo] + ".get"
								+ jbe.nameU + "());" + Candies.LF;

					}
					for (int j = 0; j < jspKit.controlE.size(); j++) {
						JspBeanElement jbe = jspKit.controlE.get(j);
						if (!jbe.controlType.equals(Cst.LIST_BOX)) {
							continue;
						}
						s += Candies.TABS + Candies.TABS
								+ "redirectAttributes.addAttribute(\""
								+ jspKit.controlE.get(j).name + "\", "
								+ rm.jsp + "" + Cst.JU[rm.tipo]
								+ ".getArr_" + jspKit.controlE.get(
										j).name + "());" + 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;
						}
						s += Candies.TABS + Candies.TABS
								+ "redirectAttributes.addAttribute(\""
								+ jspKit.controlE.get(j).name + "\", "
								+ rm.jsp + "" + Cst.JU[rm.tipo] + ".get"
								+ jspKit.controlE.get(j).nameU + "());"
								+ Candies.LF;

					}
					s += Candies.TABS + Candies.TABS + "return screw"
							+ rm.jsp + Candies.CRUD[crudJ] + "("
							+ rm.jsp + "Form);" + Candies.LF;
					s += Candies.TABS + "}" + Candies.LF;
				}
				File crudMethods = new File(skill.candies.snippetsYS
						+ "java/Crud.java");
				String cr = Equipment.oneStringUnicode_A(crudMethods,
						"UTF-8");
				cr = cr.replaceAll("\\x2bjsp\\x2b", rm.jsp);
				cr = cr.replaceAll("\\x2bJsp\\x2b", rm.jspU);
				cr = cr.replaceAll("\\x2bscrewModel\\x2b", rm.pathJsp);
				s += cr;
			} else if (rm.tipo == Cst.FORM) {  // &!jspKit.crud
				// producedMaterial = Strings.replaceFirst(
				// producedMaterial, "+importForm+", impForm);
				s += Candies.LF + Candies.TABS
						+ "@RequestMapping(value = \"/screw" + rm.jsp
						+ "\", method = RequestMethod.POST)"
						+ Candies.LF;
				s += Candies.TABS + "public String compile" + rm.jsp
						+ "(";
				if (skill.hiberValid | skill.springValid) {
					s += "@ModelAttribute(\"" + rm.jsp + Cst.JU[rm.tipo]
							+ "\") ";
					s += "@Valid "; // / v 1.10.2
				}
				s += rm.jspU + Cst.JU[rm.tipo] + "Impl " + rm.jsp
						+ Cst.JU[rm.tipo] + "," + Candies.LF;
				s += Candies.TABS + Candies.TABS
						+ "BindingResult result," + Candies.LF;
				s += Candies.TABS + Candies.TABS
						+ "RedirectAttributes redirectAttributes) {"
						+ Candies.LF;
				s += Candies.TABS + Candies.TABS
						+ "/* here you can use " + rm.jsp
						+ Cst.JU[rm.tipo] + " */" + Candies.LF;
				if (skill.hiberValid | skill.springValid) {
					s += Candies.TABS + Candies.TABS
							+ "if (result.hasErrors()) {" + Candies.LF;
					s += Candies.TABS + Candies.TABS + Candies.TABS
							+ "return \"redirect:/" + rm.pathRedirect
							+ "\";" + Candies.LF;
					s += Candies.TABS + Candies.TABS + "}" + Candies.LF;
				}
				for (int j = 0; j < jspKit.controlE.size(); j++) {
					JspBeanElement jbe = jspKit.controlE.get(j);
					if (!jbe.controlType.equals(Cst.CHECK_BOX)) {
						continue;
					}
					s += Candies.TABS + Candies.TABS
							+ "redirectAttributes.addAttribute(\""
							+ jbe.name + "\", " + rm.jsp + ""
							+ Cst.JU[rm.tipo] + ".is" + jbe.nameU
							+ "());" + Candies.LF;

				}
				for (int j = 0; j < jspKit.controlE.size(); j++) {
					JspBeanElement jbe = jspKit.controlE.get(j);
					if (!jbe.controlType.equals(Cst.TEXT_AREA)) {
						continue;
					}
					s += Candies.TABS + Candies.TABS
							+ "redirectAttributes.addAttribute(\""
							+ jbe.name + "\", " + rm.jsp + ""
							+ Cst.JU[rm.tipo] + ".get" + jbe.nameU
							+ "());" + Candies.LF;

				}
				for (int j = 0; j < jspKit.controlE.size(); j++) {
					JspBeanElement jbe = jspKit.controlE.get(j);
					if (!jbe.controlType.equals(Cst.LIST_BOX)) {
						continue;
					}
					// s += Candies.TABS + Candies.TABS
					// + "redirectAttributes.addAttribute(\""
					// + jbe.name + "\", " + rm.jsp + ""
					// + Cst.JU[rm.tipo] + ".get" + jbe.nameU
					// + "());" + Candies.LF;
					s += Candies.TABS + Candies.TABS
							+ "redirectAttributes.addAttribute(\""
							+ jspKit.controlE.get(j).name + "\", "
							+ rm.jsp + "" + Cst.JU[rm.tipo] + ".getArr_"
							+ jspKit.controlE.get(j).name + "());"
							+ 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;
					}
					s += Candies.TABS + Candies.TABS
							+ "redirectAttributes.addAttribute(\""
							+ jbe.name + "\", " + rm.jsp + ""
							+ Cst.JU[rm.tipo] + ".get" + jbe.nameU
							+ "());" + Candies.LF;
				}
				s += Candies.TABS + Candies.TABS + "return \"redirect:/"
						+ rm.pathRedirect + "\";" + Candies.LF;
				s += Candies.TABS + "}" + Candies.LF;
			} else if (rm.tipo == Cst.PLAIN) {
				// producedMaterial = Strings.replaceFirst(
				// producedMaterial, "+importPlain+", impPlain);
			}
		}
		producedMaterial += s;
		producedMaterial += Candies.TABS + "/* " + Cst.KNIFE + " */"
				+ Candies.LF;
		producedMaterial += "}" + Candies.LF;
		/* WE COOK EVERYTHING */
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+importModel+", impMod);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+importForm+", impForm);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+importPlain+", impPlain);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bzoccolo\\x2b", skill.pckg);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+autowireds+", aws);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+importAutowired+", impAuto);
	}

	private void tableReading(JspBeanElement jbe, RequestMap rm) {
		/* now I distinguish between a select with a single column
		 * (evaluation = 1) and select with multiple columns (evaluation
		 * = 2) */
		String interfaccia0 = ((String[][]) (jbe.dao.substance))[1][0];
		// impForm += "import org.hibernate.HibernateException;"
		// + Candies.LF;
		if (jbe.hsqln.valutazioneColonne == 1) {
			/* one column case */
			s += Candies.TABS + Candies.TABS + "List<Object> "
					+ jbe.name + "E = null;" + Candies.LF;
			s += Candies.TABS + Candies.TABS + "try {" + Candies.LF;
			s += Candies.TABS + Candies.TABS + Candies.TABS + jbe.name
					+ "E = " + interfaccia0 + ".oneColumnQuery(\""
					+ jbe.hsqln.text + "\");" + Candies.LF;
		} else {
			/* case many columns */
			s += Candies.TABS + Candies.TABS + "List<Object[]> "
					+ jbe.name + "E = null;" + Candies.LF;
			s += Candies.TABS + Candies.TABS + "try {" + Candies.LF;
			s += Candies.TABS + Candies.TABS + Candies.TABS + jbe.name
					+ "E = " + interfaccia0 + ".manyColumnQuery(\""
					+ jbe.hsqln.text + "\");" + Candies.LF;
		}
		String interfaccia1 = "private " + interfaccia0.replaceFirst(
				Cst.RAIL, Cst.IRAILU) + " " + Mix.lowercaseFirstChar(
						interfaccia0) + ";";
		/* I have to avoid repetition */
		if (aws.indexOf(interfaccia1) == -1) {
			aws += Candies.TABS + "@Autowired" + Candies.LF;
			aws += Candies.TABS + interfaccia1 + Candies.LF;
		}
		s += Candies.TABS + Candies.TABS
				+ "} catch (HibernateException e) {" + Candies.LF;
		s += Candies.TABS + Candies.TABS + Candies.TABS
				+ "e.printStackTrace();" + Candies.LF;
		s += Candies.TABS + Candies.TABS + "} catch (Exception e) {"
				+ Candies.LF;
		s += Candies.TABS + Candies.TABS + Candies.TABS
				+ "e.printStackTrace();" + Candies.LF;
		s += Candies.TABS + Candies.TABS + "}" + Candies.LF;
		s += Candies.TABS + Candies.TABS + rm.jsp + Cst.JU[rm.tipo]
				+ ".setArr_" + jbe.name + "(" + jbe.name + "E);"
				+ Candies.LF;
	}

	private void tokensReading(JspBeanElement jbe, RequestMap rm) {
		s += Candies.TABS + Candies.TABS + "List<Object> " + jbe.name
				+ "E = null;" + Candies.LF;
		s += Candies.TABS + Candies.TABS
				+ "List<Object> tE = new ArrayList<Object>();"
				+ Candies.LF;
		String[][] wE = (String[][]) (jbe.ftokens.substance);
		for (int tK = 0; tK < wE[0].length; tK++) {
			s += Candies.TABS + Candies.TABS + "tE.add(\"" + wE[0][tK]
					+ "\");" + Candies.LF;
		}
		s += Candies.TABS + Candies.TABS + "try {" + Candies.LF;
		s += Candies.TABS + Candies.TABS + Candies.TABS + jbe.name
				+ "E = tE;" + Candies.LF;
		s += Candies.TABS + Candies.TABS + "} catch (Exception e) {"
				+ Candies.LF;
		s += Candies.TABS + Candies.TABS + Candies.TABS
				+ "e.printStackTrace();" + Candies.LF;
		s += Candies.TABS + Candies.TABS + "}" + Candies.LF;
		s += Candies.TABS + Candies.TABS + rm.jsp + Cst.JU[rm.tipo]
				+ ".setArr_" + jbe.name + "(" + jbe.name + "E);"
				+ Candies.LF;
	}

	/* written only to comply with the abstract class FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	/** @param markupE the list of markups
	 * @throws ExactException exception specified by a numeric code */
	public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

		skill.javaWebController = skill.javaWebY + "/controller";
		skill.buildDirectory(skill.javaWebController);

	}

	private boolean equivalente(RequestMap rm, RequestMap rmW) {
		if (rm == null | rmW == null) {
			return false;
		} else {
			return rm.path.equals(rmW.path) & rm.jsp.equals(rmW.jsp)
					& rm.tipo == rmW.tipo;
		}
	}
}