1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.form.templates.components;
35
36 import info.magnolia.jcr.util.MetaDataUtil;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.jcr.util.PropertyUtil;
39 import info.magnolia.module.form.engine.FormStepState;
40 import info.magnolia.module.form.stepnavigation.Link;
41 import info.magnolia.module.form.stepnavigation.LinkImpl;
42 import info.magnolia.module.form.templates.components.multistep.NavigationUtils;
43 import info.magnolia.module.form.templates.components.multistep.SubStepFormEngine;
44 import info.magnolia.objectfactory.Components;
45 import info.magnolia.registry.RegistrationException;
46 import info.magnolia.rendering.context.RenderingContext;
47 import info.magnolia.rendering.model.RenderingModel;
48 import info.magnolia.rendering.template.RenderableDefinition;
49 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
50 import info.magnolia.templating.functions.TemplatingFunctions;
51
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.Iterator;
55 import java.util.List;
56
57 import javax.jcr.Node;
58 import javax.jcr.RepositoryException;
59
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63
64
65
66
67
68
69 public class SubStepFormModel extends AbstractFormModel<RenderableDefinition> {
70
71 private static Logger log = LoggerFactory.getLogger(SubStepFormModel.class);
72
73 public SubStepFormModel(Node content, RenderableDefinition definition, RenderingModel<?> parent, TemplatingFunctions functions) {
74 super(content, definition, parent, functions);
75 }
76
77 @Override
78 protected SubStepFormEngine createFormEngine() throws RepositoryException {
79
80 Node startPage = Components.getComponent(RenderingContext.class).getMainContent().getParent();
81
82 Node startParagraphNode = NavigationUtils.findParagraphOfType(startPage, FormParagraph.class);
83
84 if (startParagraphNode == null) {
85
86 throw new IllegalStateException("FormStepParagraph on page [" + NodeUtil.getPathIfPossible(content) + "] could not find a FormParagraph in its parent");
87 }
88
89 String templateId = MetaDataUtil.getTemplate(startParagraphNode);
90 FormParagraph startParagraph = null;
91 try {
92 startParagraph = (FormParagraph) Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
93 } catch (RegistrationException e) {
94 throw new RuntimeException(e.getMessage(), e);
95 }
96 SubStepFormEngine subStepFormEngine = Components.newInstance(SubStepFormEngine.class, startParagraphNode, startParagraph, startPage);
97
98 subStepFormEngine.setStartPage(startPage);
99 return subStepFormEngine;
100 }
101
102
103 public Collection<Link> getPreviousStepsNavigation() throws RepositoryException {
104 List<Link> items = new ArrayList<Link>();
105 Node currentPage = Components.getComponent(RenderingContext.class).getMainContent();
106 Node currentStepContent = NavigationUtils.findParagraphOfType(currentPage, FormStepParagraph.class);
107 if(this.getFormState() != null) {
108 Iterator<FormStepState> stepsIt = this.getFormState().getSteps().values().iterator();
109 while (stepsIt.hasNext()) {
110 FormStepState step = stepsIt.next();
111 Node stepNode = NodeUtil.getNodeByIdentifier(getNode().getSession().getWorkspace().getName(), step.getParagraphUuid());
112 if(step.getParagraphUuid().equals(NodeUtil.getNodeIdentifierIfPossible(currentStepContent))) {
113 break;
114 }
115 items.add((new LinkImpl(stepNode)));
116 }
117 }
118 return items;
119 }
120
121 public Collection<Link> getNextStepsNavigation() throws RepositoryException {
122
123 List<Link> items = new ArrayList<Link>();
124 Node currentPage = Components.getComponent(RenderingContext.class).getMainContent();
125 List<Node> list = NavigationUtils.getSameTypeSiblingsAfter(currentPage);
126
127 for (Node stepNode: list) {
128 Node currentStepContent = NavigationUtils.findParagraphOfType(currentPage, FormStepParagraph.class);
129 if (currentStepContent != null) {
130 items.add((new LinkImpl(stepNode)));
131 }
132 }
133 return items;
134 }
135
136 public boolean getDisplayNavigation() throws RepositoryException {
137
138 Node currentPage = Components.getComponent(RenderingContext.class).getMainContent();
139 boolean displayStepNavigation = false;
140 Node formParagraph = NavigationUtils.findParagraphOfType(currentPage, FormParagraph.class);
141 if (formParagraph == null) {
142 formParagraph = NavigationUtils.findParagraphOfType(currentPage.getParent(), FormParagraph.class);
143 }
144 if (formParagraph != null) {
145 displayStepNavigation = PropertyUtil.getBoolean(formParagraph, "displayStepNavigation", false);
146 }
147 return displayStepNavigation;
148 }
149 }