Просмотр исходного кода

Updated setData and setDataLayout to use getDataFromQueries, also removed annoying "no rows supplied" error in onSubmit

Andrej Studen 2 лет назад
Родитель
Сommit
d1c3719578
2 измененных файлов с 190 добавлено и 195 удалено
  1. 181 194
      web/crfTecant/crfVisit.js
  2. 9 1
      web/crfTecant/runQuery.js

+ 181 - 194
web/crfTecant/crfVisit.js

@@ -110,6 +110,55 @@ function doNothing(){
 	print('doNothing called');
 }
 
+
+function createQueryObject(){
+   //create object ready for makeQuery
+   let x=new Object();
+   x.print=print;
+   x.getContainer=getContainer;
+   return x;
+}
+
+function getSnapshotObject(){
+   if (!("dataQueriesSnapshot" in config.formConfig))
+      config.formConfig.dataQueriesSnapshot=createQueryObject();
+   return config.formConfig.dataQueriesSnapshot;
+}
+
+function getQuerySnapshot(queryName){
+   //check whether queryName is in snapshotObject?
+   return getSnapshotObject()[queryName];
+}
+
+function getLayoutObject(){
+   if (!("dataQueriesLayout" in config.formConfig))
+      config.formConfig.dataQueriesLayout=createQueryObject();
+   return config.formConfig.dataQueriesLayout;
+}
+
+function getQueryLayout(queryName){
+   //check whether queryName is in snapshotObject?
+   return getLayoutObject()[queryName];
+}
+
+function getLookupObject(){
+   if (!("lookup" in config.formConfig))
+      config.formConfig.lookup=createQueryObject();
+   return config.formConfig.lookup;
+}
+
+function getLookup(queryName){
+   let x=getLookupObject();
+   if (queryName in x) return x[queryName];
+   return null;
+}
+
+function getQueryList(){
+   if (!("queryList" in config.formConfig))
+      config.formConfig.queryList=new Object();
+   return config.formConfig.queryList;
+}
+
 function generateDebugSection(){
 	//let debug=true;
 	//if (debug) print("generateDebugSection "+sectionName);
@@ -608,39 +657,6 @@ function toggleVisibility(divName,buttonName){
   	}
 }
 
