Skip to content

Commit 696988c

Browse files
committed
Correct issue where new projects were orphaned. Refactor code, add comments.
1 parent 63db37e commit 696988c

File tree

2 files changed

+159
-40
lines changed

2 files changed

+159
-40
lines changed

src/main/java/com/parallax/server/blocklyprop/db/dao/impl/ProjectDaoImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,16 +643,21 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
643643
@Override
644644
public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newName, String newBoard) {
645645

646-
LOG.info("Saving project code as '{}'", newName);
646+
LOG.info("Saving project code from project {} as '{}'", idProject, newName);
647647

648648
// Retreive the source project
649649
ProjectRecord original = getProject(idProject);
650+
650651
if (original == null) {
651652
LOG.error("Original project {} is missing. Unable to save code as...", idProject);
652653
throw new NullPointerException("Project doesn't exist");
653-
} else if (newBoard == null) {
654+
}
655+
656+
// Use the board type from the parent project if it was not provided
657+
if (newBoard == null) {
654658
newBoard = original.getBoard();
655659
}
660+
656661

657662
// Obtain the current bp user record.
658663
Long idUser = BlocklyPropSecurityUtils.getCurrentUserId();
@@ -663,6 +668,7 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
663668
// shared or community project
664669
// --------------------------------------------------------------------
665670
if (original.getIdUser().equals(idUser) || original.getShared()) {
671+
666672
ProjectRecord cloned = createProject(
667673
newName,
668674
original.getDescription(),
@@ -674,7 +680,13 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
674680
false, // Set project unshared
675681
original.getId());
676682

683+
if (cloned == null) {
684+
LOG.warn("Unable to create a copy og the project.");
685+
}
677686
return cloned;
687+
} else {
688+
LOG.warn("Unable to copy the project. UID: {}, PUID: {}, Shared: {}",
689+
idUser, original.getIdUser(), original.getShared());
678690
}
679691
} else {
680692
LOG.info("Unable to retreive BP user id");

src/main/java/com/parallax/server/blocklyprop/rest/RestSharedProject.java

Lines changed: 145 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Parallax Inc.
2+
* Copyright (c) 2019 Parallax Inc.
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
55
* and associated documentation files (the “Software”), to deal in the Software without
@@ -16,7 +16,7 @@
1616
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1717
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19-
* SOFTWARE.
19+
* SOFTWARE.
2020
*/
2121

2222
package com.parallax.server.blocklyprop.rest;
@@ -42,35 +42,67 @@
4242
import javax.ws.rs.QueryParam;
4343
import javax.ws.rs.core.Response;
4444

45+
import org.jetbrains.annotations.NotNull;
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
4748

4849

4950
/**
51+
* Manage requests for public projects
5052
*
5153
* @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.
5259
*/
5360
@Path("/shared/project")
5461
@Group(name = "/shared/project", title = "Project management")
5562
@HttpCode("500>Internal Server Error,200>Success Response")
5663
public class RestSharedProject {
5764

65+
/**
66+
* Get a connection to the logging system
67+
*/
5868
private static final Logger LOG = LoggerFactory.getLogger(RestSharedProject.class);
5969

70+
71+
/**
72+
* Get a handle to project services
73+
*/
6074
private ProjectService projectService;
6175

76+
77+
/**
78+
* Get a handle to a project converter
79+
*/
6280
private ProjectConverter projectConverter;
6381

82+
83+
/**
84+
* Inject project services
85+
*
86+
* @param projectService
87+
* An instance of the ProjectService object
88+
*/
6489
@Inject
6590
public void setProjectService(ProjectService projectService) {
6691
this.projectService = projectService;
6792
}
6893

94+
95+
/**
96+
* Inject project conversion services
97+
* @param projectConverter
98+
* An instance of the ProjectConverter object
99+
*/
69100
@Inject
70101
public void setProjectConverter(ProjectConverter projectConverter) {
71102
this.projectConverter = projectConverter;
72103
}
73104

105+
74106
/**
75107
* Return a list of community projects.
76108
*
@@ -101,33 +133,33 @@ public Response get(
101133
@QueryParam("limit") Integer limit,
102134
@QueryParam("offset") Integer offset) {
103135

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);
107139

108140
boolean parametersValid = false;
109141

110142
// Sort flag evaluation
111143
if (sort != null) {
112144
for (TableSort t : TableSort.values()) {
113-
LOG.debug("REST:/shared/project/list/ Sort test for '{}'", t);
145+
LOG.debug("{} Sort test for '{}'",endPoint, t);
114146

115147
if (sort == t) {
116148
parametersValid = true;
117149
break;
118150
}
119151
}
120152

121-
if (parametersValid == false) {
122-
LOG.warn("REST:/shared/project/list/ Sort parameter failed");
153+
if (!parametersValid) {
154+
LOG.warn("{} Sort parameter failed", endPoint);
123155
return Response.status(Response.Status.NOT_ACCEPTABLE).build();
124156
}
125157
}
126158

127159
// Sort order evaluation
128160
if (order != null) {
129161
parametersValid = false;
130-
LOG.debug("REST:/shared/project/list/ Checking order");
162+
LOG.debug("{} Checking order", endPoint);
131163

132164
for (TableOrder t : TableOrder.values()) {
133165
if (order == t) {
@@ -136,41 +168,52 @@ public Response get(
136168
}
137169
}
138170

139-
if (parametersValid == false) {
171+
if (!parametersValid) {
140172
return Response.status(Response.Status.NOT_ACCEPTABLE).build();
141173
}
142174
}
143175

144176
// Limit result set value
145177
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);
147179
limit = 50;
148180
}
149181

150182
// Check ofset from the beginning of the record set
151183
if ((offset == null) || (offset < 0)) {
152184
offset = 0;
153185
}
154-
186+
187+
// Get a block of projects
155188
List<ProjectRecord> projects
156189
= projectService.getSharedProjects(sort, order, limit, offset);
157190

158191
// Obtain a count of the total number of community projects available
159192
int projectCount = projectService.countSharedProjects();
160193

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();
172195
}
173196

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+
*/
174217
@GET
175218
@Path("/list/user/{id}")
176219
@Detail("Get shared projects by user")
@@ -183,25 +226,44 @@ public Response get(
183226
@QueryParam("offset") Integer offset,
184227
@PathParam("id") Long idUser) {
185228

186-
LOG.info("REST:/shared/project/list/user/ Get request received for user '{}'", idUser);
229+
String endPoint = "REST:/shared/project/list/user/";
187230

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);
190232

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;
196242
}
197243

198-
result.add("rows", jsonProjects);
199-
result.addProperty("total", projectCount);
200244

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+
202250
}
203251

204252

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+
*/
205267
@GET
206268
@Path("/get/{id}")
207269
@Detail("Get project by id")
@@ -212,20 +274,21 @@ public Response get(
212274
@HeaderParam("X-Timestamp") Long timestamp,
213275
@PathParam("id") Long idProject) {
214276

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);
216280

217281
try {
218-
LOG.info("Getting project record.");
219282
ProjectRecord project = projectService.getProject(idProject);
220283

221284
if (project == null) {
222285
LOG.info("project record was not found");
223286
return Response.status(Response.Status.NOT_FOUND).build();
224287
}
225288

226-
LOG.info("Converting project to JSON string");
289+
LOG.info("Converting project {} to JSON string", idProject);
227290
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());
229292

230293
return Response.ok(result.toString()).build();
231294
}
@@ -235,6 +298,22 @@ public Response get(
235298
}
236299
}
237300

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+
*/
238317
@GET
239318
@Path("/editor/{id}")
240319
@Detail("Get project by id for editor")
@@ -267,4 +346,32 @@ public Response getEditor(
267346
}
268347
}
269348

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+
270377
}

0 commit comments

Comments
 (0)