generateRegistration.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. let generateRegistration={};
  2. generateRegistration.fName="[generateRegistration]";
  3. generateRegistration.set=
  4. function(parentClass){
  5. this.parent=parentClass;
  6. }
  7. generateRegistration.selectRows=
  8. function(gObj,cb){
  9. this.parent.print(this.fName+": selectRows");
  10. let xRows=new Object();
  11. let that=this;
  12. xRows.schemaName=gObj.schemaName;
  13. xRows.queryName=gObj.queryName;
  14. xRows.success=cb;
  15. xRows.failure=function(errorInfo){that.fail(errorInfo);};
  16. LABKEY.Query.selectRows(xRows);
  17. this.parent.print(this.fName+": selectRows completed");
  18. }
  19. generateRegistration.insertRows=
  20. function(gObj,rows){
  21. this.parent.print(this.fName+": insertRows");
  22. let iRows=new Object();
  23. iRows.schemaName=gObj.schemaName;
  24. iRows.queryName=gObj.queryName;
  25. iRows.rows=rows;
  26. iRows.success=function(data){gObj.callback(data);};
  27. LABKEY.Query.insertRows(iRows);
  28. }
  29. generateRegistration.zeroPad=
  30. function(val,strLength=3){
  31. let strK=val.toString();
  32. return strK.padStart(strLength,'0');
  33. }
  34. generateRegistration.findFirstAvailableKey=
  35. function(rows){
  36. let k=-1;
  37. for (let i=0;i<rows.length;i++){
  38. if (rows[i]['Key']>k){
  39. k=rows[i]['Key'];
  40. }
  41. }
  42. this.parent.print(this.fName+': Key candidate: '+(k+1));
  43. return k+1;
  44. }
  45. generateRegistration.generateObjectAtKey=
  46. function(gObj,k){
  47. let regCode=gObj.codeBase+this.zeroPad(k);
  48. this.parent.print(this.fName+": regCode "+regCode);
  49. let row=new Object();
  50. row['Key']=k;
  51. row[gObj.codeField]=regCode;
  52. if ("addData" in gObj){
  53. for (let q in gObj.addData){
  54. row[q]=gObj.addData[q];
  55. }
  56. }
  57. return row;
  58. }
  59. generateRegistration.getCode=
  60. function(gObj,row){
  61. return row[gObj.codeField];
  62. }
  63. generateRegistration.updateField=
  64. function(gObj,text){
  65. let el=this.parent.config.document.getElementById(gObj.elementId);
  66. this.parent.print(this.fName+": updateField "+gObj.elementId+'/'+el);
  67. el.value=text;
  68. }
  69. generateRegistration.generateId=
  70. function(gObj,data){
  71. this.parent.print(this.fName+": generateId "+data.rows.length);
  72. let k=this.findFirstAvailableKey(data.rows);
  73. let row=this.generateObjectAtKey(gObj,k);
  74. this.updateField(gObj,this.getCode(gObj,row));
  75. let rows=new Array();
  76. rows.push(row);
  77. this.insertRows(gObj,rows);
  78. }
  79. generateRegistration.doNothing=
  80. function(data){
  81. this.parent.print(this.fName+": doNothing() called");
  82. }
  83. generateRegistration.fail=
  84. function(errorInfo){
  85. this.parent.print(this.fName+": error "+errorInfo.exception);
  86. }
  87. generateRegistration.execute=
  88. function(gObj){
  89. let that=this;
  90. //this.parent.print(this.fName+": execute "+gObj.elementId);
  91. this.inspect(gObj);
  92. this.selectRows(gObj,function(data){that.generateId(gObj,data);});
  93. }
  94. generateRegistration.inspect=
  95. function(gObj){
  96. this.parent.print(this.fName);
  97. this.parent.print("query: "+gObj.schemaName+'/'+gObj.queryName);
  98. this.parent.print("codeBase "+gObj.codeBase+" codeField "+gObj.codeField);
  99. this.parent.print("elementId "+gObj.elementId);
  100. this.parent.print("callback "+gObj.callback);
  101. this.parent.print("version 1.01");
  102. }
  103. //generic function for all functors
  104. //config is there by default
  105. //
  106. //pars is semicolon delimeted list of parName=value pairs;
  107. //required:
  108. //codeBase - prepend ids with this set of letters
  109. //schemaName - schema of queryName
  110. //queryName - query that keeps assigned ids
  111. //codeField - id field in queryName
  112. //
  113. //outputId is the field that gets updated with the button result
  114. //
  115. //object is initialized from a list in LabKey
  116. //
  117. generateRegistration.getObject=
  118. function(qPar,outputId){
  119. let gObj=new Object();
  120. gObj.codeBase=qPar["codeBase"];
  121. gObj.schemaName=qPar["schemaName"];
  122. gObj.queryName=qPar["queryName"];
  123. gObj.codeField=qPar["codeField"];
  124. gObj.elementId=outputId;
  125. //should set codeBase and elementId after initialization
  126. return gObj;
  127. }