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.workflow.modules.forms.web;
35  
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.Map;
39  import java.util.stream.Collectors;
40  
41  import javax.inject.Inject;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.forms.business.FormResponse;
47  import fr.paris.lutece.plugins.forms.business.Question;
48  import fr.paris.lutece.plugins.forms.business.Step;
49  import fr.paris.lutece.plugins.forms.business.StepHome;
50  import fr.paris.lutece.plugins.forms.web.entrytype.DisplayType;
51  import fr.paris.lutece.plugins.workflow.modules.forms.business.ResubmitFormResponse;
52  import fr.paris.lutece.plugins.workflow.modules.forms.service.IResubmitFormResponseService;
53  import fr.paris.lutece.plugins.workflow.modules.forms.service.signrequest.ResubmitFormResponseRequestAuthenticatorService;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.message.SiteMessage;
56  import fr.paris.lutece.portal.service.message.SiteMessageException;
57  import fr.paris.lutece.portal.service.plugin.Plugin;
58  import fr.paris.lutece.portal.service.security.UserNotSignedException;
59  import fr.paris.lutece.portal.service.spring.SpringContextService;
60  import fr.paris.lutece.portal.service.template.AppTemplateService;
61  import fr.paris.lutece.portal.web.constants.Messages;
62  import fr.paris.lutece.portal.web.xpages.XPage;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  import fr.paris.lutece.util.signrequest.AbstractPrivateKeyAuthenticator;
65  
66  public class ResubmitFormResponseApp extends AbstractFormResponseApp<ResubmitFormResponse>
67  {
68      private static final long serialVersionUID = -6753642997148910492L;
69  
70      // TEMPLATES
71      private static final String TEMPLATE_RESUBMIT_FORM = "skin/plugins/workflow/modules/forms/resubmit_form.html";
72  
73      // MESSAGES
74      private static final String MESSAGE_RECORD_ALREADY_COMPLETED = "module.workflow.forms.message.response_already_completed";
75      private static final String PROPERTY_XPAGE_RESUBMIT_FORM_PAGETITLE = "module.workflow.forms.resubmit_form.page_title";
76      private static final String PROPERTY_XPAGE_RESUBMIT_FORM_PATHLABEL = "module.workflow.forms.resubmit_form.page_label";
77  
78      // PARAMETERS
79      private static final String PARAMETER_ID_HISTORY = "id_history";
80      private static final String PARAMETER_ID_TASK = "id_task";
81  
82      // MARKS
83      private static final String MARK_RESUBMIT_FORM = "resubmit_form";
84  
85      // SERVICES
86      @Inject
87      private IResubmitFormResponseService _resubmitFormResponseService = SpringContextService.getBean( "workflow-forms.taskResubmitResponseService" );
88  
89      @Override
90      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws UserNotSignedException, SiteMessageException
91      {
92          XPage page = null;
93          if ( !getRequestAuthenticator( ).isRequestAuthenticated( request ) )
94          {
95              // Throws Exception
96              _formsTaskService.setSiteMessage( request, Messages.USER_ACCESS_DENIED, SiteMessage.TYPE_STOP, request.getParameter( PARAMETER_URL_RETURN ) );
97              return null;
98          }
99          String strIdHistory = request.getParameter( PARAMETER_ID_HISTORY );
100         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
101 
102         if ( StringUtils.isNumeric( strIdHistory ) && StringUtils.isNumeric( strIdTask ) )
103         {
104             int nIdHistory = Integer.parseInt( strIdHistory );
105             int nIdTask = Integer.parseInt( strIdTask );
106 
107             ResubmitFormResponse resubmitFormResponse = _resubmitFormResponseService.find( nIdHistory, nIdTask );
108             if ( resubmitFormResponse != null && !resubmitFormResponse.isComplete( ) )
109             {
110                 if ( isRecordStateValid( resubmitFormResponse, request.getLocale( ) ) )
111                 {
112                     doAction( request, resubmitFormResponse, nIdTask, nIdHistory );
113                     page = getFormResponseXPage( request, resubmitFormResponse );
114                 }
115                 else
116                 {
117                     _formsTaskService.setSiteMessage( request, Messages.USER_ACCESS_DENIED, SiteMessage.TYPE_STOP,
118                             request.getParameter( PARAMETER_URL_RETURN ) );
119                 }
120             }
121             else
122             {
123                 _formsTaskService.setSiteMessage( request, MESSAGE_RECORD_ALREADY_COMPLETED, SiteMessage.TYPE_INFO,
124                         request.getParameter( PARAMETER_URL_RETURN ) );
125             }
126         }
127         else
128         {
129             _formsTaskService.setSiteMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP, request.getParameter( PARAMETER_URL_RETURN ) );
130         }
131 
132         return page;
133     }
134 
135     /**
136      * Get the ResubmitFormResponse page
137      * 
138      * @param request
139      *            the HTTP request
140      * @param ResubmitFormResponse
141      *            the ResubmitFormResponse
142      * @return a XPage
143      */
144     @Override
145     protected XPage getFormResponseXPage( HttpServletRequest request, ResubmitFormResponse resubmitFormResponse )
146     {
147         XPage page = new XPage( );
148 
149         FormResponse formResponse = _formsTaskService.getFormResponseFromIdHistory( resubmitFormResponse.getIdHistory( ) );
150         List<Question> listQuestions = _resubmitFormResponseService.getListQuestionToEdit( formResponse, resubmitFormResponse.getListResubmitReponseValues( ) );
151 
152         List<Step> listStep = listQuestions.stream( ).map( Question::getStep ).map( Step::getId ).distinct( ).map( StepHome::findByPrimaryKey )
153                 .collect( Collectors.toList( ) );
154 
155         List<String> listStepDisplayTree = _formsTaskService.buildFormStepDisplayTreeList( request, listStep, listQuestions, formResponse,
156                 DisplayType.RESUBMIT_FRONTOFFICE );
157 
158         Map<String, Object> model = initModelFormPage( request, formResponse, listStepDisplayTree );
159         model.put( MARK_RESUBMIT_FORM, resubmitFormResponse );
160 
161         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_RESUBMIT_FORM, request.getLocale( ), model );
162 
163         page.setTitle( I18nService.getLocalizedString( PROPERTY_XPAGE_RESUBMIT_FORM_PAGETITLE, request.getLocale( ) ) );
164         page.setPathLabel( I18nService.getLocalizedString( PROPERTY_XPAGE_RESUBMIT_FORM_PATHLABEL, request.getLocale( ) ) );
165         page.setContent( template.getHtml( ) );
166 
167         return page;
168     }
169 
170     /**
171      * Do edit a response
172      * 
173      * @param request
174      *            the HTTP request
175      * @param response
176      *            the response
177      * @return true if the record must be updated, false otherwise
178      * @throws SiteMessageException
179      *             a site message if there is a problem
180      */
181     @Override
182     protected boolean doEditResponse( HttpServletRequest request, ResubmitFormResponse response, int idTask, int idHistory ) throws SiteMessageException
183     {
184         if ( isRecordStateValid( response, request.getLocale( ) ) )
185         {
186             if ( _resubmitFormResponseService.doEditResponseData( request, response, idTask, idHistory ) )
187             {
188                 _resubmitFormResponseService.doChangeResponseState( response, request.getLocale( ) );
189 
190                 _resubmitFormResponseService.doCompleteResponse( response );
191 
192                 return true;
193             }
194             return false;
195         }
196         else
197         {
198             _formsTaskService.setSiteMessage( request, Messages.USER_ACCESS_DENIED, SiteMessage.TYPE_STOP, request.getParameter( PARAMETER_URL_RETURN ) );
199         }
200         return false;
201     }
202 
203     @Override
204     protected ResubmitFormResponse findAbstractCompleteFormResponse( int nIdHistory, int nIdTask )
205     {
206         return _resubmitFormResponseService.find( nIdHistory, nIdTask );
207     }
208 
209     @Override
210     protected AbstractPrivateKeyAuthenticator getRequestAuthenticator( )
211     {
212         return ResubmitFormResponseRequestAuthenticatorService.getRequestAuthenticator( );
213     }
214 
215     @Override
216     protected boolean isRecordStateValid( ResubmitFormResponse resubmitFormResponse, Locale locale )
217     {
218         return _resubmitFormResponseService.isRecordStateValid( resubmitFormResponse, locale );
219     }
220 }