//
// 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.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;

/**
 * <p>
 * Class for producing the web.xml file.
 * </p>
 * 
 * @author M. Maglioni
 *         <p>
 *         Company: Ramecera™<br>
 *         Creation date: 2020-04-09<br>
 *         Last updated: 2020-09-29<br>
 *         </p>
 */
final public class Web_xml extends FileComponent {

	/** @param skill together with the descriptive data of the
	 * @param snippetG application */
	public Web_xml(Skill skill, String snippetG) {
		super();
		this.skill = skill;
		this.snippetG = snippetG;
	}

	/** sysnthesis */
	@Override
	public void synthesis(HashMap<String, Markup> markupE,
			List<Train> trainE, CandyScrew candyScrew)

			throws FileNotFoundException, IOException, ExactException {

		final String XML_Y = "/WEB-INF/spring/";

		String web_xmlF = null;
		String mimeMappingF = "webmime.txt";
		String errorPageF = "weberror.txt";
		if (skill.spring) {
			web_xmlF = "web.xml.spring.txt";
		} else {
			web_xmlF = "web.xml.txt";
		}
		String web_xmlG = skill.candies.snippetsYS + web_xmlF;
		String mimeMappingG = skill.candies.snippetsYS + mimeMappingF;
		String errorPageG = skill.candies.snippetsYS + errorPageF;
		producedMaterial = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ Candies.LF;
		switch (skill.applicationServer.toLowerCase()) {
		case ("jboss eap 6.4"):
		case ("jboss eap 7.1"):
		case ("jboss eap 7.1.spring3"):
		case ("jboss eap 7.1.uv"):
		case ("jboss eap 6.4.uv"):
			producedMaterial += "<web-app id=\""
					+ skill.applicationNameLower + "\" version=\"4.0\" "
					+ "xmlns=\"http://java.sun.com/xml/ns/javaee\""
					+ Candies.LF;
			producedMaterial += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
					+ Candies.LF;
			producedMaterial += "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd\">"
					+ Candies.LF;
			break;
		case ("jboss eap 5.2"):
		case ("jboss eap 5.2.uv"):
			producedMaterial += "<web-app id=\""
					+ skill.applicationNameLower + "\" version=\"2.5\" "
					+ "xmlns=\"http://java.sun.com/xml/ns/javaee\""
					+ Candies.LF;
			producedMaterial += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
					+ Candies.LF;
			producedMaterial += "xsi:schemaLocation=\"http://java.sun.com/xml/"
					+ "ns/javaee http://java.sun.com/xml/ns/javaee"
					+ "/web-app_2_5.xsd\">" + Candies.LF;
			break;
		case ("tomcat 7.0"):
		case ("tomcat 7.0.uv"):
			producedMaterial += "<web-app id=\""
					+ skill.applicationNameLower + "\" version=\"2.4\" "
					+ "xmlns=\"http://java.sun.com/xml/ns/j2ee\""
					+ Candies.LF;
			producedMaterial += "xmlns:xsi=\"http://www.w3.org/2001"
					+ "/XMLSchema-instance\"" + Candies.LF;
			producedMaterial += "xsi:schemaLocation=\"http://java.sun.com"
					+ "/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee"
					+ "/web-app_2_4.xsd\">" + Candies.LF;
			break;
		default:
			ExactException ne = new ExactException();
			ne.code = 1200;
			ne.datum = "The declared application server  «"
					+ skill.applicationServer
					+ "» version is not expected.";
			throw ne;
		}
		producedMaterial += Candies.LF;
		producedMaterial += Candies.TABS + "<display-name>"
				+ skill.applicationNameLower.toUpperCase()
				+ "</display-name>" + Candies.LF;
		producedMaterial += Equipment.oneString(web_xmlG) + Candies.LF
				+ Candies.LF;
		if (skill.mimeMapping) {
			producedMaterial += Equipment.oneString(mimeMappingG)
					+ Candies.LF;
		}
		if (skill.errorPage) {
			producedMaterial += Equipment.oneString(errorPageG)
					+ Candies.LF;
		}
		if (skill.captcha) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "web_captcha.txt")
					+ Candies.LF + Candies.LF;
		}
		if (skill.antiSamy) {
			producedMaterial += Candies.TABS + "<context-param>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<param-name>policy_file_path</param-name>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<param-value>/" + skill.pckg
					+ "/web/sanitizer/policy.xml</param-value>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "</context-param>"
					+ Candies.LF;
		}
		producedMaterial += Candies.TABS + "<context-param>"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<param-name>contextConfigLocation</param-name>"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<param-value>" + Candies.LF;
		if (skill.quartzVers != null) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "/WEB-INF/spring/"
					+ skill.applicationNameLower + "-"
					+ FileComponent.QUARTZ_JOB_XML_F + Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "/WEB-INF/spring/root-context.xml" + Candies.LF;
		if (skill.internalProperties | skill.springSecurity | markupE
				.get("hdefrail").recurrenceE.size() > 0) {
			if (skill.internalProperties) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + XML_Y
						+ skill.applicationNameLower + "-props.xml"
						+ Candies.LF;
			}
			if (skill.springSecurity) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + XML_Y
						+ skill.applicationNameLower + "-security.xml"
						+ Candies.LF;
			}
			if (markupE.get("hdefrail").recurrenceE.size() > 0) {
				for (int i = 0; i < markupE.get("hdefrail").recurrenceE
						.size(); i++) {
					Train t = markupE.get("hdefrail").recurrenceE.get(
							i);
					producedMaterial += Candies.TABS + Candies.TABS
							+ Candies.TABS + XML_Y + Cst.RAIL
							+ t.locomotor.text + "-orm.xml"
							+ Candies.LF;
					producedMaterial += Candies.TABS + Candies.TABS
							+ Candies.TABS + XML_Y + Cst.RAIL
							+ t.locomotor.text + "-service.xml"
							+ Candies.LF;
				}
			}
		}
		producedMaterial += Candies.TABS + Candies.TABS
				+ "</param-value>" + Candies.LF;
		producedMaterial += Candies.TABS + "</context-param>"
				+ Candies.LF + Candies.LF;
		producedMaterial += Equipment.oneString(skill.candies.snippetsYS
				+ "listn_0.txt") + Candies.LF;
		if (skill.springSecurity) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "uvListener.txt")
					+ Candies.LF;
		}
		if (skill.antiSamy) {
			String listener = Candies.TABS + "<listener>" + Candies.LF;
			listener += Candies.TABS + Candies.TABS + "<listener-class>"
					+ skill.pckg + ".web.listener"
					+ ".AntiSamyListener</listener-class>" + Candies.LF;
			listener += Candies.TABS + "</listener>" + Candies.LF
					+ Candies.LF;
			producedMaterial += listener;
		}
		/* secuFilte deve essere il primo di tutti i filtri, e deve
		 * riportare le clausolE REQUEST e ERROR */
		if (skill.springSecurity) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "secuFilte.txt")
					+ Candies.LF;
		}
		if (skill.filter) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "filte.txt")
					+ Candies.LF;
		}
		/* CUCINIAMO IL TUTTO */
		if (skill.quartzVers == null) {
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+startup+", "");
		} else {
			producedMaterial = Strings.replaceFirst(producedMaterial,
					"+startup+",
					"<load-on-startup>1</load-on-startup>");
		}
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bzoccolo\\x2b", skill.pckg);
		producedMaterial += "</web-app>";
	}

	/* scritta solo per ottemperare alla className astratta
	 * FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	/** @param markupE l'elenco dei markup
	 * @param skill insieme delle caratteristiche dell'applicazione
	 * @param recycleBin file dove sono spostati i file cancellati
	 * @throws ExactException eccezione precisata da un code numerico */
	public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

	}
}