-function generateButtonBU(divName,title,buttonName,callback,
-	callbackParameters){
-	let debug=true;
-	if (debug) print("generateButtonBU");
-	
-	let tb=config.document.createElement('table');
-	tb.className="t2";
-	
-	let r1=tb.insertRow();
-	th=config.document.createElement('th');
-	r1.appendChild(th);
-	th.innerHTML=title;
-	//*!*
-	let c2=r1.insertCell();
-	let i1=config.document.createElement("input");	
-	i1.type="button";
-	i1.value=buttonName;
-	i1.style.fontSize="20px";
-	i1.onclick=function(){callback(callbackParameters);}
-	c2.appendChild(i1);	
-
-	let c1=r1.insertCell();
-	c1.setAttribute("colspan","1");
-	c1.id=callbackParameters.submitReportId;
-
-	let el=config.document.getElementById(divName);
-	if (debug) print("generateButton: element["+divName+"]: "+el);
-	
-	
-	el.appendChild(tb);
-	
-	
-}
 
 function generateButton(divName,caption,label,callbackLabel,callback){
 	let debug=true;
@@ -1099,8 +1115,9 @@ function populateTable(listName,writeMode){
 	let entry=new Object();
 	
 	//data snapshot
-	let fQuery=config.formConfig.dataQueries[listName];
-	
+	let fQuery=getQuerySnapshot(listName);
+   let queryLayout=getQueryLayout(listName);
+
 	//here I assume that listName was parsed during setDataLayout and setData 
 	//so that rows was set (even if they are empty)
 	print(fName+"]: nrows "+fQuery.rows.length);
@@ -1108,12 +1125,12 @@ function populateTable(listName,writeMode){
 	if (fQuery.rows.length>0)
 		entry=fQuery.rows[0];
 	
-	let fields=fQuery.fields;
+	let fields=queryLayout.fields;
 		
 	for (f in fields){	
 		let field=fields[f];
 		//each field is a new row
-		print(fName+": Adding field: "+f+'/'+field.name+' hidden: '+field.hidden);
+		print(fName+": Adding field: "+f+'/'+field.name+' hidden: '+field.hidden+' type:'+field.type);
 		if (field.hidden) continue;
 		if (field.name=="crfRef") continue;
 		populateFieldRow(entry,field,setup);
@@ -1141,14 +1158,14 @@ function generateTable(listName,divName,additionalData,setup){
 
 
 	//data snapshot
-	let fQuery=config.formConfig.dataQueries[listName];
-
+	let fQuerySnapshot=getQuerySnapshot(listName);
+   let queryLayout=getQueryLayout(listName);
 	//here I assume that listName was parsed during setDataLayout and setData 
 	//so that rows was set (even if they are empty)
-	print(fName+": Nrows "+fQuery.rows.length);
+	print(fName+": Nrows "+fQuerySnapshot.rows.length);
 	
-	if (fQuery.rows.length>0)
-		entry=fQuery.rows[0];
+	if (fQuerySnapshot.rows.length>0)
+		entry=fQuerySnapshot.rows[0];
 
    
    if ("reviewTable" in setup){
@@ -1161,7 +1178,7 @@ function generateTable(listName,divName,additionalData,setup){
 	config.document.getElementById(divName).appendChild(tb);
 
 	//this are the fields (probably constant)
-	let fields=fQuery.fields;
+	let fields=queryLayout.fields;
 		
 	for (f in fields){
 		let field=fields[f];
@@ -1302,13 +1319,15 @@ function saveReview(queryName,elementId,setup){
 	
 	if (!unique) mode='insert';
 
-	let fQuery=config.formConfig.dataQueries[queryName];
+   //data snapshot
+	let fQuerySnapshot=getQuerySnapshot(queryName);
+	let queryLayout=getQueryLayout(queryName);
 
-	if (fQuery.rows.length==0) useInsert=true;
+	if (fQuerySnapshot.rows.length==0) useInsert=true;
 	let entry=new Object();
 	
 	if (unique){
-		entry=fQuery.rows[0];
+		entry=fQuerySnapshot.rows[0];
  	}
 	entry.crfRef=getCRFrefData();
 
@@ -1319,7 +1338,7 @@ function saveReview(queryName,elementId,setup){
 	//	if (debug) print("Setting queryName: "+entry.queryName);
 	//}
 
-	let fields=fQuery.fields;
+	let fields=queryLayout.fields;
 	for (f in fields){
 
 		let field=fields[f];
@@ -1621,14 +1640,17 @@ function onSubmit(){
 }
 
 function verifyData(){
-   return;
 	let fName='[verifyData]';
-   let queries=config.formConfig.dataQueries;
-	for (q in queries){
-		let qData=queries[q];
+   let qList=getQueryList();
+	for (q in qList){
+		let qData=getQuerySnapshot(q);
 		if (q=="reviewComments") continue;
 		//copy snapshot to history
-		cvInsertRows('lists',q+'History',qData.rows,doNothing,getContainer('data'));
+      if (qData.rows.length==0){
+         print(fName+' no rows for '+q);
+      }
+      else
+		   cvInsertRows('lists',q+'History',qData.rows,doNothing,getContainer('data'));
 		//if it doesn't have additionalData, it is a sub query
 		if (!(q in config.formConfig.additionalData)){
 			continue;
@@ -2190,8 +2212,8 @@ function updateRegistration(){
 	let idFieldName=pM.getCrfEntryFieldName("STUDY");
 	//have to reload query data
 	let regQueryPars=parseVariables(config.formConfig.settings['registrationQuery']);
-    let regQuery=regQueryPars['query'];
-	let fQuery=config.formConfig.dataQueries[regQuery];
+   let regQuery=regQueryPars['query'];
+	let fQuery=getQuerySnapshot(regQuery);
 
 	if (fQuery.rows.length==0) {
 		print(fName+" registration is empty");
@@ -2276,7 +2298,7 @@ function afterData(){
 		print(fName+" ["+queryName+"]: showFlag: "+entry["showFlag"]);
 		print(fName+" ["+queryName+"]: accessMode: "+entry[accessMode]);
 
-		const nData=config.formConfig.dataQueries[queryName].rows.length;
+		const nData=getQuerySnapshot(queryName).rows.length;
 		
 		print(fName+" ["+queryName+"]: nData: "+nData);
 
@@ -2366,7 +2388,7 @@ function populateSection(queryName){
 	divRLi.style.display="block";
 	if (divGBu!=undefined) divGBu.style.display="none";
 
-	let nData=config.formConfig.dataQueries[queryName].rows.length;
+	let nData=getQuerySnapshot(queryName).rows.length;
 	print('['+queryName+']: nrows '+nData);
 	if (nData>0){
 		populateTable(queryName,0);
@@ -2447,7 +2469,7 @@ function checkGenerationFields(queryName){
 		let queryId=row.queryName;
 		let fQueryName=config.formConfig.queryMap[queryId];
 		if (fQueryName==queryName) continue;
-		let fQuery=config.formConfig.dataQueries[fQueryName];
+		let fQuery=getQuerySnapshot(fQueryName);
 		print('Checking '+fQueryName+' nrows: '+fQuery.rows.length);
 		if (fQuery.rows.length==0){ 
 			generateError(queryName,fQueryName);
@@ -2585,7 +2607,7 @@ function generateListEntry(formId,queryName,cb){
 		if (formRows[i].Key!=formId) continue;
 		qForm=formRows[i];
 	}
-	let nData=config.formConfig.dataQueries[queryName].rows.length;
+	let nData=getQuerySnapshot()[queryName].rows.length;
 
    if (nData>0) return;
 
@@ -2628,7 +2650,7 @@ function setFormConfig(){
 	//add object to store form related data
 	config.formConfig=new Object();
 
-	config.formConfig.softwareVersion='T.15.65';
+	config.formConfig.softwareVersion='T.15.68';
 	let debug=true;
 
 	if (debug)
@@ -2993,43 +3015,39 @@ function afterFormDatasets(data){
 //>>>>>>>>>>>>>>>>>new>>>>>>>>>>>>
 
 function setDataLayout(cb){
+   let fName='[setDataLayout]';
+   print(fName);
 	let rowsSetup=config.formConfig.formSetupRows;
-	config.formConfig.dataQueries=new Object();
-	let dS=config.formConfig.dataQueries;//reference only
+   let queryArray=new Array();
+	let dS=getLayoutObject();//reference only
+   let qList=getQueryList();
 	let qMap=config.formConfig.queryMap;
-	config.formConfig.lookup=new Object();
+	//config.formConfig.lookup=new Object();
 	for (let i=0;i<rowsSetup.length;i++){
 		let entry=rowsSetup[i];
 		//skip review rows
 		if (entry['showFlag']=='REVIEW')
 			continue;
 		let queryId=entry['queryName'];
-		let queryName=qMap[entry['queryName']];
-		dS[queryName]=new Object();
-		dS[queryName].title=entry['title'];
+		let q=qMap[queryId];
+		queryArray.push(makeQuery(dS,'data',q,q,[]));
+      qList[q]=0;
+      print(fName+' adding '+q);
 		if (entry['showQuery']!="NONE"){
-			let sqName=entry['showQuery'];
-			dS[sqName]=new Object();
-			dS[sqName].title=findTitle(sqName);
+			let sq=entry['showQuery'];
+		   queryArray.push(makeQuery(dS,'data',sq,sq,[]));
+         qList[sq]=0;
+         print(fName+' adding '+sq);
 			
 		}
 	}
-	//always add reviews
-	//
 
-	config.formConfig.dataQueries['reviewComments']=new Object();
-
-	//perhaps we will need queryId, but this is stuff for later
-	for (q in config.formConfig.dataQueries){
-		//callback will be a watchdog and will complete only 
-		//when all data will be gathered
-		let dq=config.formConfig.dataQueries[q];
-		dq.collectingLayout="INITIALIZED";
-
-		let foo=function(data){afterDatasets(data,cb);}
-      cvSelectRows('lists',q,[],foo,getContainer('data'));
-
-	}
+	//always add reviews
+   let q='reviewComments';
+   queryArray.push(makeQuery(dS,'data',q,q,[]));
+   qList[q]=0;
+   let action=function(){processLayout(cb);};
+   getDataFromQueries(queryArray,action);
 }
 
 function findTitle(queryName){
@@ -3045,135 +3063,104 @@ function findTitle(queryName){
 }
 
 //this happens after the for loop, so all dataQueries objects are set
-function afterDatasets(data,cb){
-   let fName='[afterDatasets]';
-	let qobject=config.formConfig.dataQueries[data.queryName];
-	print(fName+" inspecting layout for "+data.queryName+" "+qobject);
-	qobject.fields=data.metaData.fields;
-	qobject.collectingLayout="STARTED";
-	//qobject.started="TRUE";
-	qobject.lookup=new Object();//keep track of objects
-
-	let i=0;	
-	for (let f in qobject.fields){
-		//anything else is simple but lookup
-		let field=qobject.fields[f];
-		if (!("lookup" in field)) continue;
-		qobject.lookup[f]="PENDING";
-		print(fName+" adding pending lookup for field "+f);
-		if (field.lookup.queryName in config.formConfig.lookup){
-		       qobject.lookup[f]="DONE";
-	       	       continue;
-		}
-		print(fName+" setting up query for field "+f);
-		config.formConfig.lookup[field.lookup.queryName]=new Object();
-		let lObject=config.formConfig.lookup[field.lookup.queryName];
-		lObject.keyColumn=field.lookup.keyColumn;
-		lObject.displayColumn=field.lookup.displayColumn;
-
-
-		let columns=field.lookup.keyColumn+","+field.lookup.displayColumn;
-		let action=function(data){addLookup(data,qobject,f,cb);};
-      let container=field.lookup.containerPath;
-
-      print(fName+" sending query: ["+
-				field.lookup.queryName+"] "+
-				field.lookup.keyColumn+'/'+
-				field.lookup.displayColumn);
-
+function processLayout(cb){
+   let fName='[processLayout]';
+   let qList=getQueryList();
+   //for layouts
+   let queryArray=new Array();
+   let targetObject=getLookupObject();
+   let lookupSet=new Object();
+   for (let q in qList){
+      let qobject=getQueryLayout(q);
+	   print(fName+" inspecting layout for "+q+" "+qobject);
+	   qobject.fields=qobject.metaData.fields;
+	   qobject.title=findTitle(q);
+
+      //check for lookups
+	   for (let f in qobject.fields){
+		   //anything else is simple but lookup
+		   let field=qobject.fields[f];
+		   if (!("lookup" in field)) continue;
+         let lookup=field.lookup;
+         let qObject=getLookup(lookup.queryName);
+         if (qObject) continue;
+         //add to list
+         let qName=lookup.queryName;
+         let qCode=qName+':'+lookup.keyColumn+':'+lookup.displayColumn;
+         let e=makeQuery(targetObject,'data',qName,qCode,[]);
+         //adjust minor settings
+         if (lookup.containerPath) e.containerPath=lookup.containerPath;
+         e.schemaName=lookup.schemaName;
+         e.columns=lookup.keyColumn+','+lookup.displayColumn;
+         lookupSet[qCode]=e;
+         print(fName+' inserting '+qCode);
+      }
+   }
+   for (let x in lookupSet){
+      queryArray.push(lookupSet[x]);
+      print(fName+' adding '+x);
+      for (let v in lookupSet[x]){
+         print(fName+' value ['+v+'] '+lookupSet[x][v]);
+      }
+   }
+   //print(fName+' print '+targetObject.print);
+   let action=function(){processLookup(cb);};
+   print(fName+' getDataFromQueries');
+   getDataFromQueries(queryArray,action);
+   print(fName+' getDataFromQueries done');
+}
+
+function processLookup(cb){
+   let fName="[processLookup]";
+   let obj=getLookupObject();
+   for (let q in obj){
+	   print(fName+" "+q);
+      let a=q.split(':');
+      if (a.length<3) continue;
+      let lookupName=a[0];
+      let key=a[1];
+      let val=a[2];
+      obj[lookupName]=new Object();
+      print(fName+' adding ['+lookupName+'] '+key+'/'+val);
+      let lObject=obj[lookupName];
+
+	   lObject.LUT=new Array();//key to value
+	   lObject.ValToKey=new Array();//value to key
+	   lObject.keyColumn=key
+	   lObject.displayColumn=val;
       
-      cvSelectRows(field.lookup.schemaName,field.lookup.queryName,[],action,container,null,columns);
-		i+=1;
-	}
-	if (i==0){
-		print(fName+" no lookups for "+data.queryName);
-		qobject.collectingLayout="FINISHED";
-	}
-	//check if done (both here and in lookup
-	//this only passes if no lookups are anywhere in the dataset assembly
-	if (!dataLayoutSet()) return;
-	cb();
-}
-
-function addLookup(data,qobject,f,cb){
-   let fName="[addLookup]";
-	print(fName+" "+data.queryName+' '+qobject+' '+f);
-	let lObject=config.formConfig.lookup[data.queryName];
-	lObject.LUT=new Array();//key to value
-	lObject.ValToKey=new Array();//value to key
-	let key=lObject.keyColumn;
-	let val=lObject.displayColumn;
-
-	for (let i=0;i<data.rows.length;i++){
-		lObject.LUT[data.rows[i][key]]=data.rows[i][val];
-		lObject.ValToKey[data.rows[i][val]]=data.rows[i][key];
-	}
-	qobject.lookup[f]="DONE";
-	if (!dataLayoutSet()) return;
+      let qRows=obj[q].rows;
+	   for (let i=0;i<qRows.length;i++){
+         let r=qRows[i];
+         print(fName+' LUT ['+r[key]+'] '+r[val]);
+		   lObject.LUT[r[key]]=r[val];
+		   lObject.ValToKey[r[val]]=r[key];
+	   }
+   }
 	cb();
 }
 
-function dataLayoutSet(){
-   let fName="[dataLayoutSet]";
-	print(fName+" checking layout completeness");
-	let dq=config.formConfig.dataQueries;
-	for (f in dq){
-		print(fName+" ["+f+"]: "+dq[f].collectingLayout);
-		if (dq[f].collectingLayout=="INITIALIZED")
-			return 0;
-		if (dq[f].collectingLayout=="FINISHED")
-			continue; //OK
-		let qobject=dq[f];
-		for (q in qobject.lookup){
-			if (qobject.lookup[q]=="DONE") {
-				print(fName+" ["+f+"/"+q+"]: DONE");
-				continue;//OK
-			}
-			print(fName+" ["+f+"/"+q+"]: "+qobject.lookup[q]);
-			return 0;
-		}
-		dq[f].collectingLayout="FINISHED";
-	}
-	print(fName+" success");
-	return 1;	
-}
-
 function setData(cb){
 	fName='[setData]';
-	//print(fName+': cb '+cb);	
 	let crfMatch=getCRFref();
 	let parentCrf=config.formConfig.crfEntry['parentCrf'];
 	if (parentCrf!=undefined) crfMatch=parentCrf;
 
 	print(fName+' form crf ['+getCRFref()+'] matching for crfRef='+crfMatch);
-	//collect data and execute callback cb for queries in cb.queryList
-	for (q in config.formConfig.dataQueries){
-		let fQuery=config.formConfig.dataQueries[q];
-		fQuery.collectingData="STARTED";
 
+   let queryArray=new Array();
+   let targetObject=getSnapshotObject();
+	//collect data and execute callback cb for queries in cb.queryList
+   let qList=getQueryList();
+	for (q in qList){
 
 		let filters=[LABKEY.Filter.create("crfRef",crfMatch)];
-      let action=function(data){assembleData(data,cb);};
-      cvSelectRows('lists',q,filters,action,getContainer('data'));
-	}
-}
+      queryArray.push(makeQuery(targetObject,'data',q,q,filters));
 
-function assembleData(data,cb){
-	let fName='[assembleData/'+data.queryName+']';
-	let fQuery=config.formConfig.dataQueries[data.queryName];
-	fQuery.rows=data.rows;
-	fQuery.collectingData="FINISHED";
-	print(fName+': adding data');
-	for (q in config.formConfig.dataQueries){
-		let dq=config.formConfig.dataQueries[q];
-		//print("assembleData ["+q+"]: "+dq.collectingData);
-		if (dq.collectingData=="STARTED") return;
 	}
-	print(fName+': completing');
-	cb();
+   getDataFromQueries(queryArray,cb);
 }
 
-
 function uploadFile(inputElement,context){
 	//context should have ID and dirName attributes; 
 	//path will be dirName/ID/fieldName_ID.suf

+ 9 - 1
web/crfTecant/runQuery.js

@@ -48,11 +48,19 @@ function afterQuery(data,id,queryArray,cb){
 		cb();
 		return;
 	}
-
+   
 
 	let e=queryArray[id];
+   for (v in e){
+      e.targetObject.print(fName+' value ['+v+'] '+e[v]);
+   }
 	let qconfig=new Object();
 	qconfig.containerPath=e.targetObject.getContainer(e.containerName);
+   if ("containerPath" in e){
+      e.targetObject.print(fName+' containerPath '+e.containerPath);
+      qconfig.containerPath=e.containerPath;
+   }
+
 	qconfig.schemaName="lists";
 	if ("schemaName" in e){
 		e.targetObject.print(fName+' schemaName='+e.schemaName);