View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.modules.workflow.web;
35  
36  import fr.paris.lutece.plugins.announce.modules.workflow.business.TaskNotifyAnnounceConfig;
37  import fr.paris.lutece.plugins.announce.modules.workflow.service.TaskNotifyAnnounce;
38  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
39  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
40  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
41  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
42  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
43  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
44  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
45  import fr.paris.lutece.portal.service.i18n.I18nService;
46  import fr.paris.lutece.portal.service.mail.MailService;
47  import fr.paris.lutece.portal.service.message.AdminMessage;
48  import fr.paris.lutece.portal.service.message.AdminMessageService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  import fr.paris.lutece.util.string.StringUtil;
53  
54  import org.apache.commons.lang.StringUtils;
55  
56  import java.util.HashMap;
57  import java.util.Locale;
58  import java.util.Map;
59  
60  import javax.inject.Inject;
61  import javax.inject.Named;
62  import javax.servlet.http.HttpServletRequest;
63  
64  /**
65   * 
66   * NotifyAnnounceTaskComponent
67   * 
68   */
69  public class NotifyAnnounceTaskComponent extends NoFormTaskComponent
70  {
71      // MARKS
72      private static final String MARK_CONFIG = "config";
73      private static final String MARK_LOCALE = "locale";
74      private static final String MARK_WEBAPP_URL = "webapp_url";
75      private static final String MARK_DEFAULT_SENDER_NAME = "default_sender_name";
76  
77      // PARAMETERS
78      private static final String PARAMETER_SUBJECT = "subject";
79      private static final String PARAMETER_MESSAGE = "message";
80      private static final String PARAMETER_SENDER_NAME = "sender_name";
81      private static final String PARAMETER_SENDER_EMAIL = "sender_email";
82      private static final String PARAMETER_RECIPIENTS_CC = "recipients_cc";
83      private static final String PARAMETER_RECIPIENTS_BCC = "recipients_bcc";
84  
85      // TEMPLATES
86      private static final String TEMPLATE_TASK_NOTIFY_ANNOUNCE_CONFIG = "admin/plugins/announce/modules/workflow/task_notify_announce_config.html";
87  
88      // FIELDS
89      private static final String FIELD_SUBJECT = "module.announce.workflow.task_notify_announce_config.label_subject";
90      private static final String FIELD_MESSAGE = "module.announce.workflow.task_notify_announce_config.label_message";
91      private static final String FIELD_SENDER_NAME = "module.announce.workflow.task_notify_announce_config.label_sender_name";
92      private static final String FIELD_SENDER_EMAIL = "module.announce.workflow.task_notify_announce_config.label_sender_email";
93      private static final String FIELD_SENDER_EMAIL_NOT_VALID = "module.announce.workflow.task_notify_announce_config.sender_email_not_valid";
94  
95      // MESSAGES
96      private static final String MESSAGE_MANDATORY_FIELD = "module.announce.workflow.message.mandatory.field";
97      private static final String MESSAGE_EMAIL_SENT_TO_USER = "module.announce.workflow.message.emailSentToUser";
98  
99      // SERVICES
100     @Inject
101     @Named( TaskNotifyAnnounce.CONFIG_SERVICE_BEAN_NAME )
102     private ITaskConfigService _taskNotifyAnnounceConfigService;
103     @Inject
104     @Named( ActionService.BEAN_SERVICE )
105     private ActionService _actionService;
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
112     {
113         TaskNotifyAnnounceConfig config = _taskNotifyAnnounceConfigService.findByPrimaryKey( task.getId( ) );
114 
115         ActionFilter filter = new ActionFilter( );
116         filter.setAutomaticReflexiveAction( false );
117         Action action = _actionService.findByPrimaryKey( task.getAction( ).getId( ) );
118         filter.setIdStateBefore( action.getStateAfter( ).getId( ) );
119 
120         String strDefaultSenderName = MailService.getNoReplyEmail( );
121 
122         Map<String, Object> model = new HashMap<String, Object>( );
123 
124         model.put( MARK_CONFIG, config );
125         model.put( MARK_DEFAULT_SENDER_NAME, strDefaultSenderName );
126         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
127         model.put( MARK_LOCALE, locale );
128 
129         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_ANNOUNCE_CONFIG, locale, model );
130 
131         return template.getHtml( );
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
139     {
140         String strSenderName = request.getParameter( PARAMETER_SENDER_NAME );
141         String strSenderEmail = request.getParameter( PARAMETER_SENDER_EMAIL );
142         String strSubject = request.getParameter( PARAMETER_SUBJECT );
143         String strMessage = request.getParameter( PARAMETER_MESSAGE );
144         String strRecipientsCc = request.getParameter( PARAMETER_RECIPIENTS_CC );
145         String strRecipientsBcc = request.getParameter( PARAMETER_RECIPIENTS_BCC );
146         String strError = StringUtils.EMPTY;
147 
148         if ( StringUtils.isBlank( strSenderName ) )
149         {
150             strError = FIELD_SENDER_NAME;
151         }
152 
153         if ( StringUtils.isBlank( strSenderEmail ) )
154         {
155             strError = FIELD_SENDER_EMAIL;
156         }
157         else
158             if ( StringUtils.isBlank( strSubject ) )
159             {
160                 strError = FIELD_SUBJECT;
161             }
162             else
163                 if ( StringUtils.isBlank( strMessage ) )
164                 {
165                     strError = FIELD_MESSAGE;
166                 }
167 
168         if ( !StringUtil.checkEmail( strSenderEmail ) )
169         {
170             strError = FIELD_SENDER_EMAIL_NOT_VALID;
171         }
172 
173         if ( !strError.equals( WorkflowUtils.EMPTY_STRING ) )
174         {
175             Object [ ] tabRequiredFields = {
176                     I18nService.getLocalizedString( strError, locale )
177             };
178 
179             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
180         }
181 
182         TaskNotifyAnnounceConfig config = _taskNotifyAnnounceConfigService.findByPrimaryKey( task.getId( ) );
183         Boolean bCreate = false;
184 
185         if ( config == null )
186         {
187             config = new TaskNotifyAnnounceConfig( );
188             config.setIdTask( task.getId( ) );
189             bCreate = true;
190         }
191 
192         config.setMessage( strMessage );
193         config.setSenderName( strSenderName );
194         config.setSubject( strSubject );
195         config.setRecipientsCc( StringUtils.isNotEmpty( strRecipientsCc ) ? strRecipientsCc : StringUtils.EMPTY );
196         config.setRecipientsBcc( StringUtils.isNotEmpty( strRecipientsBcc ) ? strRecipientsBcc : StringUtils.EMPTY );
197 
198         if ( bCreate )
199         {
200             _taskNotifyAnnounceConfigService.create( config );
201         }
202         else
203         {
204             _taskNotifyAnnounceConfigService.update( config );
205         }
206 
207         return null;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
215     {
216         return I18nService.getLocalizedString( MESSAGE_EMAIL_SENT_TO_USER, locale );
217     }
218 }