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.crm.service;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.Demand;
37  import fr.paris.lutece.plugins.crm.business.notification.Notification;
38  import fr.paris.lutece.plugins.crm.business.user.CRMUser;
39  import fr.paris.lutece.plugins.crm.service.demand.DemandService;
40  import fr.paris.lutece.plugins.crm.service.notification.NotificationService;
41  import fr.paris.lutece.plugins.crm.service.user.CRMUserService;
42  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  /**
49   *
50   * CRMService
51   *
52   */
53  public final class CRMService
54  {
55      private static final String BEAN_CRM_CRMSERVICE = "crm.crmService";
56      private NotificationService _notificationService;
57      private DemandService _demandService;
58      private CRMUserService _crmUserService;
59  
60      /**
61       * Private constructor
62       */
63      private CRMService( )
64      {
65      }
66  
67      /**
68       * Get the instance of {@link CRMService}
69       * 
70       * @return the instance
71       */
72      public static CRMService getService( )
73      {
74          return SpringContextService.getBean( BEAN_CRM_CRMSERVICE );
75      }
76  
77      /**
78       * Register the demand
79       * 
80       * @param nIdDemandType
81       *            the id demand type
82       * @param strUserGuid
83       *            the user guid
84       * @param strData
85       *            the data
86       * @param strStatusText
87       *            the status of the demand
88       * @param nIdStatusCRM
89       *            the id status crm
90       * @return the newly created id demand
91       */
92      public int registerDemand( int nIdDemandType, String strUserGuid, String strData, String strStatusText, int nIdStatusCRM )
93      {
94          int nIdDemand = -1;
95          CRMUser crmUser = _crmUserService.findByUserGuid( strUserGuid );
96  
97          if ( crmUser != null )
98          {
99              nIdDemand = registerDemand( nIdDemandType, crmUser.getIdCRMUser( ), strData, strStatusText, nIdStatusCRM );
100         }
101 
102         return nIdDemand;
103     }
104 
105     /**
106      * Register the demand
107      * 
108      * @param nIdDemandType
109      *            the id demand type
110      * @param nIdCRMUser
111      *            the ID CRM user
112      * @param strData
113      *            the data
114      * @param strStatusText
115      *            the status of the demand
116      * @param nIdStatusCRM
117      *            the id status crm
118      * @return the newly created id demand
119      */
120     public int registerDemand( int nIdDemandType, int nIdCRMUser, String strData, String strStatusText, int nIdStatusCRM )
121     {
122 
123         return registerDemand( null, nIdDemandType, nIdCRMUser, strData, strStatusText, nIdStatusCRM );
124     }
125 
126     /**
127      * Register the demand
128      * 
129      * @param strRemoteId
130      *            the remoteId
131      * @param nIdDemandType
132      *            the id demand type
133      * @param nIdCRMUser
134      *            the ID CRM user
135      * @param strData
136      *            the data
137      * @param strStatusText
138      *            the status of the demand
139      * @param nIdStatusCRM
140      *            the id status crm
141      * @return the newly created id demand
142      */
143     public int registerDemand( String strRemoteId, int nIdDemandType, int nIdCRMUser, String strData, String strStatusText, int nIdStatusCRM )
144     {
145         int nIdDemand = -1;
146         CRMUser crmUser = _crmUserService.findByPrimaryKey( nIdCRMUser );
147 
148         if ( crmUser != null )
149         {
150             Demands/crm/business/demand/Demand.html#Demand">Demand demand = new Demand( );
151             demand.setIdDemandType( nIdDemandType );
152             demand.setIdCRMUser( nIdCRMUser );
153             demand.setData( StringUtils.isNotEmpty( strData ) ? strData : StringUtils.EMPTY );
154             demand.setStatusText( StringUtils.isNotEmpty( strStatusText ) ? strStatusText : StringUtils.EMPTY );
155             demand.setIdStatusCRM( nIdStatusCRM );
156             demand.setRemoteId( strRemoteId );
157             // Test if the multiple key Remote Id and Id Demand Type do not already exist
158             if ( _demandService.findByRemoteKey( strRemoteId, nIdDemandType ) == null )
159             {
160                 nIdDemand = _demandService.create( demand );
161             }
162         }
163 
164         return nIdDemand;
165     }
166 
167     /**
168      * Set the status of the demand
169      * 
170      * @param nIdDemand
171      *            the id demand
172      * @param strData
173      *            the data
174      * @param strStatusText
175      *            the status of the demand
176      * @param nIdStatusCRM
177      *            the id status crm
178      */
179     public void setStatus( int nIdDemand, String strData, String strStatusText, int nIdStatusCRM )
180     {
181         Demand demand = _demandService.findByPrimaryKey( nIdDemand );
182 
183         if ( demand != null )
184         {
185             if ( StringUtils.isNotBlank( strData ) )
186             {
187                 demand.setData( strData );
188             }
189 
190             if ( nIdStatusCRM != CRMConstants.INVALID_ID_INT )
191             {
192                 demand.setIdStatusCRM( nIdStatusCRM );
193             }
194 
195             demand.setStatusText( strStatusText );
196             _demandService.update( demand );
197         }
198     }
199 
200     /**
201      * Delete a demand
202      * 
203      * @param nIdDemand
204      *            the id demand
205      */
206     public void deleteDemand( int nIdDemand )
207     {
208         _demandService.remove( nIdDemand );
209     }
210 
211     /**
212      * Create a notification for a demand
213      * 
214      * @param nIdDemand
215      *            the id demand
216      * @param strObject
217      *            the object of the notification
218      * @param strMessage
219      *            the message of the notification
220      * @param strSender
221      *            the sender
222      */
223     public void notify( int nIdDemand, String strObject, String strMessage, String strSender )
224     {
225         String strNotificationSender = StringUtils.EMPTY;
226 
227         if ( StringUtils.isBlank( strSender ) )
228         {
229             // If the sender is not filled, the email from webmaster.properties is filled instead
230             strNotificationSender = AppPropertiesService.getProperty( CRMConstants.PROPERTY_WEBMASTER_EMAIL );
231         }
232         else
233         {
234             strNotificationSender = strSender;
235         }
236 
237         Notificationss/notification/Notification.html#Notification">Notification notification = new Notification( );
238         notification.setIdDemand( nIdDemand );
239         notification.setObject( strObject );
240         notification.setMessage( strMessage );
241         notification.setSender( strNotificationSender );
242         _notificationService.create( notification );
243     }
244 
245     /**
246      * Set the notification service
247      * 
248      * @param notificationService
249      *            the notification service
250      */
251     public void setNotificationService( NotificationService notificationService )
252     {
253         _notificationService = notificationService;
254     }
255 
256     /**
257      * Set the demand service
258      * 
259      * @param demandService
260      *            the demand service
261      */
262     public void setDemandService( DemandService demandService )
263     {
264         _demandService = demandService;
265     }
266 
267     /**
268      * Set the crm user service
269      * 
270      * @param crmUserService
271      *            the crm user service
272      */
273     public void setCRMUserService( CRMUserService crmUserService )
274     {
275         _crmUserService = crmUserService;
276     }
277 }