var runQuery={}; runQuery.print= function(msg){ console.log(msg); } runQuery.insertRows= function(schema,query,rows,action=null,container=null,failure=null){ this.modifyRows('insert',schema,query,rows,action,container); } runQuery.deleteRows= function(schema,query,rows,action=null,container=null,failure=null){ this.modifyRows('delete',schema,query,rows,action,container); } runQuery.modifyRows= function(mode,schema,query,rows,action=null,container=null,failure=null){ //insert rows to container/schema/query and return with action let fName="[cvModifyRows/"+mode+"]"; this.print(fName+' '+schema+'/'+query); let qconfig=new Object(); qconfig.schemaName=schema; qconfig.queryName=query; if (container) qconfig.containerPath=container; if (!rows) { this.print(fName+' rows '+rows); return; } qconfig.rows=rows; qconfig.success=function(data){;}; if (action) qconfig.success=action; if (mode=='insert') LABKEY.Query.insertRows(qconfig); if (mode=='update') LABKEY.Query.updateRows(qconfig); if (mode=='delete') LABKEY.Query.deleteRows(qconfig); this.print(fName+" done"); } runQuery.selectRows= function(schema,query,filters=[],action=null, container=null, failure=null, columns=null){ let fName="[cvSelectRows]"; this.print(fName+' '+schema+' '+query+' '+container); let qconfig=new Object(); qconfig.schemaName=schema; qconfig.queryName=query; if (container) qconfig.containerPath=container; qconfig.filterArray=filters; qconfig.success=function(data){;}; if (action) qconfig.success=action; if (failure) qconfig.failure=failure; if (columns) qconfig.columns=columns; LABKEY.Query.selectRows(qconfig); this.print(fName+" done"); } runQuery.makeQuery= function(targetObject,containerName,queryName,fieldName=null,filterArray=null,schemaName=null){ //call with makeQuery(config.formConfig,getContainer(name),... let e=new Object(); e.containerName=containerName; e.queryName=queryName; e.fieldName=queryName; if (fieldName) e.fieldName=fieldName; e.filterArray=[]; if (filterArray) e.filterArray=filterArray; e.targetObject=targetObject; e.schemaName='lists'; if (schemaName) e.schemaName=schemaName; return e; } runQuery.makeModification= function(mode,containerName,schemaName,queryName,rows){ let e=new Object(); e.mode=mode; e.containerName=containerName; e.schemaName=schemaName; e.queryName=queryName; e.rows=rows; return e; } runQuery.getDataFromQueries= function(parentClass,queryArray,cb){ //queryArray should contain elements created with make query //- fieldName to set the data variable //- containerName to select container (data,config,CRF) //- queryName to select query //- filterArray to perform filtering, empty array works //- callback cb to be called with no arguments // this.afterQuery(new Object(),-1,parentClass,queryArray,cb); } runQuery.modifyDataFromQueries= function(parentClass,queryArray,cb){ //queryArray should contain elements created with make query //- fieldName to set the data variable //- containerName to select container (data,config,CRF) //- queryName to select query //- filterArray to perform filtering, empty array works //- callback cb to be called with no arguments // this.afterQueryUpload(new Object(),-1,parentClass,queryArray,cb); } runQuery.afterQuery= function(data,id,parentClass,queryArray,cb){ let fName='[afterQuery]'; if (id>-1){ let e1=queryArray[id]; let fieldName=e1.fieldName; parentClass.print(fName+' ['+fieldName+']: '+data.rows.length); e1.targetObject[fieldName]=data; } id+=1; if (id==queryArray.length) { if (cb) cb(); return; } let e=queryArray[id]; for (v in e){ parentClass.print(fName+' value ['+v+'] '+e[v]); } let containerPath=parentClass.getContainer(e.containerName); if ("containerPath" in e){ parentClass.print(fName+' containerPath '+e.containerPath); containerPath=e.containerPath; } let schemaName="lists"; if ("schemaName" in e){ parentClass.print(fName+' schemaName='+e.schemaName); schemaName=e.schemaName; } let columns=null; if ("columns" in e){ parentClass.print(fName+' columns='+e.columns); columns=e.columns; } //this should point to configuration container //don't filter -> so we can pick up other forms (say registration) later on //qconfig.filterArray=[LABKEY.Filter.create('Key',config.formId)]; let filterArray=[]; if ("filterArray" in e) filterArray=e.filterArray; //qconfig.filterArray=[LABKEY.Filter.create('formStatus',1)] let that=this; let action=function(data){that.afterQuery(data,id,parentClass,queryArray,cb);}; let failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);}; this.selectRows(schemaName,e.queryName,filterArray,action, containerPath, failure, columns); } runQuery.afterQueryUpload= function(data,id,parentClass,queryArray,cb){ let fName='[afterQueryUpload]'; if (id>-1){ let x=queryArray[id]; let q=x.queryName parentClass.print(fName+' ['+q+']: '+data.rows.length); } id+=1; if (id==queryArray.length) { if (cb) cb(); return; } let e=queryArray[id]; for (v in e){ parentClass.print(fName+' value ['+v+'] '+e[v]); } let containerPath=parentClass.getContainer(e.containerName); if ("containerPath" in e){ parentClass.print(fName+' containerPath '+e.containerPath); containerPath=e.containerPath; } let that=this; let action=function(data){that.afterQueryUpload(data,id,parentClass,queryArray,cb);}; let failure=function(errorInfo,responseObj){that.onTAFailure(parentClass,errorInfo,responseObj);}; this.modifyRows(e.mode,e.schemaName,e.queryName,e.rows,action,containerPath,failure); } runQuery.onTAFailure= function(parentClass, errorInfo, responseObj){ //don't have configObject to rely to parentClass.print('[afterQuery]: Failure: '+errorInfo.exception); }