View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.grusupply.web.rs;
35  
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Optional;
41  
42  import javax.ws.rs.GET;
43  import javax.ws.rs.Path;
44  import javax.ws.rs.Produces;
45  import javax.ws.rs.QueryParam;
46  import javax.ws.rs.core.MediaType;
47  import javax.ws.rs.core.Response;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  import fr.paris.lutece.plugins.grubusiness.business.demand.Demand;
52  import fr.paris.lutece.plugins.grubusiness.business.notification.EnumNotificationType;
53  import fr.paris.lutece.plugins.grubusiness.business.notification.Notification;
54  import fr.paris.lutece.plugins.grubusiness.business.web.rs.DemandDisplay;
55  import fr.paris.lutece.plugins.grubusiness.business.web.rs.DemandResult;
56  import fr.paris.lutece.plugins.grubusiness.business.web.rs.EnumGenericStatus;
57  import fr.paris.lutece.plugins.grubusiness.business.web.rs.NotificationResult;
58  import fr.paris.lutece.plugins.grubusiness.business.web.rs.SearchResult;
59  import fr.paris.lutece.plugins.grustoragedb.business.DemandHome;
60  import fr.paris.lutece.plugins.grustoragedb.business.NotificationHome;
61  import fr.paris.lutece.plugins.grustoragedb.business.Status;
62  import fr.paris.lutece.plugins.grustoragedb.business.StatusHome;
63  import fr.paris.lutece.plugins.grustoragedb.business.DemandType;
64  import fr.paris.lutece.plugins.grustoragedb.business.DemandTypeHome;
65  import fr.paris.lutece.plugins.grusupply.constant.GruSupplyConstants;
66  import fr.paris.lutece.plugins.grusupply.utils.GrusupplyUtils;
67  import fr.paris.lutece.plugins.rest.service.RestConstants;
68  import fr.paris.lutece.portal.service.i18n.I18nService;
69  import fr.paris.lutece.portal.service.util.AppPropertiesService;
70  import fr.paris.lutece.portal.web.l10n.LocaleService;
71  import fr.paris.lutece.util.html.Paginator;
72  
73  /**
74   * 
75   * Service Rest DemandNotificationRestService
76   *
77   */
78  @Path( RestConstants.BASE_PATH + GruSupplyConstants.PLUGIN_NAME + GruSupplyConstants.PATH_DEMAND )
79  public class DemandNotificationRestService
80  {
81      
82      /**
83       * Return list of demand
84       * 
85       * @param strDemandType
86       * @param strPage
87       */
88      @GET
89      @Path( GruSupplyConstants.PATH_DEMAND_LIST )
90      @Produces( MediaType.APPLICATION_JSON )
91      public Response getListDemand( @QueryParam( GruSupplyConstants.QUERY_PARAM_ID_DEMAND_TYPE ) String strIdDemandType, 
92              @QueryParam( GruSupplyConstants.QUERY_PARAM_INDEX ) String strIndex,
93              @QueryParam( GruSupplyConstants.QUERY_PARAM_CUSTOMER_ID ) String strCustomerId,
94              @QueryParam( GruSupplyConstants.QUERY_PARAM_NOTIFICATION_TYPE ) String strNotificationType )
95      {
96          int nIndex = StringUtils.isEmpty( strIndex ) ? 1 : Integer.parseInt( strIndex );
97          int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( GruSupplyConstants.LIMIT_DEMAND_API_REST, 10 );
98  
99          DemandResult result = new DemandResult( );
100         if( StringUtils.isEmpty( strCustomerId ) )
101         {
102             result.setStatus( SearchResult.ERROR_FIELD_MANDATORY );
103             result.setErrorMessage( GruSupplyConstants.MESSAGE_ERROR_DEMAND );
104             return Response.status( Response.Status.BAD_REQUEST ).entity( GrusupplyUtils.convertToJsonString( result ) ).build( );
105         }
106         
107         List<Integer> listIds = DemandHome.getIdsByCustomerIdAndDemandTypeId( strCustomerId, strNotificationType, strIdDemandType );
108         return getResponse( result, nIndex, nDefaultItemsPerPage, listIds );
109     }
110 
111     /**
112      * Get list by status
113      * 
114      * @param strIdDemandType
115      * @param strIndex
116      * @param strCustomerId
117      * @return list of active demand
118      */
119     @GET
120     @Path( GruSupplyConstants.PATH_DEMAND_STATUS )
121     @Produces( MediaType.APPLICATION_JSON )
122     public Response getListOfDemandByStatus( @QueryParam( GruSupplyConstants.QUERY_PARAM_ID_DEMAND_TYPE ) String strIdDemandType,
123             @QueryParam( GruSupplyConstants.QUERY_PARAM_INDEX ) String strIndex, 
124             @QueryParam( GruSupplyConstants.QUERY_PARAM_CUSTOMER_ID ) String strCustomerId,
125             @QueryParam( GruSupplyConstants.QUERY_PARAM_LIST_STATUS ) String strListStatus,
126             @QueryParam( GruSupplyConstants.QUERY_PARAM_NOTIFICATION_TYPE ) String strNotificationType )
127     {
128         int nIndex = StringUtils.isEmpty( strIndex ) ? 1 : Integer.parseInt( strIndex );
129         int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( GruSupplyConstants.LIMIT_DEMAND_API_REST, 10 );
130 
131         DemandResult result = new DemandResult( );
132         if( StringUtils.isEmpty( strCustomerId ) || StringUtils.isEmpty( strListStatus ) )
133         {
134             result.setStatus( SearchResult.ERROR_FIELD_MANDATORY );
135             result.setErrorMessage( GruSupplyConstants.MESSAGE_ERROR_STATUS );
136             
137             return Response.status( Response.Status.BAD_REQUEST ).entity( GrusupplyUtils.convertToJsonString( result ) ).build( );
138         }
139         
140         List<String> listStatus = Arrays.asList( strListStatus.split( "," ) );
141         List<Integer> listIds = DemandHome.getIdsByStatus( strCustomerId, listStatus, strNotificationType, strIdDemandType );
142 
143         return getResponse( result, nIndex, nDefaultItemsPerPage, listIds );
144     }
145 
146     /**
147      * Get response
148      * @param result
149      * @param nIndex
150      * @param nDefaultItemsPerPage
151      * @param listIds
152      * @return
153      */
154     private Response getResponse( DemandResult result, int nIndex, int nDefaultItemsPerPage, List<Integer> listIds )
155     {
156         
157         if ( !listIds.isEmpty( ) )
158         {
159             Paginator<Integer> paginator = new Paginator<>( listIds, nDefaultItemsPerPage, StringUtils.EMPTY, StringUtils.EMPTY, String.valueOf( nIndex ) );
160 
161             result.setListDemandDisplay( getListDemandDisplay( paginator.getPageItems( ) ) );
162             result.setIndex( String.valueOf( nIndex ) );
163             result.setPaginator( nIndex + "/" + paginator.getPagesCount( ) );
164             result.setStatus( Response.Status.OK.name( ) );
165             result.setNumberResult( listIds.size( ) );
166         }
167         
168         return Response.status( Response.Status.OK ).entity( GrusupplyUtils.convertToJsonString( result ) ).build( );
169     }
170 
171     /**
172      * 
173      * @param listIds
174      * @return list of demand display
175      */
176     private List<DemandDisplay> getListDemandDisplay( List<Integer> listIds )
177     {
178         List<DemandDisplay> listDemandDisplay = new ArrayList<>( );
179         List<Demand> listDemand = DemandHome.getByIds( listIds );
180         for( Demand demand : listDemand )
181         {   
182             DemandDisplay demandDisplay = new DemandDisplay( );
183             demandDisplay.setDemand( demand );
184             demandDisplay.setStatus( getLabelStatus( demand ) );
185             
186             listDemandDisplay.add( demandDisplay );
187         }
188         Collections.reverse(listDemandDisplay);
189         return listDemandDisplay;
190     }
191     
192     /**
193      * Get status label
194      * @param demand
195      * @return status
196      */
197     private String getLabelStatus( Demand demand )
198     {
199         Notification notification = NotificationHome.getLastNotifByDemandIdAndDemandTypeId( String.valueOf( demand.getDemandId( ) ), String.valueOf( demand.getTypeId( ) ) ) ;
200                  
201         if( notification.getMyDashboardNotification( ) != null )
202         {
203             EnumGenericStatus enumGenericStatus = EnumGenericStatus.getByStatusId( notification.getMyDashboardNotification( ).getStatusId( ) );                
204             if( enumGenericStatus != null )
205             {
206                 return I18nService.getLocalizedString( enumGenericStatus.getLabel( ), LocaleService.getDefault( ) );
207             }
208             else
209             {                  
210                 Optional<Status> status = StatusHome.findByStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
211                 if( status.isPresent( ) )
212                 {
213                     enumGenericStatus =  EnumGenericStatus.valueOf( status.get( ).getCodeStatus( ) );
214                     if( enumGenericStatus != null )
215                     {
216                         return I18nService.getLocalizedString( enumGenericStatus.getLabel( ), LocaleService.getDefault( ) );
217                     }
218                 }
219             }
220             return notification.getMyDashboardNotification( ).getStatusText( );
221         }
222         
223         return StringUtils.EMPTY;
224     }
225     
226     /**
227      * Gets list of notification
228      * 
229      * @param strIdDemand
230      */
231     @GET
232     @Path( GruSupplyConstants.PATH_NOTIFICATION_LIST )
233     @Produces( MediaType.APPLICATION_JSON )
234     public Response getListNotification( @QueryParam( GruSupplyConstants.QUERY_PARAM_ID_DEMAND ) String strIdDemand,
235             @QueryParam( GruSupplyConstants.QUERY_PARAM_ID_DEMAND_TYPE ) String strIdDemandType,
236             @QueryParam( GruSupplyConstants.QUERY_PARAM_CUSTOMER_ID ) String strCustomerId )
237     {
238         NotificationResult result = new NotificationResult( );
239         
240         if ( StringUtils.isNotEmpty( strIdDemand ) && StringUtils.isNotEmpty( strIdDemandType ) 
241                 && StringUtils.isNotEmpty( strCustomerId ) )
242         {            
243             
244             List<Notification> notifications = NotificationHome.getByDemandIdTypeIdCustomerId( strIdDemand, strIdDemandType, strCustomerId );
245 
246             result.setNotifications( notifications );
247             result.setStatus( Response.Status.OK.name( ) );
248             result.setNumberResult( notifications.size( ) );
249             
250             return Response.status( Response.Status.OK ).entity( GrusupplyUtils.convertToJsonString( result ) ).build( );
251         }
252         else
253         {
254             result.setStatus( SearchResult.ERROR_FIELD_MANDATORY );
255             result.setErrorMessage( GruSupplyConstants.MESSAGE_ERROR_NOTIF);
256             return Response.status( Response.Status.BAD_REQUEST ).entity( GrusupplyUtils.convertToJsonString( result ) ).build( );
257         }
258     }
259 
260     /**
261      * Gets list of demand types
262      * 
263      * @return list of demand types
264      */
265     @GET
266     @Path( GruSupplyConstants.PATH_TYPE_DEMAND )
267     @Produces( MediaType.APPLICATION_JSON )
268     public Response getDemandTypes( )
269     {
270         List<DemandType> listDemandTypes = DemandTypeHome.getDemandTypesList( );
271 
272         String strResult = GrusupplyUtils.convertToJsonString( listDemandTypes );
273         return Response.ok( strResult ).build( );
274     }
275 
276     /**
277      * Gets list of notification types
278      * 
279      * @return list of demand types
280      */
281     @GET
282     @Path( GruSupplyConstants.PATH_TYPE_NOTIFICATION )
283     @Produces( MediaType.APPLICATION_JSON )
284     public Response getNotificationTypes( )
285     {
286         String strResult = GrusupplyUtils.convertToJsonString( EnumNotificationType.values( ) );
287         return Response.ok( strResult ).build( );
288     }
289 }