1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
76
77
78 @Path( RestConstants.BASE_PATH + GruSupplyConstants.PLUGIN_NAME + GruSupplyConstants.PATH_DEMAND )
79 public class DemandNotificationRestService
80 {
81
82
83
84
85
86
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
113
114
115
116
117
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
148
149
150
151
152
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
174
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
194
195
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
228
229
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
262
263
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
278
279
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 }