generateRegistration.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. function selectRows(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 insertRows(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(){gObj.doNothing();};
  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. return row;
  43. }
  44. function getCode(row){
  45. return row[this.codeField];
  46. }
  47. function updateField(text){
  48. let el=this.config.document.getElementById(this.elementId);
  49. el.value=text;
  50. }
  51. function generateId(data){
  52. this.config.print(this.fName+": generateId "+data.rows.length);
  53. let k=this.findFirstAvailableKey(data.rows);
  54. let row=this.generateObjectAtKey(k);
  55. this.updateField(this.getCode(row));
  56. let rows=new Array();
  57. rows.push(row);
  58. this.insertRows(rows);
  59. }
  60. function doNothing(data){
  61. }
  62. function fail(errorInfo){
  63. this.config.print(this.fName+": error "+errorInfo.exception);
  64. }
  65. function execute(){
  66. let gObj=this;
  67. this.config.print(this.fName+": execute");
  68. this.selectRows(function(data){gObj.generateId(data);});
  69. }
  70. function inspect(){
  71. this.config.print(this.fName);
  72. this.config.print("query: "+this.schemaName+'/'+this.queryName);
  73. this.config.print("codeBase "+this.codeBase+" codeField "+this.codeField);
  74. this.config.print("version 25");
  75. }
  76. //generic function for all functors
  77. //config is there by default
  78. //
  79. //pars is semicolon delimeted list of parName=value pairs;
  80. //required:
  81. //codeBase - prepend ids with this set of letters
  82. //schemaName - schema of queryName
  83. //queryName - query that keeps assigned ids
  84. //codeField - id field in queryName
  85. //
  86. //outputId is the field that gets updated with the button result
  87. //
  88. //object is initialized from a list in LabKey
  89. //
  90. function getGenerationObject(config,qPar,outputId){
  91. let gObj=new Object();
  92. gObj.config=config;
  93. gObj.insertRows=insertRows;
  94. gObj.selectRows=selectRows;
  95. gObj.zeroPad=zeroPad;
  96. gObj.findFirstAvailableKey=findFirstAvailableKey;
  97. gObj.generateObjectAtKey=generateObjectAtKey;
  98. gObj.getCode=getCode;
  99. gObj.updateField=updateField;
  100. gObj.generateId=generateId;
  101. gObj.codeBase=qPar["codeBase"];
  102. gObj.schemaName=qPar["schemaName"];
  103. gObj.queryName=qPar["queryName"];
  104. gObj.codeField=qPar["codeField"];
  105. gObj.elementId=outputId;
  106. gObj.execute=execute;
  107. gObj.inspect=inspect;
  108. gObj.fName="[generateRegistration]";
  109. gObj.fail=fail;
  110. //should set codeBase and elementId after initialization
  111. return gObj;
  112. }