Skip to content

Commit

Permalink
added sex & code update on update subject
Browse files Browse the repository at this point in the history
  • Loading branch information
Zigur committed Jan 17, 2016
1 parent b020f86 commit 6a08232
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions lib/crud/PgCrudStrategies.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @module
* @module
* @name PgCrudStrategies
* @author Massimiliano Izzo
* @description this handler works as a context for the transaction strategy
*
*
*/
/*jshint node: true */
/*jshint esnext: true */
Expand Down Expand Up @@ -36,7 +36,7 @@ pgData2DataFileAssociationMap.set('data', {

/**
* @private
* @description evaluate whether the metadata field has a measure unit. Only numeric values are allowed a unit.
* @description evaluate whether the metadata field has a measure unit. Only numeric values are allowed a unit.
* @return {boolean} - true if the metadata field has unit
*/
function isUnitAllowed(field, fieldInstance) {
Expand Down Expand Up @@ -270,17 +270,17 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
}

/**
* @method
* @method
* @name getDataTypesByRolePrivileges
* @param{Object} criteria, may contain the following parameters:
* - idOperator [integer]: the ID of the operator doing the current request
* - model [string]: the MODEL of the dataTypes
* - model [string]: the MODEL of the dataTypes
* - parentDataType [integer]: the ID of the parent dataType
* - idDataTypes [Array<integer>/String]: an array of the IDs of the allowed dataTypes
* - privilegeLevel [enum]: can be one of {view_overview, view_details, download, edit} (ordered list)
*/
getDataTypesByRolePrivileges(criteria) {

let knex = this.knex;
let query = knex.select('data_type.id', 'data_type.name', 'data_type.schema', 'datatype_privileges.privilege_level').from('data_type');
query.innerJoin('datatype_privileges', 'data_type.id', 'datatype_privileges.data_type');
Expand All @@ -289,7 +289,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
query.innerJoin('operator', 'operator.id', 'group_members__operator_groups.operator_groups');

query.where('operator.id', criteria.idOperator).andWhere('data_type.model', criteria.model);

if (criteria.privilegeLevel) {
query.where('datatype_privileges.privilege_level', '>=', criteria.privilegeLevel);
}
Expand All @@ -303,10 +303,10 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
let idDataTypes = _.isArray(criteria.idDataTypes) ? criteria.idDataTypes : criteria.idDataTypes.split(',').map(val => _.parseInt(val));
query.whereIn('data_type.id', idDataTypes);
}

// added for debugging
console.log(query.toString());

return query.then(function(results) {
return results;
});
Expand Down Expand Up @@ -355,7 +355,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
console.log(idFiles);
console.log("PgKnexCrudStrategy.createData - creating associations...");
return BluebirdPromise.map(idFiles, function(idFile) {

let associationTable = pgData2DataFileAssociationMap.get(tableName);
let association = {};
association[associationTable.data] = idData;
Expand All @@ -376,7 +376,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
* @return idData the ID of the newly created Data
*/
createData(data, dataTypeName) {
let knex = this.knex, that = this;
let knex = this.knex, that = this;
let files = data.files ? _.cloneDeep(data.files) : [];
delete data.files;
let createdData = null;
Expand Down Expand Up @@ -406,7 +406,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
console.log("KnexStrategy.createData - data instance created with ID: " + createdData.id);
return that.handleFiles(files, createdData.id, dataTypeName, trx);
})

.then(function() {
createdData.files = _.map(files, file => {
return _.mapKeys(file, (value, key) => { return _.camelCase(key); });
Expand Down Expand Up @@ -469,7 +469,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
console.log("KnexStrategy.updateData - data instance updated for ID: " + updatedData.id);
return that.handleFiles(notExistingFiles, updatedData.id, dataTypeName, trx);
})

.then(function() {
notExistingFiles = _.map(notExistingFiles, file => {
return _.mapKeys(file, (value, key) => { return _.camelCase(key); });
Expand Down Expand Up @@ -506,7 +506,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
}

/**
* @method
* @method
* @name createSample
* @description transactional Sample creation with File upload to the File System (e.g. iRODS)
* @return{integer} idSample - created sample ID
Expand All @@ -523,7 +523,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
console.log ("KnexStrategy.createSample - creating new sample instance...");

// fing the greatest (i.e. the last) inserted biobank_code for that biobank
// TODO test this thing
// TODO test this thing
let query = knex.first('id', 'biobank_code').from('sample')
.whereNull('parent_sample').andWhere('biobank', '=', sample.biobank)
.orderBy('id', 'desc').transacting(trx);
Expand All @@ -538,7 +538,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
let lastBiobankCode = lastSample ? _.parseInt(lastSample.biobank_code) : START_SAMPLE_CODE;
let nextCode = _.isNaN(lastBiobankCode) ? START_SAMPLE_CODE : '0' + (lastBiobankCode + 1);
console.log('PgKnexCrudStrategy.createSample - nextCode: ' + nextCode);
let sampleCode = sample.biobankCode || nextCode || START_SAMPLE_CODE;
let sampleCode = sample.biobankCode || nextCode || START_SAMPLE_CODE;
return knex.returning('*').insert({
'biobank_code': sampleCode,
'type': sample.type,
Expand All @@ -550,7 +550,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
'updated_at': new Date()
}).into('sample').transacting(trx);
})

// store files on the FileSystem of choice (e.g. iRODS) in their final collection
.then(function(rows) {
createdSample = _.mapKeys(rows[0], (value, key) => { return _.camelCase(key); });
Expand All @@ -561,8 +561,8 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
// add stored files to the created sample entity
.then(function() {
createdSample.files = _.map(files, file => {
return _.mapKeys(file, (value, key) => {
return key === 'parent_subject' ? 'donor' : _.camelCase(key);
return _.mapKeys(file, (value, key) => {
return key === 'parent_subject' ? 'donor' : _.camelCase(key);
});
});
});
Expand All @@ -576,7 +576,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
console.log("KnexStrategy.createSample - error caught");
console.log(error);
throw new TransactionError(error.message);
});
});

}

Expand Down Expand Up @@ -622,8 +622,8 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
// add stored files to the created sample entity
.then(function() {
notExistingFiles = _.map(notExistingFiles, file => {
return _.mapKeys(file, (value, key) => {
return key === 'parent_subject' ? 'donor' : _.camelCase(key);
return _.mapKeys(file, (value, key) => {
return key === 'parent_subject' ? 'donor' : _.camelCase(key);
});
});
updatedSample.files = existingFiles.concat(notExistingFiles);
Expand Down Expand Up @@ -700,7 +700,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
.then(function(result) {
let lastId = result.rows && result.rows[0] && _.parseInt(result.rows[0].last_value);
let subjCode = SUBJ_CODE_PREFIX + (lastId+1);
console.log("KnexStrategy.createSubject - subject code: " + subjCode);
console.log("KnexStrategy.createSubject - subject code: " + subjCode);
return knex.returning('*').insert({
'code': subject.code || subjCode, // if a code is provided by the user use that
'sex': subject.sex || 'N.D.',
Expand Down Expand Up @@ -801,6 +801,8 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
'id': subject.id,
'type': subject.type
}).update({
'code': subject.code || subjCode, // if a code is provided by the user use that
'sex': subject.sex || 'N.D.',
'personal_info': subject.personalInfo,
'tags': JSON.stringify(subject.tags),
'notes': subject.notes,
Expand All @@ -810,7 +812,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {

})

// update Projects if present
// update Projects if present
// first delete all existing Projects
.then(function(rows) {
updatedSubject = _.mapKeys(rows[0], (value, key) => { return _.camelCase(key); });
Expand Down Expand Up @@ -860,9 +862,9 @@ class PgKnexCrudStrategy extends PgCrudStrategy {


/**
* @method
* @method
* @name putMetadataFieldsIntoEAV
* @description extract the Metadata Fields from the JSON schema and stores each one in a dedicated
* @description extract the Metadata Fields from the JSON schema and stores each one in a dedicated
* ATTRIBUTE table, for use in an EAV catalogue
* @param {integer} idDataType - the identifier of the DataType (i.e. ENTITY)
* @param {Array} fields - the array containing all the MetadataFields to be inserted (or updated?)
Expand Down Expand Up @@ -920,9 +922,9 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
/**
* @method
* @name putMetadataValuesIntoEAV
* @description extract the metadata values from the "metadata" column of the "data" ("subject" or "sample") entity,
* @description extract the metadata values from the "metadata" column of the "data" ("subject" or "sample") entity,
* and store it in the appropriate EAV Value(s) table. Five value tables are provided, one for each fundamental data type (text, integer, float,
* date and boolean)
* date and boolean)
* @param {Object} data - the Data (Subject, Sample or Generic) that must extracted and loadad in the EAV catalogue
*
*/
Expand All @@ -937,20 +939,20 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
.then(function(row) {

// identify the table (e.g. data, subject, sample...)
let entityTable = row.model.toLowerCase().toUnderscore();
let entityTable = row.model.toLowerCase().toUnderscore();
console.log("KnexStrategy.putMetadataValuesIntoEAV - entity table is: " + entityTable);

// store each metadata value in the appropriate EAV catalogue
return BluebirdPromise.map(Object.keys(data.metadata), function(metadataField) {

console.log("KnexStrategy.putMetadataValuesIntoEAV - trying to retrieve the metadataField: " + metadataField);
// find the attribute
// find the attribute
return knex('eav_attribute').where({
'data_type': data.type,
'name': metadataField
}).transacting(trx)

//
//
.then(function(eavAttribute) {
if (eavAttribute.length !== 1) {
throw new Error("none or more than one attribute was restrieved!!");
Expand Down Expand Up @@ -1004,9 +1006,9 @@ class PgKnexCrudStrategy extends PgCrudStrategy {
return knex.returning('id').insert(eavValues).into(eavValueTable).transacting(trx);
}

// something is wrong, throw new error
// something is wrong, throw new error
else {
console.log("KnexStrategy.putMetadataValuesIntoEAV - metadata field" + metadataField +
console.log("KnexStrategy.putMetadataValuesIntoEAV - metadata field" + metadataField +
"missing or it does not possess a valid value");
}
});
Expand All @@ -1017,7 +1019,7 @@ class PgKnexCrudStrategy extends PgCrudStrategy {

console.log("KnexStrategy.putMetadataValuesIntoEAV - inserted successfully new metadata value: IDS " + ids);
return ids;
});
});
})

.then(function(insertedIds) {
Expand Down

0 comments on commit 6a08232

Please sign in to comment.