//
// 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.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 UserContextImpl_java extends FileComponent {

	/** @param skill set of application descriptive data
	 * @param snippetG path and name of the snippet file (fragment) */
	public UserContextImpl_java(Skill skill, String snippetG) {
		super();
		this.skill = skill;
		this.snippetG = snippetG;
	}

	/** array of 3 positions: 0) annotation of the key; 1) type of key;
	 * 2) name of the key */
	public String[] utenteChiave;

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

		if (skill.authenticationMode.equals("trivial")) {
			producedMaterial = "package " + skill.pckg
					+ ".web.authentication;" + Candies.LF + Candies.LF;
			producedMaterial += "import org.springframework.beans.factory.annotation.Autowired;"
					+ Candies.LF;
			producedMaterial += "import org.springframework.stereotype.Component;"
					+ Candies.LF;
			producedMaterial += "import " + skill.pckg
					+ ".web.authentication.KingDao;" + Candies.LF;
			producedMaterial += "import " + skill.pckg
					+ ".domain.King;" + Candies.LF;
			if (skill.log4jVers != null) {
				producedMaterial += "import org.apache.log4j.Logger;"
						+ Candies.LF + Candies.LF;
			}
			/* this
			 * 
			 * @Component is not there in the case of simple and
			 * domain */
			producedMaterial += "@Component" + Candies.LF;
			producedMaterial += "public class UserContextStub implements UserContext {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS
					+ "private final KingDao userService;" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "private String currentUserId = null;"
					+ Candies.LF + Candies.LF;
			if (skill.log4jVers != null) {
				producedMaterial += Candies.TABS
						+ "/** ordinary logger */" + Candies.LF;
				producedMaterial += Candies.TABS
						+ "@SuppressWarnings(\"unused\")" + Candies.LF;
				producedMaterial += Candies.TABS
						+ "private static Logger logger = Logger.getLogger("
						+ Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "UserContextStub.class);" + Candies.LF
						+ Candies.LF;
			}
			producedMaterial += Candies.TABS + "@Autowired"
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "public UserContextStub(KingDao userService) {"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (userService == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException("
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "\"userService cannot be null\");" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "this.userService = userService;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + "@Override"
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "public King getCurrentUser() {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "return userService.getUser(currentUserId);"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;

			producedMaterial += Candies.TABS + "@Override"
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "public void setCurrentUser(King user) {"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (user == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException(\"user cannot be null\");"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "String currentId = user.getUserid();"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (currentId == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException("
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "\"user.getUserid() cannot be null\");"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "this.currentUserId = currentId;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;
			producedMaterial += "}";
		} else {
			utenteChiave = locateKey(skill.tableDBE.get(
					skill.uK));
			producedMaterial = "package " + skill.pckg
					+ ".web.authentication;" + Candies.LF + Candies.LF;
			producedMaterial += "import org.springframework.beans.factory.annotation.Autowired;"
					+ Candies.LF;
			producedMaterial += "import org.springframework.dao.DataAccessException;"
					+ Candies.LF;
			producedMaterial += "import " + skill.pckg + "." + Cst.RAIL
					+ skill.tabUser[1] + ".dao.I" + Cst.RAILU + Mix
							.uppercaseFirstChar(skill.tabUser[1])
					+ skill.tabUser[0] + "Dao;" + Candies.LF;
			producedMaterial += "import " + (skill.networkDomain
					+ ".screwdriver." + skill.tabUser[2] + "."
					+ skill.tabUser[3]).toLowerCase() + "."
					+ skill.tabUser[0] + ";" + Candies.LF;

			if (skill.log4jVers != null) {
				producedMaterial += "import org.apache.log4j.Logger;"
						+ Candies.LF + Candies.LF;
			}

			producedMaterial += "public class UserContextStub implements UserContext {"
					+ Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + "private final I"
					+ Cst.RAILU + skill.tabUser[1] + skill.tabUser[0]
					+ "Dao<" + Mix.uppercaseFirstChar(skill.tabUser[0])
					+ "> userService;" + Candies.LF + Candies.LF;
			producedMaterial += Candies.TABS + "private "
					+ utenteChiave[1] + " currentUserId;" + Candies.LF
					+ Candies.LF;
			if (skill.log4jVers != null) {
				producedMaterial += Candies.TABS
						+ "/** ordinary logger */" + Candies.LF;
				producedMaterial += Candies.TABS
						+ "@SuppressWarnings(\"unused\")" + Candies.LF;
				producedMaterial += Candies.TABS
						+ "private static Logger logger = Logger.getLogger("
						+ Candies.LF;
				producedMaterial += Candies.TABS + Candies.TABS
						+ "UserContextStub.class);" + Candies.LF
						+ Candies.LF;
			}
			producedMaterial += Candies.TABS + "@Autowired"
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "public UserContextStub(I" + Cst.RAILU
					+ skill.tabUser[1] + skill.tabUser[0] + "Dao<" + Mix
							.uppercaseFirstChar(skill.tabUser[0])
					+ "> userService) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (userService == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException("
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS
					+ "\"userService cannot be null\");" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "this.userService = userService;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF
					+ Candies.LF;
			producedMaterial += Candies.TABS + "@Override"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "public "
					+ skill.tabUser[0] + " getCurrentUser() {"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "try {"
					+ Candies.LF;
			if (skill.jpaVersion == 1) {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS
						+ "return userService.findById(currentUserId, false);"
						+ Candies.LF;
			} else {
				producedMaterial += Candies.TABS + Candies.TABS
						+ Candies.TABS + "return (" + Mix
								.uppercaseFirstChar(skill.tabUser[0])
						+ ")userService.findUniqueById(" + Mix
								.uppercaseFirstChar(skill.tabUser[0])
						+ ".class, currentUserId);" + Candies.LF;
			}
			producedMaterial += Candies.TABS + Candies.TABS
					+ "} catch (DataAccessException e) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "e.printStackTrace();"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + "return null;" + 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.TABS + "return null;" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;

			producedMaterial += Candies.TABS + "@Override"
					+ Candies.LF;
			producedMaterial += Candies.TABS
					+ "public void setCurrentUser(" + skill.tabUser[0]
					+ " user) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (user == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException(\"user cannot be null\");"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ utenteChiave[1] + " currentId = user.get" + Mix
							.uppercaseFirstChar(utenteChiave[2]) + "();"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "if (currentId == null) {" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS
					+ "throw new IllegalArgumentException("
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ Candies.TABS + Candies.TABS + "\"user.get" + Mix
							.uppercaseFirstChar(utenteChiave[2])
					+ "() cannot be null\");" + Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS + "}"
					+ Candies.LF;
			producedMaterial += Candies.TABS + Candies.TABS
					+ "this.currentUserId = currentId;" + Candies.LF;
			producedMaterial += Candies.TABS + "}" + Candies.LF;
			producedMaterial += "}";
		}
	}

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

	}
}
