let generateRegistration={}; generateRegistration.fName="[generateRegistration]"; generateRegistration.set= function(parentClass){ this.parent=parentClass; } generateRegistration.selectRows= function(gObj,cb){ this.parent.print(this.fName+": selectRows"); let xRows=new Object(); let that=this; xRows.schemaName=gObj.schemaName; xRows.queryName=gObj.queryName; xRows.success=cb; xRows.failure=function(errorInfo){that.fail(errorInfo);}; LABKEY.Query.selectRows(xRows); this.parent.print(this.fName+": selectRows completed"); } generateRegistration.insertRows= function(gObj,rows){ this.parent.print(this.fName+": insertRows"); let iRows=new Object(); iRows.schemaName=gObj.schemaName; iRows.queryName=gObj.queryName; iRows.rows=rows; iRows.success=function(data){gObj.callback(data);}; LABKEY.Query.insertRows(iRows); } generateRegistration.zeroPad= function(val,strLength=3){ let strK=val.toString(); return strK.padStart(strLength,'0'); } generateRegistration.findFirstAvailableKey= function(rows){ let k=-1; for (let i=0;ik){ k=rows[i]['Key']; } } this.parent.print(this.fName+': Key candidate: '+(k+1)); return k+1; } generateRegistration.generateObjectAtKey= function(gObj,k){ let regCode=gObj.codeBase+this.zeroPad(k); this.parent.print(this.fName+": regCode "+regCode); let row=new Object(); row['Key']=k; row[gObj.codeField]=regCode; if ("addData" in gObj){ for (let q in gObj.addData){ row[q]=gObj.addData[q]; } } return row; } generateRegistration.getCode= function(gObj,row){ return row[gObj.codeField]; } generateRegistration.updateField= function(gObj,text){ let el=this.parent.config.document.getElementById(gObj.elementId); this.parent.print(this.fName+": updateField "+gObj.elementId+'/'+el); el.value=text; } generateRegistration.generateId= function(gObj,data){ this.parent.print(this.fName+": generateId "+data.rows.length); let k=this.findFirstAvailableKey(data.rows); let row=this.generateObjectAtKey(gObj,k); this.updateField(gObj,this.getCode(gObj,row)); let rows=new Array(); rows.push(row); this.insertRows(gObj,rows); } generateRegistration.doNothing= function(data){ this.parent.print(this.fName+": doNothing() called"); } generateRegistration.fail= function(errorInfo){ this.parent.print(this.fName+": error "+errorInfo.exception); } generateRegistration.execute= function(gObj){ let that=this; //this.parent.print(this.fName+": execute "+gObj.elementId); this.inspect(gObj); this.selectRows(gObj,function(data){that.generateId(gObj,data);}); } generateRegistration.inspect= function(gObj){ this.parent.print(this.fName); this.parent.print("query: "+gObj.schemaName+'/'+gObj.queryName); this.parent.print("codeBase "+gObj.codeBase+" codeField "+gObj.codeField); this.parent.print("elementId "+gObj.elementId); this.parent.print("callback "+gObj.callback); this.parent.print("version 1.01"); } //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 // generateRegistration.getObject= function(qPar,outputId){ let gObj=new Object(); gObj.codeBase=qPar["codeBase"]; gObj.schemaName=qPar["schemaName"]; gObj.queryName=qPar["queryName"]; gObj.codeField=qPar["codeField"]; gObj.elementId=outputId; //should set codeBase and elementId after initialization return gObj; }