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

/** last update date: 2020-04-09
 * 
 * @author M. Maglioni */
final public class Orm_xml extends FileComponent {

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

		skill.springPropertiesG = skill.candies.snippetsYS + snippetG;
		Train f = markupE.get("hdefrail").recurrenceE.get(skill.railK);
		producedMaterial = Equipment.oneString(skill.springPropertiesG)
				+ Candies.LF;
		producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
				+ f.locomotor.text
				+ "DataSource\" class=\"org.springframework.jndi.JndiObjectFactoryBean\">"
				+ Candies.LF;
		if (skill.applicationServer.toLowerCase().startsWith(
				"jboss eap 6.4")) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"jndiName\" value=\"java:${"
					+ skill.jndi.toLowerCase() + ".jndiname}\" />"
					+ Candies.LF;
		} else {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"jndiName\" value=\"java:${"
					+ skill.jndi.toLowerCase() + ".jndiname}\" />"
					+ Candies.LF;
		}
		String g = Mix.dress_C(skill.mainResources)
				+ "config.properties";
		String configPropertiesS = Equipment.oneString(g);
		if (configPropertiesS.indexOf(skill.jndi.toLowerCase()) == -1) {
			configPropertiesS = configPropertiesS.replaceAll("#", "# ");
			configPropertiesS = Mix.compactingSpaces_A(
					configPropertiesS);
			if (configPropertiesS.indexOf(Cst.KNIFE) > -1) {
				configPropertiesS.replaceFirst(Cst.KNIFE, Cst.UPPER
						+ Candies.LF + "# " + Cst.LOWER);
			}
			if (configPropertiesS.indexOf("# " + Cst.LOWER) > -1
					& skill.dbms.equals("Postgres")) {
				configPropertiesS = Strings.replaceFirst(
						configPropertiesS, "# " + Cst.LOWER, skill.jndi
								.toLowerCase()
								+ ".jndiname=jboss/datasources/"
								+ skill.jndi + Candies.LF + "# "
								+ Cst.LOWER);
			} else if (configPropertiesS.indexOf("# "
					+ Cst.LOWER) > -1) {
				configPropertiesS = Strings.replaceFirst(
						configPropertiesS, "# " + Cst.LOWER, skill.jndi
								.toLowerCase() + ".jndiname="
								+ skill.jndi + Candies.LF + "# "
								+ Cst.LOWER);
			}
			Equipment.scriviFileDaStringa(new File(g),
					configPropertiesS);
		}
		producedMaterial += Candies.TABS + "</bean>" + Candies.LF;
		if (skill.jpaVersion == 1) {
			producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
					+ f.locomotor.text
					+ "SessionFactory\" class=\"org.springframework.orm.hibernate"
					+ skill.hibernateReductVersion
					+ ".annotation.AnnotationSessionFactoryBean\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"dataSource\" ref=\"" + Cst.RAIL
					+ f.locomotor.text + "DataSource\" />" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"packagesToScan\" value=\""
					+ skill.networkDomain + ".screwdriver." + skill.jndi
							.toLowerCase() + "." + skill.dbSchema
									.toLowerCase() + "\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"hibernateProperties\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "<value>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "hibernate.default_schema="
					+ skill.dbSchema + Candies.LF;
			if (f.wagonE.get("hdbms").text.equalsIgnoreCase(
					"postgres")) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS
						+ "hibernate.dialect=org.hibernate.dialect."
						+ "PostgreSQLDialect" + Candies.LF;
			} else {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS
						+ "hibernate.dialect=org.hibernate.dialect."
						+ f.wagonE.get("hdbms").text + "Dialect"
						+ Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "hibernate.cache.use_second_level_cache=false"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "hibernate.cache.use_query_cache=false"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "hibernate.show_sql=false"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "hibernate.format_sql=false"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "hibernate.query.substitutions true=1, false=0"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "</value>"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "</property>" + Candies.LF;
			producedMaterial += Candies.TABS + "</bean>" + Candies.LF;

			producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
					+ f.locomotor.text
					+ "Template\" class=\"org.springframework.orm.hibernate"
					+ skill.hibernateReductVersion
					+ ".HibernateTemplate\">" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<constructor-arg index=\"0\">" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<ref bean=\"" + Cst.RAIL
					+ f.locomotor.text + "SessionFactory\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "</constructor-arg>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<constructor-arg index=\"1\" type=\"boolean\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<value>true</value>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "</constructor-arg>" + Candies.LF;
			producedMaterial += Candies.TABS + "</bean>" + Candies.LF;

			producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
					+ f.locomotor.text
					+ "TxManager\" class=\"org.springframework.orm.hibernate"
					+ skill.hibernateReductVersion
					+ ".HibernateTransactionManager\">" + Candies.LF;
			if (skill.springVersion1m < 4) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ "<property name=\"sessionFactory\"><ref local=\""
						+ Cst.RAIL + f.locomotor.text
						+ "SessionFactory\" /></property>" + Candies.LF;
			} else {
				producedMaterial += Candies.TABS + Candies.TABS
						+ "<property name=\"sessionFactory\" ref=\""
						+ Cst.RAIL + f.locomotor.text
						+ "SessionFactory\" />" + Candies.LF;
			}
			producedMaterial += Candies.TABS + "</bean>" + Candies.LF;
		} else {
			producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
					+ f.locomotor.text
					+ "EntityManagerFactory\" class=\"org.springframework."
					+ "orm.jpa.LocalContainerEntityManagerFactoryBean\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"packagesToScan\" value=\""
					+ skill.networkDomain + ".screwdriver." + skill.jndi
							.toLowerCase() + "." + skill.dbSchema
									.toLowerCase() + "\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"dataSource\" ref=\"" + Cst.RAIL
					+ f.locomotor.text + "DataSource\" />" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"jpaProperties\">" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "<props>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.default_schema\">"
					+ skill.dbSchema + "</prop>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.show_sql\">" + "false"
					+ "</prop>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.format_sql\">" + "false"
					+ "</prop>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.cache.use_second_level_cache\">"
					+ "false" + "</prop>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.cache.use_query_cache\">"
					+ "false" + "</prop>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "<prop key=\"hibernate.cache.provider_class\">"
					+ "org.hibernate.cache.NoCacheProvider" + "</prop>"
					+ Candies.LF;
			if (f.wagonE.get("hdbms").text.equalsIgnoreCase(
					"postgres")) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS
						+ "<prop key=\"hibernate.dialect\">org.hibernate.dialect."
						+ "PostgreSQLDialect" + "</prop>" + Candies.LF;
			} else {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + Candies.TABS
						+ "<prop key=\"hibernate.dialect\">org.hibernate.dialect."
						+ f.wagonE.get("hdbms").text + "Dialect"
						+ "</prop>" + Candies.LF;

			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "</props>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "</property>" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"persistenceProvider\">"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "<bean class=\"org.hibernate.jpa.HibernatePersistenceProvider\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "</property>" + Candies.LF;
			producedMaterial += Candies.TABS + "</bean>" + Candies.LF;

			producedMaterial += Candies.TABS + "<bean id=\"" + Cst.RAIL
					+ f.locomotor.text
					+ "TxManager\" class=\"org.springframework.transaction"
					+ ".jta.JtaTransactionManager\" />" + Candies.LF;
		}
		producedMaterial += Candies.TABS
				+ "<tx:annotation-driven transaction-manager=\""
				+ Cst.RAIL + f.locomotor.text + "TxManager\" />"
				+ Candies.LF;
		producedMaterial += Candies.TABS
				+ "<context:component-scan base-package=\"" + skill.pckg
				+ "." + Cst.RAIL + f.locomotor.text + ".dao\">"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "<context:include-filter type=\"annotation\""
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "expression=\"org.springframework.stereotype.Repository\" />"
				+ Candies.LF;
		producedMaterial += Candies.TABS + "</context:component-scan>"
				+ Candies.LF;
		producedMaterial += Candies.TABS
				+ "<context:property-placeholder location=\"classpath:config.properties\" />"
				+ Candies.LF;
		producedMaterial += "</beans>";
	}

	/* 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 {

	}
}
