@wbg-mde/repository
Version:
Managing all common method for file system CRUD operations.
240 lines (239 loc) • 11.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const template_service_1 = require("../shared/services/template.service");
const export_master_config_1 = require("../shared/configs/export.master.config");
const app_repo_utility_1 = require("../shared/app.repo.utility");
const js2xmlparser = require("@wbg-mde/js2xmlparser");
const _ = require('lodash');
class ExportGeospatial {
constructor() {
this.templateService = new template_service_1.TemplateService();
}
exportStudy(filePath, project, callback, asJson) {
try {
let header = project.header;
let metadata = project.metadata;
this.templateService.getTreeViewSchema(header.type, header.language, (response) => {
let xmlJson = this.formatMetadata(response.nodes, response.schema, response.form, metadata);
if (asJson === true) {
app_repo_utility_1.App_Repository_Utility.writeFile(filePath, JSON.stringify(xmlJson));
}
else {
let xml = js2xmlparser.parse(export_master_config_1.Export_Master_Config.xmlNodeProps.geoRoot, xmlJson, {
cdataInvalidChars: true,
format: {
doubleQuotes: true
},
declaration: {
encoding: 'UTF-8'
}
});
app_repo_utility_1.App_Repository_Utility.writeFile(filePath, xml);
}
callback({
result: 'ok'
});
});
}
catch (e) {
callback({
result: 'error',
message: e
});
app_repo_utility_1.App_Repository_Utility.LogText('Export >Geospatial > exportStudy > ' + e, 3);
}
}
formatMetadata(treeNodes, schema, form, metadata) {
let formattedJson = {};
try {
formattedJson[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute] = this.getHeaderAttributes();
let self = this;
_.forEach(treeNodes, function (treeNode) {
formattedJson = self.getDDIObject(treeNode, metadata, formattedJson, '');
});
}
catch (e) {
app_repo_utility_1.App_Repository_Utility.LogText('Export > Geospatial > formatMetadata > ' + e, 3);
}
return formattedJson;
}
getHeaderAttributes() {
let headeAttribute = {
"xmlns:gco": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gco,
"xmlns": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns,
"xmlns:gmd": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gmd,
"xmlns:gmi": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gmi,
"xmlns:gmx": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gmx,
"xmlns:gsr": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gsr,
"xmlns:xsi": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_xsi,
"xmlns:fn": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_fn,
"xmlns:gss": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gss,
"xmlns:gts": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gts,
"xmlns:gml": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_gml,
"xmlns:xlink": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xmlns_xlink,
"xmlns:schemaLocation": export_master_config_1.Export_Master_Config.geospatialHeaderAttr.xsi_schemaLocation
};
return headeAttribute;
}
getDDIObject(form, data, outData, parentKey) {
try {
if (form.formschema && form.formschema.items && form.formschema.items instanceof Array) {
let self = this;
_.forEach(form.formschema.items, function (__node) {
if (__node.formschema && __node.formschema.items && __node.formschema.items instanceof Array) {
self.getDDIObject(__node, data, outData, parentKey);
}
else {
self.exportObject(__node, data, outData, parentKey);
}
});
}
}
catch (e) {
app_repo_utility_1.App_Repository_Utility.LogText('Export > DDI > getDDIObject > ' + e, 3);
}
return outData;
}
exportArray(schema, data, outdata, parentKey) {
if (schema.items.length === 1 && schema.items[0].type === 'fieldset') {
this.exportArray(schema.items[0], data, outdata, parentKey);
}
else {
let self = this;
_.forEach(schema.items, function (item) {
self.exportObject(item, data, outdata, parentKey);
});
}
}
exportObject(schema, data, outData, parentKey) {
let keys = schema.key.replace(parentKey, '').split('.');
let nodeVal = _.get(data, keys);
let self = this;
switch (schema.type) {
case 'array':
{
if (nodeVal) {
let nodeValues = [];
_.forEach(nodeVal, function (item) {
let obj = {};
self.exportArray(schema, item, obj, schema.key + '[].');
nodeValues.push(obj);
});
if (schema["exportKey"]) {
let exportKeys = schema["exportKey"].split(",");
_.forEach(exportKeys, function (__key) {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, __key, nodeValues);
});
}
else {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, schema["key"], nodeVal);
}
}
}
break;
case 'table':
{
if (nodeVal) {
let tableValue = self.formatTableVal(nodeVal, schema.items);
if (tableValue && !_.isEmpty(tableValue)) {
if (schema["exportKey"]) {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, schema["exportKey"], tableValue);
}
else {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, schema["key"], tableValue);
}
}
}
}
break;
case 'fieldset':
{
_.forEach(schema.items, function (item) {
self.exportObject(item, data, outData, parentKey);
});
}
break;
default:
{
if (nodeVal) {
if (schema["exportKey"]) {
let exportKeys = schema["exportKey"].split(",");
_.forEach(exportKeys, function (__key) {
if (__key.indexOf("_") !== -1 && __key.indexOf("Code") !== -1 && __key.indexOf("@") !== -1 && __key.indexOf("codeListValue") === -1) {
var t = __key.split(".");
var a = _.filter(t, (s) => {
return s.indexOf("_") !== -1 && s.indexOf("Code") !== -1;
});
a = a[0].split(":")[1];
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, __key, "http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#" + a);
}
else
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, __key, nodeVal);
});
}
else {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(outData, schema["key"], nodeVal);
}
}
}
break;
}
}
formatPeriods(nodeVal) {
try {
if (nodeVal && nodeVal instanceof Array) {
let periods = new Array();
_.forEach(nodeVal, function (val) {
let startDt = {};
let endDt = {};
if (val.cycle) {
startDt["cycle"] = val.cycle;
endDt["cycle"] = val.cycle;
}
if (val.date) {
startDt["date"] = val.date;
startDt["event"] = "start";
periods.push(startDt);
}
if (val.event) {
endDt["date"] = val.event;
endDt["event"] = "end";
periods.push(endDt);
}
});
return periods;
}
}
catch (e) {
app_repo_utility_1.App_Repository_Utility.LogText('Export > formatPeriods > ' + e, 3);
return undefined;
}
}
formatTableVal(table, cols) {
let lstObj = new Array();
try {
for (let row of table) {
let obj = {};
obj[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute] = {};
for (let col of cols) {
let keys = col.key.split(".");
let colName = keys[keys.length - 1];
if (row[colName]) {
if (col.exportKey) {
app_repo_utility_1.App_Repository_Utility.setNestedProperty(obj, col.exportKey, row[colName]);
}
else {
obj[export_master_config_1.Export_Master_Config.xmlNodeProps.headerAttribute][colName] = row[colName];
}
}
}
lstObj.push(obj);
}
}
catch (e) {
app_repo_utility_1.App_Repository_Utility.LogText('Export > formatTableVal > ' + e, 3);
}
return lstObj;
}
}
exports.ExportGeospatial = ExportGeospatial;