//
// 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;
import it.ramecera.screwdriver.outfits.ClockHands;

/**
 * <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-04-09<br>
 *         </p>
 */
final public class Quartz_job_xml extends FileComponent {

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

		String quartz_job_xmlG = skill.candies.snippetsYS + "xml/"
				+ QUARTZ_JOB_XML_F;
		producedMaterial = Equipment.oneString(skill.candies.snippetsYS
				+ snippetG) + Candies.LF;
		producedMaterial += Equipment.oneString(quartz_job_xmlG)
				+ Candies.LF + Candies.LF;

		for (ClockHands l : skill.handE) {
			producedMaterial += Candies.TABS + "<bean id=\"trg"
					+ l.jobNameU
					+ "\" class=\"org.springframework.scheduling.quartz.";
			if (skill.clockHands.simple == null) {
				producedMaterial += "Cron";
			} else {
				producedMaterial += "Simple";
			}
			producedMaterial += "TriggerBean\">" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"jobDetail\" ref=\"" + l.jobName
					+ "\" />" + Candies.LF;
			if (l.simple != null) {
				// x2c is comma
				String[] p = l.simple.split("\\x2c");
				producedMaterial += Candies.TABS + Candies.TABS
						+ "<property name=\"startDelay\" value=\""
						+ p[0].trim() + "\" />" + Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "<property name=\"repeatInterval\" value=\""
						+ p[1].trim() + "\" />" + Candies.LF;
			}
			if (l.cron != null) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ "<property name=\"cronExpression\" value=\""
						+ l.cron.trim() + "\" />" + Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ "<property name=\"group\" value=\""
					+ skill.applicationNameLower + "_trigger_group\" />"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "</bean>" + Candies.LF;

		}

		producedMaterial += "</beans>" + Candies.LF;

		/* WE COOK EVERYTHING */
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bapplicaz\\x2b", skill.applicationNameLower);
		producedMaterial = producedMaterial.replaceAll("\\x2brail\\x2b",
				Cst.RAIL + skill.clockHands.rail);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bquartztask\\x2b", skill.clockHands.taskName);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bQuartztask\\x2b", skill.clockHands.taskNameU);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bquartzjob\\x2b", skill.clockHands.jobName);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bQuartzjob\\x2b", skill.clockHands.jobNameU);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bzoccolo\\x2b", skill.pckg);
	}

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

	}
}
