View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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  package fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.web;
35  
36  import java.lang.reflect.InvocationTargetException;
37  import java.util.HashMap;
38  import java.util.Locale;
39  import java.util.Map;
40  import java.util.Set;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.validation.ConstraintViolation;
44  
45  import org.apache.commons.beanutils.BeanUtils;
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.business.TaskNotifyCRMConfig;
49  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.service.INotifyCRMService;
50  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.util.constants.NotifyCRMConstants;
51  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
52  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
53  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
54  import fr.paris.lutece.portal.service.message.AdminMessage;
55  import fr.paris.lutece.portal.service.message.AdminMessageService;
56  import fr.paris.lutece.portal.service.spring.SpringContextService;
57  import fr.paris.lutece.portal.service.template.AppTemplateService;
58  import fr.paris.lutece.portal.service.util.AppLogService;
59  import fr.paris.lutece.portal.service.util.AppPathService;
60  import fr.paris.lutece.portal.web.constants.Messages;
61  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
62  import fr.paris.lutece.util.html.HtmlTemplate;
63  
64  
65  /**
66   *
67   * NotifyCRMTaskComponent
68   *
69   */
70  public class NotifyCRMTaskComponent extends NoFormTaskComponent
71  {
72      private static final String TEMPLATE_TASK_NOTIFY_CRM_CONFIG = "admin/plugins/workflow/modules/notifycrmnoesb/task_notify_crm_config.html";
73      
74      private static final String BEAN_NOTIFY_CRM_SERVICE = "workflow-notifycrmnoesb.notifyCRMService";
75      
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
82      {
83          // In case there are no errors, then the config is created/updated
84          
85          ITaskConfigService taskNotifyCRMConfigService = (ITaskConfigService)SpringContextService.getBean( NotifyCRMConstants.BEAN_TASK_CONFIG_SERVICE );
86          TaskNotifyCRMConfig config = taskNotifyCRMConfigService.findByPrimaryKey( task.getId(  ) );
87          
88          boolean bCreate = false;
89          if ( config == null )
90          {
91              config = new TaskNotifyCRMConfig(  );
92              config.setIdTask( task.getId(  ) );
93              bCreate = true;
94          }
95  
96          try
97          {
98              BeanUtils.populate( config, request.getParameterMap(  ) );
99          }
100         catch ( IllegalAccessException | InvocationTargetException e )
101         {
102             AppLogService.error( "NotifyCRMTaskComponent - Unable to fetch data from request", e );
103         }
104 
105         String strApply = request.getParameter( NotifyCRMConstants.PARAMETER_APPLY );
106 
107         // Check if the AdminUser clicked on "Apply" or on "Save"
108         if ( StringUtils.isEmpty( strApply ) )
109         {
110             // Check mandatory fields
111             Set<ConstraintViolation<TaskNotifyCRMConfig>> constraintViolations = BeanValidationUtil.validate( config );
112 
113             if ( constraintViolations.size(  ) > 0 )
114             {
115                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
116             }
117         }
118 
119         if ( bCreate )
120         {
121             taskNotifyCRMConfigService.create( config );
122         }
123         else
124         {
125             taskNotifyCRMConfigService.update( config );
126         }
127 
128         return null;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
136     {
137         ITaskConfigService taskNotifyCRMConfigService = (ITaskConfigService)SpringContextService.getBean( NotifyCRMConstants.BEAN_TASK_CONFIG_SERVICE );
138         INotifyCRMService notifyCRMService = (INotifyCRMService)SpringContextService.getBean( BEAN_NOTIFY_CRM_SERVICE );
139         
140         Map<String, Object> model = new HashMap<String, Object>(  );
141 
142         model.put( NotifyCRMConstants.MARK_CONFIG, taskNotifyCRMConfigService.findByPrimaryKey( task.getId(  ) ) );
143         model.put( NotifyCRMConstants.MARK_LIST_MARKERS, notifyCRMService.getListMarkers( ) );
144         model.put( NotifyCRMConstants.MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
145         
146 
147         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFY_CRM_CONFIG, locale, model );
148 
149         return template.getHtml(  );
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
157     {
158         return null;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
166     {
167         return null;
168     }
169 }