runQuery.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var runQuery={};
  2. //external dependencies:
  3. //LABKEY.Query
  4. //print -> configObject.print
  5. runQuery.makeQuery=
  6. function(targetObject,containerName,queryName,fieldName,filterArray){
  7. //call with makeQuery(config.formConfig,getContainer(name),...
  8. let e=new Object();
  9. e.containerName=containerName;
  10. e.queryName=queryName;
  11. e.fieldName=fieldName;
  12. e.filterArray=filterArray;
  13. e.targetObject=targetObject;
  14. return e;
  15. }
  16. runQuery.getDataFromQueries=
  17. function(parentClass,queryArray,cb){
  18. //queryArray should contain elements with
  19. //- fieldName to set the data variable
  20. //- containerName to select container (data,config,CRF)
  21. //- queryName to select query
  22. //- filterArray to perform filtering, empty array works
  23. //- callback cb to be called with no arguments
  24. //
  25. this.afterQuery(new Object(),-1,parentClass,queryArray,cb);
  26. }
  27. runQuery.afterQuery=
  28. function(data,id,parentClass,queryArray,cb){
  29. //queryArray should contain elements with
  30. //- fieldName to set the data variable
  31. //- containerName to select container (data,config,CRF)
  32. //- queryName to select query
  33. //- filterArray to perform filtering, empty array works
  34. //- callback cb to be called with no arguments
  35. //
  36. //it should be called with id -1.
  37. //
  38. //targetObject.print('afterQuery1['+id+'/'+queryArray.length+']: ');
  39. let fName='[afterQuery]';
  40. if (id>-1){
  41. let e1=queryArray[id];
  42. let fieldName=e1.fieldName;
  43. parentClass.print(fName+' ['+fieldName+']: '+data.rows.length);
  44. e1.targetObject[fieldName]=data;
  45. }
  46. id+=1;
  47. if (id==queryArray.length) {
  48. cb();
  49. return;
  50. }
  51. let e=queryArray[id];
  52. for (v in e){
  53. parentClass.print(fName+' value ['+v+'] '+e[v]);
  54. }
  55. let qconfig=new Object();
  56. qconfig.containerPath=parentClass.getContainer(e.containerName);
  57. if ("containerPath" in e){
  58. parentClass.print(fName+' containerPath '+e.containerPath);
  59. qconfig.containerPath=e.containerPath;
  60. }
  61. qconfig.schemaName="lists";
  62. if ("schemaName" in e){
  63. parentClass.print(fName+' schemaName='+e.schemaName);
  64. qconfig.schemaName=e.schemaName;
  65. }
  66. if ("columns" in e){
  67. parentClass.print(fName+' columns='+e.columns);
  68. qconfig.columns=e.columns;
  69. }
  70. qconfig.queryName=e.queryName;
  71. //this should point to configuration container
  72. //don't filter -> so we can pick up other forms (say registration) later on
  73. //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)];
  74. if ("filterArray" in e)
  75. qconfig.filterArray=e.filterArray;
  76. //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)]
  77. let that=this;
  78. qconfig.success=function(data){that.afterQuery(data,id,parentClass,queryArray,cb);};
  79. qconfig.failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);};
  80. LABKEY.Query.selectRows(qconfig);
  81. }
  82. runQuery.onTAFailure=
  83. function(parentClass, errorInfo, responseObj){
  84. //don't have configObject to rely to
  85. parentClass.print('[afterQuery]: Failure: '+errorInfo.exception);
  86. }