package extended.forms; import javax.jcr.Node; import javax.jcr.RepositoryException; import info.magnolia.jcr.util.MetaDataUtil; import info.magnolia.jcr.util.NodeUtil; import info.magnolia.module.form.templates.components.FormParagraph; import info.magnolia.module.form.templates.components.SubStepFormModel; import info.magnolia.module.form.templates.components.multistep.NavigationUtils; import info.magnolia.module.form.templates.components.multistep.SubStepFormEngine; import info.magnolia.objectfactory.Components; import info.magnolia.registry.RegistrationException; import info.magnolia.rendering.context.RenderingContext; import info.magnolia.rendering.model.RenderingModel; import info.magnolia.rendering.template.RenderableDefinition; import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry; import info.magnolia.templating.functions.TemplatingFunctions; /** * Custom sub step form model which allows sub steps to save the temporary data in the session when * the user clicks 'back'. */ public class ExtendedSubStepFormModel extends SubStepFormModel { /** * Constructor. * @param content the content * @param definition the definition * @param parent the parent * @param functions the templating function */ public ExtendedSubStepFormModel(Node content, RenderableDefinition definition, RenderingModel parent, TemplatingFunctions functions) { super(content, definition, parent, functions); } @Override protected SubStepFormEngine createFormEngine() throws RepositoryException { Node startPage = Components.getComponent(RenderingContext.class).getMainContent().getParent(); Node startParagraphNode = NavigationUtils.findParagraphOfType(startPage, FormParagraph.class); if (startParagraphNode == null) { // Ideally we would return a view that describes the problem and how to resolve it throw new IllegalStateException("FormStepParagraph on page [" + NodeUtil.getPathIfPossible(content) + "] could not find a FormParagraph in its parent"); } String templateId = MetaDataUtil.getTemplate(startParagraphNode); FormParagraph startParagraph = null; try { startParagraph = (FormParagraph) Components.getComponent(TemplateDefinitionRegistry.class) .getTemplateDefinition(templateId); } catch (RegistrationException e) { throw new RuntimeException(e.getMessage(), e); } ExtendedSubStepFormEngine subStepFormEngine = Components.newInstance(ExtendedSubStepFormEngine.class, startParagraphNode, startParagraph, startPage); subStepFormEngine.setStartPage(startPage); return subStepFormEngine; } }