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.service.task;
35  
36  import fr.paris.lutece.plugins.crmclient.service.ICRMClientService;
37  import fr.paris.lutece.plugins.crmclient.util.CRMException;
38  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.business.TaskNotifyCRMConfig;
39  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.service.INotifyCRMService;
40  import fr.paris.lutece.plugins.workflow.modules.notifycrmnoesb.service.taskinfo.NotifyCRMTaskInfoProvider;
41  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
42  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
43  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
44  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import java.util.Locale;
49  import javax.inject.Inject;
50  import javax.inject.Named;
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   *
56   * NotifyCRMService
57   *
58   */
59  public class NotifyCRMTask extends SimpleTask
60  {
61      @Inject
62      @Named("workflow-notifycrmnoesb.taskNotifyCRMConfigService")
63      private ITaskConfigService _taskConfigService;
64      @Inject
65      private IResourceHistoryService _resourceHistoryService;
66      private static final String CRM_CLIENT_SERVICE_BEAN_NAME = "crmclient.crmClientService";
67      private static final String NOTIFY_CRM_SERVICE_BEAN_NAME = "workflow-notifycrmnoesb.notifyCRMService";
68      private static final String TASK_INFO_PROVIDER_BEAN_NAME = "workflow-notifycrmnoesb.notifyCRMTaskInfoProvider";
69      private static final String TASK_NOTIFY_CRM_TITLE = "Notifier Mon Compte";
70      
71      /**
72       * Set the task config service
73       * @param taskConfigService the task config service
74       */
75      public void setTaskConfigService( ITaskConfigService taskConfigService )
76      {
77          _taskConfigService = taskConfigService;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public String getTitle( Locale locale )
85      {
86          return TASK_NOTIFY_CRM_TITLE;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void doRemoveConfig(  )
94      {
95          _taskConfigService.remove( getId(  ) );
96      }
97      
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale  )
103     {
104         ICRMClientService crmClientService = (ICRMClientService)SpringContextService.getBean( CRM_CLIENT_SERVICE_BEAN_NAME );
105         INotifyCRMService notifyCrmService = (INotifyCRMService)SpringContextService.getBean( NOTIFY_CRM_SERVICE_BEAN_NAME );
106         NotifyCRMTaskInfoProvider taskProvider = (NotifyCRMTaskInfoProvider)SpringContextService.getBean( TASK_INFO_PROVIDER_BEAN_NAME );
107         
108         ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
109         TaskNotifyCRMConfig notifyCRMConfig = (TaskNotifyCRMConfig) _taskConfigService.findByPrimaryKey( getId(  ) );
110         
111         //Get infos for calling CRM Rest API
112         int nIdResource = resourceHistory.getIdResource( );
113         String strIdDemand = notifyCrmService.getIdDemand( nIdResource );
114         String strUserGuid = notifyCrmService.getUserGuid( nIdResource );
115         String strIdDemandType = AppPropertiesService.getProperty( "module.workflow.notifycrmnoesb.crmDemandTypeId" );
116         String strStatusText = notifyCRMConfig.getStatusText( );
117         String strSender = notifyCRMConfig.getSender( );
118         String strObject = notifyCRMConfig.getObject( );
119         String strMessageFillWithUserInfos = taskProvider.getTaskResourceInfo( nIdResourceHistory, getId( ), request );
120         
121         if ( notifyCRMConfig.getDemandCRMCreation( ) )
122         {
123             //A new demand is created, with a first notification
124             try
125             {
126                 crmClientService.sendCreateDemandByUserGuidV2( strIdDemand, strIdDemandType, strUserGuid, "0", strStatusText, null, null);
127                 crmClientService.notifyV2( strIdDemand, strIdDemandType, strObject, strMessageFillWithUserInfos, strSender, null);
128             }
129             catch (CRMException e)
130             {
131                 AppLogService.error( "NotifyCRMTask : unable to send creation demand ", e );
132             }
133             
134         }
135         else
136         {
137             //A existing demand is updated
138             try
139             {
140                 crmClientService.sendUpdateDemandV2( strIdDemand, strIdDemandType, strStatusText, null, "0" , null);
141                 crmClientService.notifyV2( strIdDemand, strIdDemandType, strObject, strMessageFillWithUserInfos, strSender, null);
142             }
143             catch (CRMException e)
144             {
145                 AppLogService.error( "NotifyCRMTask : unable to send updating demand ", e );
146             }
147         }
148     }
149 }