1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
68
69
70 public class CommentTaskComponent extends AbstractTaskComponent
71 {
72
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
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
88 private static final String PARAMETER_COMMENT_VALUE = "comment_value";
89
90
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
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
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
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
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
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 }