runQuery.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. for (v in e){
  49. e.targetObject.print(fName+' value ['+v+'] '+e[v]);
  50. }
  51. let qconfig=new Object();
  52. qconfig.containerPath=e.targetObject.getContainer(e.containerName);
  53. if ("containerPath" in e){
  54. e.targetObject.print(fName+' containerPath '+e.containerPath);
  55. qconfig.containerPath=e.containerPath;
  56. }
  57. qconfig.schemaName="lists";
  58. if ("schemaName" in e){
  59. e.targetObject.print(fName+' schemaName='+e.schemaName);
  60. qconfig.schemaName=e.schemaName;
  61. }
  62. if ("columns" in e){
  63. e.targetObject.print(fName+' columns='+e.columns);
  64. qconfig.columns=e.columns;
  65. }
  66. qconfig.queryName=e.queryName;
  67. //this should point to configuration container
  68. //don't filter -> so we can pick up other forms (say registration) later on
  69. //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)];
  70. if ("filterArray" in e)
  71. qconfig.filterArray=e.filterArray;
  72. //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)]
  73. qconfig.success=function(data){afterQuery(data,id,queryArray,cb);};
  74. qconfig.failure=function(errorInfo,responseObj){onTAFailure(e.targetObject,errorInfo,responseObj);};
  75. LABKEY.Query.selectRows(qconfig);
  76. }
  77. function onTAFailure(targetObj, errorInfo, responseObj){
  78. //don't have configObject to rely to
  79. targetObj.print('[afterQuery]: Failure: '+errorInfo.exception);
  80. }