Skip to content

Commit

Permalink
Merge pull request #214 from ReproNim/feauture-csv-export-data-acquis…
Browse files Browse the repository at this point in the history
…ition

fix: rendering all subjects using a particular instrument in csv export
  • Loading branch information
sanuann authored Jun 19, 2018
2 parents a471fdb + 32ae03a commit 3cf07aa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 79 deletions.
36 changes: 17 additions & 19 deletions eapp/modules/data-collection/js/dc-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ $('#btn-export-csv').click(function() {
success: function (data) {
if (data) {
console.log('Going to Export to CSV ', data);
downloadCSV({ filename: "data-acquisition.csv", data: data });
downloadCSV({ filename: "data-acquisition.csv", data: data.field_subjects });

} // end if

Expand All @@ -107,8 +107,6 @@ $('#btn-export-csv').click(function() {
});

$('#projectId').append('<h5> Project Name: '+ collectionObj['Name'] +'</h5>');
//$('#planId').append('<h5> Plan: '+ cObj['Project Name'] +'</h5>')


$('#btn-back-dc').click(function(){
window.location.href = serverURL+"/data-collection/html/dc-list.html"
Expand All @@ -118,38 +116,38 @@ $('#btn-back-mn').click(function(){
});

function convertObjectsToCSV(data) {
console.log("inside convert array", data)
//console.log("inside convert array", data)
var result, headerStr='', valueStr='', columnDelimiter, lineDelimiter, inputData;
inputData = data.field_subjects || null;
inputData = data || null;
if (inputData == null || !Object.keys(inputData).length) {
console.log("data null")
return null;
}
columnDelimiter = data.columnDelimiter || ',';
lineDelimiter = data.lineDelimiter || '\n';
let totalSubjects = Object.keys(inputData)
if(totalSubjects.length > 0) {
headerStr = 'Subject'
for (let i = 0; i < totalSubjects.length; i++) {
valueStr += totalSubjects[i]
let fieldsForSubject = inputData[totalSubjects[i]]
for (let j = 0; j < fieldsForSubject.length; j++) {
let fieldKey = (Object.keys(fieldsForSubject[j]))[0]
let totalEntities = Object.keys(inputData);
if(totalEntities.length>0) {
headerStr = ''
for (let i = 0; i < totalEntities.length; i++) {
let fieldsAndSubject = inputData[totalEntities[i]];
for (let j = 0; j < fieldsAndSubject.length; j++) {
let fieldObj = fieldsAndSubject[j]
let fieldValue = Object.values(fieldObj)[0]
let fieldKey = Object.keys(fieldObj)[0]
if(i==0) {//Do only for the first subject since header is same for all subjects
headerStr += columnDelimiter + fieldKey
headerStr += fieldKey + columnDelimiter
}
let fieldValue = fieldsForSubject[j][fieldKey]
valueStr += columnDelimiter + fieldValue
valueStr += fieldValue + columnDelimiter
}
valueStr +=lineDelimiter
}
}
} //end for
} //end if
result = headerStr + lineDelimiter + valueStr
return result;
}

function downloadCSV(args) {
console.log("inside download csv method", args.data)
//console.log("inside download csv method", args.data)
var result, filename, link;
var csv = convertObjectsToCSV(args.data);
if (csv == null) return;
Expand Down
93 changes: 33 additions & 60 deletions eapp/routes/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = () => {
})

app.get('/query/graphs/projects/:projectId/instruments',ensureAuthenticated, function(req, res){
console.log("projectId: ", req.params.projectId, " projectId: ", req.params.projectId)
console.log("projectId: ", req.params.projectId)
var listOfGraphs = new Promise(function(resolve){
store.registeredGraphs(function(results, graphs) {
var values = []
Expand All @@ -176,7 +176,7 @@ app.get('/query/graphs/projects/:projectId/instruments',ensureAuthenticated, fun
var graphOfPromises = regGraphs.map(function(graph){
return new Promise(function(resolve){
store.execute(queryInstruments(req.params.projectId,"<"+graph+">"), function(err,results){
console.log('In list instruments api::: [execute] for graph: ', graph)
//console.log('In list instruments api::: [execute] for graph: ', graph)
let instrumentArray = []
if(err){
console.log("err: ", err)
Expand All @@ -186,15 +186,12 @@ app.get('/query/graphs/projects/:projectId/instruments',ensureAuthenticated, fun
//console.log("results: ", results)
if(typeof results !== 'undefined' && results !== []){
//console.log("results value undefined? ", typeof(results) === 'undefined')
console.log("results length", results.length)
console.log("[if] results is defined: [0]", results[0])

console.log("[if] results is defined: [1]", results[1])
//console.log("results length", results.length)
//console.log("[if] results is defined: [0]", results[0])

if(typeof results[0]!=='undefined' && results[0].hasOwnProperty("projectId")){

for(let i = 0; i< results.length; i++){

//console.log(results[i].instrument.value)
instrumentArray.push(results[i].instrument.value)
}
Expand All @@ -218,7 +215,7 @@ app.get('/query/graphs/projects/:projectId/instruments',ensureAuthenticated, fun
let attrVal = []
for(let i=0; i<objs.length; i++){
if(objs[i] !== []){
console.log("objs[i].projectId: ", objs[i].projectId," objs[i].instrument: ", objs[i].instrument)
//console.log("objs[i].projectId: ", objs[i].projectId," objs[i].instrument: ", objs[i].instrument)
}
if(objs[i].hasOwnProperty('instrument')){
for(let j=0; j<objs[i].instrument.length; j++){
Expand Down Expand Up @@ -270,19 +267,12 @@ app.get('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthenticat
}
//console.log("--------results:----------", results)
if(typeof results !== 'undefined' && results !== []){
//console.log("results value undefined? ", typeof(results) === 'undefined')
//console.log("[if] results is defined: [0]", results[0])
let entity = {}
if(typeof results[0]!=='undefined' && results[0].hasOwnProperty("entity")){
//console.log("looking for own property")
entity[results[0].entity.value] = []

//let prev_entity = results[0].entity.value
entity[results[0].entity.value] = []
let earr =[]
let subjectValue = ''
for(let i = 0; i< results.length; i++){
//console.log("---results[i].entity.value---", results[i].entity.value)
//console.log("--^^-results[i].entity.value type-^^--", typeof(results[i].entity.value))
if(entity.hasOwnProperty(results[i].entity.value)){ //just picking fields of first entity
if(results[i].v.token === 'literal'){
let fieldName = results[i].p.value
Expand All @@ -300,31 +290,21 @@ app.get('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthenticat
if(results[i].p.token==='uri' && results[i].p.value === 'http://www.w3.org/ns/prov#wasAttributedTo'){
let fieldName = "http://purl.org/nidash/nidm#subject"
subjectValue = results[i].v.value
if(! subjectArray.includes(results[i].p.value)){
subjectArray.push(results[i].v.value)
}
}
//******
}
/*else{
else{
entity[results[i].entity.value] = []
earr = []

}*/
}


}//End For loop

console.log("projectId", req.params.projectId)
console.log("instrumentName", req.params.instrument_name,)
console.log("instrument_fields", fieldsArray,)
console.log("subjects", subjectArray,)

resolve({
"projectId": req.params.projectId,
"instrumentName": req.params.instrument_name,
"instrument_fields": fieldsArray,
"subjects": subjectArray,
})
}else{
resolve({})
Expand All @@ -340,26 +320,22 @@ app.get('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthenticat
return Promise.all(graphOfPromises)
}).then(function(objs){
let attrVal = ''
let attrVal2 = ''
for(let i=0; i<objs.length; i++){
if(objs[i].hasOwnProperty('instrument_fields')){
attrVal = objs[i].instrument_fields
//break
}
if(objs[i].hasOwnProperty('subjects')){
attrVal2 = objs[i].subjects
}
}
res.json({'instrument fields':attrVal, 'subjects':attrVal2})
res.json({'instrument fields':attrVal})
}).catch(function(error){
console.log("error:", error)
})
})

app.post('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthenticated, function(req, res){
console.log("projectId: ", req.params.projectId, " instrumentName: ", req.params.instrument_name)
console.log(req.body)
console.log(req.body.selectedFields)
//console.log(req.body)
//console.log(req.body.selectedFields)

//fetch the list of fields user selected and form a Map
let selectedFields = req.body.selectedFields
Expand All @@ -384,7 +360,7 @@ app.post('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthentica
regGraphs.push(values[i])
}
}
console.log("Filtered graphs:", regGraphs)
//console.log("Filtered graphs:", regGraphs)
var graphOfPromises = regGraphs.map(function(graph){
return new Promise(function(resolve){
store.execute(getInstrumentFields(req.params.projectId,req.params.instrument_name,"<"+graph+">"), function(err,results){
Expand All @@ -399,22 +375,19 @@ app.post('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthentica
//console.log("--------results:----------", results)
if(typeof results !== 'undefined' && results !== []){
let entity = {}
let exportdata = {}
if(typeof results[0]!=='undefined' && results[0].hasOwnProperty("entity")){
//console.log("looking for own property")
console.log("looking for own property")
entity[results[0].entity.value] = []
let earr =[]
let subjectValue = ''
for(let i = 0; i< results.length; i++){

let earr =[]
if(entity.hasOwnProperty(results[i].entity.value)){ //just picking fields of first entity
if(results[i].v.token === 'literal'){
let fieldNameStr = results[i].p.value
let fieldValue = results[i].v.value
let fieldNameArray = fieldNameStr.split('#')
let fieldName = fieldNameArray[1]
earr = entity[results[i].entity.value]
if(selectedFieldNameArray.includes(fieldName)) {
// earr = entity[results[i].entity.value]
let vObj = {}
vObj[fieldName] = fieldValue
earr.push(vObj)
Expand All @@ -426,28 +399,24 @@ app.post('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthentica
}
//*** deal with participants
if(results[i].p.token==='uri' && results[i].p.value === 'http://www.w3.org/ns/prov#wasAttributedTo'){
let fieldName = "http://purl.org/nidash/nidm#subject"
let fieldName = "Subject"
let subjectValueStr = results[i].v.value
let subjectValueArr = subjectValueStr.split('#')
subjectValue = subjectValueArr[1]
if(! subjectArray.includes(results[i].p.value)){
subjectArray.push(results[i].v.value)
}
let subjectValue = subjectValueArr[1]
earr = entity[results[i].entity.value]
let vObj = {}
vObj[fieldName] = subjectValue
earr.splice(0,0, vObj) // insert subject at first position of array
entity[results[i].entity.value] = earr
}

}


}//End For loop

if(subjectValue !=''){
exportdata[subjectValue] = []
exportdata[subjectValue] = earr
else{
entity[results[i].entity.value] = []
earr = []
}
}
console.log("field_subjects", exportdata)

resolve({
"field_subjects": exportdata
"field_subjects": entity
})
}else{
resolve({})
Expand All @@ -458,19 +427,23 @@ app.post('/query/graphs/instrument/:projectId/:instrument_name',ensureAuthentica
}
// }
})//execute

})//promise
})//graph of promises
return Promise.all(graphOfPromises)
}).then(function(objs){
let attrVal = ''
//let attrVal2 = ''
let attrVal2 = ''
//let attrVal3 = ''
for(let i=0; i<objs.length; i++){
if(objs[i].hasOwnProperty('field_subjects')){
attrVal = objs[i].field_subjects
}
if(objs[i].hasOwnProperty('subjectArray')){
attrVal2 = objs[i].subjectArray
}
}
res.json({ 'field_subjects':attrVal})
res.json({ 'field_subjects':attrVal, 'subjectArray': attrVal2 })
}).catch(function(error){
console.log("error:", error)
})
Expand Down

0 comments on commit 3cf07aa

Please sign in to comment.