@@ -412,8 +412,10 @@ public List<ProjectRecord> getUserProjects(
412412 * @param order
413413 * @param limit
414414 * @param offset
415- * @param idUser
415+
416416 * @return
417+ * Returns a list of ProjectRecord objects corresponding to the projects
418+ * matching the selection creiteria
417419 */
418420 @ Override
419421 public List <ProjectRecord > getSharedProjects (
@@ -563,39 +565,68 @@ public boolean deleteProject(Long idProject) {
563565 }
564566
565567 /**
566- * TODO: add details.
568+ * Update the code block in the specified project
567569 *
568570 * @param idProject
569571 * @param code
572+ *
570573 * @return
574+ * Returns the specified project record, otherwise it returns a null if
575+ * the current user does not own the project and the project is not shared
576+ * or public, or the requested project record was not found.
577+ *
578+ * @implNote This method will actually create a new project record based on the
579+ * existing project under specific conditions. Since this is an update record method,
580+ * the creation of a new project my be unexpected at higher layers of the application.
571581 */
572582 @ Override
573583 public ProjectRecord updateProjectCode (Long idProject , String code ) {
574584 LOG .info ("Update code for project {}." , idProject );
585+
586+ // Retrieve the specified project
575587 ProjectRecord record = create .selectFrom (Tables .PROJECT )
576588 .where (Tables .PROJECT .ID .equal (idProject ))
577589 .fetchOne ();
578590
591+ // Get a timestamp used to update the modified field of the project record
579592 GregorianCalendar cal = new GregorianCalendar ();
580593 cal .setTime (new java .util .Date ());
581594
582595 if (record != null ) {
596+ // Found the project. Verify that the current user owns it
583597 Long idUser = BlocklyPropSecurityUtils .getCurrentUserId ();
598+
599+ // TODO: Detecting a zero user id
600+ if (idUser == 0 ) {
601+ LOG .error ("Detected current user ID is zero for project {}" , idProject );
602+ return null ;
603+ }
604+
605+ if (record .getIdUser () == 0 ) {
606+ LOG .error ("Detected project user ID is zero for project {}" , idProject );
607+ return null ;
608+ }
609+
610+ // Update the project if the current user owns it
584611 if (record .getIdUser ().equals (idUser )) {
585612 record .setCode (code );
586613 record .setModified (cal );
587614 record .setCodeBlockVersion (BLOCKLY_LIBRARY_VERSION );
588615 record .update ();
589616 return record ;
590617 } else {
618+ // If the project is a shared project, allow the current user
619+ // to clone the project into their library
591620 if (record .getShared ()) {
592621 ProjectRecord cloned = doProjectClone (record );
593622 cloned .setCode (code );
594623 cloned .setModified (cal );
595624 cloned .setCodeBlockVersion (BLOCKLY_LIBRARY_VERSION );
625+ cloned .setIdUser (idUser ); // The logged in user owns this copy of the project
596626 cloned .update ();
597627 return cloned ;
598628 }
629+
599630 LOG .error ("User {} tried and failed to update project {}." , idUser , idProject );
600631 throw new UnauthorizedException ();
601632 }
@@ -605,6 +636,8 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
605636 }
606637 }
607638
639+
640+
608641 /**
609642 * Save the current project as a new project
610643 *
@@ -617,16 +650,21 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
617650 @ Override
618651 public ProjectRecord saveProjectCodeAs (Long idProject , String code , String newName , String newBoard ) {
619652
620- LOG .info ("Saving project code as '{}'" , newName );
653+ LOG .info ("Saving project code from project {} as '{}'" , idProject , newName );
621654
622655 // Retreive the source project
623656 ProjectRecord original = getProject (idProject );
657+
624658 if (original == null ) {
625659 LOG .error ("Original project {} is missing. Unable to save code as..." , idProject );
626660 throw new NullPointerException ("Project doesn't exist" );
627- } else if (newBoard == null ) {
661+ }
662+
663+ // Use the board type from the parent project if it was not provided
664+ if (newBoard == null ) {
628665 newBoard = original .getBoard ();
629666 }
667+
630668
631669 // Obtain the current bp user record.
632670 Long idUser = BlocklyPropSecurityUtils .getCurrentUserId ();
@@ -637,6 +675,7 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
637675 // shared or community project
638676 // --------------------------------------------------------------------
639677 if (original .getIdUser ().equals (idUser ) || original .getShared ()) {
678+
640679 ProjectRecord cloned = createProject (
641680 newName ,
642681 original .getDescription (),
@@ -648,7 +687,13 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
648687 false , // Set project unshared
649688 original .getId ());
650689
690+ if (cloned == null ) {
691+ LOG .warn ("Unable to create a copy og the project." );
692+ }
651693 return cloned ;
694+ } else {
695+ LOG .warn ("Unable to copy the project. UID: {}, PUID: {}, Shared: {}" ,
696+ idUser , original .getIdUser (), original .getShared ());
652697 }
653698 } else {
654699 LOG .info ("Unable to retreive BP user id" );
@@ -700,7 +745,7 @@ private ProjectRecord doProjectClone(ProjectRecord original) {
700745 original .getBoard (),
701746 original .getPrivate (),
702747 original .getShared (),
703- original .getId ()
748+ original .getId () // set the parent project id
704749 );
705750
706751// cloned.setBasedOn(original.getId());
0 commit comments