generateRegistration.js 3.5 KB

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