//
// 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.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 the production of the pom.xml file.
 * </p>
 * 
 * @author M. Maglioni
 *         <p>
 *         Company: Ramecera™<br>
 *         Creation date: 2020-04-09<br>
 *         Last updated: 2020-04-09<br>
 *         </p>
 */
final public class Pom_xml extends FileComponent {

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

	/** @param markupE the list of markups
	 * @param trainE the list of trains
	 * @param candyScrew the list of properties
	 * @throws IOException standard exception
	 * @throws FileNotFoundException standard exception
	 * @throws ExactException standard exception */
	public void synthesis(HashMap<String, Markup> markupE,
			List<Train> trainE, CandyScrew candyScrew)

			throws FileNotFoundException, IOException, ExactException {

		producedMaterial = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ Candies.LF;
		producedMaterial += "<project xmlns=\"http://maven.apache.org/POM/"
				+ Candies.SEGNO_A + "\""
				+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
				+ " xsi:schemaLocation=\"http://maven.apache.org/POM/"
				+ Candies.SEGNO_A + ""
				+ " http://maven.apache.org/xsd/maven-"
				+ Candies.SEGNO_A + ".xsd\">";
		producedMaterial = producedMaterial.replaceAll(Candies.SEGNO_A,
				skill.mavenModel) + Candies.LF;
		producedMaterial += Candies.TABS + "<modelVersion>"
				+ skill.mavenModel + "</modelVersion>" + Candies.LF;
		producedMaterial += Candies.TABS + "<groupId>"
				+ skill.mavenGroupId + "</groupId>" + Candies.LF;
		producedMaterial += Candies.TABS + "<artifactId>"
				+ skill.applicationNameLower + "</artifactId>"
				+ Candies.LF;
		producedMaterial += Candies.TABS + "<version>"
				+ skill.mavenVersion + "</version>" + Candies.LF;
		producedMaterial += Candies.TABS + "<packaging>"
				+ skill.mavenPackage + "</packaging>" + Candies.LF;
		producedMaterial += Candies.TABS + "<name>"
				+ skill.applicationNameLower + "</name>" + Candies.LF;
		producedMaterial += Candies.TABS + "<properties>" + Candies.LF;
		if (skill.spring) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<spring.version>" + skill.springVersion
					+ "</spring.version>" + Candies.LF;
		}
		if (skill.springSecurity) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<org.springframework.security.version>"
					+ skill.springSecurityVersion
					+ "</org.springframework.security.version>"
					+ Candies.LF;
		}
		if (skill.tiles) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<apache.tiles.version>" + skill.tilesVersion
					+ "</apache.tiles.version>" + Candies.LF;
		}
		// if (skill.mavenCompiler) {
		// materialProduct + = Candies.TABS + Candies.TABS
		// + "<maven.compiler.source>"
		// + skill.mavenCompilerVersion
		// + "</maven.compiler.source>" + Candies.LF;
		// materialProduct + = Candies.TABS + Candies.TABS
		// + "<maven.compiler.target>"
		// + skill.mavenCompilerVersion
		// + "</maven.compiler.target>" + Candies.LF;
		// }
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<project.build.sourceEncoding>"
				+ skill.charsetSenzaTrattino
				+ "</project.build.sourceEncoding>" + Candies.LF;
		if (skill.pomProperties) {
			skill.pomPropertiesG = skill.candies.snippetsYS + "pompr"
					+ "_" + markupE.get("pompr").recurrenceE.get(
							0).locomotor.substance + ".txt";
			producedMaterial += Equipment.oneString(
					skill.pomPropertiesG) + Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<m2eclipse.wtp.contextRoot>"
				+ skill.applicationNameLower
				+ "</m2eclipse.wtp.contextRoot>" + Candies.LF;
		producedMaterial += Candies.TABS + "</properties>" + Candies.LF;
		if (skill.mavenBuild) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "pom_build.txt")
					+ Candies.LF;
		}
		producedMaterial += Candies.TABS + "<!-- " + Cst.KNIFE + " -->"
				+ Candies.LF;
		producedMaterial += Candies.TABS + "<dependencies>"
				+ Candies.LF;
		if (skill.spring) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_spring_"
							+ skill.springVersion + ".txt")
					+ Candies.LF;
		}
		if (skill.springSecurity) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_secu_"
							+ skill.springSecurityReductVersion
							+ ".txt") + Candies.LF;
		}
		if (skill.tiles) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_tiles.txt")
					+ Candies.LF;
		}
		if (skill.antiSamy) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "antisamyDepen.txt")
					+ Candies.LF;
		}
		if (skill.ldap) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_apache_ldap.txt")
					+ Candies.LF;
		}
		if (skill.captcha) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_captcha_"
							+ skill.captchaVersion + ".txt")
					+ Candies.LF;
		}
		if (skill.webFlow) {
			producedMaterial += Equipment.oneString(
					skill.candies.snippetsYS + "depen_webflow_"
							+ skill.webflowVersion + ".txt")
					+ Candies.LF;
		}
		if (skill.jpaVersion == 2) {
			if (skill.jeeVersion == null) {
				skill.jeeVersion = "6.0";
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>javax</groupId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>javaee-api</artifactId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.jeeVersion + "</version>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<scope>provided</scope>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>javax.persistence</groupId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>persistence-api</artifactId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ "1.0.2" + "</version>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;

			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>antlr</groupId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>antlr</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ "2.7.7" + "</version>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
		}
		if (skill.dbSchema != null
				& skill.versioneArtifactJPA != null) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<groupId>"
					+ skill.networkDomain + ".ict</groupId>"
					+ Candies.LF;// v1.11.1 d2020-04-09
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<artifactId>"
					+ skill.jndi.toLowerCase() + "-" + skill.dbSchema
					+ "</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.versioneArtifactJPA + "</version>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
		}
		if (skill.log4jVers != null && skill.log4jVers.startsWith(
				"1")) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>log4j</groupId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>log4j</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.log4jVers + "</version>" + Candies.LF;
			if (!skill.applicationServer.startsWith("Tomcat")) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS
						+ "<scope>provided</scope>" + Candies.LF;
			}
			/* log4j is not necessarily contained in the app server */
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
		} else if (skill.log4jVers != null && skill.log4jVers
				.startsWith("2")) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>org.apache.logging.log4j</groupId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>log4j-api</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.log4jVers + "</version>" + Candies.LF;
			/* log4j is not necessarily contained in the app server */
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>org.apache.logging.log4j</groupId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>log4j-core</artifactId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.log4jVers + "</version>" + Candies.LF;
			/* log4j is not necessarily contained in the app server */
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
		}
		if (skill.quartzVers != null) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<dependency>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<groupId>org.quartz-scheduler</groupId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<artifactId>quartz</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<version>"
					+ skill.quartzVers + "</version>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "<exclusions>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "<exclusion>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS + "<groupId>org.slf4j</groupId>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "<artifactId>slf4j-api</artifactId>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + Candies.TABS
					+ "</exclusion>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "</exclusions>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</dependency>" + Candies.LF
					+ Candies.LF;
		}
		if (skill.dependencies) {
			producedMaterial += Equipment.oneString(skill.dependenciesG)
					+ Candies.LF;
		}
		if (skill.hibernate) {
			producedMaterial += Equipment.oneString(skill.depenHiberG)
					+ Candies.LF;
		}
		if (skill.jackson) {
			producedMaterial += Equipment.oneString(skill.depenJacksonG)
					+ Candies.LF;
		}
		if (skill.hiberValid) {
			producedMaterial += Equipment.oneString(skill.depenHValidG)
					+ Candies.LF;
		}
		if (skill.springValid) {
			producedMaterial += Equipment.oneString(
					skill.depenValidatorG) + Candies.LF;
		}
		producedMaterial += Candies.TABS + "<!-- screwdriver_knife -->"
				+ Candies.LF;
		producedMaterial += Candies.TABS + "</dependencies>"
				+ Candies.LF;
		producedMaterial += "</project>";

		/* WE COOK EVERYTHING */
		/* x2b is the + sign */
		producedMaterial = producedMaterial.replaceFirst(
				"\\x2bfinal.name\\x2b", skill.applicationNameLower);
		if (skill.mavenCompilerPlugin) {
			producedMaterial = producedMaterial.replaceFirst(
					"\\x2bmaven.compiler.plugin\\x2b",
					skill.mavenCompilerPluginVersion);
		}
		if (skill.mavenWarPlugin) {
			producedMaterial = producedMaterial.replaceFirst(
					"\\x2bmaven.war.plugin\\x2b",
					skill.mavenWarPluginVersion);
		}
		if (skill.mavenCleanPlugin) {
			producedMaterial = producedMaterial.replaceFirst(
					"\\x2bmaven.clean.plugin\\x2b",
					skill.mavenCleanPluginVersion);
		}
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bhvalid\\x2b", skill.hiberValidVersion);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bjackson\\x2b", skill.jacksonVersion);
	}

	/* 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 {
		if (skill.dependencies) {
			String dependenciesF = markupE.get("depen").stub + "_"
					+ ((Train) markupE.get("depen").recurrenceE.get(
							0)).locomotor.substance + ".txt";
			skill.dependenciesG = skill.candies.snippetsYS
					+ dependenciesF;
		}
	}
}
