Skip to content

Commit 502b8f9

Browse files
committed
Update DAO code for readability
1 parent 0260196 commit 502b8f9

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public void setDSLContext(DSLContext dsl) {
4343
*
4444
* Create a ProjectRecord object from an existing project.
4545
*
46+
* Note: There are private getProject methods that retrieve
47+
* a project record based on a number of parameters
48+
* passed in.
49+
*
4650
* TODO: add details.
4751
*
4852
* @param idProject
@@ -58,6 +62,8 @@ public ProjectRecord getProject(Long idProject) {
5862
.fetchOne();
5963

6064
// Return the project after checking if for depricated blocks
65+
//
66+
// Todo: Verify that the record was fetched - it sometimes is not.
6167
return alterReadRecord(record);
6268
}
6369

@@ -215,8 +221,10 @@ public ProjectRecord createProject(
215221
*/
216222
@Override
217223
public ProjectRecord updateProject(
218-
Long idProject, String name, String description,
219-
String descriptionHtml, boolean privateProject,
224+
Long idProject, String name,
225+
String description,
226+
String descriptionHtml,
227+
boolean privateProject,
220228
boolean sharedProject) {
221229

222230
LOG.info("Update project {}.", idProject);
@@ -259,8 +267,12 @@ public ProjectRecord updateProject(
259267
*/
260268
@Override
261269
public ProjectRecord updateProject(
262-
Long idProject, String name, String description,
263-
String descriptionHtml, String code, boolean privateProject,
270+
Long idProject,
271+
String name,
272+
String description,
273+
String descriptionHtml,
274+
String code,
275+
boolean privateProject,
264276
boolean sharedProject) {
265277

266278
LOG.info("Update project {}.", idProject);
@@ -301,10 +313,14 @@ public ProjectRecord saveCode(Long idProject, String code) {
301313
ProjectRecord record = getProject(idProject, true);
302314
if (record != null) {
303315
record.setCode(code);
304-
ProjectRecord returningRecord = create.update(Tables.PROJECT).set(record).returning().fetchOne();
316+
ProjectRecord returningRecord = create
317+
.update(Tables.PROJECT)
318+
.set(record)
319+
.returning()
320+
.fetchOne();
305321
return returningRecord;
306322
}
307-
LOG.warn("Unable to save code for project {}", idProject);
323+
LOG.error("Unable to save code for project {}", idProject);
308324
return null;
309325
}
310326

@@ -550,7 +566,9 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
550566
return null;
551567
}
552568

553-
569+
// Private over-ride of the public getProject()
570+
//
571+
//
554572
private ProjectRecord getProject(Long idProject, boolean toEdit) {
555573
LOG.info("Retreiving project {}.", idProject);
556574
ProjectRecord record = create
@@ -564,6 +582,8 @@ private ProjectRecord getProject(Long idProject, boolean toEdit) {
564582
// Return a project if the edit flag is off or the edit flag is
565583
// on and the project owner is the current user
566584
if (!toEdit || record.getIdUser().equals(idUser)) {
585+
586+
// Todo: Verify that the record was fetched - it sometimes is not.
567587
return alterReadRecord(record);
568588
} else {
569589
LOG.error("User {} attempted to edit project {} without authorization.",
@@ -573,6 +593,8 @@ private ProjectRecord getProject(Long idProject, boolean toEdit) {
573593
}
574594

575595
// Return the project after checking if for depricated blocks
596+
//
597+
// Todo: Verify that the record was fetched - it sometimes is not.
576598
return alterReadRecord(record);
577599
}
578600

@@ -597,12 +619,17 @@ private ProjectRecord doProjectClone(ProjectRecord original) {
597619
}
598620

599621

600-
// Swap out old block definitions
622+
// Evaluate project code and replace any depricated or updated blocks
623+
//
624+
// Return a ProjectRecord object. The code field may be altered to correct
625+
// any old, depricated or updated blocks. The method will throw an
626+
// exception if the ProjectRecord parameter is null or something has gone
627+
// horribly wrong with the string conversions.
628+
//
601629
private ProjectRecord alterReadRecord(ProjectRecord record) {
602630
LOG.info("Verify project block characteristics");
603631
String currentCode, newCode;
604632

605-
606633
if (record == null) {
607634
LOG.error("Null project record detected.");
608635
throw new NullPointerException("Cannot alter a null project record.");
@@ -613,7 +640,7 @@ private ProjectRecord alterReadRecord(ProjectRecord record) {
613640

614641
// Return immediately if there is no code to adjust
615642
if (currentCode == null) {
616-
LOG.warn("Project is empty.");
643+
LOG.warn("Project () code block is empty.", record.getId());
617644
return record;
618645
}
619646

@@ -633,13 +660,13 @@ private ProjectRecord alterReadRecord(ProjectRecord record) {
633660

634661
// Check for any difference from the original code
635662
if (! currentCode.equals(newCode)) {
636-
LOG.info("Updating converted project.");
663+
LOG.info("Updated depricated project code blocks in project {}.", record.getId());
637664
record.setCode(newCode);
638665
}
639666
}
640667

641668
catch (Exception ex) {
642-
LOG.error("Exception trapped. Messate is: {}", ex.getMessage());
669+
LOG.error("Exception trapped. Message is: {}", ex.getMessage());
643670
}
644671

645672
return record;

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import javax.ws.rs.Produces;
2626
import javax.ws.rs.QueryParam;
2727
import javax.ws.rs.core.Response;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
2831

2932
/**
3033
*
@@ -34,6 +37,7 @@
3437
@Group(name = "/shared/project", title = "Project management")
3538
@HttpCode("500>Internal Server Error,200>Success Response")
3639
public class RestSharedProject {
40+
private static final Logger LOG = LoggerFactory.getLogger(RestSharedProject.class);
3741

3842
private ProjectService projectService;
3943

@@ -55,7 +59,7 @@ public void setProjectConverter(ProjectConverter projectConverter) {
5559
@Name("Get all shared projects")
5660
@Produces("application/json")
5761
public Response get(@QueryParam("sort") TableSort sort, @QueryParam("order") TableOrder order, @QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset) {
58-
System.out.println("Sort: " + sort);
62+
LOG.info("Sort: {}", sort);
5963

6064
List<ProjectRecord> projects = projectService.getSharedProjects(sort, order, limit, offset);
6165
int projectCount = projectService.countSharedProjects();
@@ -78,7 +82,7 @@ public Response get(@QueryParam("sort") TableSort sort, @QueryParam("order") Tab
7882
@Name("Get shared projects by user")
7983
@Produces("application/json")
8084
public Response get(@QueryParam("sort") TableSort sort, @QueryParam("order") TableOrder order, @QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset, @PathParam("id") Long idUser) {
81-
System.out.println("Sort: " + sort);
85+
LOG.info("Sort: {}", sort);
8286

8387
List<ProjectRecord> projects = projectService.getSharedProjectsByUser(sort, order, limit, offset, idUser);
8488
int projectCount = projectService.countSharedProjectsByUser(idUser);
@@ -102,15 +106,21 @@ public Response get(@QueryParam("sort") TableSort sort, @QueryParam("order") Tab
102106
@Name("Get project by id")
103107
@Produces("application/json")
104108
public Response get(@HeaderParam("X-Authorization") String authorization, @HeaderParam("X-Timestamp") Long timestamp, @PathParam("id") Long idProject) {
105-
ProjectRecord project = projectService.getProject(idProject);
106-
107-
if (project == null) {
109+
110+
try {
111+
ProjectRecord project = projectService.getProject(idProject);
112+
113+
if (project == null) {
114+
return Response.status(Response.Status.NOT_FOUND).build();
115+
}
116+
117+
JsonObject result = projectConverter.toJson(project);
118+
return Response.ok(result.toString()).build();
119+
}
120+
catch (Exception e) {
121+
LOG.error("Exception in {} detected. Message is: {}", e.getClass(), e.getLocalizedMessage());
108122
return Response.status(Response.Status.NOT_FOUND).build();
109123
}
110-
111-
JsonObject result = projectConverter.toJson(project);
112-
113-
return Response.ok(result.toString()).build();
114124
}
115125

116126
@GET
@@ -119,18 +129,24 @@ public Response get(@HeaderParam("X-Authorization") String authorization, @Heade
119129
@Name("Get project by id for editor")
120130
@Produces("application/json")
121131
public Response getEditor(@HeaderParam("X-Authorization") String authorization, @HeaderParam("X-Timestamp") Long timestamp, @PathParam("id") Long idProject) {
122-
System.out.println("Authorization: " + authorization);
132+
LOG.info("Authorization: {}", authorization);
123133

124-
ProjectRecord project = projectService.getProject(idProject);
134+
try {
135+
ProjectRecord project = projectService.getProject(idProject);
125136

126-
if (project == null) {
127-
return Response.status(Response.Status.NOT_FOUND).build();
128-
}
137+
if (project == null) {
138+
return Response.status(Response.Status.NOT_FOUND).build();
139+
}
129140

130-
JsonObject result = projectConverter.toJson(project);
131-
result.addProperty("code", project.getCode());
141+
JsonObject result = projectConverter.toJson(project);
142+
result.addProperty("code", project.getCode());
132143

133-
return Response.ok(result.toString()).build();
144+
return Response.ok(result.toString()).build();
145+
}
146+
catch (Exception e) {
147+
LOG.error("Exception in {} detected. Message is: {}", e.getClass(), e.getLocalizedMessage());
148+
return Response.status(Response.Status.NOT_FOUND).build();
149+
}
134150
}
135151

136152
}

0 commit comments

Comments
 (0)