Interface SiteStoreVariableFormData

All Superinterfaces:
FormData

public interface SiteStoreVariableFormData extends FormData
Interface definition for a form data container containing sitestore variables.
Since:
4.2.440
See Also:
Example:
Example how to modify variables of SiteStoreFolder
import 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);
		}
	}
}
  • Method Summary

    Modifier and Type
    Method
    Description
    createVariable(Language language, String variableName)
    Creates a new variable with desired name in this container and return the created form field for the given language.
    @NotNull FormField<String>
    get(Language language, String fieldName)
    Provides a property container describing the field with the given name.
    void
    removeVariable(String variableName)
    Removes the variable with desired name from this container.

    Methods inherited from interface de.espirit.firstspirit.forms.FormData

    getForm
  • Method Details

    • createVariable

      FormField<String> createVariable(Language language, String variableName)
      Creates a new variable with desired name in this container and return the created form field for the given language.
      Since:
      4.2.440
    • removeVariable

      void removeVariable(String variableName)
      Removes the variable with desired name from this container.
      Since:
      4.2.440
    • get

      @NotNull @NotNull FormField<String> get(Language language, String fieldName) throws NoSuchFormFieldException
      Provides a property container describing the field with the given name.

      Use FormData.getForm().appendEditorNames(null) to get a list of valid field names. For language indepent fields (GomFormElement#usesLanguages() delivers false) one could supply null for the parameter language.

      Specified by:
      get in interface FormData
      Parameters:
      language - The language to get the property for or null for language independent fields.
      fieldName - The name of the field to look up.
      Returns:
      The field's property container.
      Throws:
      NoSuchFormFieldException - If there is no entry for the given name.
      Since:
      4.2.440