View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Implements behaviour for sub pages in multi step forms. Finds the next step by searching for the first subsequent
65   * sibling that has a paragraph that uses or extends {@link info.magnolia.module.form.templates.components.FormStepParagraph}.
66   *
67   * @version $Id$
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();//MgnlContext.getAggregationState().getMainContent().getParent().getJCRNode();
81  
82          Node startParagraphNode = NavigationUtils.findParagraphOfType(startPage, FormParagraph.class);
83  
84          if (startParagraphNode == null) {
85              // Ideally we would return a view that describes the problem and how to resolve it
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          //FIXME SCRUM-628: once IoC will support constructor containing several paramater with the same type we could remove the next line.
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 }