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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import it.ramecera.salad.graphic.Placard;

/**
 * <p>
 * General utility class for managing properties files.
 * </p>
 * <p>
 * Company: Ramecera™<br>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-04-09
 * </p>
 * <p>
 * Changes:<br>
 * 2020-04-09: abandonment of SWT graphics in favor of Swing<br>
 * </p>
 * 
 * @author M. Maglioni */
public class StartingOperations {

	/**
	 * <p>
	 * Reading the properties file, this is an XML file. Since the
	 * routine already includes several error messages, in case of catch
	 * you will have to simply <b>return</b>, without displaying
	 * anything else; if anything, you can log the event.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-19-02<br>
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * 2020-04-09: replaced SWT type message-box with Swing type
	 * Placard, ver. 2.0.0<br>
	 * 2020-04-09: improvement of error messages, ver. 2.0.1<br>
	 * 2020-04-09: added graphic parameter, it must not issue message
	 * boxes when the graphic is not foreseen, for example because it is
	 * in non-graphic Unix, ver. 2.1.0<br>
	 * </p>
	 * 
	 * @param propertiesF the name of the file, positioned at the root
	 *        of the project, of the XML type, containing the true
	 *        graphic
	 * @param graphicOption true if it is possible to emit graphic
	 *        elements; false if the message simply goes to System.out
	 * @return the properties extracted from the file, in object of the
	 *         class Properties
	 * @throws ExactException standard exception */
	public static Properties myPropertiesXML(String propertiesF,
			boolean graphicOption)

			throws ExactException {

		Properties p = new Properties();
		String a;
		try {
			p.loadFromXML(new FileInputStream(propertiesF));
			return p;
		} catch (InvalidPropertiesFormatException e) {
			a = "The file " + Mix.dress_A(propertiesF)
					+ " is incorrect.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100104, "StartingOperations",
					"");
		} catch (FileNotFoundException e) {
			if (propertiesF.equals(""))
				a = "It's not indicated any properties file.";
			else
				a = "The file " + Mix.dress_A(propertiesF)
						+ " is not present.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100105, "StartingOperations",
					"");
		} catch (IOException e) {
			a = "The file " + Mix.dress_A(propertiesF)
					+ " isn't readable.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100106, "StartingOperations",
					"");
		}
	}

	/**
	 * <p>
	 * Read the properties file. Since the routine already includes
	 * several error messages, in case of catch you will have to simply
	 * <b>return</b>, without displaying anything else; if anything, you
	 * can log the event.
	 * </p>
	 * <p>
	 * Creation date: 2020-10-02<br>
	 * Last updated: 2020-10-02<br>
	 * </p>
	 * <p>
	 * Historys:<br>
	 * </p>
	 * 
	 * @param propertiesF the name of the file, positioned at the root
	 *        of the project
	 * @param graphicOption true if it is possible to emit graphic
	 *        elements; false if the message simply goes to System.out
	 * @return the properties extracted from the file, in object of the
	 *         class Properties
	 * @throws ExactException standard exception */
	public static Properties myProperties(String propertiesF,
			boolean graphicOption)

			throws ExactException {

		if (propertiesF == null) {
			throw new ExactException("Instead of indicating a "
					+ "properties file, there is a null.", 100107,
					"StartingOperations", "myProperties");
		}
		Properties p = new Properties();
		String a = "";
		try {
			p.load(new FileInputStream(propertiesF));
			return p;
		} catch (InvalidPropertiesFormatException e) {
			a = "The file " + Mix.dress_A(propertiesF)
					+ " is incorrect.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100108, "StartingOperations",
					"");
		} catch (FileNotFoundException e) {
			if (propertiesF.equals(""))
				a = "It's not indicated any properties file.";
			else
				a = "The file " + Mix.dress_A(propertiesF)
						+ " is not present.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100109, "StartingOperations",
					"");
		} catch (IOException e) {
			a = "The file " + Mix.dress_A(propertiesF)
					+ " isn't readable.";
			if (graphicOption) {
				System.out.println(a);
				new Placard(null, a);
			} else {
				System.out.println(a);
			}
			throw new ExactException(a, 100110, "StartingOperations",
					"");
		}
	}

	/**
	 * <p>
	 * Writing the properties file, this is an XML file.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * last modification: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * 2020-04-09 : replaced SWT message-box with Swing type Placard,
	 * created version 2.0.0
	 * </p>
	 * 
	 * @param propertiesF the name of the file, positioned at the root
	 *        of the project, of XML type, containing the properties
	 * @param p the properties object to write to disk
	 * @throws Exception The file cannot be written. */
	public static void yourPropertiesXML(String propertiesF,
			Properties p)

			throws Exception {

		Exception err = new Exception("Uncorrect");
		try {
			PrintStream printStream = new PrintStream(propertiesF);
			p.storeToXML(printStream, "Written on " + HandleDate
					.currentDate_C() + Candies.SPACE + HandleDate
							.currentTime_B(), "UTF-8");
			printStream.close();
		} catch (IOException e) {
			new Placard(null, "The file " + Mix.dress_A(propertiesF)
					+ " isn't writable.");
			throw err;
		} catch (NullPointerException e) {
			new Placard(null, "Setting error in file " + Mix.dress_A(
					propertiesF) + ".");
			throw err;
		}
	}

	/**
	 * <p>
	 * Reading and checking the existence of a property.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * last modification: 2020-04-09
	 * </p>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @param p the object that contains all the properties
	 * @param label the label of the property
	 * @return the data relating to that label
	 * @throws ExactException the label is not present, therefore the
	 *         data is null */
	public static String whetherPresent(Properties p,
			final String label)

			throws ExactException {

		String v = p.getProperty(label);
		if (v == null)
			throw new ExactException("The property " + Mix.dress_A(
					label) + " is missing or null.", 100111,
					"StartingOperations", "");
		return v;
	}

	/**
	 * <p>
	 * Reading and checking the existence of a Boolean property.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * last modification: 2020-04-09
	 * </p>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @since J6_1
	 * @param p the object that contains all the properties
	 * @param label the label of the property
	 * @return the data relating to that label
	 * @throws ExactException the label is not present, therefore the
	 *         data is null */
	public static boolean propertyPresenteBoolean(Properties p,
			final String label)

			throws ExactException {

		String v = p.getProperty(label).trim();
		if (v == null)
			throw new ExactException("The property " + Mix.dress_A(
					label) + " is missing or null.", 100112,
					"StartingOperations", "");
		if (v.equalsIgnoreCase("false")) {
			return false;
		} else if (v.equalsIgnoreCase("true")) {
			return true;
		} else {
			throw new ExactException("The property «" + Mix.dress_A(
					label) + " is neither true nor false.", 100113,
					"StartingOperations", "");
		}
	}
}