|
@@ -0,0 +1,459 @@
|
|
|
+
|
|
|
+const config=new Object();
|
|
|
+
|
|
|
+function print(msg){
|
|
|
+ config.document.getElementById(config.debugArea).value+="\n"+msg;
|
|
|
+}
|
|
|
+
|
|
|
+function clear(){
|
|
|
+ config.document.getElementById(config.debugArea).value="";
|
|
|
+}
|
|
|
+
|
|
|
+function doNothing(){
|
|
|
+ print('doNothing called');
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function generateDescription(){
|
|
|
+
|
|
|
+
|
|
|
+ print('Generate description');
|
|
|
+ setFormConfig();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function setContainer(label,container){
|
|
|
+ if (!(config.formConfig.hasOwnProperty('container'))){
|
|
|
+ config.formConfig.container=new Array();
|
|
|
+ }
|
|
|
+ config.formConfig.container[label]=container;
|
|
|
+}
|
|
|
+
|
|
|
+function getContainer(label){
|
|
|
+ return config.formConfig.container[label];
|
|
|
+}
|
|
|
+
|
|
|
+function makeQuery(containerName,queryName,fieldName,filterArray){
|
|
|
+ let e=new Object();
|
|
|
+ e.containerName=containerName;
|
|
|
+ e.queryName=queryName;
|
|
|
+ e.fieldName=fieldName;
|
|
|
+ e.filterArray=filterArray;
|
|
|
+ return e;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function getDataFromQueries(queryArray,cb){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ afterQuery(new Object(),-1,queryArray,cb);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function afterQuery(data,id,queryArray,cb){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ print('afterQuery['+id+'/'+queryArray.length+']: ');
|
|
|
+
|
|
|
+ if (id>-1){
|
|
|
+ let fieldName=queryArray[id].fieldName;
|
|
|
+ print('afterQuery['+fieldName+']: '+data.rows.length);
|
|
|
+ config.formConfig[fieldName]=data;
|
|
|
+ }
|
|
|
+ id+=1;
|
|
|
+ if (id==queryArray.length) {
|
|
|
+ cb();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ let e=queryArray[id];
|
|
|
+ let qconfig=new Object();
|
|
|
+ qconfig.containerPath=getContainer(e.containerName);
|
|
|
+ qconfig.schemaName="lists";
|
|
|
+ if ("schemaName" in e){
|
|
|
+ print('afterQuery: schemaName='+e.schemaName);
|
|
|
+ qconfig.schemaName=e.schemaName;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("columns" in e){
|
|
|
+ print('afterQuery: columns='+e.columns);
|
|
|
+ qconfig.columns=e.columns;
|
|
|
+ }
|
|
|
+ qconfig.queryName=e.queryName;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if ("filterArray" in e)
|
|
|
+ qconfig.filterArray=e.filterArray;
|
|
|
+
|
|
|
+
|
|
|
+ qconfig.success=function(data){afterQuery(data,id,queryArray,cb);};
|
|
|
+ qconfig.failure=doNothing;
|
|
|
+ LABKEY.Query.selectRows(qconfig);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function setFormConfig(){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ config.formConfig=new Object();
|
|
|
+
|
|
|
+ config.formConfig.softwareVersion='0.0.1';
|
|
|
+ let debug=true;
|
|
|
+
|
|
|
+ if (debug)
|
|
|
+ print("setFormConfig");
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ setContainer('data',LABKEY.ActionURL.getContainer());
|
|
|
+ setContainer('config',LABKEY.ActionURL.getContainer());
|
|
|
+ setContainer('CRF',LABKEY.ActionURL.getContainer());
|
|
|
+
|
|
|
+ let selectRows=new Object();
|
|
|
+
|
|
|
+ selectRows.containerPath=getContainer('CRF');
|
|
|
+ selectRows.schemaName='lists';
|
|
|
+ selectRows.queryName='crfSettings';
|
|
|
+
|
|
|
+ selectRows.success=afterSettings;
|
|
|
+ LABKEY.Query.selectRows(selectRows);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function afterSettings(data){
|
|
|
+
|
|
|
+ config.formConfig.settings=new Array();
|
|
|
+ for (let i=0;i<data.rows.length;i++){
|
|
|
+ let n=data.rows[i]['name'];
|
|
|
+ let v=data.rows[i]['value'];
|
|
|
+ config.formConfig.settings[n]=v;
|
|
|
+ }
|
|
|
+
|
|
|
+ let st=config.formConfig.settings;
|
|
|
+ print('afterSettings');
|
|
|
+ for (let k in st){
|
|
|
+ print('\t'+k+'='+st[k]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let vname='configContainer';
|
|
|
+ if (vname in st){
|
|
|
+ setContainer('config',st[vname]);
|
|
|
+ }
|
|
|
+ print('Config: '+getContainer('config'));
|
|
|
+ print('Data: '+getContainer('data'));
|
|
|
+ collectData();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function collectData(){
|
|
|
+
|
|
|
+ let queryArray=new Array();
|
|
|
+
|
|
|
+ queryArray.push(makeQuery('CRF','users','userData',[]));
|
|
|
+ queryArray[queryArray.length-1].schemaName='core';
|
|
|
+
|
|
|
+
|
|
|
+ queryArray.push(makeQuery('config','Forms','formData',[]));
|
|
|
+
|
|
|
+ queryArray.push(makeQuery('config','FormSetup','formSetup',[]));
|
|
|
+
|
|
|
+ queryArray.push(makeQuery('config','inputLists','inputLists',[]));
|
|
|
+
|
|
|
+
|
|
|
+ print('running getDataFromQueries');
|
|
|
+ getDataFromQueries(queryArray,fcontinue);
|
|
|
+}
|
|
|
+
|
|
|
+function findName(listId){
|
|
|
+ let frows=config.formConfig.inputLists.rows;
|
|
|
+ for (let i=0;i<frows.length;i++){
|
|
|
+ if (frows[i]['Key']!=listId) continue;
|
|
|
+ return frows[i]['queryName'];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function fcontinue(){
|
|
|
+ print('loadedData');
|
|
|
+ let queryArray=new Array();
|
|
|
+
|
|
|
+ let frows=config.formConfig.formSetup.rows;
|
|
|
+ for (let i=0;i<frows.length;i++){
|
|
|
+ let listId=frows[i]['queryName'];
|
|
|
+
|
|
|
+ let showFlag=frows[i]['showFlag'];
|
|
|
+ let showQuery=frows[i]['showQuery'];
|
|
|
+ if (showFlag=='REVIEW') continue;
|
|
|
+ let listName=findName(listId);
|
|
|
+ print(listName);
|
|
|
+ queryArray.push(makeQuery('data',listName,listName,[]));
|
|
|
+ if (showFlag=='NONE') continue;
|
|
|
+ queryArray.push(makeQuery('data',showQuery,showQuery,[]));
|
|
|
+ }
|
|
|
+
|
|
|
+ getDataFromQueries(queryArray,fcontinue1);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function getList(formId){
|
|
|
+ let fList=new Array();
|
|
|
+ let frows=config.formConfig.formSetup.rows;
|
|
|
+ for (let i=0;i<frows.length;i++){
|
|
|
+ if (frows[i]['formName']!=formId) continue;
|
|
|
+ let listId=frows[i]['queryName'];
|
|
|
+ let showFlag=frows[i]['showFlag'];
|
|
|
+ let showQuery=frows[i]['showQuery'];
|
|
|
+ if (showFlag=='REVIEW') continue;
|
|
|
+ let fObj=new Object();
|
|
|
+ fObj['queryName']=findName(listId);
|
|
|
+ fObj['title']=frows[i]['title'];
|
|
|
+ fList.push(fObj);
|
|
|
+ if (showFlag=='NONE') continue;
|
|
|
+ let fObj1=new Object();
|
|
|
+ fObj1['queryName']=showQuery;
|
|
|
+ fObj1['title']=frows[i]['title']+' (details)';
|
|
|
+ fList.push(fObj1);
|
|
|
+ }
|
|
|
+ return fList;
|
|
|
+}
|
|
|
+
|
|
|
+function printField(field){
|
|
|
+ let name=field['name'];
|
|
|
+ if (name=='Key') return;
|
|
|
+ if (name=='crfRef') return;
|
|
|
+ let type=field['type'];
|
|
|
+ let qName='';
|
|
|
+ if ('lookup' in field){
|
|
|
+ qName=field.lookup.queryName;
|
|
|
+ }
|
|
|
+ print(name+' '+type+'/'+qName);
|
|
|
+ printPDF(field);
|
|
|
+}
|
|
|
+
|
|
|
+function addLookup(lookupList,field){
|
|
|
+ if ('lookup' in field){
|
|
|
+ lookupList.add(field.lookup.queryName);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function printFields(lookupList,listName){
|
|
|
+ let fields=config.formConfig[listName].metaData.fields;
|
|
|
+
|
|
|
+ for (f in fields){
|
|
|
+ printField(fields[f]);
|
|
|
+ addLookup(lookupList,fields[f]);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function fcontinue1(){
|
|
|
+ printLayout();
|
|
|
+}
|
|
|
+
|
|
|
+function printData(){
|
|
|
+ let frows=config.formConfig.formData.rows;
|
|
|
+ let lookupList=new Set();
|
|
|
+ for (let i=0;i<frows.length;i++){
|
|
|
+ let formId=frows[i]['Key'];
|
|
|
+ print(frows[i]['formName']);
|
|
|
+ printTitlePDF(20,frows[i]['formName']);
|
|
|
+ let fList=getList(formId);
|
|
|
+ for (let j=0;j<fList.length;j++){
|
|
|
+ print(fList[j]['queryName']);
|
|
|
+ printTitlePDF(16,fList[j]['title']);
|
|
|
+ printFields(lookupList,fList[j]['queryName']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ print('all done');
|
|
|
+ let queryArray=new Array();
|
|
|
+ for (let item of lookupList){
|
|
|
+ queryArray.push(makeQuery('data',item,item,[]));
|
|
|
+ }
|
|
|
+ let cb=function(){printLookup(lookupList);};
|
|
|
+ getDataFromQueries(queryArray,cb);
|
|
|
+}
|
|
|
+
|
|
|
+function printQuery(queryName){
|
|
|
+ printTitlePDF(16,queryName);
|
|
|
+ print(queryName);
|
|
|
+ let frows=config.formConfig[queryName].rows;
|
|
|
+ print('rows: '+frows);
|
|
|
+ let fields=config.formConfig[queryName].metaData.fields;
|
|
|
+ let field=undefined;
|
|
|
+ for (f in fields){
|
|
|
+ if (fields[f].name=='Key') continue;
|
|
|
+ field=fields[f];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (let i=0;i<frows.length;i++){
|
|
|
+ printPDFEntry(field,frows[i]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function printLookup(lookupList){
|
|
|
+
|
|
|
+ printTitlePDF(20,'Enumerators');
|
|
|
+ for (let item of lookupList){
|
|
|
+ printQuery(item);
|
|
|
+ }
|
|
|
+ config.doc.end();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function checkBlob(){
|
|
|
+ print("checkBlob: "+config.blob);
|
|
|
+ if (config.blob) {
|
|
|
+ clearInterval(config.blobInterval);
|
|
|
+ config.a.href = config.window.URL.createObjectURL(config.blob);
|
|
|
+ print("HREF: "+config.a.href);
|
|
|
+ config.a.download = 'test.pdf';
|
|
|
+ config.a.click();
|
|
|
+ config.window.URL.revokeObjectURL(config.a.href);
|
|
|
+ }
|
|
|
+ config.count=config.count+1;
|
|
|
+ print("Eval: "+config.count);
|
|
|
+ if (config.count>100){
|
|
|
+ clearInterval(config.blobInterval);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function printLayout(){
|
|
|
+
|
|
|
+ config.doc=new PDFDocument();
|
|
|
+
|
|
|
+ let stream = config.doc.pipe(blobStream()).on("finish",function(){
|
|
|
+ config.blob=stream.toBlob("application/pdf");});
|
|
|
+
|
|
|
+ print("BLob: "+config.blob);
|
|
|
+ config.a = config.document.createElement("a");
|
|
|
+ config.document.body.appendChild(config.a);
|
|
|
+ config.a.innerHTML="Download PDF";
|
|
|
+ config.a.style = "display: none";
|
|
|
+ config.count=0;
|
|
|
+
|
|
|
+ config.blobInterval=setInterval(checkBlob,1000);
|
|
|
+
|
|
|
+
|
|
|
+ print("Printing form");
|
|
|
+ printData();
|
|
|
+}
|
|
|
+
|
|
|
+function printTitlePDF(fontSize,title){
|
|
|
+ config.doc.y+=10;
|
|
|
+ config.doc.font('Courier-Bold').fontSize(fontSize).text(title);
|
|
|
+}
|
|
|
+
|
|
|
+function printPDF(field){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let w=476;
|
|
|
+ let spacing=25;
|
|
|
+ let w1=(w-spacing)*0.5;
|
|
|
+ let fontSize=14;
|
|
|
+
|
|
|
+ print('printPDF: entry['+field.name);
|
|
|
+ print('printPDF: field type:'+field.type);
|
|
|
+
|
|
|
+
|
|
|
+ let label=field.caption;
|
|
|
+ let opt={width:w1};
|
|
|
+ config.doc.fontSize(fontSize);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ config.doc.font('Courier').text(label,opt);
|
|
|
+
|
|
|
+
|
|
|
+ config.doc.moveUp();
|
|
|
+
|
|
|
+
|
|
|
+ let tx=config.doc.x;
|
|
|
+ let ty=config.doc.y;
|
|
|
+
|
|
|
+
|
|
|
+ config.doc.x+=w1+spacing;
|
|
|
+ let v=field.type;
|
|
|
+ if ('lookup' in field){
|
|
|
+ v+='/'+field.lookup.queryName;
|
|
|
+ }
|
|
|
+ print('v: '+v);
|
|
|
+ config.doc.font('Courier-Bold').text(v,opt);
|
|
|
+
|
|
|
+
|
|
|
+ config.doc.x=tx;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function printPDFEntry(field,entry){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let w=476;
|
|
|
+ let spacing=25;
|
|
|
+ let w1=(w-spacing)*0.5;
|
|
|
+ let fontSize=14;
|
|
|
+
|
|
|
+ print('printPDF: entry['+field.name);
|
|
|
+ print('printPDF: field type:'+field.type);
|
|
|
+
|
|
|
+
|
|
|
+ let label=entry[field.name];
|
|
|
+ let opt={width:w1};
|
|
|
+ config.doc.fontSize(fontSize);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ config.doc.font('Courier').text(label,opt);
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|