de.espirit.firstspirit.access.editor.PageRefEditorValueExample


package de.espirit.firstspirit.access.editor;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.editor.value.ElementReference;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.store.IDProvider;
import de.espirit.firstspirit.access.store.Referenceable;
import de.espirit.firstspirit.access.store.pagestore.Section;
import org.jetbrains.annotations.Nullable;


/**
 * Example how to store and get the ElementReference from the PageRefEditorValue
 *
 * @since 4.2.34
 */
public class PageRefEditorValueExample {


	/**
	 * Stores given ElementReference as language specific value (specified by the given language) for the given PageRefEditor
	 *
	 * @since 4.2.34
	 */
	public PageRefEditorValue setValueForLanguage(final PageRefEditorValue editor, final ElementReference referenceContainer, final Referenceable object, final Language language) throws InvalidValueException {

		// 1. check if language is provided if editor is language dependent ("useLanguages='yes'" in gom syntax)
		if (editor.isLanguageDependent() && (language == null)) {
			throw new NullPointerException("Language is missing!");
		}

		// 2. store the reference to reference container
		referenceContainer.set(object);

		// 3. store this instance in the editor value
		editor.set(language, referenceContainer);

		// 4. and return the instance for further processing
		return editor;
	}


	/**
	 * Gets the language specific Referenceable for the given language and pageref editor
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public Referenceable getValueForLanguage(final PageRefEditorValue editor, final Language language) {

		// 1. get the stored instance in the editor value
		final ElementReference elementReference = editor.get(language);

		// 2. and return the referenced object from the container for further processing
		if (elementReference != null) {
			return elementReference.get();
		}
		return null;
	}


	/**
	 * Gets the language specific IDProvider for the given language and pageref editor
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public IDProvider getReferenceForLanguage(final PageRefEditorValue editor, final Language language) {

		// 1. get the {@link TargetReference } container.
		final ElementReference elementReference = editor.get(language);

		// 2. get the referenced {@link Referenceable } element.
		Referenceable referenceable = null;
		if (elementReference != null) {
			referenceable = elementReference.get();
		}

		// 3. and return the instance for further processing
		return referenceable;
	}


	/**
	 * Stores the section name to the given referenceContainer and loads the section.
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public Section<?> setSectionNameForValue(final ElementReference referenceContainer, final String sectionName) {

		// 1. set a new section to reference. The section will be loaded by it's name.
		referenceContainer.setSectionName(sectionName);

		// 2. return the loaded section instance for further processing
		return referenceContainer.getSection();

	}


	/**
	 * Sets the section to the given referenceContainer and loads the section.
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public Section<?> setSectionForValue(final ElementReference referenceContainer, final Section<?> section) {

		// 1. set a new section to reference.
		referenceContainer.setSection(section);

		// 2. return the loaded section instance for further processing
		return referenceContainer.getSection();

	}
}