UNPKG

navidev-abap-gw-api

Version:

API para la extracción de datos de servicios SAP GW

172 lines (171 loc) 8.99 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const metadataTypes_1 = require("./metadataTypes"); const Result_1 = require("navidev-adt-api/dist/shared/core/Result"); const xmlParser_1 = __importDefault(require("../shared/utilities/parser/xmlParser")); const metadataConstants_1 = require("./metadataConstants"); class AnnotationVocabV2App { constructor(connectionController, serviceBindingInfo, metadataContent) { this.connectionController = connectionController; this.serviceBindingInfo = serviceBindingInfo; this.metadataContent = metadataContent; } /** * Lectura de los datos de anotaciones y vocabulario del servicio y se añaden a los datos * del contenido del metadata. * @returns */ getAnnotVocab() { return __awaiter(this, void 0, void 0, function* () { var _a; let resultLoad = yield this.loadMetadata((_a = this.serviceBindingInfo) === null || _a === void 0 ? void 0 : _a.annotationUrl); // No controlo posibles errores porque no debería fallar, ya que cualquier error de conexión s al leer // el metadata. if (resultLoad.isSuccess) { let bodyParsed = xmlParser_1.default.fullParse(resultLoad.getValue()); xmlParser_1.default.xmlArray(bodyParsed["edmx:Edmx"]["edmx:DataServices"].Schema.Annotations).forEach((rowAnnotation) => { let target = rowAnnotation["@_Target"]; if (target) { // Si hay un / en el target, es que es una anotación de un campo. if (target.includes("/")) this.processFieldAnnotation(rowAnnotation); if (this.metadataContent.entities.findIndex((row) => row.entityType == target) != -1) this.processEntityAnnotation(rowAnnotation); } }); } return this.metadataContent; }); } /** * Procesa las anotaciones de una entidad. * @param rowAnnotation */ processEntityAnnotation(rowAnnotation) { let indexEntity = this.metadataContent.entities.findIndex((row) => row.entityType == rowAnnotation["@_Target"]); xmlParser_1.default.xmlArray(rowAnnotation.Annotation).forEach((rowSubAnnotation) => { if (rowSubAnnotation["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.LINE_ITEM) this.processLineItemAnnotation(rowSubAnnotation.Collection, indexEntity); if (rowSubAnnotation["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.IDENTIFICATION) this.processIdentificationAnnotation(rowSubAnnotation.Collection, indexEntity); }); } /** * Procesa la anotación de un LineItem. * @param lineItem */ processLineItemAnnotation(lineItem, indexEntity) { let properties = xmlParser_1.default.xmlArray(lineItem.Record); // Recorro las propiedas del LineItem con un for porque así de paso obtengo la posición donde tiene que aparecer. // Ya que los campos vienen ordenados según la anotación del CDS for (var x = 0; x < properties.length; x++) { // El propertyValue puede ser un array o una estructura. Es un array cuando tiene alguna anotación como // @ObjectModel.text.element. Pero si no tiene anotaciones parecidas es una estructura. Por ello, convierto // siempre a array para simplificar el tratamiento. let propertiesField = xmlParser_1.default.xmlArray(properties[x].PropertyValue); let rowPath = propertiesField.find((row) => row["@_Property"] == "Value"); if (rowPath) { let indexField = this.metadataContent.entities[indexEntity].fields.findIndex((row) => row.name == rowPath["@_Path"]); if (indexField != -1) this.metadataContent.entities[indexEntity].fields[indexField].lineItem = { position: x + 1, // Quiero que las posiciones empiecen en 1 }; } } } /** * Procesa la anotación de la identification. * @param identification */ processIdentificationAnnotation(identification, indexEntity) { let properties = xmlParser_1.default.xmlArray(identification.Record); // Recorro las propiedas del LineItem con un for porque así de paso obtengo la posición donde tiene que aparecer. // Ya que los campos vienen ordenados según la anotación del CDS for (var x = 0; x < properties.length; x++) { let propertiesField = xmlParser_1.default.xmlArray(properties[x].PropertyValue); let rowPath = propertiesField.find((row) => row["@_Property"] == "Value"); if (rowPath) { let indexField = this.metadataContent.entities[indexEntity].fields.findIndex((row) => row.name == rowPath["@_Path"]); if (indexField != -1) { this.metadataContent.entities[indexEntity].fields[indexField].identification = { position: x + 1, }; } } } } /** * Procesa la anotación de un campo. * @param rowAnnotation */ processFieldAnnotation(rowAnnotation) { let parts = rowAnnotation["@_Target"].split("/"); let entityName = parts[0]; let fieldName = parts[1]; let indexEntity = this.metadataContent.entities.findIndex((row) => row.entityType == entityName); if (indexEntity == -1) return; let indexField = this.metadataContent.entities[indexEntity].fields.findIndex((row) => row.name == fieldName); if (indexField == -1) return; xmlParser_1.default.xmlArray(rowAnnotation.Annotation).forEach((rowField) => { if (rowField["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.HIDDEN) this.metadataContent.entities[indexEntity].fields[indexField].hidden = true; if (rowField["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.HIDDEN_FILTER) this.metadataContent.entities[indexEntity].fields[indexField].hiddenFilter = true; if (rowField["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.TEXT_ELEMENT) { this.metadataContent.entities[indexEntity].fields[indexField].textElement = rowField["@_Path"]; xmlParser_1.default.xmlArray(rowField.Annotation).forEach((rowSubAnnotation) => { if (rowSubAnnotation["@_Term"] == metadataConstants_1.ANNOTATION_VOCAB_TERM.TEXT_ARRANGEMENT) this.metadataContent.entities[indexEntity].fields[indexField].textArrangement = this.convertTextArrangementType(rowSubAnnotation["@_EnumMember"]); }); } }); } /** * Convierte un string en un TextArrangementTypes * @param value * @returns */ convertTextArrangementType(value) { if (value == "UI.TextArrangementType/TextOnly") return metadataTypes_1.TextArrangementTypes.TextOnly; if (value == "UI.TextArrangementType/TextFirst") return metadataTypes_1.TextArrangementTypes.TextFirst; if (value == "UI.TextArrangementType/TextLast") return metadataTypes_1.TextArrangementTypes.TextLast; return metadataTypes_1.TextArrangementTypes.TextOnly; } /** * Lectura del metadata de SAP */ loadMetadata(url) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.connectionController.request(url, { method: "GET", }); if (response.isSuccess) { let httpResponse = response.getValue(); return Result_1.Result.ok(httpResponse.body); } else { return Result_1.Result.fail(response.getErrorValue()); } }); } } exports.default = AnnotationVocabV2App;