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.assignment.service;
35  
36  import fr.paris.lutece.plugins.workflow.modules.assignment.business.AssignmentHistory;
37  import fr.paris.lutece.plugins.workflow.modules.assignment.business.TaskAssignmentConfig;
38  import fr.paris.lutece.plugins.workflow.modules.assignment.business.WorkgroupConfig;
39  import fr.paris.lutece.plugins.workflow.modules.comment.business.TaskCommentConfig;
40  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
41  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
42  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
43  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
44  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
45  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
46  import fr.paris.lutece.plugins.workflowcore.service.task.Task;
47  import fr.paris.lutece.portal.business.mailinglist.Recipient;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.admin.AdminUserService;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.mail.MailService;
52  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
53  import fr.paris.lutece.portal.service.spring.SpringContextService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import java.util.ArrayList;
58  import java.util.Collection;
59  import java.util.HashMap;
60  import java.util.List;
61  import java.util.Locale;
62  import java.util.Map;
63  
64  import javax.inject.Inject;
65  import javax.inject.Named;
66  
67  import javax.servlet.http.HttpServletRequest;
68  
69  
70  
71  
72  
73  
74  public class TaskAssignment extends Task
75  {
76      
77      private static final String TEMPLATE_TASK_NOTIFICATION_MAIL = "admin/plugins/workflow/modules/notification/task_notification_mail.html";
78  
79      
80      private static final String PARAMETER_WORKGROUPS = "workgroups";
81  
82      
83      private static final String MARK_MESSAGE = "message";
84  
85      
86      private static final String PROPERTY_MAIL_SENDER_NAME = "module.workflow.assignment.task_assignment_config.mailSenderName";
87  
88      
89      private static final String BEAN_COMMENT_CONFIG_SERVICE = "workflow.taskCommentConfigService";
90      @Inject
91      @Named( BEAN_COMMENT_CONFIG_SERVICE )
92      private ITaskConfigService _taskCommentConfigService;
93      @Inject
94      private IAssignmentHistoryService _assignmentHistoryService;
95      @Inject
96      @Named( TaskAssignmentConfigService.BEAN_SERVICE )
97      private ITaskConfigService _taskAssignmentConfigService;
98      @Inject
99      private IWorkgroupConfigService _workgroupConfigService;
100 
101     
102 
103 
104     @Override
105     public void init( )
106     {
107         
108     }
109 
110     
111 
112 
113     @Override
114     public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
115     {
116         String [ ] tabWorkgroups = request.getParameterValues( PARAMETER_WORKGROUPS + "_" + this.getId( ) );
117         AdminUser admin = AdminUserService.getAdminUser( request );
118         List<String> listWorkgroup = new ArrayList<>( );
119         TaskAssignmentConfig config = _taskAssignmentConfigService.findByPrimaryKey( this.getId( ) );
120 
121         for ( int i = 0; i < tabWorkgroups.length; i++ )
122         {
123             listWorkgroup.add( tabWorkgroups [i] );
124 
125             
126             AssignmentHistoryflow/modules/assignment/business/AssignmentHistory.html#AssignmentHistory">AssignmentHistory history = new AssignmentHistory( );
127             history.setIdResourceHistory( nIdResourceHistory );
128             history.setIdTask( this.getId( ) );
129             history.setWorkgroup( tabWorkgroups [i] );
130             _assignmentHistoryService.create( history, WorkflowUtils.getPlugin( ) );
131 
132             if ( config.isNotify( ) )
133             {
134                 notifyUser( config, tabWorkgroups [i], admin, locale );
135             }
136         }
137 
138         IResourceHistoryService resourceHistoryService = SpringContextService.getBean( "workflow.resourceHistoryService" );
139         IResourceWorkflowService resourceWorkflowService = SpringContextService.getBean( "workflow.resourceWorkflowService" );
140 
141         
142         ResourceHistory resourceHistory = resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
143         ResourceWorkflow resourceWorkflow = resourceWorkflowService.findByPrimaryKey( resourceHistory.getIdResource( ), resourceHistory.getResourceType( ),
144                 resourceHistory.getWorkflow( ).getId( ) );
145         resourceWorkflow.setWorkgroups( listWorkgroup );
146         resourceWorkflowService.update( resourceWorkflow );
147     }
148 
149     private void notifyUser( TaskAssignmentConfig config, String workgroup, AdminUser admin, Locale locale )
150     {
151         WorkgroupConfig workgroupConfig = _workgroupConfigService.findByPrimaryKey( this.getId( ), workgroup, WorkflowUtils.getPlugin( ) );
152 
153         if ( ( workgroupConfig != null ) && ( workgroupConfig.getIdMailingList( ) != WorkflowUtils.CONSTANT_ID_NULL ) )
154         {
155             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( workgroupConfig.getIdMailingList( ) );
156 
157             String strSenderEmail = MailService.getNoReplyEmail( );
158 
159             Map<String, Object> model = new HashMap<>( );
160             model.put( MARK_MESSAGE, config.getMessage( ) );
161 
162             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFICATION_MAIL, locale, model );
163 
164             
165             for ( Recipient recipient : listRecipients )
166             {
167                 String strSenderName = I18nService.getLocalizedString( PROPERTY_MAIL_SENDER_NAME, locale );
168                 if ( config.isUseUserName( ) )
169                 {
170                     
171                     MailService.sendMailHtml( recipient.getEmail( ), strSenderName, strSenderEmail, config.getSubject( ), t.getHtml( ) );
172                 }
173                 else
174                 {
175                     MailService.sendMailHtml( recipient.getEmail( ), admin.getFirstName( ) + " " + admin.getLastName( ), admin.getEmail( ),
176                             config.getSubject( ), t.getHtml( ) );
177                 }
178             }
179         }
180     }
181 
182     
183 
184 
185     @Override
186     public void doRemoveConfig( )
187     {
188         
189         _taskAssignmentConfigService.remove( this.getId( ) );
190         
191         _assignmentHistoryService.removeByTask( this.getId( ), WorkflowUtils.getPlugin( ) );
192         _workgroupConfigService.removeByTask( this.getId( ), WorkflowUtils.getPlugin( ) );
193     }
194 
195     
196 
197 
198     @Override
199     public void doRemoveTaskInformation( int nIdHistory )
200     {
201         _assignmentHistoryService.removeByHistory( nIdHistory, this.getId( ), WorkflowUtils.getPlugin( ) );
202     }
203 
204     
205 
206 
207     @Override
208     public String getTitle( Locale locale )
209     {
210         TaskAssignmentConfig config = _taskAssignmentConfigService.findByPrimaryKey( this.getId( ) );
211 
212         if ( config != null )
213         {
214             return config.getTitle( );
215         }
216 
217         return WorkflowUtils.EMPTY_STRING;
218     }
219 
220     
221 
222 
223     @Override
224     public Map<String, String> getTaskFormEntries( Locale locale )
225     {
226         Map<String, String> mapEntriesForm = null;
227         TaskCommentConfig config = _taskCommentConfigService.findByPrimaryKey( this.getId( ) );
228 
229         if ( config != null )
230         {
231             mapEntriesForm = new HashMap<>( );
232             mapEntriesForm.put( PARAMETER_WORKGROUPS + "_" + this.getId( ), config.getTitle( ) );
233         }
234 
235         return mapEntriesForm;
236     }
237 }