@wbg-mde/repository
Version:
Managing all common method for file system CRUD operations.
266 lines (265 loc) • 12.5 kB
JavaScript
"use strict";
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 project_header_1 = require("../project-data/project.header");
const model_1 = require("@wbg-mde/model");
const app_repo_constants_1 = require("../shared/app.repo.constants");
const app_repo_utility_1 = require("../shared/app.repo.utility");
const resource_path_1 = require("../resource-path/resource-path");
const import_ddi_1 = require("../import-master/import.ddi");
const import_dublincore_1 = require("../import-master/import.dublincore");
const appstatus_decorator_1 = require("../decorators/appstatus.decorator");
const path = require("path");
const fs = require("fs");
const mkdirp = require("mkdirp");
const copydir = require("copy-dir");
const _ = require("lodash");
const { exec } = require('child_process');
const trace_decorator_1 = require("../decorators/trace.decorator");
let NesstarImport = class NesstarImport {
constructor() {
this.exportFormats = {
ddi: "DDI",
csv: "DELIMITED_TEXT",
dublcor: "DC"
};
this.resourcePath = new resource_path_1.ResourcePath();
this.projectHeader = new project_header_1.ProjectHeader();
this.importDDI = new import_ddi_1.ImportDDI();
this.importDblCore = new import_dublincore_1.ImportDublinCore();
}
importNesstarStudy(filePath, options, callBack, appStatus) {
this.nesstarStudy = filePath;
this.studyName = this.getFileNameFromPath(filePath);
this.nesstarExporter = this.neststarExporterPath();
this.createOutputDirectory();
appstatus_decorator_1.StatusTracker.update({ stepIndex: 1 });
this.executeDDIExporter((data) => {
appstatus_decorator_1.StatusTracker.update({ stepIndex: 2 });
this.createNewProject((project) => {
this.newDDIProject = project;
appstatus_decorator_1.StatusTracker.update({ stepIndex: 3 });
this.importNesstarDDI(data.filePath, options, (metadataResp) => {
if (metadataResp.result === 'ok') {
_.assign(this.newDDIProject.metadata, metadataResp.data);
}
appstatus_decorator_1.StatusTracker.update({ stepIndex: 4 });
this.exportDatasets(() => {
appstatus_decorator_1.StatusTracker.update({ stepIndex: 5 });
this.exportExternalResources((extResp) => {
if (extResp.result === 'ok') {
this.newDDIProject.metadata.extResources = this.formatExternalResources(extResp.data);
}
else {
this.newDDIProject.metadata.extResources = [];
}
this.updateProjectHeader();
appstatus_decorator_1.StatusTracker.update({ stepIndex: 6 });
let files = Object.keys(this.newDDIProject.metadata.variables) || [];
if (files.length > 0) {
this.newDDIProject.metadata.maxVariableId = 0;
for (let i = 0; i < files.length; i++) {
let variables = this.newDDIProject.metadata.variables[files[i]];
for (let variable of variables) {
this.newDDIProject.metadata.maxVariableId += 1;
variable.uniqueId = project.metadata.maxVariableId;
}
this.newDDIProject.metadata.variables[files[i]] = variables;
}
}
this.projectHeader.saveStudy(this.newDDIProject.header, this.newDDIProject.metadata, () => {
callBack({
project: this.newDDIProject
});
});
});
});
});
});
});
}
getTempDirectory() {
return path.join(this.resourcePath.getTempDirectory(), app_repo_constants_1.App_Repository_Constants.tempFolder.importNesstar, this.studyName);
}
neststarExporterPath() {
return path.join(this.resourcePath.assetsDirectory, app_repo_constants_1.App_Repository_Constants.userDataPaths.nesstarExporter, app_repo_constants_1.App_Repository_Constants.nesstarExporterExe);
}
getFileNameFromPath(filePath) {
return path.basename(filePath, path.extname(filePath));
}
createOutputDirectory() {
let tmpPath = this.getTempDirectory();
if (fs.existsSync(tmpPath)) {
this.removeDirectory(tmpPath);
}
mkdirp.sync(tmpPath);
}
removeDirectory(fPath) {
if (fs.existsSync(fPath)) {
fs.readdirSync(fPath).forEach((file, index) => {
let curPath = path.join(fPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
this.removeDirectory(curPath);
}
else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(fPath);
}
}
genExporterCommand(options) {
let commands = ['"' + this.nesstarExporter + '"', '"' + this.nesstarStudy + '"'];
if (typeof options.dataset === "number") {
commands.push("/DATASET=" + options.dataset);
}
if (options.format) {
commands.push("/FORMAT=" + options.format);
}
if (options.outputFile) {
commands.push("/OUTPUT_FILE=" + '"' + options.outputFile + '"');
}
return commands.join(' ');
}
executeExporter(command, callBack) {
let proc = exec(command, (error, stdout, stderr) => {
if (error) {
callBack(false);
return;
}
proc.kill();
callBack(true);
});
}
executeDDIExporter(callBack) {
let xmlPath = path.join(this.getTempDirectory(), this.studyName.concat(app_repo_constants_1.App_Repository_Constants.fileExtention.xml));
this.executeExporter(this.genExporterCommand({
format: this.exportFormats.ddi,
outputFile: xmlPath
}), (result) => {
callBack({
filePath: xmlPath
});
});
}
createNewProject(callBack) {
this.projectHeader.addNewStudy(app_repo_constants_1.App_Repository_Constants.schemaTypes.ddi, app_repo_constants_1.App_Repository_Constants.defaultLanguage, (response) => {
if (response.result === "ok") {
callBack(response.project);
}
});
}
importNesstarDDI(xmlPath, options, callBack) {
this.importDDI.readImportedFile(xmlPath, app_repo_constants_1.App_Repository_Constants.schemaTypes.ddi, app_repo_constants_1.App_Repository_Constants.defaultLanguage, options.importItems, (response) => {
callBack(response);
});
}
exportDatasets(callBack) {
let datasets = this.newDDIProject.metadata.datasets.fileDscr || [];
if (datasets.length > 0) {
this.executeDatasetExport(datasets, 0, () => {
this.copyDataFiles();
callBack();
});
}
else {
callBack();
}
}
executeDatasetExport(datasets, index, callback) {
let dataSetPath = path.join(this.getTempDirectory(), datasets[index]['ID'].concat(app_repo_constants_1.App_Repository_Constants.fileExtention.txt));
let command = this.genExporterCommand({
format: this.exportFormats.csv,
outputFile: dataSetPath,
dataset: index
});
this.executeExporter(command, () => {
index++;
if (datasets[index]) {
this.executeDatasetExport(datasets, index, callback);
}
else {
callback();
}
});
}
copyDataFiles() {
let project = this.newDDIProject;
let datasets = project.metadata.datasets.fileDscr || [];
let fileNames = _.map(datasets, "ID");
let destination = path.join(this.resourcePath.getProjectDirectory(project.header._id), app_repo_constants_1.App_Repository_Constants.projectHeader.csv_path);
let fromPath = this.getTempDirectory();
if (!fs.existsSync(destination)) {
mkdirp.sync(destination);
}
copydir.sync(fromPath, destination, (stat, filepath, filename) => {
if (!filepath || (filepath && app_repo_constants_1.App_Repository_Constants.fileExtention.txt.indexOf(path.extname(filepath)) === -1)) {
return false;
}
return true;
});
let newFilePath;
let fileBaseName;
let projectInfo = fs.readdirSync(destination).forEach(file => {
if (app_repo_constants_1.App_Repository_Constants.fileExtention.txt.indexOf(path.extname(file)) !== -1) {
newFilePath = path.join(destination, file.replace(/\.[^\.]+$/, app_repo_constants_1.App_Repository_Constants.fileExtention.csv));
fs.renameSync(path.join(destination, file), newFilePath);
fileBaseName = path.basename(file, app_repo_constants_1.App_Repository_Constants.fileExtention.txt);
let fileDscr = _.find(project.metadata.datasets.fileDscr, (item) => {
return item.ID == fileBaseName;
});
if (fileDscr) {
fileDscr.filePath = newFilePath;
}
}
});
}
exportExternalResources(callBack) {
let filePath = path.join(this.getTempDirectory(), this.studyName.concat(app_repo_constants_1.App_Repository_Constants.fileExtention.rdf));
let command = this.genExporterCommand({
format: this.exportFormats.dublcor,
outputFile: filePath
});
this.executeExporter(command, (result) => {
if (result === true) {
this.importDublicCore(filePath, (resp) => {
callBack(resp);
});
}
else {
callBack({ result: 'error', message: 'Error occured while importing external resource files' });
}
});
}
importDublicCore(filePath, callBack) {
this.importDblCore.readImportedFile(filePath, app_repo_constants_1.App_Repository_Constants.schemaTypes.dbln_core, app_repo_constants_1.App_Repository_Constants.defaultLanguage, (response) => {
callBack(response);
});
}
getProjectDirectory(project) {
return this.resourcePath.getProjectDirectory(project.header._id);
}
updateProjectHeader() {
this.newDDIProject.header.name = this.studyName;
this.newDDIProject.header.status = model_1.ProjectStatus.saved;
}
formatExternalResources(data) {
let dblnCore = [];
for (let item of data) {
let newResource = {};
newResource['resDscr'] = item;
newResource['resDscr'].resId = app_repo_utility_1.App_Repository_Utility.generateGUID();
dblnCore.push(newResource);
}
return dblnCore;
}
};
NesstarImport = __decorate([
trace_decorator_1.Trace()
], NesstarImport);
exports.NesstarImport = NesstarImport;