View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.gru.service.demand;
35  
36  import fr.paris.lutece.plugins.gru.business.NotifiedDemand;
37  import fr.paris.lutece.plugins.gru.business.demandtype.DemandType;
38  import fr.paris.lutece.plugins.gru.business.demandtype.DemandTypeHome;
39  import fr.paris.lutece.plugins.gru.business.domain.BusinessDomain;
40  import fr.paris.lutece.plugins.gru.service.demandtype.DemandTypeService;
41  import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
42  import fr.paris.lutece.plugins.grubusiness.business.demand.Demand;
43  import fr.paris.lutece.plugins.grubusiness.business.notification.Notification;
44  import fr.paris.lutece.portal.business.user.AdminUser;
45  import fr.paris.lutece.portal.service.rbac.RBACService;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.util.AppException;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.Collections;
53  import java.util.Comparator;
54  import java.util.List;
55  
56  /**
57   * Demande Service
58   */
59  public final class DemandService
60  {
61      private static final String BEAN_DEMAND_SERVICE = "gru.demandService";
62      private static fr.paris.lutece.plugins.grubusiness.business.demand.DemandService _service = SpringContextService.getBean( BEAN_DEMAND_SERVICE );;
63      private static Comparator<Demand> _comparatorDemands = new Comparator<Demand>( )
64      {
65          @Override
66          public int compare( Demand demand1, Demand demand2 )
67          {
68              int nResult = -1;
69  
70              switch( demand1.getStatusId( ) )
71              {
72                  case Demand.STATUS_INPROGRESS:
73                      switch( demand2.getStatusId( ) )
74                      {
75                          case Demand.STATUS_INPROGRESS:
76                              nResult = Long.compare( demand2.getCreationDate( ), demand1.getCreationDate( ) );
77                              break;
78  
79                          case Demand.STATUS_CLOSED:
80                              nResult = 1;
81                              break;
82  
83                          default:
84                              nResult = 1;
85                              break;
86                      }
87                      break;
88  
89                  case Demand.STATUS_CLOSED:
90                      switch( demand2.getStatusId( ) )
91                      {
92                          case Demand.STATUS_INPROGRESS:
93                              nResult = -1;
94                              break;
95  
96                          case Demand.STATUS_CLOSED:
97                              nResult = Long.compare( demand2.getClosureDate( ), demand1.getClosureDate( ) );
98                              break;
99  
100                         default:
101                             nResult = 1;
102                             break;
103                     }
104                     break;
105 
106                 default:
107                     nResult = -1;
108             }
109 
110             return nResult;
111         }
112     };
113 
114     /**
115      * Private constructor
116      */
117     private DemandService( )
118     {
119 
120     }
121 
122     /**
123      * Return a Demand object from an Id
124      * 
125      * @param strDemandId
126      *            The Demand Id
127      * @param strDemandTypeId
128      *            the demand type id
129      * @param user
130      *            The Admin User
131      * @return The demand
132      */
133     public static Demand getDemand( String strDemandId, String strDemandTypeId, AdminUser user )
134     {
135         Demand demand = _service.findByPrimaryKey( strDemandId, strDemandTypeId );
136 
137         demand.setTitle( DemandTypeService.getTypeLabel( strDemandTypeId ) );
138         demand.setShowDetails( isDetailsAuthorized( strDemandTypeId, user ) );
139 
140         return demand;
141     }
142 
143     /**
144      * Gets a list of demand for a given Customer
145      * 
146      * @param customer
147      *            The customer
148      * @param user
149      *            The admin user
150      * @param nStatus
151      *            The status
152      * @return The list
153      */
154     public static List<Demand> getDemands( Customer customer, AdminUser user, int nStatus )
155     {
156         Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
157         List<Demand> listDemand = new ArrayList<Demand>( );
158 
159         for ( Demand base : collectionBase )
160         {
161             if ( base.getStatusId( ) == nStatus && isAuthorized( base, user ) )
162             {
163                 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
164             }
165         }
166 
167         Collections.sort( listDemand, _comparatorDemands );
168 
169         return listDemand;
170     }
171 
172     /**
173      * Gets a list of demand for a given reference
174      * 
175      * @param reference
176      *            The reference
177      * @param user
178      *            The admin user
179      * @return The list
180      */
181     public static List<Demand> getDemandsByRef( String reference, AdminUser user )
182     {
183         Collection<Demand> collectionBase = _service.findByReference( reference );
184         List<Demand> listDemand = new ArrayList<Demand>( );
185 
186         for ( Demand base : collectionBase )
187         {
188             if ( isAuthorized( base, user ) )
189             {
190                 listDemand.add( DemandTypeService.setDemandActions( base, base.getCustomer( ), user ) );
191             }
192         }
193 
194         Collections.sort( listDemand, _comparatorDemands );
195 
196         return listDemand;
197     }
198 
199     /**
200      * Gets a list of demand for a given Customer filtered by types
201      * 
202      * @param customer
203      *            The customer
204      * @param listExcludedTypes
205      *            excluded types
206      * @param user
207      *            The admin user
208      * @return The list
209      */
210     public static List<Demand> getDemandsExcludingTypes( Customer customer, List<String> listExcludedTypes, AdminUser user )
211     {
212         Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
213         List<Demand> listDemand = new ArrayList<Demand>( );
214 
215         for ( Demand base : collectionBase )
216         {
217             if ( !listExcludedTypes.contains( base.getTypeId( ) ) && isAuthorized( base, user ) )
218             {
219                 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
220             }
221         }
222 
223         Collections.sort( listDemand, _comparatorDemands );
224 
225         return listDemand;
226     }
227 
228     /**
229      * Gets a list of demand for a given Customer filtered by types
230      * 
231      * @param customer
232      *            The customer
233      * @param listIncludedTypes
234      *            included types
235      * @param user
236      *            The admin user
237      * @return The list
238      */
239     public static List<Demand> getDemandsIncludingTypes( Customer customer, List<String> listIncludedTypes, AdminUser user )
240     {
241         Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
242         List<Demand> listDemand = new ArrayList<Demand>( );
243 
244         for ( Demand base : collectionBase )
245         {
246             if ( listIncludedTypes.contains( base.getTypeId( ) ) && isAuthorized( base, user ) )
247             {
248                 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
249             }
250         }
251 
252         Collections.sort( listDemand, _comparatorDemands );
253 
254         return listDemand;
255     }
256 
257     /**
258      * Check if the user can view the demand
259      * 
260      * @param base
261      *            The base demand
262      * @param user
263      *            The Admin User
264      * @return true if authorized
265      */
266     private static boolean isAuthorized( Demand base, AdminUser user )
267     {
268         DemandType type = DemandTypeHome.findByTypeId( base.getTypeId( ) );
269 
270         if ( type == null )
271         {
272             AppLogService.error( "Demand Type missing for ID : " + base.getTypeId( ) + " of demand ID : " + base.getId( ) );
273 
274             return false;
275         }
276 
277         String strBusinessDomainId = String.valueOf( type.getBusinessDomainId( ) );
278 
279         return RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_SUMMARY, user )
280                 || RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_DETAILS, user );
281     }
282 
283     /**
284      * Check if the user can view details
285      * 
286      * @param strDemandTypeId
287      *            The demand Id type
288      * @param user
289      *            The admin user
290      * @return true if authorized
291      */
292     private static boolean isDetailsAuthorized( String strDemandTypeId, AdminUser user )
293     {
294         DemandType type = DemandTypeHome.findByTypeId( strDemandTypeId );
295 
296         if ( type == null )
297         {
298             throw new AppException( "Demand Type missing for ID : " + strDemandTypeId );
299         }
300 
301         String strBusinessDomainId = String.valueOf( type.getBusinessDomainId( ) );
302 
303         return RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_DETAILS, user );
304     }
305 
306     /**
307      * Gets a list of demand for a given Customer
308      * 
309      * @param customer
310      *            The customer
311      * @param user
312      *            The admin user
313      * @param nStatus
314      *            The status
315      * @return The list
316      */
317     public static List<NotifiedDemand> getNotifiedDemands( Customer customer, AdminUser user, int nStatus )
318     {
319         List<Demand> listDemand = getDemands( customer, user, nStatus );
320         List<NotifiedDemand> listNotifiedDemand = new ArrayList<NotifiedDemand>( );
321         NotifiedDemand notifiedDemand = null;
322 
323         for ( Demand demand : listDemand )
324         {
325             notifiedDemand = updateNotifiedDemandStatus( demand );
326             listNotifiedDemand.add( notifiedDemand );
327         }
328 
329         return listNotifiedDemand;
330     }
331 
332     /**
333      * Return a NotifiedDemand object from a Demand object with status data contained in notifications
334      * 
335      * @param notifiedDemand
336      *            the notifiedDemand to update
337      * @return the updated notifiedDemande
338      */
339     public static NotifiedDemand updateNotifiedDemandStatus( Demand demand )
340     {
341         boolean bIsAgentStatusFound = false;
342         boolean bIsCustomerStatusFound = false;
343 
344         NotifiedDemand notifiedDemand = NotifiedDemand.toDemand( demand );
345 
346         if ( notifiedDemand != null && notifiedDemand.getNotifications( ) != null )
347         {
348             for ( Notification notification : notifiedDemand.getNotifications( ) )
349             {
350                 if ( !bIsAgentStatusFound && ( notification.getBackofficeNotification( ) != null ) )
351                 {
352                     notifiedDemand.setAgentStatus( notification.getBackofficeNotification( ).getStatusText( ) );
353                     bIsAgentStatusFound = true;
354                 }
355 
356                 if ( !bIsCustomerStatusFound && ( notification.getMyDashboardNotification( ) != null ) )
357                 {
358                     notifiedDemand.setCustomerStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
359                     bIsCustomerStatusFound = true;
360                 }
361 
362                 if ( bIsAgentStatusFound && bIsCustomerStatusFound )
363                 {
364                     break;
365                 }
366             }
367         }
368         return notifiedDemand;
369     }
370 
371 }