runQuery.js 2.9 KB

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