Commit 7e4228a1 authored by priit's avatar priit
Browse files

Mitme faili haldamise toetus

parent 87a2dc1a
var logger = require('log4js').getLogger('command_model');
var sessionService = require('../service/sessionService');
var async = require('async');
function CommandModel(){
......@@ -21,7 +22,7 @@ function CommandModel(){
this.stdOutExtension = stdOutExtension;
};
this.setTextValue = function (key, value) {
this.setKeyValue = function (key, value) {
self.keyValues[key] = value;
};
......@@ -42,33 +43,26 @@ function CommandModel(){
logger.debug('Output path added: ' + key +' ' + self.outputPaths[key] );
};
this.render = function ( callback ) {
this.render = function ( cb ) {
for(i in self.outputPaths){
self.keyValues[i] = self.outputPaths[i];
}
//store file values
if(fileValues.length > 0){
self._storeToFile(0, callback);
} else {
callback();
}
};
this._storeToFile = function (index, callback) {
var fileValue = fileValues[index];
sessionService.storeToFile(self.session.id, fileValue['value'],{}, function (err, path) {
if(err) return callback(err);
self.keyValues[fileValue['key']] = path;
index = index + 1;
if(fileValues.length < index){
self._storeToFile(index, callback);
} else {
callback();
async.each(
fileValues,
function (fileValue, callback) {
sessionService.storeToFile(self.session.id, fileValue['value'],{}, function (err, path) {
if(err) return callback(err);
self.keyValues[fileValue['key']] = path;
callback();
});
},
function (err) {
cb(err);
}
});
}
);
};
}
module.exports = CommandModel;
......@@ -5,12 +5,9 @@ function LocalCommand( commandModel ) {
var self = this;
var keyValues = commandModel.keyValues;
logger.debug(keyValues);
var commandTemplate = commandModel.serviceProperties.commandTemplate;
this.templateParams = commandTemplate.match(/\[(.*?)]/g);
var templateParams = commandTemplate.match(/\[(.*?)]/g);
var commandParts = commandTemplate.split(' ');
this.command = "";
......@@ -26,26 +23,39 @@ function LocalCommand( commandModel ) {
this._parseParams = function () {
for(index in this.templateParams){
for(index in templateParams){
var propertyItem = this.templateParams[index];
var propertyKey = propertyItem.substr(1, (propertyItem.length - 2));
var property = templateParams[index];
var propertyKey = property.substr(1, (property.length - 2));
var value = keyValues[propertyKey];
if(!value){
throw new Error('Value not detected for property ' + propertyKey);
}
self._replacePropertyValue(propertyItem, value);
self._setCommandPropertyValue(property, value);
}
};
this._replacePropertyValue = function (propertyItem, value) {
this._setCommandPropertyValue = function (propertyItem, value) {
var newCommandParts = [];
for (i in commandParts) {
var part = commandParts[i];
commandParts[i] = part.replace(propertyItem, value);
if( commandParts[i] == propertyItem){
if(Array.isArray(value)){
for(j in value){
newCommandParts.push(value[j]);
}
} else {
newCommandParts.push(value);
}
} else {
newCommandParts.push(commandParts[i]);
}
}
commandParts = newCommandParts;
};
}
......
......@@ -47,8 +47,7 @@ function SimpleLocalCommand(){
var model = new CommandModel();
model.serviceProperties.commandTemplate = config.wrapper.command.commandTemplate;
model.init( session );
model.setTextValue('data', session.getFiles('content').join(' '));
//model.setTextValue('data', session.getFile('content'));
model.setKeyValue('data', session.getFiles('content'));
model.render(function (err) {
logger.debug('Render callback');
callback(err, model);
......
......@@ -97,12 +97,9 @@ function Tokenizer(){
this.getCommandModel = function (session, callback) {
var model = new CommandModel();
logger.error('commandTemplate');
logger.error(config.wrapper);
model.serviceProperties.commandTemplate = config.wrapper.command.commandTemplate;
model.init( session );
model.setTextValue('data', session.requestFiles.content);
model.setKeyValue('data', session.getFile('content'));
model.addOutputPath('outputPath1', {extension: 'txt'});
model.render(function (err) {
logger.debug('Render callback');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment