View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.participatorybudget.web.notify;
35  
36  import java.util.HashMap;
37  import java.util.Locale;
38  import java.util.Map;
39  
40  import javax.inject.Inject;
41  import javax.inject.Named;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import fr.paris.lutece.plugins.participatorybudget.business.notify.TaskNotifyDocumentbpConfig;
47  import fr.paris.lutece.plugins.participatorybudget.service.notify.TaskNotifyDocumentbp;
48  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
49  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
50  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  import fr.paris.lutece.portal.service.util.AppPathService;
53  import fr.paris.lutece.portal.service.util.AppPropertiesService;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  
56  /**
57   *
58   * INotifyIdeationTaskComponent
59   *
60   */
61  public class NotifyDocumentbpTaskComponent extends NoFormTaskComponent
62  {
63  
64      // Properties
65      public static final String PROPERTY_NOTIFY_MAIL_DEFAULT_SENDER_NAME = "workflow-notifydocumentbp.notification_mail.default_sender_name";
66      public static final String PROPERTY_NOTIFY_MAIL_DEFAULT_SENDER_EMAIL = "workflow-notifydocumentbp.notification_mail.default_sender_email";
67  
68      // PARAMETERS
69      public static final String PARAMETER_SUBJECT = "subject";
70      public static final String PARAMETER_MESSAGE = "message";
71      public static final String PARAMETER_SENDER_NAME = "sender_name";
72      public static final String PARAMETER_SENDER_EMAIL = "sender_email";
73      public static final String PARAMETER_RECIPIENTS_CC = "recipients_cc";
74      public static final String PARAMETER_RECIPIENTS_BCC = "recipients_bcc";
75      public static final String PARAMETER_FOLLOWERS = "followers";
76      public static final String PARAMETER_ABONNES = "abonnes";
77  
78      // For rich text editor
79      public static final String MARK_WEBAPP_URL = "webapp_url";
80      public static final String MARK_LOCALE = "locale";
81  
82      // MARKS
83      public static final String MARK_CONFIG = "config";
84      public static final String MARK_DEFAULT_SENDER_NAME = "default_sender_name";
85      public static final String MARK_DEFAULT_SENDER_EMAIL = "default_sender_email";
86      private static final String MARK_FALSE = "false";
87  
88      // TEMPLATES
89      private static final String TEMPLATE_TASK_NOTIFY_DIRECTORY_CONFIG = "admin/plugins/workflow/modules/notifydocumentbp/task_notifydocumentbp_config.html";
90  
91      // SERVICES
92      @Inject
93      @Named( TaskNotifyDocumentbp.CONFIG_SERVICE_BEAN_NAME )
94      private ITaskConfigService _taskNotifyIdeationConfigService;
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
101     {
102         String strSenderName = request.getParameter( PARAMETER_SENDER_NAME );
103         String strSenderEmail = request.getParameter( PARAMETER_SENDER_EMAIL );
104         String strSubject = request.getParameter( PARAMETER_SUBJECT );
105         String strMessage = request.getParameter( PARAMETER_MESSAGE );
106         String strRecipientsCc = request.getParameter( PARAMETER_RECIPIENTS_CC );
107         String strRecipientsBcc = request.getParameter( PARAMETER_RECIPIENTS_BCC );
108         String strFollowers = ( request.getParameter( PARAMETER_FOLLOWERS ) == null ) ? MARK_FALSE : request.getParameter( PARAMETER_FOLLOWERS );
109         String strSumbitters = ( request.getParameter( PARAMETER_ABONNES ) == null ) ? MARK_FALSE : request.getParameter( PARAMETER_ABONNES );
110 
111         TaskNotifyDocumentbpConfig config = _taskNotifyIdeationConfigService.findByPrimaryKey( task.getId( ) );
112         Boolean bCreate = false;
113 
114         if ( config == null )
115         {
116             config = new TaskNotifyDocumentbpConfig( );
117             config.setIdTask( task.getId( ) );
118             bCreate = true;
119         }
120 
121         config.setMessage( strMessage );
122         config.setSenderName( strSenderName );
123         config.setSenderEmail( strSenderEmail );
124         config.setSubject( strSubject );
125         config.setRecipientsCc( StringUtils.isNotEmpty( strRecipientsCc ) ? strRecipientsCc : StringUtils.EMPTY );
126         config.setRecipientsBcc( StringUtils.isNotEmpty( strRecipientsBcc ) ? strRecipientsBcc : StringUtils.EMPTY );
127         config.setAbonnes( Boolean.parseBoolean( strSumbitters ) );
128 
129         if ( bCreate )
130         {
131             _taskNotifyIdeationConfigService.create( config );
132         }
133         else
134         {
135             _taskNotifyIdeationConfigService.update( config );
136         }
137 
138         return null;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
146     {
147         TaskNotifyDocumentbpConfig config = _taskNotifyIdeationConfigService.findByPrimaryKey( task.getId( ) );
148         String strDefaultSenderName = AppPropertiesService.getProperty( PROPERTY_NOTIFY_MAIL_DEFAULT_SENDER_NAME );
149         String strDefaultSenderEmail = AppPropertiesService.getProperty( PROPERTY_NOTIFY_MAIL_DEFAULT_SENDER_EMAIL );
150 
151         Map<String, Object> model = new HashMap<String, Object>( );
152 
153         model.put( MARK_CONFIG, config );
154         model.put( MARK_DEFAULT_SENDER_NAME, strDefaultSenderName );
155         model.put( MARK_DEFAULT_SENDER_EMAIL, strDefaultSenderEmail );
156         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
157         model.put( MARK_LOCALE, locale );
158 
159         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_DIRECTORY_CONFIG, locale, model );
160 
161         return template.getHtml( );
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
169     {
170         // TODO Auto-generated method stub
171         return null;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
179     {
180         // TODO Auto-generated method stub
181         return null;
182     }
183 }