1
1
/*
2
- * Copyright (c) 2018 Parallax Inc.
2
+ * Copyright (c) 2019 Parallax Inc.
3
3
*
4
4
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5
5
* and associated documentation files (the “Software”), to deal in the Software without
16
16
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
17
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- * SOFTWARE.
19
+ * SOFTWARE.
20
20
*/
21
21
22
22
package com .parallax .server .blocklyprop .rest ;
42
42
import javax .ws .rs .QueryParam ;
43
43
import javax .ws .rs .core .Response ;
44
44
45
+ import org .jetbrains .annotations .NotNull ;
45
46
import org .slf4j .Logger ;
46
47
import org .slf4j .LoggerFactory ;
47
48
48
49
49
50
/**
51
+ * Manage requests for public projects
50
52
*
51
53
* @author Michel
54
+ *
55
+ * NOTE:
56
+ * The concept of 'shared' projects has changed over time. A project
57
+ * can be private or public. A project can also be associated with a
58
+ * specific project sharing URL, regardless of its public/private status.
52
59
*/
53
60
@ Path ("/shared/project" )
54
61
@ Group (name = "/shared/project" , title = "Project management" )
55
62
@ HttpCode ("500>Internal Server Error,200>Success Response" )
56
63
public class RestSharedProject {
57
64
65
+ /**
66
+ * Get a connection to the logging system
67
+ */
58
68
private static final Logger LOG = LoggerFactory .getLogger (RestSharedProject .class );
59
69
70
+
71
+ /**
72
+ * Get a handle to project services
73
+ */
60
74
private ProjectService projectService ;
61
75
76
+
77
+ /**
78
+ * Get a handle to a project converter
79
+ */
62
80
private ProjectConverter projectConverter ;
63
81
82
+
83
+ /**
84
+ * Inject project services
85
+ *
86
+ * @param projectService
87
+ * An instance of the ProjectService object
88
+ */
64
89
@ Inject
65
90
public void setProjectService (ProjectService projectService ) {
66
91
this .projectService = projectService ;
67
92
}
68
93
94
+
95
+ /**
96
+ * Inject project conversion services
97
+ * @param projectConverter
98
+ * An instance of the ProjectConverter object
99
+ */
69
100
@ Inject
70
101
public void setProjectConverter (ProjectConverter projectConverter ) {
71
102
this .projectConverter = projectConverter ;
72
103
}
73
104
105
+
74
106
/**
75
107
* Return a list of community projects.
76
108
*
@@ -101,33 +133,33 @@ public Response get(
101
133
@ QueryParam ("limit" ) Integer limit ,
102
134
@ QueryParam ("offset" ) Integer offset ) {
103
135
104
- LOG . info ( "REST:/shared/project/list/ endpoint activated" ) ;
105
- LOG . debug ( "REST:/shared/project/list/ Sort parameter is '{}'" , sort );
106
- LOG .debug ( "REST:/shared/project/list/ Sort parameter is '{}' " , sort );
136
+ String endPoint = "REST:/shared/project/list/" ;
137
+
138
+ LOG .info ( "{} endpoint activated " , endPoint );
107
139
108
140
boolean parametersValid = false ;
109
141
110
142
// Sort flag evaluation
111
143
if (sort != null ) {
112
144
for (TableSort t : TableSort .values ()) {
113
- LOG .debug ("REST:/shared/project/list/ Sort test for '{}'" , t );
145
+ LOG .debug ("{} Sort test for '{}'" , endPoint , t );
114
146
115
147
if (sort == t ) {
116
148
parametersValid = true ;
117
149
break ;
118
150
}
119
151
}
120
152
121
- if (parametersValid == false ) {
122
- LOG .warn ("REST:/shared/project/list/ Sort parameter failed" );
153
+ if (! parametersValid ) {
154
+ LOG .warn ("{} Sort parameter failed" , endPoint );
123
155
return Response .status (Response .Status .NOT_ACCEPTABLE ).build ();
124
156
}
125
157
}
126
158
127
159
// Sort order evaluation
128
160
if (order != null ) {
129
161
parametersValid = false ;
130
- LOG .debug ("REST:/shared/project/list/ Checking order" );
162
+ LOG .debug ("{} Checking order" , endPoint );
131
163
132
164
for (TableOrder t : TableOrder .values ()) {
133
165
if (order == t ) {
@@ -136,41 +168,52 @@ public Response get(
136
168
}
137
169
}
138
170
139
- if (parametersValid == false ) {
171
+ if (! parametersValid ) {
140
172
return Response .status (Response .Status .NOT_ACCEPTABLE ).build ();
141
173
}
142
174
}
143
175
144
176
// Limit result set value
145
177
if ( (limit == null ) || (limit > 50 )) {
146
- LOG .info ("REST:/shared/project/list/ Limit throttle to 50 entries" );
178
+ LOG .info ("{} Limit throttle to 50 entries" , endPoint );
147
179
limit = 50 ;
148
180
}
149
181
150
182
// Check ofset from the beginning of the record set
151
183
if ((offset == null ) || (offset < 0 )) {
152
184
offset = 0 ;
153
185
}
154
-
186
+
187
+ // Get a block of projects
155
188
List <ProjectRecord > projects
156
189
= projectService .getSharedProjects (sort , order , limit , offset );
157
190
158
191
// Obtain a count of the total number of community projects available
159
192
int projectCount = projectService .countSharedProjects ();
160
193
161
- JsonObject result = new JsonObject ();
162
- JsonArray jsonProjects = new JsonArray ();
163
-
164
- for (ProjectRecord project : projects ) {
165
- jsonProjects .add (projectConverter .toListJson (project ));
166
- }
167
-
168
- result .add ("rows" , jsonProjects );
169
- result .addProperty ("total" , projectCount );
170
-
171
- return Response .ok (result .toString ()).build ();
194
+ return Response .ok (returnProjectsJson (projects , projectCount )).build ();
172
195
}
173
196
197
+
198
+ /**
199
+ * Get a list of projects owned by a specific user
200
+ *
201
+ * @param sort
202
+ * The project field used to evaluate the sort
203
+ *
204
+ * @param order
205
+ * Specify the sort order - ascending or descending
206
+ *
207
+ * @param limit
208
+ * Specify the maximum number of rows to return
209
+ *
210
+ * @param offset
211
+ * Specify the beginning row to return
212
+ *
213
+ * @return
214
+ * Return a response object that contains either the data requested
215
+ * or a JSON string containing the error details
216
+ */
174
217
@ GET
175
218
@ Path ("/list/user/{id}" )
176
219
@ Detail ("Get shared projects by user" )
@@ -183,25 +226,44 @@ public Response get(
183
226
@ QueryParam ("offset" ) Integer offset ,
184
227
@ PathParam ("id" ) Long idUser ) {
185
228
186
- LOG . info ( "REST:/shared/project/list/user/ Get request received for user '{}'" , idUser ) ;
229
+ String endPoint = "REST:/shared/project/list/user/" ;
187
230
188
- List <ProjectRecord > projects = projectService .getSharedProjectsByUser (sort , order , limit , offset , idUser );
189
- int projectCount = projectService .countSharedProjectsByUser (idUser );
231
+ LOG .info ("{} Get request received for user '{}'" , endPoint , idUser );
190
232
191
- JsonObject result = new JsonObject ();
192
- JsonArray jsonProjects = new JsonArray ();
193
-
194
- for (ProjectRecord project : projects ) {
195
- jsonProjects .add (projectConverter .toListJson (project ));
233
+ // Limit result set value
234
+ if ( (limit == null ) || (limit > 50 )) {
235
+ LOG .info ("{} Limit throttle to 50 entries" , endPoint );
236
+ limit = 50 ;
237
+ }
238
+
239
+ // Check ofset from the beginning of the record set
240
+ if ((offset == null ) || (offset < 0 )) {
241
+ offset = 0 ;
196
242
}
197
243
198
- result .add ("rows" , jsonProjects );
199
- result .addProperty ("total" , projectCount );
200
244
201
- return Response .ok (result .toString ()).build ();
245
+ List <ProjectRecord > projects = projectService .getSharedProjectsByUser (sort , order , limit , offset , idUser );
246
+ int projectCount = projectService .countSharedProjectsByUser (idUser );
247
+
248
+ return Response .ok (returnProjectsJson (projects , projectCount )).build ();
249
+
202
250
}
203
251
204
252
253
+ /**
254
+ *
255
+ * @param authorization
256
+ * Authorization header token
257
+ *
258
+ * @param timestamp
259
+ * A timestamp
260
+ *
261
+ * @param idProject
262
+ * The project key ID
263
+ *
264
+ * @return
265
+ * Returns a Json string containing the project details
266
+ */
205
267
@ GET
206
268
@ Path ("/get/{id}" )
207
269
@ Detail ("Get project by id" )
@@ -212,20 +274,21 @@ public Response get(
212
274
@ HeaderParam ("X-Timestamp" ) Long timestamp ,
213
275
@ PathParam ("id" ) Long idProject ) {
214
276
215
- LOG .info ("REST:/rest/shared/project/get/ Get request received for projecet '{}'" , idProject );
277
+ String endPoint = "REST:/rest/shared/project/get/" ;
278
+
279
+ LOG .info ("{} Get request received for project '{}'" , endPoint , idProject );
216
280
217
281
try {
218
- LOG .info ("Getting project record." );
219
282
ProjectRecord project = projectService .getProject (idProject );
220
283
221
284
if (project == null ) {
222
285
LOG .info ("project record was not found" );
223
286
return Response .status (Response .Status .NOT_FOUND ).build ();
224
287
}
225
288
226
- LOG .info ("Converting project to JSON string" );
289
+ LOG .info ("Converting project {} to JSON string" , idProject );
227
290
JsonObject result = projectConverter .toJson (project , false );
228
- LOG .info ("REST: /get/ " + idProject .toString () + "/ returning project {}." , project .getId ());
291
+ LOG .info ("{} " + idProject .toString () + "/ returning project {}." , endPoint , project .getId ());
229
292
230
293
return Response .ok (result .toString ()).build ();
231
294
}
@@ -235,6 +298,22 @@ public Response get(
235
298
}
236
299
}
237
300
301
+
302
+ /**
303
+ * Get project details, including the project code payload
304
+ *
305
+ * @param authorization
306
+ * Request authorization header
307
+ *
308
+ * @param timestamp
309
+ * A timestamp
310
+ *
311
+ * @param idProject
312
+ * The project key ID
313
+ *
314
+ * @return
315
+ * A string containing a Json object representing the requested project
316
+ */
238
317
@ GET
239
318
@ Path ("/editor/{id}" )
240
319
@ Detail ("Get project by id for editor" )
@@ -267,4 +346,32 @@ public Response getEditor(
267
346
}
268
347
}
269
348
349
+
350
+ /**
351
+ * Iterate a list of projects into an array of Json objects
352
+ *
353
+ * @param projects
354
+ * A List of ProjectRecord objects
355
+ *
356
+ * @param projectCount
357
+ * The number of projects available. This may not be the same value as
358
+ * the number of records contained in the passed list of ProjectRecords.
359
+ *
360
+ * @return
361
+ * A String containing the array of the converted Json objects
362
+ */
363
+ private String returnProjectsJson (@ NotNull List <ProjectRecord > projects , int projectCount ) {
364
+ JsonObject result = new JsonObject ();
365
+ JsonArray jsonProjects = new JsonArray ();
366
+
367
+ for (ProjectRecord project : projects ) {
368
+ jsonProjects .add (projectConverter .toListJson (project ));
369
+ }
370
+
371
+ result .add ("rows" , jsonProjects );
372
+ result .addProperty ("total" , projectCount );
373
+
374
+ return result .toString ();
375
+ }
376
+
270
377
}
0 commit comments