@@ -412,8 +412,10 @@ public List<ProjectRecord> getUserProjects(
412
412
* @param order
413
413
* @param limit
414
414
* @param offset
415
- * @param idUser
415
+
416
416
* @return
417
+ * Returns a list of ProjectRecord objects corresponding to the projects
418
+ * matching the selection creiteria
417
419
*/
418
420
@ Override
419
421
public List <ProjectRecord > getSharedProjects (
@@ -563,39 +565,68 @@ public boolean deleteProject(Long idProject) {
563
565
}
564
566
565
567
/**
566
- * TODO: add details.
568
+ * Update the code block in the specified project
567
569
*
568
570
* @param idProject
569
571
* @param code
572
+ *
570
573
* @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.
571
581
*/
572
582
@ Override
573
583
public ProjectRecord updateProjectCode (Long idProject , String code ) {
574
584
LOG .info ("Update code for project {}." , idProject );
585
+
586
+ // Retrieve the specified project
575
587
ProjectRecord record = create .selectFrom (Tables .PROJECT )
576
588
.where (Tables .PROJECT .ID .equal (idProject ))
577
589
.fetchOne ();
578
590
591
+ // Get a timestamp used to update the modified field of the project record
579
592
GregorianCalendar cal = new GregorianCalendar ();
580
593
cal .setTime (new java .util .Date ());
581
594
582
595
if (record != null ) {
596
+ // Found the project. Verify that the current user owns it
583
597
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
584
611
if (record .getIdUser ().equals (idUser )) {
585
612
record .setCode (code );
586
613
record .setModified (cal );
587
614
record .setCodeBlockVersion (BLOCKLY_LIBRARY_VERSION );
588
615
record .update ();
589
616
return record ;
590
617
} else {
618
+ // If the project is a shared project, allow the current user
619
+ // to clone the project into their library
591
620
if (record .getShared ()) {
592
621
ProjectRecord cloned = doProjectClone (record );
593
622
cloned .setCode (code );
594
623
cloned .setModified (cal );
595
624
cloned .setCodeBlockVersion (BLOCKLY_LIBRARY_VERSION );
625
+ cloned .setIdUser (idUser ); // The logged in user owns this copy of the project
596
626
cloned .update ();
597
627
return cloned ;
598
628
}
629
+
599
630
LOG .error ("User {} tried and failed to update project {}." , idUser , idProject );
600
631
throw new UnauthorizedException ();
601
632
}
@@ -605,6 +636,8 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
605
636
}
606
637
}
607
638
639
+
640
+
608
641
/**
609
642
* Save the current project as a new project
610
643
*
@@ -617,16 +650,21 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
617
650
@ Override
618
651
public ProjectRecord saveProjectCodeAs (Long idProject , String code , String newName , String newBoard ) {
619
652
620
- LOG .info ("Saving project code as '{}'" , newName );
653
+ LOG .info ("Saving project code from project {} as '{}'" , idProject , newName );
621
654
622
655
// Retreive the source project
623
656
ProjectRecord original = getProject (idProject );
657
+
624
658
if (original == null ) {
625
659
LOG .error ("Original project {} is missing. Unable to save code as..." , idProject );
626
660
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 ) {
628
665
newBoard = original .getBoard ();
629
666
}
667
+
630
668
631
669
// Obtain the current bp user record.
632
670
Long idUser = BlocklyPropSecurityUtils .getCurrentUserId ();
@@ -637,6 +675,7 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
637
675
// shared or community project
638
676
// --------------------------------------------------------------------
639
677
if (original .getIdUser ().equals (idUser ) || original .getShared ()) {
678
+
640
679
ProjectRecord cloned = createProject (
641
680
newName ,
642
681
original .getDescription (),
@@ -648,7 +687,13 @@ public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newNa
648
687
false , // Set project unshared
649
688
original .getId ());
650
689
690
+ if (cloned == null ) {
691
+ LOG .warn ("Unable to create a copy og the project." );
692
+ }
651
693
return cloned ;
694
+ } else {
695
+ LOG .warn ("Unable to copy the project. UID: {}, PUID: {}, Shared: {}" ,
696
+ idUser , original .getIdUser (), original .getShared ());
652
697
}
653
698
} else {
654
699
LOG .info ("Unable to retreive BP user id" );
@@ -700,7 +745,7 @@ private ProjectRecord doProjectClone(ProjectRecord original) {
700
745
original .getBoard (),
701
746
original .getPrivate (),
702
747
original .getShared (),
703
- original .getId ()
748
+ original .getId () // set the parent project id
704
749
);
705
750
706
751
// cloned.setBasedOn(original.getId());
0 commit comments