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.transparency.rs;
35
36 import fr.paris.lutece.plugins.transparency.business.Lobby;
37 import fr.paris.lutece.plugins.transparency.business.LobbyHome;
38 import fr.paris.lutece.plugins.rest.service.RestConstants;
39 import fr.paris.lutece.portal.service.i18n.I18nService;
40 import fr.paris.lutece.util.json.ErrorJsonResponse;
41 import fr.paris.lutece.util.json.JsonResponse;
42 import fr.paris.lutece.util.json.JsonUtil;
43
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.log4j.Logger;
46 import java.util.List;
47 import javax.ws.rs.GET;
48 import javax.ws.rs.Path;
49 import javax.ws.rs.PathParam;
50 import javax.ws.rs.Produces;
51 import javax.ws.rs.core.MediaType;
52 import javax.ws.rs.core.Response;
53 import org.json.JSONArray;
54 import org.json.JSONObject;
55
56
57
58
59 @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.VERSION_PATH + Constants.LOBBY_PATH )
60 public class LobbyRest
61 {
62 private static final int VERSION_1 = 1;
63 private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
64
65
66
67
68
69
70
71
72
73 @GET
74 @Path( Constants.SEARCH_PATH )
75 @Produces( MediaType.APPLICATION_JSON )
76 public Response getLobbyListLike( @PathParam( Constants.VERSION ) Integer nVersion, @PathParam( Constants.TEXT ) String strLikeText )
77 {
78 switch( nVersion )
79 {
80 case VERSION_1:
81 return getLobbyListV1( strLikeText );
82 default:
83 break;
84 }
85 _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
86 return Response.status( Response.Status.NOT_FOUND )
87 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
88 }
89
90
91
92
93
94
95
96
97 @GET
98 @Path( StringUtils.EMPTY )
99 @Produces( MediaType.APPLICATION_JSON )
100 public Response getLobbyList( @PathParam( Constants.VERSION ) Integer nVersion )
101 {
102 switch( nVersion )
103 {
104 case VERSION_1:
105 return getLobbyListV1( "" );
106 default:
107 break;
108 }
109 _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
110 return Response.status( Response.Status.NOT_FOUND )
111 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
112 }
113
114
115
116
117
118
119 private Response getLobbyListV1( String strLikeText )
120 {
121 List<Lobby> _listLobbies = LobbyHome.getLobbiesListNamedLike( "%" + strLikeText + "%" );
122
123 if ( _listLobbies.isEmpty( ) )
124 {
125 JSONObject jsonNoProposal = new JSONObject( );
126
127 jsonNoProposal.put( Constants.JSON_AUTOCOMPLETE_ID_KEY, -1 );
128 jsonNoProposal.put( Constants.JSON_AUTOCOMPLETE_VALUE_KEY,
129 I18nService.getLocalizedString( Constants.MESSAGE_NO_PROPOSAL_I18N_KEY, I18nService.getDefaultLocale( ) ) );
130
131 return Response.status( Response.Status.NO_CONTENT ).entity( jsonNoProposal.toString( ) ).build( );
132 }
133
134 JSONArray listJsonLobbies = new JSONArray( );
135 for ( Lobby lobby : _listLobbies )
136 {
137 JSONObject jsonLobby = new JSONObject( );
138
139 jsonLobby.put( Constants.JSON_AUTOCOMPLETE_ID_KEY, lobby.getId( ) );
140 jsonLobby.put( Constants.JSON_AUTOCOMPLETE_VALUE_KEY, lobby.getName( ) );
141 jsonLobby.put( Constants.JSON_AUTOCOMPLETE_LABEL_KEY, lobby.getName( ) );
142
143 listJsonLobbies.put( jsonLobby );
144 }
145 return Response.status( Response.Status.OK ).entity( listJsonLobbies.toString( ) ).build( );
146 }
147
148
149
150
151
152
153
154
155
156
157 @GET
158 @Path( Constants.ID_PATH )
159 @Produces( MediaType.APPLICATION_JSON )
160 public Response getLobby( @PathParam( Constants.VERSION ) Integer nVersion, @PathParam( Constants.ID ) Integer id )
161 {
162 switch( nVersion )
163 {
164 case VERSION_1:
165 return getLobbyV1( id );
166 default:
167 break;
168 }
169 _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
170 return Response.status( Response.Status.NOT_FOUND )
171 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) ).build( );
172 }
173
174
175
176
177
178
179
180
181 private Response getLobbyV1( Integer id )
182 {
183 Lobby _lobby = LobbyHome.findByPrimaryKey( id );
184 if ( _lobby == null )
185 {
186 _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
187 return Response.status( Response.Status.NOT_FOUND )
188 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) )
189 .build( );
190 }
191
192 return Response.status( Response.Status.OK ).entity( JsonUtil.buildJsonResponse( new JsonResponse( _lobby ) ) ).build( );
193 }
194 }