de.espirit.firstspirit.access.editor.ReferenceEditorValueExample


package de.espirit.firstspirit.access.editor;

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


/**
 * Example how to get and store references from/to the ReferenceEditorValue
 *
 * @since 4.2.34
 */

public class ReferenceEditorValueExample {


	/**
	 * Stores the given IDProvider in the reference editor for the specified language
	 *
	 * @since 4.2.34
	 */
	public ReferenceEditorValue setValueForLanguage(final ReferenceEditorValue editor, final IDProvider idProvider, 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. create a new @{link TargetReference} using the TargetReferences factory.
		final TargetReference targetReference = TargetReferences.newInstance(language, idProvider, "");
		
		// 3. store this instance in the editor value
		editor.set(language, targetReference);

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


	/**
	 * Gets the language specific TargetReference from the given editor.
	 *
	 * @since 4.2.34
	 */
	public TargetReference getValueForLanguage(final ReferenceEditorValue editor, final Language language) {

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

		// 2. and return the reference container for further processing
		return targetReference;

	}


	/**
     * Gets the language specific IDProvider from the given editor.
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public IDProvider getReferenceForLanguage(final ReferenceEditorValue editor, final Language language) {

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

		// 2. get the referenced {@link IDProvider } element.
		IDProvider idProvider = null;
		if (targetReference != null) {
			idProvider = targetReference.get();
		}

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


	/**
	 * Sets the given section name in the target reference and loads the section.
	 *
	 * @since 4.2.34
	 */
	@Nullable
	public Section<?> setSectionForValue(final TargetReference targetReference, final String sectionName) {

		// 1. set an new section to reference. the section will be loaded by it's name.
		targetReference.setSectionName(sectionName);

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

	}
}