de.espirit.firstspirit.access.editor.PictureEditorValueExample


package de.espirit.firstspirit.access.editor;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.store.mediastore.Media;
import de.espirit.firstspirit.access.editor.value.GraphicalMedium;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import org.jetbrains.annotations.NotNull;


/**
 * Example how to store a picture (GraphicalMedium) into a PictureEditorValue
 *
 * @since 4.2.34
 */
public class PictureEditorValueExample {


	/**
	 * Stores given media as language specific value (specified by the given language) for the given editor
	 *
	 * @since 4.2.34
	 */
	public PictureEditorValue setValueForLanguage(final PictureEditorValue editor, @NotNull final Media media, final Language language) {
		// 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. check if given media is of type PICTURE
		if (media.getType() != Media.PICTURE) {
			throw new IllegalArgumentException("only media of type PICTURE allowed for PictureEditorValue");
		}

		// 3. get the editor data container for the given language
		final GraphicalMedium graphicalMedium = editor.get(language);

		// 4. store the given media in data container
		graphicalMedium.setMedium(media);

		// 5. return the instance for further processing
		return editor;

	}


	/**
	 * Gets the language specific value (GraphicalMedium) for the given language and picture editor
	 *
	 * @since 4.2.34
	 */
	public GraphicalMedium getValueForLanguage(final PictureEditorValue editor, final Language language) throws InvalidValueException {

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

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