crfSetup.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. var crfSetup={};
  2. crfSetup.print=
  3. function(msg){
  4. console.log(msg);
  5. }
  6. crfSetup.init=
  7. function(cb=null){
  8. let fName="[crfSetup:init]";
  9. this.print(fName);
  10. let that=this;
  11. let action=function(){that.afterScripts(cb);}
  12. LABKEY.requiresScript(["crfTecant/runQuery.js"],action);
  13. }
  14. crfSetup.afterScripts=
  15. function(cb=null){
  16. if (cb) cb();
  17. }
  18. crfSetup.setContainer=
  19. function(label,container){
  20. if (!(this.hasOwnProperty('container'))){
  21. this.container=new Array();
  22. }
  23. this.container[label]=container;
  24. }
  25. crfSetup.getContainer=
  26. function(label){
  27. return this.container[label];
  28. }
  29. crfSetup.getSettings=
  30. function(variable){
  31. if (variable in this.settings){
  32. return this.settings[variable];
  33. }
  34. return null;
  35. }
  36. crfSetup.getRows=
  37. function(objectName){
  38. if (objectName in this)
  39. return this[objectName].rows;
  40. return new Array();
  41. }
  42. crfSetup.getMaps=
  43. function(){
  44. if (!("maps" in this))
  45. this.maps=new Object();
  46. return this.maps;
  47. }
  48. crfSetup.getEntryMaps=
  49. function(){
  50. if (!("entryMaps" in this))
  51. this.entryMaps=new Object();
  52. return this.entryMaps;
  53. }
  54. crfSetup.getMap=
  55. function(queryName){
  56. let maps=this.getMaps();
  57. if (!(queryName in maps))
  58. this.parseMap(queryName);
  59. return maps[queryName];
  60. }
  61. crfSetup.getEntryMap=
  62. function(queryName){
  63. let entryMaps=this.getEntryMaps();
  64. if (!(queryName in entryMaps))
  65. this.parseEntryMap(queryName);
  66. return entryMaps[queryName];
  67. }
  68. crfSetup.setContainers=
  69. function(cb=null){
  70. this.setContainer('data',LABKEY.ActionURL.getContainer());
  71. this.setContainer('config',LABKEY.ActionURL.getContainer());
  72. this.setContainer('CRF',LABKEY.ActionURL.getContainer());
  73. let selectRows=new Object();
  74. //this is local data
  75. selectRows.containerPath=this.getContainer('CRF');
  76. selectRows.schemaName='lists';
  77. selectRows.queryName='crfSettings';
  78. //store form related data to this object
  79. let that=this;
  80. selectRows.success=function(data){that.parseSettings(data,cb);};
  81. LABKEY.Query.selectRows(selectRows);
  82. }
  83. crfSetup.parseSettings=
  84. function(data,cb){
  85. let fName="[parseSettings]";
  86. this.settings=new Array();
  87. for (let i=0;i<data.rows.length;i++){
  88. let n=data.rows[i]['name'];
  89. let v=data.rows[i]['value'];
  90. this.settings[n]=v;
  91. }
  92. this.print(fName);
  93. for (let k in this.settings){
  94. this.print(fName+'\t'+k+'='+this.settings[k]);
  95. }
  96. //if ('dataContainer' in st){
  97. // setContainer('data',st['dataContainer']);
  98. //}
  99. let vname='configContainer';
  100. if (vname in this.settings){
  101. this.setContainer('config',this.settings[vname]);
  102. }
  103. this.print(fName+' config: '+this.getContainer('config'));
  104. this.print(fName+' data: '+this.getContainer('data'));
  105. if (cb) cb();
  106. }
  107. crfSetup.parseSetup=
  108. function(cb=null){
  109. //setup queryArray
  110. let queryArray=new Array();
  111. //targetObject
  112. let targetObject=this;
  113. //static variables
  114. queryArray.push(runQuery.makeQuery(targetObject,'data','crfStaticVariables','crfStaticVariables',[]));
  115. //Forms
  116. queryArray.push(runQuery.makeQuery(targetObject,'config','Forms','dataForms',[]));
  117. //users
  118. queryArray.push(runQuery.makeQuery(targetObject,'data','users','users',[]));
  119. queryArray[queryArray.length-1].schemaName='core';
  120. //inputLists
  121. queryArray.push(runQuery.makeQuery(targetObject,'config','inputLists','inputLists',[]));
  122. //crfEditors
  123. queryArray.push(runQuery.makeQuery(targetObject,'config','crfEditors','crfEditors',[]));
  124. //crfMonitors
  125. queryArray.push(runQuery.makeQuery(targetObject,'config','crfMonitors','crfMonitors',[]));
  126. //crfSponsors
  127. queryArray.push(runQuery.makeQuery(targetObject,'config','crfSponsors','crfSponsors',[]));
  128. //crfManagers
  129. queryArray.push(runQuery.makeQuery(targetObject,'config','crfManagers','crfManagers',[]));
  130. //FormStatus
  131. queryArray.push(runQuery.makeQuery(targetObject,'config','FormStatus','formStatus',[]));
  132. //site
  133. queryArray.push(runQuery.makeQuery(targetObject,'config','site','siteData',[]));
  134. //crfEntry
  135. queryArray.push(runQuery.makeQuery(targetObject,'data','crfEntry','crfEntries',[]));
  136. queryArray.push(
  137. runQuery.makeQuery(targetObject,'config','generateConfig','generateConfigData',[]));
  138. let that=this;
  139. let action=function(){that.addStudyProperties(cb);};
  140. runQuery.getDataFromQueries(this,queryArray,action);
  141. }
  142. crfSetup.addStudyProperties=
  143. function(cb){
  144. //setup queryArray
  145. let queryArray=new Array();
  146. let targetObject=this;
  147. queryArray.push(runQuery.makeQuery(targetObject,'data','StudyProperties','studyData',[]));
  148. let e=queryArray[queryArray.length-1];
  149. e.schemaName='study';
  150. let columnModel="";
  151. let varRows=this.getRows('crfStaticVariables');
  152. for (let i=0;i<varRows.length;i++){
  153. if (i>0) columnModel+=',';
  154. columnModel+=varRows[i]['staticVariable'];
  155. }
  156. e.columns=columnModel;
  157. let that=this;
  158. //let action=function(){that.fcontinue();};
  159. //let action=function(){that.parseQueryMap(cb);};
  160. let action=cb;
  161. runQuery.getDataFromQueries(this,queryArray,action);
  162. }
  163. crfSetup.selectFormSetupRows=
  164. function(formId){
  165. let formSetupRows=new Array();
  166. let config=this.config;
  167. let allRows=this.getRows("formSetup");
  168. for (let i=0;i<allRows.length;i++){
  169. let formEntry=allRows[i];
  170. if (formEntry.formName==formId)
  171. formSetupRows.push(formEntry);
  172. }
  173. return formSetupRows;
  174. }
  175. crfSetup.parseMap=
  176. function(queryName){
  177. let fName='[parseMap/'+queryName+']';
  178. let key="Key";
  179. let value="value";
  180. if (queryName=="inputLists")
  181. value="queryName";
  182. if (queryName=="users"){
  183. key="UserId";
  184. value="DisplayName";
  185. }
  186. if (queryName=='dataForms')
  187. value='formName';
  188. if (queryName=='formStatus')
  189. value='formStatus';
  190. this.print(fName);
  191. let rows=this.getRows(queryName);
  192. this.maps[queryName]=new Object();
  193. let qMap=this.maps[queryName];
  194. for (let i=0;i<rows.length;i++){
  195. let r=rows[i];
  196. qMap[r[key]]=r[value];
  197. //this.print(fName+' ['+r[key]+'] '+r[value]);
  198. }
  199. }
  200. crfSetup.parseEntryMap=
  201. function(queryName){
  202. let rows=this.getRows(queryName);
  203. this.entryMaps[queryName]=new Object();
  204. let qMap=this.entryMaps[queryName];
  205. let key='Key';
  206. if (queryName=='Users') key='UserId';
  207. for (let i=0;i<rows.length;i++){
  208. let r=rows[i];
  209. qMap[r[key]]=r;
  210. //this.print(fName+' ['+r[key]+'] '+r[value]);
  211. }
  212. }
  213. crfSetup.printMap=
  214. function(queryName){
  215. let fName='[printMap]';
  216. let qMap=this.getMap(queryName);
  217. for (let x in qMap){
  218. this.print(fName+' ['+x+'] '+qMap[x]);
  219. }
  220. }