View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.forms.modules.template.web;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.forms.business.CompositeDisplayType;
46  import fr.paris.lutece.plugins.forms.business.Control;
47  import fr.paris.lutece.plugins.forms.business.FormDisplay;
48  import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
49  import fr.paris.lutece.plugins.forms.business.FormResponse;
50  import fr.paris.lutece.plugins.forms.business.Group;
51  import fr.paris.lutece.plugins.forms.business.Question;
52  import fr.paris.lutece.plugins.forms.modules.template.business.TemplateDisplayHome;
53  import fr.paris.lutece.plugins.forms.modules.template.business.TemplateGroupHome;
54  import fr.paris.lutece.plugins.forms.modules.template.service.ITemplateService;
55  import fr.paris.lutece.plugins.forms.modules.template.service.TemplateService;
56  import fr.paris.lutece.plugins.forms.web.ICompositeDisplay;
57  import fr.paris.lutece.plugins.forms.web.entrytype.DisplayType;
58  import fr.paris.lutece.portal.service.spring.SpringContextService;
59  import fr.paris.lutece.portal.service.util.AppPropertiesService;
60  
61  public class CompositeTemplateGroupDisplay implements ICompositeDisplay
62  {
63      // Properties
64      private static final String PROPERTY_COMPOSITE_GROUP_ICON = "forms.composite.group.icon";
65      private static final String DEFAULT_GROUP_ICON = "indent";
66  
67      private ITemplateService _templateService = SpringContextService.getBean( TemplateService.BEAN_NAME );
68  
69      private final List<ICompositeDisplay> _listChildren = new ArrayList<>( );
70      private final FormDisplay _templateDisplay;
71      private Group _group;
72      private String _strIconName;
73  
74      /**
75       * Constructor
76       * 
77       * @param templateDisplay
78       *            the template display
79       * @param nIterationNumber
80       *            the iteration number
81       */
82      public CompositeTemplateGroupDisplay( FormDisplay templateDisplay, int nIterationNumber )
83      {
84          _templateDisplay = templateDisplay;
85  
86          initComposite( );
87      }
88  
89      /**
90       * Initializes the composite
91       * 
92       * @param formResponse
93       *            the form response
94       * @param nIterationNumber
95       *            the iteration number
96       */
97      private void initComposite( )
98      {
99          if ( !StringUtils.isEmpty( _templateDisplay.getCompositeType( ) ) )
100         {
101             _group = TemplateGroupHome.findByPrimaryKey( _templateDisplay.getCompositeId( ) );
102             _strIconName = AppPropertiesService.getProperty( PROPERTY_COMPOSITE_GROUP_ICON, DEFAULT_GROUP_ICON );
103         }
104 
105         List<FormDisplay> listTemplateDisplayChildren = TemplateDisplayHome.getFormDisplayListByParent( _templateDisplay.getStepId( ),
106                 _templateDisplay.getId( ) );
107         int iterationNumber = _group.getIterationMin( ) - 1;
108 
109         for ( int i = 0; i <= iterationNumber; i++ )
110         {
111             addChildren( listTemplateDisplayChildren, i );
112         }
113     }
114 
115     /**
116      * Adds children from the specified list of template displays
117      * 
118      * @param listTemplateDisplayChildren
119      *            the list of template displays
120      * @param nIterationNumber
121      *            the iteration number
122      */
123     private void addChildren( List<FormDisplay> listTemplateDisplayChildren, int nIterationNumber )
124     {
125         for ( FormDisplay templateDisplayChild : listTemplateDisplayChildren )
126         {
127             ICompositeDisplay composite = _templateService.templateDisplayToComposite( templateDisplayChild, nIterationNumber );
128             _listChildren.add( composite );
129         }
130     }
131 
132     @Override
133     public String getCompositeHtml( HttpServletRequest request, List<FormQuestionResponse> listFormQuestionResponse, Locale locale, DisplayType displayType )
134     {
135         // Only used when displaying the form in FO
136         return null;
137     }
138 
139     @Override
140     public void iterate( int nIdTemplateDisplay )
141     {
142         // Only used when displaying the form in FO
143     }
144 
145     @Override
146     public void removeIteration( HttpServletRequest request, int nIdGroupParent, int nIndexIterationToRemove, FormResponse formResponse )
147     {
148         // Only used when displaying the form in FO
149     }
150 
151     @Override
152     public List<ICompositeDisplay> getCompositeList( )
153     {
154         List<ICompositeDisplay> listCompositeDisplay = new ArrayList<>( );
155         listCompositeDisplay.add( this );
156 
157         for ( FormDisplay child : TemplateDisplayHome.getFormDisplayListByParent( _templateDisplay.getStepId( ), _templateDisplay.getId( ) ) )
158         {
159             ICompositeDisplay compositeChild = _templateService.templateDisplayToComposite( child, 0 );
160             listCompositeDisplay.addAll( compositeChild.getCompositeList( ) );
161         }
162         return listCompositeDisplay;
163     }
164 
165     @Override
166     public String getTitle( )
167     {
168         String strTitle = "";
169         if ( _group != null && StringUtils.isNotEmpty( _group.getTitle( ) ) )
170         {
171             strTitle = _group.getTitle( );
172         }
173         return strTitle;
174     }
175 
176     @Override
177     public String getType( )
178     {
179         return _group != null ? CompositeDisplayType.GROUP.getLabel( ) : StringUtils.EMPTY;
180     }
181 
182     @Override
183     public FormDisplay getFormDisplay( )
184     {
185         return _templateDisplay;
186     }
187 
188     @Override
189     public List<Control> getAllDisplayControls( )
190     {
191         List<Control> listDisplayControls = new ArrayList<>( );
192 
193         if ( _templateDisplay.getDisplayControl( ) != null )
194         {
195             listDisplayControls.add( _templateDisplay.getDisplayControl( ) );
196         }
197 
198         for ( ICompositeDisplay child : _listChildren )
199         {
200             listDisplayControls.addAll( child.getAllDisplayControls( ) );
201         }
202 
203         return listDisplayControls;
204     }
205 
206     @Override
207     public void addQuestions( List<Question> listQuestion )
208     {
209         for ( ICompositeDisplay child : _listChildren )
210         {
211             child.addQuestions( listQuestion );
212         }
213     }
214 
215     @Override
216     public String getIcon( )
217     {
218         return _strIconName;
219     }
220 
221     @Override
222     public void addModel( Map<String, Object> model )
223     {
224         // Only used when displaying the form in FO
225     }
226 
227     @Override
228     public boolean isVisible( )
229     {
230         // Only used when displaying the form in FO
231         return false;
232     }
233 
234     @Override
235     public ICompositeDisplay filter( List<Integer> listQuestionIds )
236     {
237         // Only used when displaying the form in FO
238         return null;
239     }
240 }