function grSelectRows(cb){ this.config.print(this.fName+": selectRows"); let xRows=new Object(); let gObj=this; xRows.schemaName=this.schemaName; xRows.queryName=this.queryName; xRows.success=cb; xRows.failure=function(errorInfo){gObj.fail(errorInfo);}; LABKEY.Query.selectRows(xRows); this.config.print(this.fName+": selectRows completed"); } function grInsertRows(rows){ this.config.print(this.fName+": insertRows"); let iRows=new Object(); let gObj=this; iRows.schemaName=this.schemaName; iRows.queryName=this.queryName; iRows.rows=rows; iRows.success=function(data){gObj.callback(data);}; LABKEY.Query.insertRows(iRows); } function zeroPad(val,strLength=3){ let strK=val.toString(); return strK.padStart(strLength,'0'); } function findFirstAvailableKey(rows){ let k=-1; for (let i=0;ik){ k=rows[i]['Key']; } } this.config.print(this.fName+': Key candidate: '+(k+1)); return k+1; } function generateObjectAtKey(k){ let regCode=this.codeBase+this.zeroPad(k); this.config.print(this.fName+": regCode "+regCode); let row=new Object(); row['Key']=k; row[this.codeField]=regCode; if ("addData" in this){ for (let q in this.addData){ row[q]=this.addData[q]; } } return row; } function getCode(row){ return row[this.codeField]; } function updateField(text){ let el=this.config.document.getElementById(this.elementId); el.value=text; } function generateId(data){ this.config.print(this.fName+": generateId "+data.rows.length); let k=this.findFirstAvailableKey(data.rows); let row=this.generateObjectAtKey(k); this.updateField(this.getCode(row)); let rows=new Array(); rows.push(row); this.insertRows(rows); } function doNothing1(data){ this.config.print(this.fName+": doNothing() called"); } function fail(errorInfo){ this.config.print(this.fName+": error "+errorInfo.exception); } function execute(){ let gObj=this; this.config.print(this.fName+": execute"); this.selectRows(function(data){gObj.generateId(data);}); } function inspect(){ this.config.print(this.fName); this.config.print("query: "+this.schemaName+'/'+this.queryName); this.config.print("codeBase "+this.codeBase+" codeField "+this.codeField); this.config.print("version 25"); } //generic function for all functors //config is there by default // //pars is semicolon delimeted list of parName=value pairs; //required: //codeBase - prepend ids with this set of letters //schemaName - schema of queryName //queryName - query that keeps assigned ids //codeField - id field in queryName // //outputId is the field that gets updated with the button result // //object is initialized from a list in LabKey // function getGenerationObject(config,qPar,outputId){ let gObj=new Object(); gObj.config=config; gObj.insertRows=grInsertRows; gObj.selectRows=grSelectRows; gObj.zeroPad=zeroPad; gObj.findFirstAvailableKey=findFirstAvailableKey; gObj.generateObjectAtKey=generateObjectAtKey; gObj.getCode=getCode; gObj.updateField=updateField; gObj.generateId=generateId; gObj.codeBase=qPar["codeBase"]; gObj.schemaName=qPar["schemaName"]; gObj.queryName=qPar["queryName"]; gObj.codeField=qPar["codeField"]; gObj.elementId=outputId; gObj.execute=execute; gObj.inspect=inspect; gObj.fName="[generateRegistration]"; gObj.fail=fail; gObj.doNothing1=doNothing1; gObj.callback=gObj.doNothing1; //should set codeBase and elementId after initialization return gObj; }