de.espirit.firstspirit.access.editor.DatasetEditorValueExample


package de.espirit.firstspirit.access.editor;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.editor.value.DatasetContainer;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.store.contentstore.Content2;
import de.espirit.firstspirit.access.store.contentstore.Dataset;
import de.espirit.or.schema.Entity;

import java.util.List;

/**
 * Example how to get the value of a DatasetEditorValue and how to store a value into a DatasetEditorValue.
 *
 * @since 4.2.34
 */
public class DatasetEditorValueExample {

	/**
	 * Stores the value into the DatasetEditorValue for the given language.
	 *
	 * @since 4.2.34
	 */
	public void setValueForLanguage(final DatasetEditorValue editor, final Content2 content, 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. retrieve data from Content2 element
		final List<? extends Entity> data = content.getData();

		// 3. retrieve the first Entity out of the Data list
		final Entity entity = data.get(0);

		// 4. retrieve the Dataset for the Entity from the content element
		final Dataset dataset = content.getDataset(entity);

		// 5. create a storable container object
		final DatasetContainer value = editor.createDatasetContainer(dataset.getEntity(), dataset.getTableTemplate(), language);

		// 6. store value into the editor value
		editor.set(language, value);
	}


	/**
	 * Stores the value into the DatasetEditorValue for the given language.
	 *
	 * @since 4.2.34
	 */
	public DatasetContainer getValueForLanguage(final DatasetEditorValue editor, final Language language) {

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

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