@@ -177,6 +177,7 @@ class Sample extends Page
177177 array ('/sub/:ssid ' , 'get ' , '_get_sub_sample ' ),
178178 array ('/sub/:ssid ' , 'patch ' , '_update_sub_sample ' ),
179179 array ('/sub/:ssid ' , 'put ' , '_update_sub_sample_full ' ),
180+ array ('/sub/cid/:cid ' , 'post ' , '_update_container_sub_samples ' ),
180181 array ('/sub ' , 'post ' , '_add_sub_sample ' ),
181182 array ('/sub/:ssid ' , 'delete ' , '_delete_sub_sample ' ),
182183 array ('/sub/queue/cid/:cid ' , 'post ' , '_queue_all_sub_samples ' ),
@@ -638,13 +639,49 @@ function _queue_all_sub_samples()
638639
639640 $ this ->db ->wait_rep_sync (false );
640641
642+ if (!sizeof ($ subs ))
643+ $ this ->_error ('No subsamples found ' );
644+
645+ $ sub_ids = array_column ($ subs , 'BLSUBSAMPLEID ' );
646+ $ chunks = array_chunk ($ sub_ids , 500 );
641647 $ ret = array ();
642- foreach ($ subs as $ sub ) {
643- array_push ($ ret , array (
644- 'BLSUBSAMPLEID ' => $ sub ['BLSUBSAMPLEID ' ],
645- 'CONTAINERQUEUESAMPLEID ' => $ this ->_do_pre_q_sample (array ('BLSUBSAMPLEID ' => $ sub ['BLSUBSAMPLEID ' ]))));
648+
649+ if ($ this ->has_arg ('UNQUEUE ' )) {
650+ foreach ($ chunks as $ chunk ) {
651+ $ placeholders = implode (', ' , array_fill (0 , count ($ chunk ), '? ' ));
652+ $ this ->db ->pq ("DELETE FROM containerqueuesample
653+ WHERE containerqueueid IS NULL
654+ AND blsubsampleid IN ( $ placeholders) " , $ chunk );
655+ }
656+
657+ $ ret = array_map (function ($ id ) {
658+ return array ('BLSUBSAMPLEID ' => $ id , 'CONTAINERQUEUESAMPLEID ' => null );
659+ }, $ sub_ids );
660+
661+ } else {
662+ foreach ($ chunks as $ chunk ) {
663+ $ placeholders = implode (', ' , array_fill (0 , count ($ chunk ), '? ' ));
664+ $ this ->db ->pq ("INSERT INTO containerqueuesample (blsubsampleid)
665+ SELECT ss.blsubsampleid
666+ FROM blsubsample ss
667+ INNER JOIN blsample s ON s.blsampleid = ss.blsampleid
668+ INNER JOIN container c ON c.containerid = s.containerid
669+ INNER JOIN dewar d ON d.dewarid = c.dewarid
670+ INNER JOIN shipping sh ON sh.shippingid = d.shippingid
671+ WHERE sh.proposalid = ?
672+ AND ss.blsubsampleid IN ( $ placeholders) " ,
673+ array_merge (array ($ this ->proposalid ), $ chunk ));
674+
675+ $ chunk_ret = $ this ->db ->pq ("SELECT blsubsampleid, containerqueuesampleid
676+ FROM containerqueuesample
677+ WHERE blsubsampleid IN ( $ placeholders)
678+ AND containerqueueid IS NULL " , $ chunk );
679+
680+ $ ret = array_merge ($ ret , $ chunk_ret );
681+ }
646682 }
647683 $ this ->_output ($ ret );
684+
648685 }
649686
650687 function _sub_samples ()
@@ -960,6 +997,68 @@ function _update_sub_sample_full()
960997 }
961998
962999
1000+ function _update_container_sub_samples ()
1001+ {
1002+ if (!$ this ->arg ('cid ' ))
1003+ $ this ->_error ('No container specified ' );
1004+
1005+ if (!$ this ->arg ('EXPERIMENTKIND ' ))
1006+ $ this ->_error ('No experiment kind provided ' );
1007+
1008+ $ rows = $ this ->db ->pq ("SELECT DISTINCT ss.diffractionplanid
1009+ FROM blsubsample ss
1010+ INNER JOIN blsample s ON s.blsampleid = ss.blsampleid
1011+ INNER JOIN diffractionplan dp ON dp.diffractionplanid = ss.diffractionplanid
1012+ INNER JOIN containerqueuesample cqs ON cqs.blsubsampleid = ss.blsubsampleid
1013+ WHERE s.containerid=:1
1014+ AND dp.experimentkind=:2
1015+ AND ss.diffractionplanid IS NOT NULL
1016+ AND cqs.containerqueuesampleid IS NOT NULL
1017+ AND cqs.containerqueueid IS NULL " ,
1018+ array ($ this ->arg ('cid ' ), $ this ->arg ('EXPERIMENTKIND ' )));
1019+
1020+ if (!sizeof ($ rows )) {
1021+ $ this ->_output (array ('TOTAL_UPDATED ' => 0 ));
1022+ return ;
1023+ }
1024+
1025+ $ all_ids = array_column ($ rows , 'DIFFRACTIONPLANID ' );
1026+ $ set_args = array ();
1027+ foreach (array (
1028+ 'REQUIREDRESOLUTION ' , 'EXPERIMENTKIND ' , 'PREFERREDBEAMSIZEX ' , 'PREFERREDBEAMSIZEY ' ,
1029+ 'EXPOSURETIME ' , 'BOXSIZEX ' , 'BOXSIZEY ' , 'AXISSTART ' , 'AXISRANGE ' , 'NUMBEROFIMAGES ' ,
1030+ 'TRANSMISSION ' , 'ENERGY ' , 'MONOCHROMATOR '
1031+ ) as $ f ) {
1032+ array_push ($ set_args , $ this ->has_arg ($ f ) ? $ this ->arg ($ f ) : null );
1033+ }
1034+
1035+ $ chunks = array_chunk ($ all_ids , 500 );
1036+ $ total_updated = 0 ;
1037+
1038+ foreach ($ chunks as $ chunk ) {
1039+ $ start_index = sizeof ($ set_args ) + 1 ;
1040+ $ placeholders = array ();
1041+
1042+ foreach ($ chunk as $ index => $ id ) {
1043+ array_push ($ placeholders , ': ' . ($ start_index + $ index ));
1044+ }
1045+
1046+ $ id_placeholders_list = implode (', ' , $ placeholders );
1047+
1048+ $ combined_args = array_merge ($ set_args , $ chunk );
1049+
1050+ $ this ->db ->pq ("UPDATE diffractionplan
1051+ SET requiredresolution=:1, experimentkind=:2, preferredbeamsizex=:3, preferredbeamsizey=:4,
1052+ exposuretime=:5, boxsizex=:6, boxsizey=:7, axisstart=:8, axisrange=:9,
1053+ numberofimages=:10, transmission=:11, energy=:12, monochromator=:13
1054+ WHERE diffractionplanid IN ( $ id_placeholders_list) " , $ combined_args );
1055+
1056+ $ total_updated += sizeof ($ chunk );
1057+ }
1058+
1059+ $ this ->_output (array ('TOTAL_UPDATED ' => $ total_updated ));
1060+ }
1061+
9631062
9641063 function _add_sub_sample ()
9651064 {
0 commit comments