Explorar o código

Adding generateRegistration

Andrej Studen %!s(int64=2) %!d(string=hai) anos
pai
achega
ccef04efe5
Modificáronse 1 ficheiros con 121 adicións e 0 borrados
  1. 121 0
      web/crfTecant/generateRegistration.js

+ 121 - 0
web/crfTecant/generateRegistration.js

@@ -0,0 +1,121 @@
+function selectRows(cb){
+   this.config.print(this.fName+": selectRows");
+   let xRows=new Object();
+   xRows.schemaName=this.schemaName;
+   xRows.queryName=this.queryName;
+   xRows.success=function(data){this.generateId(data);};
+   xRows.failure=function(errorInfo){this.fail(errorInfo);};
+   LABKEY.Query.selectRows(xRows);
+   this.config.print(this.fName+": selectRows completed");
+}
+
+function insertRows(rows){
+   this.config.print(this.fName+": insertRows");
+   let iRows=new Object();
+   iRows.schemaName=this.schemaName;
+   iRows.queryName=this.queryName;
+   iRows.rows=rows;
+   iRows.success=doNothing;
+   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;i<rows.length;i++){
+      if (rows[i]['Key']>k){
+         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;
+   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 doNothing(data){
+}
+
+function fail(errorInfo){
+   this.config.print(this.fName+": error "+errorInfo.exception);
+}
+
+function execute(){
+   this.config.print(this.fName+": execute");
+   this.selectRows(function(data){this.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 24");
+}
+
+//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=insertRows;
+   gObj.selectRows=selectRows;
+   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;
+   //should set codeBase and elementId after initialization
+   return gObj;
+}