runQuery.js 2.5 KB

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