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.demand;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.Demand;
37  import fr.paris.lutece.plugins.crm.business.demand.DemandFilter;
38  import fr.paris.lutece.plugins.crm.business.demand.DemandHome;
39  import fr.paris.lutece.plugins.crm.business.demand.DemandSort;
40  import fr.paris.lutece.plugins.crm.business.demand.DemandStatusCRM;
41  import fr.paris.lutece.plugins.crm.business.demand.DemandType;
42  import fr.paris.lutece.plugins.crm.business.demand.IPaginationProperties;
43  import fr.paris.lutece.plugins.crm.business.demand.PaginationFilterSortManager;
44  import fr.paris.lutece.plugins.crm.service.notification.NotificationService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.util.html.IPaginator;
48  import fr.paris.lutece.util.httpaccess.HttpAccessException;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import java.sql.Timestamp;
53  
54  import java.util.ArrayList;
55  import java.util.Date;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Locale;
59  import java.util.Map;
60  
61  /**
62   *
63   * DemandService
64   *
65   */
66  public class DemandService
67  {
68      private static final String BEAN_CRM_DEMANDSERVICE = "crm.demandService";
69  
70      /**
71       * Constructor
72       */
73      protected DemandService( )
74      {
75      }
76  
77      /**
78       * Get an instance of {@link DemandService}
79       * 
80       * @return an instance of {@link DemandService}
81       */
82      public static DemandService getService( )
83      {
84          return SpringContextService.getBean( BEAN_CRM_DEMANDSERVICE );
85      }
86  
87      /**
88       * Find a demand by its primary key
89       * 
90       * @param nIdDemand
91       *            the id demand
92       * @return a {@link Demand}
93       */
94      public Demand findByPrimaryKey( int nIdDemand )
95      {
96          return DemandHome.findByPrimaryKey( nIdDemand );
97      }
98  
99      /**
100      * Find a demand by Remote key
101      * 
102      * @param strRemoteId
103      *            the remote Id
104      * @param nIdDemandType
105      *            the idDemandType
106      * @return a {@link Demand}
107      */
108     public Demand findByRemoteKey( String strRemoteId, int nIdDemandType )
109     {
110         return DemandHome.findByRemoteKey( strRemoteId, nIdDemandType );
111     }
112 
113     /**
114      * Create a new demand
115      * 
116      * @param demand
117      *            the demand
118      * @return the newly created demand id
119      */
120     public int create( Demand demand )
121     {
122         int nIdDemand = -1;
123 
124         if ( demand != null )
125         {
126             demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
127             nIdDemand = DemandHome.create( demand );
128         }
129 
130         return nIdDemand;
131     }
132 
133     /**
134      * Update a demand
135      * 
136      * @param demand
137      *            the demand
138      */
139     public void update( Demand demand )
140     {
141         if ( demand != null )
142         {
143             demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
144             DemandHome.update( demand );
145         }
146     }
147 
148     /**
149      * Remove a demand
150      * 
151      * @param nIdDemand
152      *            the id demand
153      */
154     public void remove( int nIdDemand )
155     {
156         // Remove all notifications associated to the demand
157         NotificationService.getService( ).removeByIdDemand( nIdDemand );
158         DemandHome.remove( nIdDemand );
159     }
160 
161     /**
162      * Remove a demand and its resource
163      * 
164      * @param nIdDemand
165      *            the id demand
166      */
167     public void removeWithItsResource( int nIdDemand, boolean bByDaemon )
168     {
169         Demand demand = findByPrimaryKey( nIdDemand );
170 
171         if ( demand != null )
172         {
173             DemandType demandType = DemandTypeService.getService( ).findByPrimaryKey( demand.getIdDemandType( ) );
174 
175             if ( demandType != null )
176             {
177                 if ( StringUtils.isNotBlank( demandType.getUrlDelete( ) ) && bByDaemon )
178                 {
179                     try
180                     {
181                         DemandWebService.getService( ).sendRemoveDraft( demandType.getUrlDelete( ), nIdDemand, demand.getData( ).replace( "\"", "'" ) );
182                         remove( nIdDemand );
183                     }
184                     catch( HttpAccessException e )
185                     {
186                         String strError = "CRM Demand - Error connecting to '" + demandType.getUrlDelete( ) + "' : ";
187                         AppLogService.error( strError + e.getMessage( ), e );
188                     }
189                 }
190                 else
191                 {
192                     try
193                     {
194                         DemandWebService.getService( ).sendRemoveDraft( demandType.getUrlResource( ), nIdDemand, demand.getData( ).replace( "\"", "'" ) );
195                         remove( nIdDemand );
196                     }
197                     catch( HttpAccessException e )
198                     {
199                         String strError = "CRM Demand - Error connecting to '" + demandType.getUrlResource( ) + "' : ";
200                         AppLogService.error( strError + e.getMessage( ), e );
201                     }
202                 }
203             }
204         }
205     }
206 
207     /**
208      * Remove the demands given an id demand type
209      * 
210      * @param nIdDemandType
211      *            the id demand type
212      */
213     public void removeByIdDemandType( int nIdDemandType )
214     {
215         DemandFilterm/business/demand/DemandFilter.html#DemandFilter">DemandFilter dFilter = new DemandFilter( );
216         dFilter.setIdDemandType( nIdDemandType );
217 
218         for ( Demand demand : findByFilter( dFilter ) )
219         {
220             removeWithItsResource( demand.getIdDemand( ), false );
221         }
222     }
223 
224     /**
225      * Find all demands
226      * 
227      * @return a list of {@link Demand}
228      */
229     public List<Demand> findAll( )
230     {
231         return DemandHome.findAll( );
232     }
233 
234     /**
235      * Find by filter
236      * 
237      * @param dFilter
238      *            the filter
239      * @return a list of {@link Demand}
240      */
241     public List<Demand> findByFilter( DemandFilter dFilter )
242     {
243         return DemandHome.findByFilter( dFilter );
244     }
245 
246     /**
247      * Find the demands given an user crm id, results can be sorted
248      * 
249      * @param nIdCRMUser
250      *            the user crm id
251      * @param locale
252      *            {@link Locale}
253      * @param nIdStatusToSort
254      *            the id status of demands that will be sorted
255      * @param listDemandSort
256      *            the sorts to apply
257      * @return a map of (id_status_crm, List&lt;Demand&gt;)
258      */
259     public Map<String, List<Demand>> findByIdCRMUser( int nIdCRMUser, Locale locale, int nIdStatusToSort, List<DemandSort> listDemandSort )
260     {
261         Map<String, List<Demand>> map = new HashMap<String, List<Demand>>( );
262 
263         for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( ).getAllStatusCRM( locale ) )
264         {
265             DemandFilterm/business/demand/DemandFilter.html#DemandFilter">DemandFilter dFilter = new DemandFilter( );
266             dFilter.setIdCRMUser( nIdCRMUser );
267             dFilter.setIdStatusCRM( statusCRM.getIdStatusCRM( ) );
268 
269             // sort
270             if ( nIdStatusToSort == statusCRM.getIdStatusCRM( ) )
271             {
272                 dFilter.setListDemandSort( listDemandSort );
273             }
274 
275             List<Demand> listDemands = findByFilter( dFilter );
276             map.put( Integer.toString( statusCRM.getIdStatusCRM( ) ), listDemands );
277         }
278 
279         return map;
280     }
281 
282     /**
283      * Find by filter with map
284      * 
285      * @param nIdCRMUser
286      *            the user crm id
287      * @param locale
288      *            {@link Locale}
289      * @return a map of (id_status_crm, List&lt;Demand&gt;)
290      */
291     public Map<String, List<Demand>> findByFilterMap( DemandFilter dFilter, Locale locale, PaginationFilterSortManager paginationFilterSortManager )
292     {
293         Map<String, List<Demand>> map = new HashMap<String, List<Demand>>( );
294 
295         for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( ).getAllStatusCRM( locale ) )
296         {
297             int nIdStatus = statusCRM.getIdStatusCRM( );
298             DemandFilteress/demand/DemandFilter.html#DemandFilter">DemandFilter filterWithSort = new DemandFilter( );
299             filterWithSort.setDateModification( dFilter.getDateModification( ) );
300             filterWithSort.setIdCRMUser( dFilter.getIdCRMUser( ) );
301             filterWithSort.setIdDemandType( dFilter.getIdDemandType( ) );
302             filterWithSort.setIdStatusCRM( nIdStatus );
303             filterWithSort.setIsWideSearch( dFilter.getIsWideSearch( ) );
304             filterWithSort.setNotification( dFilter.getNotification( ) );
305             filterWithSort.setOperatorDateModification( dFilter.getOperatorDateModification( ) );
306 
307             DemandSort sort = paginationFilterSortManager.retrieveSort( nIdStatus );
308             List<DemandSort> listDemandSort = new ArrayList<DemandSort>( );
309 
310             if ( sort != null )
311             {
312                 listDemandSort.add( sort );
313             }
314 
315             filterWithSort.setListDemandSort( listDemandSort );
316 
317             int nTotalResult = this.countByFilter( filterWithSort );
318 
319             List<Demand> listDemands = this.findByFilterWithPagination( filterWithSort,
320                     paginationFilterSortManager.retrievePaginationProperties( nIdStatus, nTotalResult ) );
321 
322             IPaginator<Demand> paginator = paginationFilterSortManager.createAndStorePaginator( nIdStatus, listDemands, nTotalResult );
323 
324             map.put( Integer.toString( nIdStatus ), paginator.getPageItems( ) );
325         }
326 
327         return map;
328     }
329 
330     /**
331      * Find by filter with pagination
332      * 
333      * @param dFilter
334      *            the filter
335      * @param paginationProperties
336      *            the pagination properties
337      * @return a list of {@link Demand}
338      */
339     public List<Demand> findByFilterWithPagination( DemandFilter dFilter, IPaginationProperties paginationProperties )
340     {
341         return DemandHome.findByFilter( dFilter, paginationProperties );
342     }
343 
344     /**
345      * Count results by filter
346      * 
347      * @param dFilter
348      *            the filter
349      * @return the number of results
350      */
351     public int countByFilter( DemandFilter dFilter )
352     {
353         return DemandHome.countByFilter( dFilter );
354     }
355 }