34
34
@ Singleton
35
35
public class ProjectDaoImpl implements ProjectDao {
36
36
37
+ private static final int Min_BlocklyCodeSize = 48 ;
37
38
private static final Logger LOG = LoggerFactory .getLogger (ProjectDao .class );
38
39
private DSLContext create ;
39
40
@@ -63,9 +64,11 @@ public ProjectRecord getProject(Long idProject) {
63
64
.where (Tables .PROJECT .ID .equal (idProject ))
64
65
.fetchOne ();
65
66
67
+ if (record == null ) {
68
+ LOG .warn ("Unable to retreive project {}" , idProject );
69
+ return null ;
70
+ }
66
71
// Return the project after checking if for depricated blocks
67
- //
68
- // Todo: Verify that the record was fetched - it sometimes is not.
69
72
return alterReadRecord (record );
70
73
}
71
74
@@ -210,7 +213,7 @@ public ProjectRecord createProject(
210
213
211
214
// Used by the randomString function
212
215
213
- static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%& ()*-+./:;=? @[]^_`{|}~" ;
216
+ static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#()*-+./:;=@[]^_`{|}~" ;
214
217
static Random rnd = new Random ();
215
218
216
219
/**
@@ -652,22 +655,26 @@ private ProjectRecord doProjectClone(ProjectRecord original) {
652
655
// horribly wrong with the string conversions.
653
656
//
654
657
private ProjectRecord alterReadRecord (ProjectRecord record ) {
655
- LOG .info ("Verify project block characteristics" );
656
658
String currentCode , newCode ;
657
659
658
660
if (record == null ) {
659
- LOG .error ("Null project record detected ." );
660
- throw new NullPointerException ( "Cannot alter a null project record." ) ;
661
+ LOG .error ("alterReadRecod detected a null project record." );
662
+ return null ;
661
663
}
662
664
663
665
try {
666
+ LOG .info ("Verify project {} block characteristics" , record .getId ());
664
667
currentCode = record .getCode ();
665
668
666
669
// Return immediately if there is no code to adjust
667
670
if (currentCode == null ) {
668
671
LOG .warn ("Project () code block is empty." , record .getId ());
669
672
return record ;
670
673
}
674
+
675
+ if (currentCode .length () < Min_BlocklyCodeSize ) {
676
+ LOG .warn ("Project code appears to be missing" );
677
+ }
671
678
672
679
/*
673
680
* Make a copy of the project. We will use this after the updates
@@ -676,12 +683,12 @@ private ProjectRecord alterReadRecord(ProjectRecord record) {
676
683
*/
677
684
newCode = currentCode ;
678
685
679
- if (record .getType () == ProjectType .SPIN ) {
680
- newCode = fixSpinProjectBlocks (newCode );
686
+ // if (record.getType() == ProjectType.SPIN) {
687
+ // newCode = fixSpinProjectBlocks(newCode);
681
688
682
- } else if (record .getType () == ProjectType .PROPC ) {
683
- newCode = fixPropcProjectBlocks (newCode );
684
- }
689
+ // } else if (record.getType() == ProjectType.PROPC) {
690
+ newCode = fixPropcProjectBlocks (newCode , record . getType () );
691
+ // }
685
692
686
693
// Check for any difference from the original code
687
694
if (!currentCode .equals (newCode )) {
@@ -716,7 +723,7 @@ private String fixSpinProjectBlocks(String newCode) {
716
723
return newCode ;
717
724
}
718
725
719
- private String fixPropcProjectBlocks (String newCode ) {
726
+ private String fixPropcProjectBlocks (String newCode , ProjectType projType ) {
720
727
LOG .info ("Looking for depricated PropC blocks." );
721
728
722
729
newCode = newCode .replaceAll ("field name=\" OP\" >ADD</field" ,
@@ -775,11 +782,91 @@ private String fixPropcProjectBlocks(String newCode) {
775
782
776
783
newCode = newCode .replaceAll ("block type=\" logic_boolean_negate\" " ,
777
784
"block type=\" logic_negate\" " );
785
+
786
+ newCode = newCode .replaceAll ("_000 / " , "000 / " );
778
787
779
788
// Fix a small issue with calling the wrong project type.
780
789
newCode = newCode .replaceAll ("block type=\" spin_integer\" " ,
781
790
"block type=\" math_number\" " );
791
+
792
+ if (projType == ProjectType .SPIN ) {
793
+ // Change all math number blocks to the same kind
794
+ newCode = newCode .replaceAll ("block type=\" math_int_angle\" " ,
795
+ "block type=\" math_number\" " );
796
+ newCode = newCode .replaceAll ("block type=\" math_integer\" " ,
797
+ "block type=\" math_number\" " );
798
+ newCode = newCode .replaceAll ("block type=\" scribbler_random_number\" " ,
799
+ "block type=\" math_random\" " );
800
+ newCode = newCode .replaceAll ("field name=\" INT_VALUE\" " ,
801
+ "field name=\" NUM\" " );
802
+ newCode = newCode .replaceAll ("field name=\" ANGLE_VALUE\" " ,
803
+ "field name=\" NUM\" " );
804
+
805
+ newCode = newCode .replaceAll ("block type=\" digital_input\" " ,
806
+ "block type=\" check_pin\" " );
807
+ newCode = newCode .replaceAll ("block type=\" digital_output\" " ,
808
+ "block type=\" make_pin\" " );
809
+ newCode = newCode .replaceAll ("block type=\" scribbler_servo\" " ,
810
+ "block type=\" servo_move\" " );
811
+ newCode = newCode .replaceAll ("field name=\" SERVO_PIN\" " ,
812
+ "field name=\" PIN\" " );
813
+ newCode = newCode .replaceAll ("field name=\" SERVO_ANGLE\" " ,
814
+ "field name=\" ANGLE\" " );
815
+ newCode = newCode .replaceAll ("<block type=\" serial_" ,
816
+ "<block type=\" scribbler_serial_" );
817
+ newCode = newCode .replaceAll ("field name=\" TIMESCALE\" >1000<" ,
818
+ "field name=\" TIMESCALE\" >Z1<" );
819
+ newCode = newCode .replaceAll ("field name=\" TIMESCALE\" >1<" ,
820
+ "field name=\" TIMESCALE\" >Z1000<" );
821
+ newCode = newCode .replaceAll ("field name=\" TIMESCALE\" >10<" ,
822
+ "field name=\" TIMESCALE\" >100<" );
823
+ newCode = newCode .replaceAll ("field name=\" TIMESCALE\" >Z" ,
824
+ "field name=\" TIMESCALE\" >" );
825
+
826
+ newCode = newCode .replaceAll ("Scribbler#CS" ,"256" );
827
+ newCode = newCode .replaceAll ("Scribbler#NL" ,"10" );
828
+ newCode = newCode .replaceAll ("Scribbler#LF" ,"13" );
829
+ newCode = newCode .replaceAll ("Scribbler#BS" ,"127" );
830
+
831
+ newCode = newCode .replaceAll ("block type=\" scribbler_loop\" " ,
832
+ "block type=\" controls_repeat\" " );
833
+ newCode = newCode .replaceAll ("statement name=\" LOOP\" " ,
834
+ "statement name=\" DO\" " );
835
+ //newCode = newCode.replaceAll("<block type=\"scribbler_loop\" id=(.*)><statement name=\"LOOP\">",
836
+ //"<block type=\"controls_repeat\" id=$1><mutation type=\"FOREVER\"></mutation><field name=\"TYPE\">FOREVER</field><statement name=\"DO\">");
837
+ newCode = newCode .replaceAll ("<block type=\" scribbler_limited_loop\" id=(.*)><field name=\" LOOP_COUNT\" >(.*)</field><statement name=\" LOOP\" >" ,
838
+ "<block type=\" controls_repeat\" id=$1><mutation type=\" TIMES\" ></mutation><field name=\" TYPE\" >TIMES</field><value name=\" TIMES\" ><block type=\" math_number\" id=\" " + randomString (20 ) + "\" ><field name=\" NUM\" >$2</field></block></value><statement name=\" DO\" >" );
782
839
840
+ /*
841
+ // These aren't working consistently - so the fallback to to leave the old blocks alone,
842
+ // Replace old "simple" s3 blocks with equavalent block combinations
843
+
844
+ newCode = newCode.replaceAll("scribbler_if_line\" id=(.*)><mutation state=\"(.*)\"></mutation><field name=\"LINE_CONDITION\">(.*)</field><field name=\"LINE_POSITION\">(.*)</field><field name=\"LINE_COLOR\">(.*)</field><statement name=\"IF_LINE",
845
+ "controls_if\" id=$1><value name=\"IF0\"><block type=\"scribbler_simple_line\" id=\"" + randomString(20) + "\"><mutation state=\"$2\"></mutation><field name=\"LINE_CONDITION\">$3</field><field name=\"LINE_POSITION\">$4</field><field name=\"LINE_COLOR\">$5</field></block></value><statement name=\"DO0");
846
+
847
+ // Not sure if I need non-mutation replacers - if not included, they will likely get cleaned on the second save (first one adds them, second one replaces)
848
+ //newCode = newCode.replaceAll("scribbler_if_obstacle\" id=(.*)><field name=\"OBSTACLE_CONDITION\">(.*)</field><field name=\"OBSTACLE_POSITION\">(.*)</field><statement name=\"IF_OBSTACLE",
849
+ // "controls_if\" id=$1><value name=\"IF0\"><block type=\"scribbler_simple_obstacle\" id=\"" + randomString(20) + "\"><field name=\"OBSTACLE_CONDITION\">$2</field><field name=\"OBSTACLE_POSITION\">$3</field></block></value><statement name=\"DO0");
850
+
851
+ newCode = newCode.replaceAll("scribbler_if_obstacle\" id=(.*)><mutation state=\"(.*)\"></mutation><field name=\"OBSTACLE_CONDITION\">(.*)</field><field name=\"OBSTACLE_POSITION\">(.*)</field><statement name=\"IF_OBSTACLE",
852
+ "controls_if\" id=$1><value name=\"IF0\"><block type=\"scribbler_simple_obstacle\" id=\"" + randomString(20) + "\"><mutation state=\"$2\"></mutation><field name=\"OBSTACLE_CONDITION\">$3</field><field name=\"OBSTACLE_POSITION\">$4</field></block></value><statement name=\"DO0");
853
+
854
+ newCode = newCode.replaceAll("<field name=\"OBSTACLE_SENSOR_CHOICE\">RIGHT</field>",
855
+ "<mutation state=\"IS\"></mutation><field name=\"OBSTACLE_CONDITION\">IS</field><field name=\"OBSTACLE_POSITION\">RIGHT</field>");
856
+ newCode = newCode.replaceAll("<field name=\"OBSTACLE_SENSOR_CHOICE\">LEFT</field>",
857
+ "<mutation state=\"IS\"></mutation><field name=\"OBSTACLE_CONDITION\">IS</field><field name=\"OBSTACLE_POSITION\">LEFT</field>");
858
+ newCode = newCode.replaceAll("<field name=\"OBSTACLE_SENSOR_CHOICE\">&&</field>",
859
+ "<mutation state=\"IS\"></mutation><field name=\"OBSTACLE_CONDITION\">IS</field><field name=\"OBSTACLE_POSITION\">CENTER</field>");
860
+ newCode = newCode.replaceAll("<field name=\"OBSTACLE_SENSOR_CHOICE\">\\|\\|</field>",
861
+ "<mutation state=\"IS\"></mutation><field name=\"OBSTACLE_CONDITION\">IS</field><field name=\"OBSTACLE_POSITION\">DETECTED</field>");
862
+ newCode = newCode.replaceAll("<block type=\"obstacle_sensor\"",
863
+ "<block type=\"scribbler_simple_obstacle\"");
864
+
865
+ newCode = newCode.replaceAll("scribbler_if_light\" id=(.*)><mutation state=\"(.*)\"></mutation><field name=\"LIGHT_CONDITION\">(.*)</field><field name=\"LIGHT_POSITION\">(.*)</field><statement name=\"IF_LIGHT",
866
+ "controls_if\" id=$1><value name=\"IF0\"><block type=\"scribbler_simple_light\" id=\"" + randomString(20) + "\"><mutation state=\"$2\"></mutation><field name=\"LIGHT_CONDITION\">$3</field><field name=\"LIGHT_POSITION\">$4</field></block></value><statement name=\"DO0");
867
+ */
868
+ }
869
+
783
870
// Replace the Robot init block with two blocks, need to generate unique 20-digit blockID:
784
871
newCode = newCode .replaceAll ("</field><field name=\" RAMPING\" >" ,
785
872
"</field></block><block type=\" ab_drive_ramping\" id=\" " + randomString (20 ) + "\" ><field name=\" RAMPING\" >" );
0 commit comments