Skip to content

Commit

Permalink
iRODS Rest strategy for data management implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
massi committed Dec 17, 2014
1 parent 582e9cf commit 635380c
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @author Massimiliano Izzo
*/
var IrodsRestStrategy = require('./lib/IrodsRestStrategy.js');
var FileSystemManager = require('./lib/FileSystemManager.js');
module.exports.IrodsRestStrategy = IrodsRestStrategy;
module.exports.FileSystemManager = FileSystemManager;
26 changes: 26 additions & 0 deletions lib/FileSystemManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @author Massimiliano Izzo
* @description this builder works as a context for the query/search strategy
*/
var IrodsRestStrategy = require('./IrodsRestStrategy.js');

function FileSystemManager(strategy, options) {
if (!fileSystem) {
strategy = new IrodsRestStrategy(options);
}
this.setStrategy(strategy);
}

FileSystemManager.prototype = {

setStrategy: function(strategy) {
this.strategy = strategy;
},

storeFile: function() {
this.strategy.storeFile.apply(this.fileSystem, arguments);
}

};

module.exports = FileSystemManager;
106 changes: 106 additions & 0 deletions lib/IrodsRestStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @author Massimiliano Izzo
*/
var storeFileRule = [
'xtensFileMove {',
'*destColl = str(*irodsHome)++"/"++str(*repoColl)++"/"++str(*dataTypeName);',
'writeLine("serverLog", "idData is *idData");',
'if (int(*idData) > 0) then {',
'*destColl = *destColl ++ "/" ++ str(*idData);',
'}',
'writeLine("serverLog", "destColl is *destColl");',
'msiCollCreate(str(*destColl), "1", *status) ::: msiRollback;',
'*source = str(*irodsHome)++"/"++str(*landingColl)++"/"++str(*fileName);',
'writeLine("serverLog", "source is *source");',
'*destination = str(*destColl)++"/"++str(*fileName);',
'writeLine("serverLog", "destination is *destination");',
'msiDataObjRename(*source, *destination, "0", *status);',
'}',
'INPUT *irodsHome = "/biolabZone/home/superbiorods", *landingColl="land", *fileName = "void.txt", *dataTypeName = "none", *repoColl="test-repo", *idData =0',
'OUTPUT *ruleExecOut'
].join('\r\n');

/**
* @description API to manage data through iRODS Rest API
*/
function IrodsRestStrategy(irodsConf) {
if (!irodsConf) {
// throw some error
return;
}
this.irodsHome = irodsConf.irodsHome;
this.landingCollection = irodsConf.landingCollection;
this.repoCollection = irodsConf.repoCollection;
this.irodsRest = irodsConf.irodsRest;
this.username = irodsConf.username;
this.password = irodsConf.password;
}

IrodsRestStrategy.prototype = {

storeFile: function(xtensDataFile, idData, dataTypeName, callback) {

var file = xtensDataFile;
var irodsRuleInputParameters = [
{name: "*irodsHome", value: this.irodsHome},
{name: "*dataTypeName", value: dataTypeName},
{name: "*fileName", value: file.name},
{name: "*landingColl", value: this.landingCollection},
{name: "*repoColl", value: this.repoCollection}
];

if (idData) {
console.log("adding idData...");
irodsRuleInputParameters.push({name: "*idData", value: idData});
}

var postOptions = {
hostname: this.irodsRest.hostname,
port: this.irodsRest.port,
path: this.irodsRest.path + '/rule',
method: 'POST',
auth: this.username + ':' + this.password,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};

var postRequest = http.request(postOptions,function(res) {
res.setEncoding('utf8');

var resBody = '';

res.on('data', function(chunk) {
resBody += chunk;
});

res.on('end', function() {
console.log('res.end');
console.log(resBody);
file.uri = this.irodsHome + "/" + this.repoColl + "/" + dataTypeName + "/" +
(idData ? idData + "/" : "") + file.name;
delete file.name;
console.log("irods.moveFile: uri = " + file.uri);
console.log("irods.moveFile: success...calling callback");
callback();
});
});

postRequest.on('error',function(err) {
console.log('irods.moveFile: problem with request: ' + err.message);
callback(err);
});

var body = {
ruleProcessingType: "INTERNAL",
ruleAsOriginalText: storeFileRule,
irodsRuleInputParameters: irodsRuleInputParameters
};

postRequest.write(JSON.stringify(body));
postRequest.end();

}

};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "xtens-fs",
"version": "1.0.0",
"description": "A module for managing interaction with the file system in XTENS",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/biolab-unige/xtens-fs.git"
},
"keywords": [
"file",
"storage",
"xtens",
"irods"
],
"author": "Massimiliano Izzo",
"license": "BSD",
"bugs": {
"url": "https://github.com/biolab-unige/xtens-fs/issues"
},
"homepage": "https://github.com/biolab-unige/xtens-fs"
}

0 comments on commit 635380c

Please sign in to comment.