de.espirit.firstspirit.access.store.sitestore.SiteStoreFolderVariablesExample


package de.espirit.firstspirit.access.store.sitestore;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.editor.SiteStoreVariableValue;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.store.Data;
import de.espirit.firstspirit.access.store.DataValue;
import de.espirit.firstspirit.access.store.ElementDeletedException;
import de.espirit.firstspirit.access.store.LockException;
import de.espirit.firstspirit.forms.FormField;
import de.espirit.firstspirit.forms.NoSuchFormFieldException;


/**
 * Examples how to modify variables of SiteStoreFolder.
 *
 * @since 4.2.34
 */
public class SiteStoreFolderVariablesExample {

	/**
	 * Example how to add a variable.
	 *
	 * @since 4.2.34
	 * @deprecated since 4.2.440 - see {@link #addVariable_FormData(SiteStoreFolder)} instead
	 */
	@Deprecated
	public void addVariable(final SiteStoreFolder folder) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvalidValueException, LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		try {
			// 2. retrieve Data (variables) from SiteStoreFolder
			final Data data = folder.getData();

			// 3. try to retrieve a DataValue (variable) with desired name
			DataValue dataValue = data.get(variableName);

			// 4. if a DataValue is existent, no new one can created using the desired name
			if (dataValue != null) {
				throw new IllegalArgumentException("Variable named " + variableName + " already existent!");
			}

			// 5. create new DataValue (variable)
			dataValue = data.create(variableName, folder.getProject().getUserService(), null);

			// 6. retrieve the EditorValue from DataValue
			final SiteStoreVariableValue editor = (SiteStoreVariableValue) dataValue.getEditor();

			// 7. set desired value for each language
			for (final Language language : folder.getProject().getLanguages()) {
				editor.set(language, "myValue");
			}

			// 8. don't forget to set changed data back to folder
			folder.setData(data);

			// 9. save changes and unlock the folder
			folder.save("add variable", false);
		} finally {
			folder.setLock(false, false);
		}
	}


	/**
	 * Example how to add a variable.
	 *
	 * @since 4.2.440
	 */
	public void addVariable_FormData(final SiteStoreFolder folder) throws LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		try {
			// 2. retrieve FormData (variables) from SiteStoreFolder
			final SiteStoreVariableFormData formData = folder.getFormData();

			// 3. try to retrieve a DataValue (variable) with desired name
			FormField<String> formField;
			final Language masterLanguage = folder.getProject().getMasterLanguage();
			try {
				formField = formData.get(masterLanguage, variableName);
			} catch (NoSuchFormFieldException e) {
				formField = formData.createVariable(masterLanguage, variableName);
			}

			// 4. set new value
			formField.set("myValue");

			// 5. don't forget to set changed data back to folder
			folder.setFormData(formData);

			// 6. save changes and unlock the folder
			folder.save("add variable", false);
		} finally {
			folder.setLock(false, false);
		}
	}


	/**
	 * Example how to change a variables value.
	 *
	 * @since 4.2.34
	 * @deprecated since 4.2.440 - see {@link #setVariableValue_FormData(SiteStoreFolder)} instead
	 */
	@Deprecated
	public void setVariableValue(final SiteStoreFolder folder) throws InvalidValueException, LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		try {
			// 2. retrieve Data (variables) from SiteStoreFolder
			final Data data = folder.getData();

			// 3. try to retrieve a DataValue (variable) with desired name
			final DataValue dataValue = data.get(variableName);

			// 4. check existence of the DataValue (variable)
			if (dataValue == null) {
				throw new IllegalArgumentException("Variable named " + variableName + " doesn't exist!");
			}

			// 5. retrieve the EditorValue from DataValue
			final SiteStoreVariableValue editor = (SiteStoreVariableValue) dataValue.getEditor();

			// 6. set desired value for each language
			for (final Language language : folder.getProject().getLanguages()) {
				editor.set(language, "myNewValue");
			}

			// 7. don't forget to set changed data back to folder
			folder.setData(data);

			// 8. save changes and unlock the folder
			folder.save("set variable value", false);
		} finally {
			folder.setLock(false, false);
		}
	}


	/**
	 * Example how to change a variable value.
	 *
	 * @since 4.2.440
	 */
	public void setVariableValue_FormData(final SiteStoreFolder folder) throws InvalidValueException, LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		// 2. retrieve FormData (variables) from SiteStoreFolder
		final SiteStoreVariableFormData formData = folder.getFormData();

		try {
			// 3. try to retrieve a form field with desired name
			final FormField<String> field = formData.get(folder.getProject().getMasterLanguage(), variableName);

			// 4. set new value
			field.set("myNewValue");

			// 5. don't forget to set changed formdata back to folder
			folder.setFormData(formData);

			// 6. save changes and unlock the folder
			folder.save("set variable value", false);
		} finally {
			folder.setLock(false, false);
		}
	}


	/**
	 * Example how to remove a variable.
	 *
	 * @since 4.2.440
	 * @deprecated since 4.2.440 - see {@link #removeVariable_FormData(SiteStoreFolder)} instead
	 */
	@Deprecated
	public void removeVariable(final SiteStoreFolder folder) throws LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		try {
			// 2. retrieve Data (variables) from SiteStoreFolder
			final Data data = folder.getData();

			// 3. try to retrieve a DataValue (variable) with desired name
			final DataValue dataValue = data.get(variableName);

			// 4. check existence of the DataValue (variable)
			if (dataValue == null) {
				throw new IllegalArgumentException("Variable named " + variableName + " doesn't exist!");
			}

			// 5. remove the DataValue
			data.remove(dataValue);

			// 6. don't forget to set changed data back to folder
			folder.setData(data);

			// 7. save changes and unlock the folder
			folder.save("remove variable", false);
		} finally {
			folder.setLock(false, false);
		}
	}


	/**
	 * Example how to remove a variable.
	 *
	 * @since 4.2.34
	 */
	public void removeVariable_FormData(final SiteStoreFolder folder) throws LockException, ElementDeletedException {

		final String variableName = "myVariable";

		// 1. lock the folder
		folder.setLock(true, false);

		try {
			// 2. retrieve FormData (variables) from SiteStoreFolder
			final SiteStoreVariableFormData formData = folder.getFormData();

			// 3. remove variable with desired name
			formData.removeVariable(variableName);

			// 4. don't forget to set changed formdata back to folder
			folder.setFormData(formData);

			// 5. save changes and unlock the folder
			folder.save("remove variable", false);
		} finally {
			folder.setLock(false, false);
		}
	}
}