de.espirit.firstspirit.access.editor.ContentAreaListValueExample


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.SectionList;
import de.espirit.firstspirit.access.project.Project;
import de.espirit.firstspirit.access.store.pagestore.Section;
import de.espirit.firstspirit.access.store.templatestore.SectionTemplate;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;


/**
 * Example how to get the list of sections and create new section(s) from a ContentAreaListValue and store a section into a ContentAreaListValue.
 *
 * @since 4.2.34
 */
public class ContentAreaListValueExample {


	/**
	 * Creates a new section in the given ContentAreaListValue editor - using a section template of the given template store.
	 *
	 * @since 4.2.200
	 */
	public Section createSection(final ContentAreaListValue editor, final Project project) throws InvalidValueException {

		// 1. get project masterlanguage
		final Language masterLanguage = project.getMasterLanguage();

		// 2. get the Section List
		// - ContentAreaList is language independent by contract, therefore it doesn't matter which language is used
		final SectionList sectionList = editor.get(masterLanguage);

		// 3. use the first section template of allowed templates
		final Iterator<SectionTemplate> allowedIterator = editor.getAllowedSectionTemplates().iterator();
		if (! allowedIterator.hasNext()) {
			throw new IllegalStateException("no allowed templates defined for this editor");
		}
		final SectionTemplate sectionTemplate = allowedIterator.next();

		// 4. create the new section -> will be added automatically
		final Section section = sectionList.create("NewSection", sectionTemplate);

		// 5. store the modified section list to the editor
		editor.set(masterLanguage, sectionList);

		return section;
	}


	/**
	 * Returns the first section of the given ContentAreaListValue editor.
	 *
	 * @since 4.2.200
	 */
	@Nullable
	public Section getFirstSection(final ContentAreaListValue editor, final Project project) {

		// 1. get project masterlanguage
		final Language masterLanguage = project.getMasterLanguage();

		// 2. get the SectionList
		// - ContentAreaList is language independent by contract, therefore it doesn't matter which language is used
		final SectionList sectionList = editor.get(masterLanguage);

		// 3. get the first Section out of the SectionList
		Section section = null;
		if ( ! sectionList.isEmpty()) {
			section = sectionList.get(0);
		}

		return section;
	}


	/**
	 * Returns the SectionList (persistence object) of the given ContentAreaListValue editor
	 *
	 * @since 4.2.200
	 */
	@Nullable
	public SectionList getSectionList(final ContentAreaListValue editor, final Project project) {
		// 1. get project masterlanguage
		final Language masterLanguage = project.getMasterLanguage();

		// 2. get the SectionList
		// - ContentAreaList is language independent by contract, therefore it doesn't matter which language is used
		return editor.get(masterLanguage);
	}
}