55 */
66package com .parallax .server .blocklyprop .converter ;
77
8- import com .google .gson .JsonObject ;
9- import com .google .inject .Inject ;
108import com .parallax .server .blocklyprop .db .dao .ProjectDao ;
119import com .parallax .server .blocklyprop .db .generated .tables .pojos .Project ;
1210import com .parallax .server .blocklyprop .db .generated .tables .records .ProjectRecord ;
1311import com .parallax .server .blocklyprop .db .generated .tables .records .ProjectSharingRecord ;
1412import com .parallax .server .blocklyprop .security .BlocklyPropSecurityUtils ;
15- // import com.parallax.server.blocklyprop.services.ProjectService;
1613import com .parallax .server .blocklyprop .services .ProjectSharingService ;
1714import com .parallax .server .blocklyprop .services .UserService ;
1815import com .parallax .server .blocklyprop .utils .DateConversion ;
16+
17+ import com .google .gson .JsonObject ;
18+ import com .google .inject .Inject ;
1919import java .util .List ;
2020import org .slf4j .Logger ;
2121import org .slf4j .LoggerFactory ;
@@ -36,7 +36,6 @@ public class ProjectConverter {
3636
3737 private ProjectDao projectDao ;
3838 private UserService userService ;
39- // private ProjectService projectService;
4039 private ProjectSharingService projectSharingService ;
4140
4241 // Internal flag to enable/disable parent project details
@@ -53,12 +52,6 @@ public void setUserService(UserService userService) {
5352 this .userService = userService ;
5453 }
5554
56- /*
57- @Inject
58- public void setProjectService(ProjectService projectService) {
59- this.projectService = projectService;
60- }
61- */
6255 @ Inject
6356 public void setProjectSharingService (ProjectSharingService projectSharingService ) {
6457 this .projectSharingService = projectSharingService ;
@@ -72,6 +65,8 @@ public void setProjectSharingService(ProjectSharingService projectSharingService
7265 * @return
7366 */
7467 public JsonObject toListJson (ProjectRecord project ) {
68+ LOG .debug ("Converting a ProjectRecord to a Json object" );
69+
7570 JsonObject result = new JsonObject ();
7671
7772 if (project != null ) {
@@ -105,6 +100,9 @@ public JsonObject toListJson(ProjectRecord project) {
105100 return result ;
106101 }
107102
103+ // TODO: Refactor code to eliminate the parent project details. We don't use it
104+
105+
108106 // Overrride method to provide option to turn off parent project details
109107 public JsonObject toJson (ProjectRecord project , Boolean includeParentDetails ) {
110108 includeParentProjectDetails = includeParentDetails ;
@@ -125,72 +123,107 @@ public JsonObject toJson(Project project, Boolean includeParentDetails) {
125123 * @return
126124 */
127125 public JsonObject toJson (ProjectRecord project ) {
126+ LOG .debug ("Converting a ProjectRecord object to JSON" );
127+
128+ JsonObject result = null ;
128129
129130 if (project == null ) {
130131 LOG .error ("Recieved a null project. Unable to convert it to JSON" );
131132 return null ;
132133 }
133134
134- JsonObject result = new JsonObject ();
135-
136- LOG .info ("Converting a ProjectRecord object to JSON" );
135+ try {
136+ result = new JsonObject ();
137137
138- result .addProperty ("id" , project .getId ());
139- result .addProperty ("name" , project .getName ());
140- result .addProperty ("description" , project .getDescription ());
141- result .addProperty ("description-html" , project .getDescriptionHtml ());
142- result .addProperty ("type" , project .getType ().name ());
143- result .addProperty ("board" , project .getBoard ());
144- result .addProperty ("private" , project .getPrivate ());
145- result .addProperty ("shared" , project .getShared ());
146- result .addProperty ("created" , DateConversion .toDateTimeString (project .getCreated ().getTime ()));
147- result .addProperty ("modified" , DateConversion .toDateTimeString (project .getModified ().getTime ()));
138+ result .addProperty ("id" , project .getId ());
139+ result .addProperty ("name" , project .getName ());
140+ result .addProperty ("description" , project .getDescription ());
141+ result .addProperty ("description-html" , project .getDescriptionHtml ());
142+ result .addProperty ("type" , project .getType ().name ());
143+ result .addProperty ("board" , project .getBoard ());
144+ result .addProperty ("private" , project .getPrivate ());
145+ result .addProperty ("shared" , project .getShared ());
146+ result .addProperty ("created" , DateConversion .toDateTimeString (project .getCreated ().getTime ()));
147+ result .addProperty ("modified" , DateConversion .toDateTimeString (project .getModified ().getTime ()));
148148
149- LOG .debug ("Evaluating project owner" );
150- // Does current user own this project?
151- boolean isYours = project .getIdUser ().equals (BlocklyPropSecurityUtils .getCurrentUserId ());
152- result .addProperty ("yours" , isYours );
153-
154- result .addProperty ("user" , userService .getUserScreenName (project .getIdUser ()));
149+ LOG .debug ("Evaluating project owner" );
150+
151+ // Does current user own this project?
152+ boolean isYours = project .getIdUser ().equals (BlocklyPropSecurityUtils .getCurrentUserId ());
155153
156- // Obtain a project share key if it is available
157- if (isYours ) {
154+ LOG .debug ("isYours = {}" , isYours );
155+
156+ result .addProperty ("yours" , isYours );
157+ result .addProperty ("user" , userService .getUserScreenName (project .getIdUser ()));
158158
159- LOG .debug ("Retrieving the project sharing record for project {}" ,project .getId ());
159+ LOG .debug ("Checking isYours State" );
160+
161+ // Obtain a project share key if it is available
162+ if (isYours ) {
163+
164+ LOG .debug ("Retrieving the project sharing record for project {}" ,project .getId ());
160165
161- List <ProjectSharingRecord > projectSharingRecords =
162- projectSharingService .getSharingInfo (project .getId ());
166+ List <ProjectSharingRecord > projectSharingRecords =
167+ projectSharingService .getSharingInfo (project .getId ());
163168
164- LOG .debug ("Checking results from project sharing request" );
169+ LOG .debug ("Checking results from project sharing request" );
165170
166- if (projectSharingRecords != null && !projectSharingRecords .isEmpty ()) {
167- result .addProperty ("share-key" , projectSharingRecords .get (0 ).getSharekey ());
171+ if (projectSharingRecords == null ) {
172+ LOG .debug ("The project sharing record is null." );
173+ return result ;
174+ }
175+
176+ if (projectSharingRecords .isEmpty ()) {
177+ LOG .debug ("The project sharing record is empty" );
178+ return result ;
179+ }
180+
181+ LOG .debug ("Project shared key is {}" , projectSharingRecords .get (0 ).getSharekey ());
182+
183+ if (projectSharingRecords != null && !projectSharingRecords .isEmpty ()) {
184+ LOG .debug ("Adding shared key to the results" );
185+ result .addProperty ("share-key" , projectSharingRecords .get (0 ).getSharekey ());
186+ }
168187 }
169- }
170188
171- if (includeParentProjectDetails ) {
172- // Look for parent project - WHY????
173- if (project .getBasedOn () != null ) {
174- JsonObject basedOn = new JsonObject ();
175- ProjectRecord basedOnProject = projectDao .getProject (project .getBasedOn ());
176- if (basedOnProject != null ) {
177- basedOn .addProperty ("id" , basedOnProject .getId ());
178- basedOn .addProperty ("name" , basedOnProject .getName ());
179- boolean basedOnProjectisYours = basedOnProject .getIdUser ().equals (BlocklyPropSecurityUtils .getCurrentUserId ());
180- basedOn .addProperty ("yours" , basedOnProjectisYours );
181- if (!isYours ) {
182- basedOn .addProperty ("user" , userService .getUserScreenName (basedOnProject .getIdUser ()));
189+ LOG .debug ("Include parent project details is {}" , includeParentProjectDetails );
190+
191+ if (includeParentProjectDetails ) {
192+ // Look for parent project - WHY????
193+ if (project .getBasedOn () != null ) {
194+ JsonObject basedOn = new JsonObject ();
195+ ProjectRecord basedOnProject = projectDao .getProject (project .getBasedOn ());
196+
197+ if (basedOnProject != null ) {
198+ basedOn .addProperty ("id" , basedOnProject .getId ());
199+ basedOn .addProperty ("name" , basedOnProject .getName ());
200+ boolean basedOnProjectisYours = basedOnProject .getIdUser ().equals (BlocklyPropSecurityUtils .getCurrentUserId ());
201+ basedOn .addProperty ("yours" , basedOnProjectisYours );
202+
203+ if (!isYours ) {
204+ basedOn .addProperty ("user" , userService .getUserScreenName (basedOnProject .getIdUser ()));
205+ }
206+
207+ result .add ("basedOn" , basedOn );
183208 }
184- result .add ("basedOn" , basedOn );
185209 }
186210 }
187- }
188211
212+ LOG .debug ("Returning result: {}" , result );
213+
214+ return result ;
215+ }
216+
217+ catch (Exception ex ) {
218+ LOG .error ("Json Conversion failed with message: {}" , ex .getMessage ());
219+ }
220+
189221 return result ;
222+
190223 }
191224
192225
193- public JsonObject toJson (Project project ) {
226+ private JsonObject toJson (Project project ) {
194227 JsonObject result = new JsonObject ();
195228 result .addProperty ("id" , project .getId ());
196229 result .addProperty ("name" , project .getName ());
0 commit comments