Bläddra i källkod

Making onSave and onDataUpload remove redundant entries from datasets or lists

Andrej 3 dagar sedan
förälder
incheckning
92303e9778
2 ändrade filer med 52 tillägg och 29 borttagningar
  1. 14 3
      web/crf/crfData.js
  2. 38 26
      web/crf/crfVisitNew.js

+ 14 - 3
web/crf/crfData.js

@@ -319,6 +319,7 @@ function(id,crfRef,cb=null){
    let modArray=new Array();
    let schemaName='study';
    let containerName='data';
+   let participantField=crfSetup.getRows('studyData')[0]["SubjectColumnName"];
    for (let qId in qList){
 
       let entry=qList[qId];
@@ -332,22 +333,29 @@ function(id,crfRef,cb=null){
       this.print(fName+' studies '+studyRows.length+' lists '+listRows.length);
       //rows to be UPDATED (already in dataset)
       let modRows=new Array();
+      let delRows=new Array();
       for (let i=0;i<studyRows.length;i++){
          let entry=studyRows[i];
          //
-         if (! (i<listRows.length) ) continue;
+         if (! (i<listRows.length) ) {
+            //delete study rows without list rows
+            delRows.push(entry);
+            continue;
+         }
          let entryList=listRows[i];
 		   //keeps study only variables (ParticipantId, SequenceNum)
 		   for (let f in entryList) {
 			   entry[f]=entryList[f];
 			   this.print(fName+" copying ["+f+"]: "+entry[f]+"/"+entryList[f]);
 		   }
+         //update study fields
+         entry[participantField]=id;
+         entry.SequenceNum=entry.crfRef % 1000000000+i/100;
          modRows.push(entry);
       }
 
-      //rows to be INSERTED 
+      //rows to be INSERTED  (more in list than in study)
       let insRows=new Array();
-      let participantField=crfSetup.getRows('studyData')[0]["SubjectColumnName"];
       for (let i=studyRows.length;i<listRows.length;i++){
          let entry=listRows[i];
          //make sure you have the participantField right
@@ -364,6 +372,9 @@ function(id,crfRef,cb=null){
          insRows.push(entry);
       }
 
+      //determine rows that need to be deleted from querySnapshot
+      if (delRows.length>0)
+         modArray.push(runQuery.makeModification('delete',containerName,schemaName,q,delRows));
       if (modRows.length>0)
          modArray.push(runQuery.makeModification('update',containerName,schemaName,q,modRows));
       //determine rows that need to be inserted from querySnapshot

+ 38 - 26
web/crf/crfVisitNew.js

@@ -496,7 +496,7 @@ function(divName,caption,label,callbackLabel,callback=null){
 
 	let c1=r1.insertCell();
 	c1.setAttribute("colspan","1");
-	//this is only for saveReview?
+	//this is only for onSave?
 	c1.id=divName+'_reportField';
 	//c1.id=config.submitReportId;
 	
@@ -807,24 +807,21 @@ function(entry,field,setup){
 	let vName=field.name;
 	let fName='[populateFieldName/'+vName+']';
 
-   //this.print(fName+" label element: "+labelElement);
-   //this.print(fName+" label element: "+labelElement.childNodes.length);
+   //update field label as well (driven by display of multiple entries
+   let label=field.shortCaption;
    if ("_mergeData" in entry){
       let dataArray=entry["_mergeData"][vName];
-      this.print(fName+" vals "+dataArray);
-      let label=field.shortCaption+" [";
+      label+=" [";
       for (let i=0;i<dataArray.length;i++){
          if (i) label+="/";
          label+=dataArray[i].toString();
       }
       label+="]";
-      crfHTML.setTextNode(crfHTML.getElement(setup.getLabelId(vName)).childNodes[0],label);
    }
-   //this.print(fName+" label "+label);
+   crfHTML.setTextNode(crfHTML.getElement(setup.getLabelId(vName)).childNodes[0],label);
 
 	let varValue="UNDEF";
 
-	//if (vName in setup.filters) varValue=setup.filters[vName];
 	if (vName in entry) varValue=entry[vName];
 	//if part of the filter, set it to value
 	if (vName in setup.filters) varValue=setup.filters[vName];
@@ -1047,9 +1044,9 @@ function(listName,divName,additionalData,setup){
 	cell1.setAttribute("colspan","2");
 	cell1.id=setup.getInputId("rerviewLastSave");
 	cell1.innerHTML="No recent update";
-	//saveReview is a generic name for saving content of the html page to a list entry
+	//onSave is a generic name for saving content of the html page to a list entry
    let that=this;
-	input.onclick=function(){that.saveReview(listName,cell1.id,setup)};
+	input.onclick=function(){that.onSave(listName,cell1.id,setup)};
 }	
 
 crfVisit.setEntryFromElement=
@@ -1144,14 +1141,21 @@ function(fRows,setup,fields){
    //fill the _mergeData field with observations during merge
    //where each field gets a comment that will be displayed
    //with instructions of the field
+   let candidates=this.selectEntryCandidates(fRows,setup,fields);
+   return this.mergeEntries(candidates,setup,fields);
+}
+
+crfVisit.selectEntryCandidates=
+function(fRows,setup,fields){
+
    let fName='[selectEntry]';
 
-   if (!("unique" in setup)) return null;
-   if (fRows.length==0) return null;
+   if (!("unique" in setup)) return fRows;
+   if (fRows.length==0) return fRows;
 
    keys=Object.keys(setup.setVariables);
    if (keys.length==0)
-      return this.mergeEntries(fRows,setup,fields);
+      return fRows;
 
    
    candidates=new Array();
@@ -1170,19 +1174,22 @@ function(fRows,setup,fields){
          continue;
       }
       this.print(fName+' adding '+i);
-      candidates.push(fRows[i])
+      candidates.push(fRows[i]);
    }
-   return this.mergeEntries(candidates,setup,fields);
+   return candidates;
 
 }
 
 crfVisit.mergeEntries=
 function(fRows,setup,fields){
+   if (fRows.length==0)
+      return null;
+
    let fName="[mergeEntries]";
    this.print(fName+" entries["+fRows.length+"]");
    let entry=fRows[0];
    if (fRows.length==1){
-      return entry
+      return entry;
    }
    let mergeData=new Object();
 
@@ -1195,7 +1202,7 @@ function(fRows,setup,fields){
       for (let i=0;i<fRows.length;i++){
          let varValue=fRows[i][field.name];
          if (LUT!=null) varValue=LUT[varValue];
-         v.push(varValue)
+         v.push(varValue);
       }
 
       this.print(fName+" "+field.name+" "+v);
@@ -1208,11 +1215,11 @@ function(fRows,setup,fields){
 }
 
 
-crfVisit.saveReview=
+crfVisit.onSave=
 function(queryName,elementId,setup){
 	//loads any queryName
 
-   let fName='[saveReview/'+queryName+']';
+   let fName='[onSave/'+queryName+']';
 	this.print(fName+" elementId "+elementId);
 
 
@@ -1227,14 +1234,18 @@ function(queryName,elementId,setup){
 	let fields=queryLayout.fields;
 
    //determine mode based on entry uniqueness and presence of data
-   let entry=this.selectEntry(fQuerySnapshot.rows,setup,fields);
+   let entries=this.selectEntryCandidates(fQuerySnapshot.rows,setup,fields);
    let mode='update';
+   let entry=null;
 
-   if (!entry){
+   if (entries.length==0){
       entry=new Object();
       mode='insert';
+   }  else{
+      //shift removes first element and returns it
+      entry=entries.shift();
    }
-   this.print(fName+' unique '+unique+' mode '+mode+' nRows '+nRows);
+   this.print(fName+' unique '+unique+' mode '+mode+' nRows '+nRows+" redundant "+entries.length);
 
 	entry.crfRef=this.getCrfRefData();
 
@@ -1244,7 +1255,7 @@ function(queryName,elementId,setup){
 	for (f in fields){
 
 		let field=fields[f];
-		this.print(fName+" saveReview field: "+field.name);
+		this.print(fName+" onSave field: "+field.name);
 		if (field.hidden) continue;
       if (field.name in setup.setVariables){
          entry[field.name]=setup.setVariables[field.name];
@@ -1278,8 +1289,9 @@ function(queryName,elementId,setup){
          action=function(data){that.updateRegistration(updateFlag,data);}
       } 
    }
-
-   runQuery.modifyRows(mode,'lists',queryName,[entry],action,cPath);
+   let modifyEntries=function(data){runQuery.modifyRows(mode,'lists',queryName,[entry],action,cPath);}
+   runQuery.modifyRows('delete','lists',queryName,entries,modifyEntries,cPath);
+   //modifyEntries([]);
 
 }
 
@@ -1680,7 +1692,7 @@ function(){
 
    let staticData=new Object();
    let titles=new Object();
-   staticData['version']='0.16.3'	
+   staticData['version']='0.16.4'	
    titles['version']='Software version';
    let varRows=crfSetup.getRows('crfStaticVariables');
    for (let i=0;i<varRows.length;i++){