View Javadoc
1   /*
2    * Copyright (c) 2002-2019, Mairie de 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  /*
35   * To change this license header, choose License Headers in Project Properties.
36   * To change this template file, choose Tools | Templates
37   * and open the template in the editor.
38   */
39  package fr.paris.lutece.plugins.workflow.modules.reply.web.task;
40  
41  import fr.paris.lutece.plugins.workflow.modules.reply.business.config.TaskReplyConfig;
42  import fr.paris.lutece.plugins.workflow.modules.reply.business.task.Reply;
43  import fr.paris.lutece.plugins.workflow.modules.reply.service.IReplyService;
44  import fr.paris.lutece.plugins.workflow.modules.reply.service.ReplyService;
45  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
46  import fr.paris.lutece.plugins.workflow.web.task.AbstractTaskComponent;
47  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
48  import fr.paris.lutece.portal.service.admin.AdminUserService;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.spring.SpringContextService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  import java.util.HashMap;
56  import java.util.Locale;
57  import java.util.Map;
58  import javax.servlet.http.HttpServletRequest;
59  import org.apache.commons.lang.StringUtils;
60  
61  /**
62   *
63   * @author leridons
64   */
65  public class TaskReplyComponent extends AbstractTaskComponent
66  {
67  
68      // TEMPLATES
69      private static final String TEMPLATE_TASK_REPLY_CONFIG = "admin/plugins/workflow/modules/reply/task_reply_config.html";
70      private static final String TEMPLATE_TASK_REPLY_FORM = "admin/plugins/workflow/modules/reply/task_reply_form.html";
71  
72      // MARKERS
73      private static final String MARK_CONFIG = "config";
74      private static final String MARK_WEBAPP_URL = "webapp_url";
75      private static final String MARK_LOCALE = "locale";
76      private static final String MARK_MESSAGE = "message";
77  
78      // PARAMETERS
79      private static final String PARAMETER_REPLY_VALUE = "reply_message";
80  
81      // MESSAGES
82      private static final String MESSAGE_MANDATORY_FIELD = "module.workflow.reply.task_reply_config.message.mandatory.field";
83      private static final String MESSAGE_NO_CONFIGURATION_FOR_TASK_REPLY = "module.workflow.reply.task_reply_config.message.no_configuration_for_task_reply";
84  
85      @Override
86      public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
87      {
88          Map<String, Object> model = new HashMap<>( );
89  
90          TaskReplyConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
91          model.put( MARK_CONFIG, config );
92  
93          String strMessage = request.getParameter( PARAMETER_REPLY_VALUE + "_" + task.getId( ) );
94  
95          model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
96          model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
97          model.put( MARK_MESSAGE, strMessage );
98  
99          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_REPLY_FORM, locale, model );
100 
101         return template.getHtml( );
102     }
103 
104     @Override
105     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
106     {
107         Map<String, Object> model = new HashMap<>( );
108 
109         TaskReplyConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
110         model.put( MARK_CONFIG, config );
111 
112         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_REPLY_CONFIG, locale, model );
113 
114         return template.getHtml( );
115     }
116 
117     @Override
118     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
119     {
120         TaskReplyConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
121 
122         String strInfo = "";
123         if ( config != null )
124             strInfo = config.getTitle( );
125 
126         IReplyService replyService = SpringContextService.getBean( ReplyService.BEAN_SERVICE );
127         Reply reply = replyService.find( nIdHistory, task.getId( ) );
128 
129         if ( reply != null )
130             strInfo += " : " + reply.getMessage( );
131 
132         return strInfo;
133     }
134 
135     @Override
136     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
137     {
138         throw new UnsupportedOperationException( "Not supported yet." ); // To change body of generated methods, choose Tools | Templates.
139     }
140 
141     @Override
142     public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
143     {
144         String strError = WorkflowUtils.EMPTY_STRING;
145         String strReplyMessageValue = request.getParameter( PARAMETER_REPLY_VALUE + "_" + task.getId( ) );
146         TaskReplyConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
147 
148         if ( config == null )
149         {
150             return AdminMessageService.getMessageUrl( request, MESSAGE_NO_CONFIGURATION_FOR_TASK_REPLY, AdminMessage.TYPE_STOP );
151         }
152 
153         if ( config.isMandatory( ) && ( ( strReplyMessageValue == null ) || strReplyMessageValue.trim( ).equals( WorkflowUtils.EMPTY_STRING ) ) )
154         {
155             strError = config.getTitle( );
156         }
157 
158         if ( StringUtils.isNotBlank( strError ) )
159         {
160             Object [ ] tabRequiredFields = {
161                 strError
162             };
163 
164             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
165         }
166 
167         return null;
168     }
169 
170 }