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

/** @author M. Maglioni */
final public class UserAuthenticationProvider_java extends
		FileComponent {

	/** @param skill set of application descriptive data
	 * @param snippetG path and name of the snippet file (fragment) */
	public UserAuthenticationProvider_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 {

		skill.applicUserAuthenticationProviderG = skill.candies.snippetsYS
				+ snippetG;
		producedMaterial = Equipment.oneString(
				skill.applicUserAuthenticationProviderG) + Candies.LF
				+ Candies.LF;
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"package +zoccolo+", "package " + skill.pckg);
		producedMaterial = Strings.replaceFirst(producedMaterial,
				"+userpass+", Mix.uppercaseFirstChar(
						skill.authenticationMode));
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bApplicaz\\x2b", Mix.uppercaseFirstChar(
						skill.applicationNameLower));
		producedMaterial = producedMaterial.replaceAll(
				"import \\x2bzoccolo\\x2b", "import " + skill.pckg);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bdomain\\x2b", skill.networkDomain);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bdsname\\x2b", skill.tabUser[2].toLowerCase());
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bdbschema\\x2b", skill.tabUser[3]);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2bRail\\x2b", Cst.RAILU + skill.tabUser[1]);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2brail\\x2b", Cst.RAIL + skill.tabUser[1]);
		producedMaterial = producedMaterial.replaceAll(
				"\\x2btabuser\\x2b", Mix.uppercaseFirstChar(
						skill.tabUser[0]));
		producedMaterial += Candies.TABS + "@Override" + Candies.LF;
		producedMaterial += Candies.TABS
				+ "public Authentication authenticate("
				+ "Authentication authentication)" + Candies.LF
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "throws AuthenticationException {" + Candies.LF
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Mix
				.uppercaseFirstChar(skill.authenticationMode)
				+ "UsernamePasswordAuthenticationToken token" + " = ("
				+ Mix.uppercaseFirstChar(skill.authenticationMode)
				+ "UsernamePasswordAuthenticationToken)"
				+ " authentication;" + Candies.LF;

		producedMaterial += Candies.TABS + Candies.TABS
				+ "String userName = token.getName();" + Candies.LF;
		if (skill.authenticationMode.equals("domain")) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "String domain = token.getDomain();" + Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS
				+ "Utenti user = null;" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "try {"
				+ Candies.LF;
		if (skill.jpaVersion == 1) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "user = " + Cst.RAIL
					+ skill.tabUser[1] + Mix.uppercaseFirstChar(
							skill.tabUser[0])
					+ ".findById(userName, true);" + Candies.LF;
		} else {
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "user = (" + Mix.uppercaseFirstChar(
							skill.tabUser[0]) + ")" + Cst.RAIL
					+ skill.tabUser[1] + Mix.uppercaseFirstChar(
							skill.tabUser[0]) + ".findUniqueById(" + Mix
									.uppercaseFirstChar(skill.tabUser[0])
					+ ".class, userName);" + Candies.LF;
		}
		producedMaterial += Candies.TABS + Candies.TABS
				+ "} catch (DataAccessException e) {" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "} catch (Exception e) {" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "e.printStackTrace();" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "}"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "if (user == null) {" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "throw new UsernameNotFoundException(" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "\"Invalid username/password\");" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "}"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "String password = user.get" + Mix.uppercaseFirstChar(
						skill.password) + "();" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "if (!password.equals(token.getCredentials())) {"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "throw new BadCredentialsException(" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + Candies.TABS
				+ "\"Invalid username/password\");" + Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS + "}"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "Collection<? extends GrantedAuthority> authorities"
				+ " = UserAuthorityUtils.createAuthorities(user);"
				+ Candies.LF;
		if (skill.authenticationMode.equals("domain")) {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return new" + " " + Mix.uppercaseFirstChar(
							skill.authenticationMode)
					+ "UsernamePasswordAuthenticationToken(user,"
					+ " password, domain, authorities);" + Candies.LF;
		} else {
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return new" + " " + Mix.uppercaseFirstChar(
							skill.authenticationMode)
					+ "UsernamePasswordAuthenticationToken(user,"
					+ " password, authorities);" + Candies.LF;
		}
		producedMaterial += Candies.TABS + "}" + Candies.LF
				+ Candies.LF;
		producedMaterial += Candies.TABS + "@Override" + Candies.LF;
		producedMaterial += Candies.TABS
				+ "public boolean supports(Class<?> authentication) {"
				+ Candies.LF;
		producedMaterial += Candies.TABS + Candies.TABS
				+ "return true;" + Candies.LF;
		producedMaterial += Candies.TABS + "}" + Candies.LF;

		producedMaterial += Candies.TABS + "/* screwdriver_knife */"
				+ Candies.LF;
		producedMaterial += "}" + Candies.LF;
	}

	/* written only to comply with the abstract class FileComponent */
	@SuppressWarnings("javadoc")
	@Override
	/** @param markupE the list of markups
	 * @param skill together with the descriptive data of the
	 *        application
	 * @param file basket where the deleted files are moved
	 * @throws ExactException exception specified by a numerical code */
	public void directory(HashMap<String, Markup> markupE)

			throws ExactException {

	}
}
