de.espirit.firstspirit.access.editor.LinkEditorValueExample


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.link.Link;
import de.espirit.firstspirit.access.store.templatestore.LinkTemplate;

import java.util.List;


/**
 * Example how to create and store a link into a LinkEditorValue.
 *
 * @since 4.2.34
 */
public class LinkEditorValueExample {


	/**
	 * Given a LinkEditorValue and a Language create a Link instance and store this in the editor.
	 * For language independent link editors (which should be rare) the provided language could be <code>null</code>.
	 *
	 * @since 4.2.34
	 */
	public Link setValueForLanguage(final LinkEditorValue linkEditor, final Language language) throws InvalidValueException {
		// 1. check if language is provided if editor is language dependent ("useLanguages='yes'" in gom syntax)
		if (linkEditor.isLanguageDependent() && (language == null)) {
			throw new NullPointerException("Language is missing!");
		}
		// 2. get the list of supported link templates (LINKEDITORS tag in gom syntax)
		final List<LinkTemplate> allowedLinkTemplates = linkEditor.getAllowedLinkTemplates();
		if (allowedLinkTemplates.isEmpty()) {
			// a link without allowed templates - configuration problem
			throw new IllegalStateException("The provided link editor allows no links!");
		}
		// 3. choose an arbitrary link template
		final LinkTemplate linkTemplate = allowedLinkTemplates.get(0);
		// 4. create a link instance for this template
		final Link link = linkTemplate.createLink(language);
		// 5. store this instance in the editor value
		linkEditor.set(language, link);
		// 6. and return the instance for further processing
		return link;
	}


	/**
	 * Returns the value of this LinkEditorValue for the given language.
	 *
	 * @since 4.2.34
	 */
	public Link getValueForLanguage(final LinkEditorValue editor, final Language language) {

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

		// 2. and return the value for further processing
		return value;
	}
}