crfData.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //not tested yet.
  2. //to use, add crfTecant/crfData.js to requiresScript and in the call-back, run init
  3. //will work with crfSetup as setup object
  4. var crfData={};
  5. crfData.init=
  6. function(cb=null){
  7. this.print('[crfData:init]');
  8. let that=this;
  9. let action=function(){that.afterScripts(cb);};
  10. LABKEY.requiresScript(["crfTecant/runQuery.js","crfTecant/variableList.js"],action);
  11. }
  12. crfData.afterScripts=
  13. function(cb=null){
  14. if (cb) cb();
  15. }
  16. crfData.setSetup=
  17. function(setup){
  18. this.setup=setup;
  19. }
  20. crfData.getContainer=
  21. function(label){
  22. return this.setup.getContainer(label);
  23. }
  24. crfData.print=
  25. function(msg){
  26. console.log(msg);
  27. }
  28. //getters
  29. crfData.getSnapshotObject=
  30. function(){
  31. if (!("dataQueriesSnapshot" in this))
  32. this.dataQueriesSnapshot=new Object();
  33. return this.dataQueriesSnapshot;
  34. }
  35. crfData.getQuerySnapshot=
  36. function(queryName){
  37. //check whether queryName is in snapshotObject?
  38. return this.getSnapshotObject()[queryName];
  39. }
  40. crfData.getLayoutObject=
  41. function(){
  42. if (!("dataQueriesLayout" in this))
  43. this.dataQueriesLayout=new Object();
  44. return this.dataQueriesLayout;
  45. }
  46. crfData.getQueryLayout=
  47. function(queryName){
  48. //check whether queryName is in snapshotObject?
  49. return this.getLayoutObject()[queryName];
  50. }
  51. crfData.getLookupObject=
  52. function(){
  53. if (!("lookup" in this))
  54. this.lookup=new Object();
  55. return this.lookup;
  56. }
  57. crfData.getLookup=
  58. function(queryName){
  59. let x=this.getLookupObject();
  60. if (queryName in x) return x[queryName];
  61. return null;
  62. }
  63. crfData.getQueryList=
  64. function(){
  65. if (!("queryList" in this))
  66. this.queryList=new Object();
  67. return this.queryList;
  68. }
  69. crfData.getRegistration=
  70. function(){
  71. let regQueryPars=variableList.parseVariables(this.setup.getSettings('registrationQuery'));
  72. let query=regQueryPars['query'];
  73. return this.getQuerySnapshot(query).rows;
  74. }
  75. crfData.getRegistrationMap=
  76. function(value=null){
  77. let rows=this.getRegistration();
  78. let qMap=new Object();
  79. let key='Key';
  80. if (!value) value='participantStudyId';
  81. for (let i=0;i<rows.length;i++){
  82. qMap[rows[i][key]]=rows[i][value];
  83. }
  84. return qMap;
  85. }
  86. crfData.setDataLayout=
  87. function(formId,cb){
  88. let fName='[setDataLayout]';
  89. this.print(fName);
  90. let rowsSetup=this.setup.selectFormSetupRows(formId);
  91. let queryArray=new Array();
  92. let dS=this.getLayoutObject();//reference only
  93. let qList=this.getQueryList();
  94. let qMap=this.setup.getMap('inputLists');
  95. //config.formConfig.lookup=new Object();
  96. for (let i=0;i<rowsSetup.length;i++){
  97. let entry=rowsSetup[i];
  98. //skip review rows
  99. if (entry['showFlag']=='REVIEW')
  100. continue;
  101. let queryId=entry['queryName'];
  102. let q=qMap[queryId];
  103. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  104. qList[q]=0;
  105. this.print(fName+' adding '+q);
  106. if (entry['showQuery']!="NONE"){
  107. let sq=entry['showQuery'];
  108. queryArray.push(runQuery.makeQuery(dS,'data',sq,sq,[]));
  109. qList[sq]=0;
  110. this.print(fName+' adding '+sq);
  111. }
  112. }
  113. //always add reviews
  114. let q='reviewComments';
  115. queryArray.push(runQuery.makeQuery(dS,'data',q,q,[]));
  116. qList[q]=0;
  117. let that=this;
  118. let action=function(){that.processLayout(cb);};
  119. runQuery.getDataFromQueries(this,queryArray,action);
  120. }
  121. //this happens after the for loop, so all dataQueries objects are set
  122. crfData.processLayout=
  123. function(cb=null){
  124. let fName='[processLayout]';
  125. let qList=this.getQueryList();
  126. //for layouts
  127. let queryArray=new Array();
  128. let targetObject=this.getLookupObject();
  129. let lookupSet=new Object();
  130. for (let q in qList){
  131. let qobject=this.getQueryLayout(q);
  132. this.print(fName+" inspecting layout for "+q+" "+qobject);
  133. qobject.fields=qobject.metaData.fields;
  134. qobject.title=this.findTitle(q);
  135. //check for lookups
  136. for (let f in qobject.fields){
  137. //anything else is simple but lookup
  138. let field=qobject.fields[f];
  139. if (!("lookup" in field)) continue;
  140. let lookup=field.lookup;
  141. let qObject=this.getLookup(lookup.queryName);
  142. if (qObject) continue;
  143. //add to list
  144. let qName=lookup.queryName;
  145. let qCode=qName+':'+lookup.keyColumn+':'+lookup.displayColumn;
  146. let e=runQuery.makeQuery(targetObject,'data',qName,qCode,[]);
  147. //adjust minor settings
  148. if (lookup.containerPath) e.containerPath=lookup.containerPath;
  149. e.schemaName=lookup.schemaName;
  150. e.columns=lookup.keyColumn+','+lookup.displayColumn;
  151. lookupSet[qCode]=e;
  152. this.print(fName+' inserting '+qCode);
  153. }
  154. }
  155. for (let x in lookupSet){
  156. queryArray.push(lookupSet[x]);
  157. this.print(fName+' adding '+x);
  158. for (let v in lookupSet[x]){
  159. this.print(fName+' value ['+v+'] '+lookupSet[x][v]);
  160. }
  161. }
  162. //this.print(fName+' print '+targetObject.print);
  163. let that=this;
  164. let action=function(){that.processLookup(cb);};
  165. this.print(fName+' getDataFromQueries');
  166. runQuery.getDataFromQueries(this,queryArray,action);
  167. this.print(fName+' getDataFromQueries done');
  168. }
  169. crfData.processLookup=
  170. function(cb=null){
  171. let fName="[processLookup]";
  172. let obj=this.getLookupObject();
  173. for (let q in obj){
  174. this.print(fName+" "+q);
  175. let a=q.split(':');
  176. if (a.length<3) continue;
  177. let lookupName=a[0];
  178. let key=a[1];
  179. let val=a[2];
  180. obj[lookupName]=new Object();
  181. this.print(fName+' adding ['+lookupName+'] '+key+'/'+val);
  182. let lObject=obj[lookupName];
  183. lObject.LUT=new Array();//key to value
  184. lObject.ValToKey=new Array();//value to key
  185. lObject.keyColumn=key
  186. lObject.displayColumn=val;
  187. let qRows=obj[q].rows;
  188. for (let i=0;i<qRows.length;i++){
  189. let r=qRows[i];
  190. this.print(fName+' LUT ['+r[key]+'] '+r[val]);
  191. lObject.LUT[r[key]]=r[val];
  192. lObject.ValToKey[r[val]]=r[key];
  193. }
  194. }
  195. if (cb) cb();
  196. }
  197. crfData.setData=
  198. function(crfRef,cb=null){
  199. fName='[setData]';
  200. //let crfMatch=this.getCRFref();
  201. //let parentCrf=config.formConfig.crfEntry['parentCrf'];
  202. //if (parentCrf!=undefined) crfMatch=parentCrf;
  203. this.print(fName+' form crf ['+crfRef+'] ');
  204. let queryArray=new Array();
  205. let targetObject=this.getSnapshotObject();
  206. //collect data and execute callback cb for queries in cb.queryList
  207. let qList=this.setup.getQueryList();
  208. for (q in qList){
  209. let filters=[LABKEY.Filter.create("crfRef",crfRef)];
  210. queryArray.push(runQuery.makeQuery(targetObject,'data',q,q,filters));
  211. }
  212. runQuery.getDataFromQueries(this,queryArray,cb);
  213. }
  214. crfData.setRegistration=
  215. function(cb=null){
  216. let regQueryPars=variableList.parseVariables(this.setup.getSettings('registrationQuery'));
  217. let q=regQueryPars['query'];
  218. let queryArray=new Array();
  219. let targetObject=this.getSnapshotObject();
  220. queryArray.push(runQuery.makeQuery(targetObject,'data',q,q,[]));
  221. runQuery.getDataFromQueries(this,queryArray,cb);
  222. }