@wbg-mde/r-factory
Version:
Metadata editor R integration module
185 lines (184 loc) • 8.86 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const handlebars = require("handlebars");
const repository_1 = require("@wbg-mde/repository");
const shared_1 = require("../shared");
const api_helper_1 = require("../shared/api-helper");
const _ = require('lodash');
let ExportDataDictionary = class ExportDataDictionary {
exportFixedWidthText(filePath, variables, csvPath, projectId, libraryPath, lcl, callback) {
this.exportToFixedWidthTextFormat(filePath, variables, csvPath, projectId, libraryPath, lcl, callback);
}
exportDictionary(dictionaryPath, variables, csvPath, projectId, libraryPath, lcl, callback) {
this.exportToDictionaryFormat(dictionaryPath, variables, csvPath, projectId, libraryPath, lcl, callback);
}
exportSPSSSyntax(syntaxFilePath, variables, csvPath, projectId, libraryPath, lcl, callback) {
this.exportToSPSSSyntaxFormat(syntaxFilePath, variables, csvPath, projectId, libraryPath, lcl, callback);
}
exportToFixedWidthTextFormat(filePath, variables, csvPath, projectId, libraryPath, lcl, callback) {
try {
this.exportToFixedWidthFormat(variables, csvPath, projectId, libraryPath, filePath, lcl).then((resp) => {
callback(resp);
});
}
catch (e) {
callback({ result: 'Error', message: e });
}
}
exportToDictionaryFormat(dictionaryPath, variables, csvPath, projectId, libraryPath, lcl, callback) {
try {
let fixedwidthFileName = dictionaryPath.slice(0, -4) + '.dat';
this.exportToFixedWidthFormat(variables, csvPath, projectId, libraryPath, fixedwidthFileName, lcl).then((resp) => {
if (resp.result === 'ok') {
let data = resp.data;
let compiledTemplate = handlebars.compile(shared_1.ExportFileTemplates.stataDictionaryTemplate);
handlebars.registerHelper('fn-startcolumn', function (text, padspace) {
return String(" _column(" + text + ")").slice(-(padspace));
});
handlebars.registerHelper('fn-padcolumn', function (text, padspace) {
return String(" " + text + "").slice(-(padspace));
});
let templData = this.getTemplateData(fixedwidthFileName, dictionaryPath, data);
let dataDictionary = compiledTemplate(templData);
repository_1.Repository_Utility.writeFile(dictionaryPath, dataDictionary);
let compiledDoFileTemplate = handlebars.compile(shared_1.ExportFileTemplates.stataDoFileTemplate);
let doScript = compiledDoFileTemplate(templData);
let doScriptFileName = dictionaryPath.slice(0, -4) + '.DO';
repository_1.Repository_Utility.writeFile(doScriptFileName, doScript);
callback({ result: 'ok' });
}
else {
callback(resp);
}
});
}
catch (e) {
callback({ result: 'Error', message: e });
}
}
exportToSPSSSyntaxFormat(syntaxFilePath, variables, csvPath, projectId, libraryPath, lcl, callback) {
try {
let fixedwidthFileName = syntaxFilePath.slice(0, -4) + '.txt';
this.exportToFixedWidthFormat(variables, csvPath, projectId, libraryPath, fixedwidthFileName, lcl).then((resp) => {
if (resp.result === 'ok') {
let data = resp.data;
let compiledTemplate = handlebars.compile(shared_1.ExportFileTemplates.spssSyntaxTemplate);
let dataDictionary = compiledTemplate(this.getTemplateData(fixedwidthFileName, syntaxFilePath, data));
repository_1.Repository_Utility.writeFile(syntaxFilePath, dataDictionary);
callback({ result: 'ok' });
}
else {
callback(resp);
}
});
}
catch (e) {
console.log(e);
callback({ result: 'Error', message: e });
}
}
exportToFixedWidthFormat(variables, csvPath, projectId, libraryPath, fileName, lcl) {
let dataset = shared_1.Utility.formatVariable(variables, [
'name',
'internalName',
'catVal',
'width',
'dcml',
'StartPos',
'EndPos',
'type',
'StorageType',
'ReadFormat',
'dataType'
]);
return api_helper_1.APIHelper.call('exportFWF', 'post', {
"varDescr": dataset,
"datafile": csvPath,
"outpath": fileName
}).then((response) => {
if (response.data && response.data.result === 'ok') {
for (let data of response.data.data) {
let varname = data.name;
let variable = _.find(variables, { name: varname });
if (!variable.location) {
variable.location = {};
}
variable.location.width = data.width;
variable.location.StartPos = data.StartPos;
variable.location.EndPos = data.EndPos;
variable.StorageType = data.StorageType;
variable.ReadFormat = data.ReadFormat;
}
return Object.assign({ result: 'ok', data: variables });
}
else {
return Object.assign({ result: 'error', message: response.data.message });
}
});
}
getTemplateData(fileName, dictionaryPath, variables) {
var templateData = {};
templateData["filePath"] = fileName;
;
templateData["dicPath"] = dictionaryPath;
templateData["outPath"] = dictionaryPath.slice(0, -4) + '.dta';
;
templateData["valueLabelled"] = false;
templateData["variables"] = _.map(variables, (variable) => {
let isMultiChar = (variable.location.width > 1);
let isString = (variable.varFormat && variable.varFormat.type == "character");
let isDecimal = (variable.dcml > 0);
let isValueLablled = false;
let lablledValues = new Array();
if (variable.catgry) {
var isLabelled = function (catgry) {
return (catgry.labl);
};
lablledValues = _.filter(variable.catgry, isLabelled);
if (lablledValues.length > 0) {
templateData["valueLabelled"] = true;
isValueLablled = true;
}
}
return {
name: variable.name,
labl: variable.labl || variable.name,
width: String(variable.location.width),
dcml: String(variable.dcml),
labelledValues: lablledValues,
startColumn: variable.location.StartPos,
endColumn: variable.location.EndPos,
dataType: variable.StorageType,
format: variable.ReadFormat,
multiChar: isMultiChar,
isString: isString,
isDecimal: isDecimal,
isValueLablled: isValueLablled
};
});
return templateData;
}
};
ExportDataDictionary = __decorate([
repository_1.Trace({
params: {
exportFixedWidthText: [1],
exportDictionary: [1],
exportSPSSSyntax: [1],
exportToFixedWidthTextFormat: [2],
exportToDictionaryFormat: [2],
exportToSPSSSyntaxFormat: [2],
exportToFixedWidthFormat: [1],
getVariableInfo: [0],
formatDataset: ['*'],
getTemplateData: [2]
}
})
], ExportDataDictionary);
exports.ExportDataDictionary = ExportDataDictionary;