UNPKG

@wbg-mde/repository

Version:

Managing all common method for file system CRUD operations.

308 lines (307 loc) 14.4 kB
"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 export_master_config_1 = require("../shared/configs/export.master.config"); const template_service_1 = require("../shared/services/template.service"); const import_study_service_1 = require("../shared/services/import.study.service"); const app_repo_utility_1 = require("../shared/app.repo.utility"); const _ = require('lodash'); const trace_decorator_1 = require("../decorators/trace.decorator"); let ImportGeospatial = class ImportGeospatial { constructor() { this.templateService = new template_service_1.TemplateService(); this.importService = new import_study_service_1.ImportStudyService(); } readImportedFile(filePath, studyType, language, callBack) { try { this.importService.parseXMLtoJSON(filePath, (parser_resp) => { if (parser_resp.result == 'ok') { let data = parser_resp.data; let metadata = {}; if (data[export_master_config_1.Export_Master_Config.xmlNodeProps.geoRoot]) { let ddiDoc = data[export_master_config_1.Export_Master_Config.xmlNodeProps.geoRoot]; this.templateService.getTreeViewSchema(studyType, language, (response) => { let nodes = response; this.getDDIObject(nodes, ddiDoc, metadata); callBack({ result: "ok", data: metadata }); }); } else { callBack({ result: "warning", messages: "This is not a valid Geospatial document." }); } } else { callBack(parser_resp); } }); } catch (e) { callBack({ result: "error", messages: e }); app_repo_utility_1.App_Repository_Utility.LogText('Import > DDI > readImportedFile > ' + e, 3); } } getDDIObject(form, data, outData) { try { if (form.nodes && form.nodes instanceof Array) { let self = this; _.forEach(form.nodes, function (__node) { if (__node.nodes && __node.nodes instanceof Array) { self.getDDIObject(__node, data, outData); } else if (__node.formschema && __node.formschema.items instanceof Array) { self.getDDIObject(__node, data, outData); } else { if (__node.formschema.exportKey || __node.formschema.key) { let __data = app_repo_utility_1.App_Repository_Utility.getNestedProperty(data, __node.formschema.exportKey || __node.formschema.key); if (__data) { __data = self.convertToDataType(__node.formschema, __data); self.importService.setNestedProperty(outData, __node.key, __data); } } } }); } else if (form.formschema && form.formschema.items && form.formschema.items instanceof Array) { this.buildObject(form.formschema, data, outData, ''); } return outData; } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Import > DDI > getDDIObject > ' + e, 3); return outData; } } buildObject(schema, data, outData, parentKey) { let self = this; schema.key = schema.key.replace(parentKey, ''); var ___nodedata = app_repo_utility_1.App_Repository_Utility.getNestedProperty(data, schema.exportKey || schema.key); switch (schema.type) { case 'array': case 'table': { if (___nodedata) { let nodeValues = []; _.forEach(___nodedata, function (item) { let obj = {}; self.buildArrayObject(schema, item, obj, schema.key + '[].'); nodeValues.push(obj); }); self.importService.setNestedProperty(outData, schema.key, nodeValues); } } break; case 'fieldset': { _.forEach(schema.items, function (item) { self.buildObject(item, data, outData, parentKey); }); } break; default: { if (___nodedata) { if (___nodedata instanceof Array && ___nodedata.length === 1) { ___nodedata = ___nodedata[0]; } app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, schema.key, ___nodedata); } } } } buildArrayObject(schema, data, outData, parentKey) { if (schema.items.length === 1 && schema.items[0].type === 'fieldset') { this.buildArrayObject(schema.items[0], data, outData, parentKey); } else { let self = this; _.forEach(schema.items, function (item) { self.buildObject(item, data, outData, parentKey); }); } } buildFileObject(data) { try { if (data instanceof Array) { return this.buildFileObject(data[0]); } else if (data instanceof Object) { let outObj = {}; _.forEach(data, (item, key) => { if (key == export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute) { _.forEach(item, (attrItem, attrKey) => { outObj[attrKey] = attrItem; }); } else if (item instanceof Object || item instanceof Array) { outObj[key] = this.buildFileObject(item); } else { outObj[key] = item; } }); return outObj; } else { return data; } } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Import > DDI > buildFileObject > ' + e, 3); return data; } } convertToDataType(schema, value) { try { let type = schema == undefined ? "" : schema.type; switch (type) { case export_master_config_1.Export_Master_Config.formSchemaTypes.table: let tableVal = new Array(); _.forEach(value, function (val) { let rowVal = {}; let i = 0; _.forEach(schema.items, function (item) { let keys = item.key.split("."); let propName = keys[keys.length - 1]; if (item.exportKey) { if (val instanceof Object) { let propVal = app_repo_utility_1.App_Repository_Utility.getNestedProperty(val, item.exportKey); if (propVal) { if (propVal instanceof Array) { rowVal[propName] = propVal[0]; } else { rowVal[propName] = propVal; } } } else { if (item.exportKey == export_master_config_1.Export_Master_Config.xmlNodeProps.elementProp) { rowVal[propName] = val; } } } else { if (val[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute]) { if (val[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute][propName]) { rowVal[propName] = val[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute][propName]; } } } }); tableVal.push(rowVal); }); if (schema.period) { let periods = new Array(); _.forEach(tableVal, function (item) { if (item.event == "start") { let period = {}; period["date"] = item.date; period["event"] = ""; period["cycle"] = item.cycle; periods.push(period); } else { if (periods.length > 0) { periods[periods.length - 1]["event"] = item.date; } else { let period = {}; period["event"] = item.date; period["cycle"] = item.cycle; periods.push(period); } } }); tableVal = periods; } return tableVal; case export_master_config_1.Export_Master_Config.formSchemaTypes.date: if (value instanceof Array) { value = value[0]; return value; } else { return value; } case "text": case "": if (value instanceof Array) { value = value[0]; return value; } else { return value; } default: if (value instanceof Array) { return value[0]; } else { return value; } } } catch (e) { app_repo_utility_1.App_Repository_Utility.LogText('Import > DDI > buildFileObject > ' + e, 3); return value; } } getTemplateFile(nodes) { let templateFIle = export_master_config_1.Export_Master_Config.defaultOptions.tmplFile; if (nodes[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute]) { let headerAttr = nodes[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute]; if (headerAttr[export_master_config_1.Export_Master_Config.customAttributes.template]) { templateFIle = headerAttr[export_master_config_1.Export_Master_Config.customAttributes.template]; } } return templateFIle; } getStudyType(nodes) { let studyType = export_master_config_1.Export_Master_Config.defaultOptions.studyType; if (nodes[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute]) { let headerAttr = nodes[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute]; if (headerAttr[export_master_config_1.Export_Master_Config.customAttributes.type]) { studyType = headerAttr[export_master_config_1.Export_Master_Config.customAttributes.type]; } } return studyType; } getLocation(locations, varDescr) { if (locations instanceof Array && locations[0]) { let location = locations[0]; varDescr[export_master_config_1.Export_Master_Config.variableDescriptionProps.location] = {}; _.forEach(location, (locVal, locKey) => { if (locVal[0]) { varDescr[export_master_config_1.Export_Master_Config.variableDescriptionProps.location][locKey] = locVal[0]; } }); } return varDescr; } }; ImportGeospatial = __decorate([ trace_decorator_1.Trace({ exclude: [ 'getDDIObject', 'buildObject', 'buildArrayObject', 'buildFileObject', 'convertToDataType' ], params: { getTemplateFile: ['*'], getStudyType: ['*'] } }) ], ImportGeospatial); exports.ImportGeospatial = ImportGeospatial;