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

import java.util.ArrayList;
import java.util.List;

import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;
import it.ramecera.screwdriver.Cst;
import it.ramecera.screwdriver.Wagon;
import it.ramecera.screwdriver.utilities.ValidConstScrew;

/**
 * <p>
 * Elements of each control present in a JSP.
 * </p>
 * <p>
 * Company: Ramecera™<br>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-10-10
 * </p>
 * <p>
 * History: 2020-08-02 implementation of N13; 2020-10-10 implementation
 * of N12
 * </p>
 * 
 * @author M. Maglioni */
final public class JspBeanElement {

	/** name of the property */
	public String name;
	/** name of the property with uppercase initial character */
	public String nameU;
	/** data type: String, int, boolean, etc. */
	public String datumType;
	/** if it is a data that participates in the structure of the
	 * connected JSP (true), or if it is only auxiliary (false); in
	 * practice, that one indicated by belong; it is auxiliary if it is
	 * part of the bean but is not directly exposed in the JSP */
	public boolean participate;
	/** the label of the graphic control in the form */
	public String label;
	/** the sign, that is the string that appears on the list-box when
	 * it is closed */
	public Wagon cartel;
	/** if it has a button that immediately sends the chosen data
	 * online, the label inside the button; otherwise null */
	public Wagon button_label;
	/** based on which side the fill SQL query starts */
	public Wagon dao;
	/** the SQL query wagon to fill a dropdown graphic control */
	public Wagon hsqln;
	/** the list of tokens to fill a dropdown graphic control */
	public Wagon ftokens;
	/** only for radio buttons group, the initially selected element */
	public int startingRadio;
	/** type of the control connected to this datum */
	public String controlType;
	/** the list of validation constraints */
	public List<ValidConstScrew> validConstE = new ArrayList<ValidConstScrew>();

	/** default constructor */
	public JspBeanElement() {

	}

	/**
	 * <p>
	 * Company: Ramecera™<br>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-06-20
	 * </p>
	 * 
	 * @param name name of the property
	 * @param datumType data type
	 * @param participate if it participates in the JSP text (true), or
	 *        if it is only auxiliary (false)
	 * @param label the label of the graphic control in the form
	 * @param button_label the label of the button that puts immediately
	 *        the data online
	 * @param validConstE validation constants
	 * @param controlType type of the control graphic element
	 * @author M. Maglioni */
	public JspBeanElement(final String name, final String datumType,
			final boolean participate, final String label,
			Wagon button_label, List<ValidConstScrew> validConstE,
			final String controlType) {
		super();
		this.name = name;
		this.nameU = Mix.uppercaseFirstChar(this.name);
		this.datumType = datumType;
		this.participate = participate;
		this.label = label;
		this.button_label = button_label;
		this.validConstE = validConstE;
		this.controlType = controlType;
	}

	/**
	 * <p>
	 * Company: Ramecera™<br>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-10-10
	 * </p>
	 * <p>
	 * History: 2020-08-02 implementation of N13 (see
	 * martoraProject.ods); 2020-10-10 implementation of N12
	 * </p>
	 * 
	 * @param name name of the property
	 * @param datumType data type
	 * @param participate if it participates in the JSP text (true), or
	 *        if it is only auxiliary (false)
	 * @param label the label of the grafdico control in the form
	 * @param cartel the cartel, that is the string that appears on the
	 *        list-box when it is closed
	 * @param button_label the label of the button that puts the
	 * @param dao data online based on which side the filling SQL query
	 * @param hsqln the filling SQL query
	 * @param controlType type of the control element
	 * @param ftokens the filling list of tokens
	 * @param startingRadio only for radio buttons group, the element
	 *        initially selected
	 * @author M. Maglioni
	 * @throws ExactException standard exception */
	public JspBeanElement(final String name, final String datumType,
			final boolean participate, final String label, Wagon cartel,
			Wagon button_label, Wagon dao, Wagon hsqln,
			final String controlType, Wagon ftokens,
			final int startingRadio)

			throws ExactException {

		super();
		this.name = name;
		this.nameU = Mix.uppercaseFirstChar(this.name);
		this.datumType = datumType;
		this.participate = participate;
		this.label = label;
		this.cartel = cartel;
		this.button_label = button_label;
		ExactException ee = new ExactException();
		this.dao = dao;
		if (dao != null) {
			String[][] w = (String[][]) dao.substance;
			for (int i = 0; i < w[0].length; i++) {
				if (!w[0][i].startsWith(Cst.IRAILU)) {
					ee.datum = "The value of markup dao+: " + Mix
							.dress_A(w[0][i])
							+ " is wrong, it must start with prefix "
							+ Mix.dress_A(Cst.IRAILU) + ".";
					ee.code = 1300;
					ee.className = this.getClass().getName();
					ee.method = "constructor of " + ee.className;
					throw ee;
				} else if (!w[0][i].endsWith("Dao")) {
					ee.datum = "The value of markup dao+: " + Mix
							.dress_A(w[0][i])
							+ " is wrong, it must end with suffix \"Dao\".";
					ee.code = 1301;
					ee.className = this.getClass().getName();
					ee.method = "constructor of " + ee.className;
					throw ee;
				} else if (!w[0][i].matches(
						"IRail\\d+\\p{Upper}\\p{Graph}*")) {
					ee.datum = "The value of markup dao: " + Mix
							.dress_A(w[0][i])
							+ " is wrong, it is a bad structured string.";
					ee.code = 1302;
					ee.className = this.getClass().getName();
					ee.method = "constructor of " + ee.className;
					throw ee;
				}
				w[1][i] = w[0][i].replaceFirst("IR", "r");
			}
			this.dao.substance = w;
		}
		this.hsqln = hsqln;
		this.ftokens = ftokens;
		this.controlType = controlType;
		this.startingRadio = startingRadio;
	}

	/**
	 * <p>
	 * Classic toString method.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @return the compound string
	 * @author M. Maglioni */
	public String toString() {
		String s = "";
		s += "name=" + name;
		if (datumType != null) {
			s += "; type of datum=" + datumType;
		}
		s += "; participate=" + participate + "; label=" + label;
		if (cartel != null) {
			s += "; cartel=" + cartel.text;
		}
		if (button_label != null) {
			s += "; button_label=" + button_label.text;
		}
		if (hsqln != null) {
			s += "; hsqln=" + hsqln;
		}
		if (controlType != null) {
			s += "; control type=" + controlType;
		}
		if (validConstE != null) {
			for (int j = 0; j < validConstE.size(); j++) {
				s += "; valid constant" + j + "=" + validConstE.get(j);
			}
		}
		if (dao != null) {

		}
		return s;
	}
}