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.myportal.modules.myapps.web.rs;
35
36 import java.util.Base64;
37 import java.util.List;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.GET;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.core.Context;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46
47 import org.apache.commons.lang.StringUtils;
48
49 import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabase;
50 import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseHome;
51 import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUser;
52 import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUserHome;
53 import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabasePlugin;
54 import fr.paris.lutece.plugins.myapps.modules.database.service.MyAppsDatabaseService;
55 import fr.paris.lutece.plugins.rest.service.RestConstants;
56 import fr.paris.lutece.portal.service.image.ImageResource;
57 import fr.paris.lutece.portal.service.plugin.Plugin;
58 import fr.paris.lutece.portal.service.plugin.PluginService;
59 import fr.paris.lutece.portal.service.security.LuteceUser;
60 import fr.paris.lutece.portal.service.security.SecurityService;
61 import net.sf.json.JSONArray;
62 import net.sf.json.JSONObject;
63
64
65
66
67 @Path( RestConstants.BASE_PATH + MyPortalMyAppsRest.PLUGIN_PATH )
68 public class MyPortalMyAppsRest
69 {
70
71 protected static final String USER_GUID = "user_guid";
72 protected static final String PLUGIN_PATH = "myapps/";
73 protected static final String PATH_APPS = "public/apps/";
74 protected static final String PATH_APPS_BY_USERGUID = "private/apps/{" + MyPortalMyAppsRest.USER_GUID + "}";
75
76
77 private static final String FORMAT_MYAPPS_STATUS_RESPONSE = "status";
78 private static final String FORMAT_MYAPPS_RESPONSE_RESULT = "result";
79 private static final String FORMAT_MYAPPS_KEY = "apps";
80 private static final String FORMAT_MYAPPS_ID = "id";
81 private static final String FORMAT_MYAPPS_NAME = "name";
82 private static final String FORMAT_MYAPPS_ICON = "icon";
83 private static final String FORMAT_MYAPPS_ORDER = "order";
84
85
86 private static final String STATUS_OK = "OK";
87 private static final String STATUS_KO = "KO";
88
89
90
91
92
93
94
95
96
97 @GET
98 @Path( MyPortalMyAppsRest.PATH_APPS )
99 public Response getUserMyAppsList( @Context HttpServletRequest request )
100 {
101 String strStatus = STATUS_OK;
102 String strFavoritesList = StringUtils.EMPTY;
103
104 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
105
106 if ( user != null )
107 {
108
109 try
110 {
111
112 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
113 List<MyAppsDatabaseUser> listMyAppsDatabaseUser = MyAppsDatabaseUserHome.getUserListApplications( user.getName( ), pluginMyAppsDatabase );
114
115
116 if ( listMyAppsDatabaseUser != null && !listMyAppsDatabaseUser.isEmpty( ) )
117 {
118 strFavoritesList = formatUserMyAppsList( listMyAppsDatabaseUser );
119 }
120 }
121 catch( Exception exception )
122 {
123
124 strStatus = STATUS_KO;
125 }
126 }
127 else
128 {
129 strStatus = STATUS_KO;
130
131 }
132
133
134 String strResponse = formatResponse( strStatus, strFavoritesList );
135
136
137 return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
138 }
139
140
141
142
143
144
145
146
147
148
149 @GET
150 @Path( MyPortalMyAppsRest.PATH_APPS_BY_USERGUID )
151 public Response getUserMyAppsListByGuid( @Context HttpServletRequest request, @PathParam( MyPortalMyAppsRest.USER_GUID ) String strGuid )
152 {
153 String strStatus = STATUS_OK;
154 String strFavoritesList = StringUtils.EMPTY;
155
156
157 if ( !StringUtils.isEmpty( strGuid ) )
158 {
159
160 try
161 {
162
163 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
164 List<MyAppsDatabaseUser> listMyAppsDatabaseUser = MyAppsDatabaseUserHome.getUserListApplications( strGuid, pluginMyAppsDatabase );
165
166
167 if ( listMyAppsDatabaseUser != null && !listMyAppsDatabaseUser.isEmpty( ) )
168 {
169 strFavoritesList = formatUserMyAppsList( listMyAppsDatabaseUser );
170 }
171 }
172 catch( Exception exception )
173 {
174
175 strStatus = STATUS_KO;
176 }
177 }
178 else
179 {
180 strStatus = STATUS_KO;
181
182 }
183
184
185 String strResponse = formatResponse( strStatus, strFavoritesList );
186
187
188 return Response.ok( strResponse, MediaType.APPLICATION_JSON ).build( );
189 }
190
191
192
193
194
195
196
197
198
199
200 private String formatResponse( String strStatus, String strResponse )
201 {
202 JSONObject jsonResponse = new JSONObject( );
203 jsonResponse.accumulate( FORMAT_MYAPPS_STATUS_RESPONSE, strStatus );
204 jsonResponse.accumulate( FORMAT_MYAPPS_RESPONSE_RESULT, strResponse );
205
206 return jsonResponse.toString( );
207 }
208
209
210
211
212
213
214
215
216 private String formatUserMyAppsList( List<MyAppsDatabaseUser> listMyAppsDatabaseUser )
217 {
218 JSONObject jsonResponse = new JSONObject( );
219 JSONArray jsonAllMyApps = new JSONArray( );
220
221 for ( MyAppsDatabaseUser myAppsDatabaseUser : listMyAppsDatabaseUser )
222 {
223 JSONObject jsonMyApps = new JSONObject( );
224 add( jsonMyApps, myAppsDatabaseUser );
225 jsonAllMyApps.add( jsonMyApps );
226 }
227
228 jsonResponse.accumulate( FORMAT_MYAPPS_KEY, jsonAllMyApps );
229
230 return jsonResponse.toString( );
231 }
232
233
234
235
236
237
238
239
240
241 private void add( JSONObject jsonMyApps, MyAppsDatabaseUser myAppsDatabaseUser )
242 {
243 if ( jsonMyApps != null && myAppsDatabaseUser != null )
244 {
245
246 int nIdApplication = myAppsDatabaseUser.getIdApplication( );
247 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
248 MyAppsDatabase myAppsDatabase = (MyAppsDatabase) MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nIdApplication, pluginMyAppsDatabase );
249
250
251 if ( myAppsDatabase != null )
252 {
253 int nIdAppsApplication = myAppsDatabase.getIdApplication( );
254 jsonMyApps.accumulate( FORMAT_MYAPPS_ID, nIdApplication );
255 jsonMyApps.accumulate( FORMAT_MYAPPS_NAME, myAppsDatabase.getName( ) );
256 jsonMyApps.accumulate( FORMAT_MYAPPS_ICON, getApplicationIcon( nIdAppsApplication ) );
257 }
258
259
260 jsonMyApps.accumulate( FORMAT_MYAPPS_ORDER, myAppsDatabaseUser.getApplicationOrder( ) );
261 }
262 }
263
264
265
266
267
268
269
270
271 private String getApplicationIcon( int nIdApplication )
272 {
273 String strIconEncoded = StringUtils.EMPTY;
274
275 Plugin pluginMyAppsDatabase = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
276 ImageResource imageResource = MyAppsDatabaseHome.getImageResource( nIdApplication, pluginMyAppsDatabase );
277
278 if ( imageResource != null && imageResource.getImage( ) != null )
279 {
280 strIconEncoded = Base64.getEncoder( ).encodeToString( imageResource.getImage( ) );
281 }
282
283 return strIconEncoded;
284 }
285 }