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.comment.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.inject.Inject;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.lang3.StringUtils;
46  
47  import fr.paris.lutece.api.user.User;
48  import fr.paris.lutece.plugins.workflow.modules.comment.business.CommentValue;
49  import fr.paris.lutece.plugins.workflow.modules.comment.business.TaskCommentConfig;
50  import fr.paris.lutece.plugins.workflow.modules.comment.service.CommentResourceIdService;
51  import fr.paris.lutece.plugins.workflow.modules.comment.service.ICommentValueService;
52  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
53  import fr.paris.lutece.plugins.workflow.web.task.AbstractTaskComponent;
54  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
55  import fr.paris.lutece.portal.business.user.AdminUser;
56  import fr.paris.lutece.portal.service.admin.AdminUserService;
57  import fr.paris.lutece.portal.service.content.ContentPostProcessor;
58  import fr.paris.lutece.portal.service.message.AdminMessage;
59  import fr.paris.lutece.portal.service.message.AdminMessageService;
60  import fr.paris.lutece.portal.service.rbac.RBACService;
61  import fr.paris.lutece.portal.service.template.AppTemplateService;
62  import fr.paris.lutece.portal.service.util.AppPathService;
63  import fr.paris.lutece.util.html.HtmlTemplate;
64  
65  /**
66   *
67   * CommentTaskComponent
68   *
69   */
70  public class CommentTaskComponent extends AbstractTaskComponent
71  {
72      // TEMPLATES
73      private static final String TEMPLATE_TASK_COMMENT_CONFIG = "admin/plugins/workflow/modules/comment/task_comment_config.html";
74      private static final String TEMPLATE_TASK_COMMENT_FORM = "admin/plugins/workflow/modules/comment/task_comment_form.html";
75      private static final String TEMPLATE_TASK_COMMENT_INFORMATION = "admin/plugins/workflow/modules/comment/task_comment_information.html";
76  
77      // MARKS
78      private static final String MARK_ID_HISTORY = "id_history";
79      private static final String MARK_TASK = "task";
80      private static final String MARK_CONFIG = "config";
81      private static final String MARK_COMMENT_VALUE = "comment_value";
82      private static final String MARK_WEBAPP_URL = "webapp_url";
83      private static final String MARK_LOCALE = "locale";
84      private static final String MARK_HAS_PERMISSION_DELETE = "has_permission_delete";
85      private static final String MARK_IS_OWNER = "is_owner";
86  
87      // PARAMETERS
88      private static final String PARAMETER_COMMENT_VALUE = "comment_value";
89  
90      // MESSAGES
91      private static final String MESSAGE_MANDATORY_FIELD = "module.workflow.comment.task_comment_config.message.mandatory.field";
92      private static final String MESSAGE_NO_CONFIGURATION_FOR_TASK_COMMENT = "module.workflow.comment.task_comment_config.message.no_configuration_for_task_comment";
93  
94      // SERVICES
95      @Inject
96      private ICommentValueService _commentValueService;
97      private List<ContentPostProcessor> _listContentPostProcessors;
98  
99      public CommentTaskComponent( )
100     {
101         super( );
102     }
103 
104     public CommentTaskComponent( List<ContentPostProcessor> listContentPostProcessors )
105     {
106         this( );
107         _listContentPostProcessors = listContentPostProcessors;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
115     {
116         String strError = WorkflowUtils.EMPTY_STRING;
117         String strCommentValue = request.getParameter( PARAMETER_COMMENT_VALUE + "_" + task.getId( ) );
118         TaskCommentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
119 
120         if ( config == null )
121         {
122             return AdminMessageService.getMessageUrl( request, MESSAGE_NO_CONFIGURATION_FOR_TASK_COMMENT, AdminMessage.TYPE_STOP );
123         }
124 
125         if ( config.isMandatory( ) && ( ( strCommentValue == null ) || strCommentValue.trim( ).equals( WorkflowUtils.EMPTY_STRING ) ) )
126         {
127             strError = config.getTitle( );
128         }
129 
130         if ( StringUtils.isNotBlank( strError ) )
131         {
132             Object [ ] tabRequiredFields = {
133                     strError
134             };
135 
136             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
137         }
138 
139         return null;
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
147     {
148         Map<String, Object> model = new HashMap<>( );
149 
150         TaskCommentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
151         model.put( MARK_CONFIG, config );
152 
153         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_COMMENT_CONFIG, locale, model );
154 
155         return template.getHtml( );
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
163     {
164         Map<String, Object> model = new HashMap<>( );
165         TaskCommentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
166         String strComment = request.getParameter( PARAMETER_COMMENT_VALUE + "_" + task.getId( ) );
167         model.put( MARK_CONFIG, config );
168         model.put( MARK_COMMENT_VALUE, strComment );
169 
170         if ( config.isRichText( ) )
171         {
172             model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
173             model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
174         }
175 
176         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_COMMENT_FORM, locale, model );
177 
178         return template.getHtml( );
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
186     {
187         CommentValue commentValue = _commentValueService.findByPrimaryKey( nIdHistory, task.getId( ), WorkflowUtils.getPlugin( ) );
188 
189         if ( commentValue != null && StringUtils.isNotBlank( commentValue.getValue( ) ) && CollectionUtils.isNotEmpty( _listContentPostProcessors ) )
190         {
191             String strComment = commentValue.getValue( );
192             for ( ContentPostProcessor contentPostProcessor : _listContentPostProcessors )
193             {
194                 strComment = contentPostProcessor.process( request, strComment );
195             }
196             commentValue.setValue( strComment );
197         }
198 
199         Map<String, Object> model = new HashMap<>( );
200         TaskCommentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
201         AdminUser userConnected = AdminUserService.getAdminUser( request );
202 
203         model.put( MARK_ID_HISTORY, nIdHistory );
204         model.put( MARK_TASK, task );
205         model.put( MARK_CONFIG, config );
206         model.put( MARK_COMMENT_VALUE, commentValue );
207         model.put( MARK_HAS_PERMISSION_DELETE, RBACService.isAuthorized( commentValue, CommentResourceIdService.PERMISSION_DELETE, (User) userConnected ) );
208         model.put( MARK_IS_OWNER, _commentValueService.isOwner( nIdHistory, userConnected ) );
209 
210         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_COMMENT_INFORMATION, locale, model );
211 
212         return template.getHtml( );
213     }
214 }