View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.tipiforms.service.task;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.inject.Inject;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.apache.commons.lang3.math.NumberUtils;
43  
44  import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
45  import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
46  import fr.paris.lutece.plugins.forms.business.FormResponse;
47  import fr.paris.lutece.plugins.forms.business.FormResponseHome;
48  import fr.paris.lutece.plugins.forms.business.Question;
49  import fr.paris.lutece.plugins.genericattributes.business.Response;
50  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
51  import fr.paris.lutece.plugins.workflow.modules.tipi.service.ITipiRefDetHistoryService;
52  import fr.paris.lutece.plugins.workflow.modules.tipi.service.ITipiService;
53  import fr.paris.lutece.plugins.workflow.modules.tipi.service.task.AbstractTipiProviderTask;
54  import fr.paris.lutece.plugins.workflow.modules.tipiforms.business.task.TaskTipiFormsProviderConfig;
55  import fr.paris.lutece.plugins.workflow.modules.tipiforms.business.task.TaskTipiFormsProviderConfigDAO;
56  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
58  import fr.paris.lutece.portal.service.i18n.I18nService;
59  import fr.paris.lutece.portal.service.util.AppException;
60  
61  /**
62   * 
63   * This class is a {@code Task} which provides pieces of information for the TIPI transaction from a form
64   *
65   */
66  public class TipiFormsProviderTask extends AbstractTipiProviderTask
67  {
68      // Message
69      private static final String MESSAGE_TASK_TITLE = "module.workflow.tipiforms.task_forms_provider_title";
70  
71      private final TaskTipiFormsProviderConfigDAO _taskTipiFormsProviderConfigDAO;
72  
73      /**
74       * Constructor
75       * 
76       * @param taskTipiFormsProviderConfigDAO
77       *            the configuration DAO
78       * @param resourceHistoryService
79       *            the resource history service
80       * @param tipiService
81       *            the tipi service
82       * @param tipiRefDetHistoryService
83       *            the RefDet history service
84       */
85      @Inject
86      public TipiFormsProviderTask( TaskTipiFormsProviderConfigDAO taskTipiFormsProviderConfigDAO, IResourceHistoryService resourceHistoryService,
87              ITipiService tipiService, ITipiRefDetHistoryService tipiRefDetHistoryService )
88      {
89          super( resourceHistoryService, tipiService, tipiRefDetHistoryService );
90  
91          _taskTipiFormsProviderConfigDAO = taskTipiFormsProviderConfigDAO;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getTitle( Locale local )
99      {
100         return I18nService.getLocalizedString( MESSAGE_TASK_TITLE, local );
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     protected String provideRefDet( ResourceHistory resourceHistory )
108     {
109         FormResponse formResponse = findFormResponseFrom( resourceHistory );
110         TaskTipiFormsProviderConfig config = findConfig( );
111 
112         return findResponseValue( formResponse, config.getIdRefDetQuestion( ) );
113     }
114 
115     /**
116      * Finds a form response from the specified resource history
117      * 
118      * @param resourceHistory
119      *            the resource history
120      * @return the form response
121      */
122     private FormResponse findFormResponseFrom( ResourceHistory resourceHistory )
123     {
124         FormResponse formResponse = null;
125 
126         if ( FormResponse.RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) ) )
127         {
128             formResponse = FormResponseHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
129         }
130         else
131         {
132             throw new AppException( "This task must be used with a form" );
133         }
134 
135         return formResponse;
136     }
137 
138     /**
139      * Finds the configuration linked to this task
140      * 
141      * @return the configuration
142      */
143     private TaskTipiFormsProviderConfig findConfig( )
144     {
145         return _taskTipiFormsProviderConfigDAO.load( getId( ) );
146     }
147 
148     /**
149      * Finds the response value in the specified form response by using the specified question id
150      * 
151      * @param formResponse
152      *            the form response
153      * @param nIdQuestion
154      *            the question id
155      * @return the response value
156      */
157     private String findResponseValue( FormResponse formResponse, int nIdQuestion )
158     {
159         String strValue = StringUtils.EMPTY;
160         boolean bFound = false;
161 
162         for ( FormQuestionResponse formQuestionResponse : FormQuestionResponseHome.getFormQuestionResponseListByFormResponse( formResponse.getId( ) ) )
163         {
164             Question question = formQuestionResponse.getQuestion( );
165 
166             if ( question.getId( ) == nIdQuestion )
167             {
168                 List<Response> listResponse = formQuestionResponse.getEntryResponse( );
169 
170                 if ( listResponse.size( ) > 1 )
171                 {
172                     throw new AppException( "The question contains several responses !" );
173                 }
174 
175      
176                strValue= EntryTypeServiceManager.getEntryTypeService( question.getEntry( ) ).getResponseValueForExport( question.getEntry( ), null, listResponse.get( 0 ), Locale.FRENCH );
177                 bFound = true;
178             }
179         }
180 
181         if ( !bFound )
182         {
183             throw new AppException( "The question with the following id does not exist : " + nIdQuestion );
184         }
185 
186         return strValue;
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     @Override
193     protected int provideAmount( ResourceHistory resourceHistory )
194     {
195         FormResponse formResponse = findFormResponseFrom( resourceHistory );
196         TaskTipiFormsProviderConfig config = findConfig( );
197 
198         return NumberUtils.toInt( findResponseValue( formResponse, config.getIdAmountQuestion( ) ) );
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     protected String provideEmail( ResourceHistory resourceHistory )
206     {
207         FormResponse formResponse = findFormResponseFrom( resourceHistory );
208         TaskTipiFormsProviderConfig config = findConfig( );
209 
210         return findResponseValue( formResponse, config.getIdEmailQuestion( ) );
211     }
212 
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public void doRemoveConfig( )
218     {
219         _taskTipiFormsProviderConfigDAO.delete( getId( ) );
220     }
221 }