@ngageoint/mage.arcgis.service
Version:
A mage service plugin that synchronizes mage observations to a configured ArcGIS feature layer.
201 lines • 6.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormFields = exports.EventTransform = void 0;
/**
* Contains information used to transform observations from a single event.
*/
class EventTransform {
/**
* Creates a new instance of EventTransform.
* @param {ArcGISPluginConfig} config The plugins configuration.
* @param {MageEvent | null} mageEvent The MAGE Event.
*/
constructor(config, mageEvent) {
/**
* Form field mappings between form ids and form fields.
*/
this.formFields = new Map();
this.mageEvent = mageEvent;
this.initialize(config);
}
/**
* Initialize the event fields.
* @param {ArcGISPluginConfig} config The plugins configuration.
*/
initialize(config) {
const allFields = new Set();
allFields.add(config.observationIdField);
if (config.eventIdField != null) {
allFields.add(config.eventIdField);
}
if (config.eventNameField != null) {
allFields.add(config.eventNameField);
}
if (config.userIdField != null) {
allFields.add(config.userIdField);
}
if (config.usernameField != null) {
allFields.add(config.usernameField);
}
if (config.userDisplayNameField != null) {
allFields.add(config.userDisplayNameField);
}
if (config.deviceIdField != null) {
allFields.add(config.deviceIdField);
}
if (config.createdAtField != null) {
allFields.add(config.createdAtField);
}
if (config.lastModifiedField != null) {
allFields.add(config.lastModifiedField);
}
if (config.geometryType != null) {
allFields.add(config.geometryType);
}
if (this.mageEvent != null) {
const formAttributes = this.configValue(config.fieldAttributes, this.mageEvent.name, this.mageEvent.id);
// Initialize active form active fields
for (const form of this.mageEvent.activeForms) {
this.initializeFields(form, allFields, formAttributes);
}
// Initialize active form archived fields
this.initializeArchivedFields(allFields, formAttributes);
// Initialize archived form fields
for (const form of this.mageEvent.archivedForms) {
this.initializeFields(form, allFields, formAttributes);
}
}
}
/**
* Initialize the form fields.
* @param {Form} form The form.
* @param {Set<string>} allFields Used to build and track all event fields.
* @param {any} formAttributes Form attributes override mappings
*/
initializeFields(form, allFields, formAttributes) {
const fields = new FormFields(form);
const fieldAttributes = this.configValue(formAttributes, fields.name, fields.id);
for (const field of form.fields) {
let attribute = field.title;
if (form.archived || !field.archived) {
attribute = this.initializeField(attribute, fields.name, allFields, fieldAttributes);
}
fields.set(field.title, attribute, field.archived);
}
this.formFields.set(form.id, fields);
}
/**
* Initialize the archived form fields.
* @param {Set<string>} allFields Used to build and track all event fields.
* @param {any} formAttributes Form attributes override mappings
*/
initializeArchivedFields(allFields, formAttributes) {
for (const fields of this.formFields.values()) {
const fieldAttributes = this.configValue(formAttributes, fields.name, fields.id);
for (const field of fields.archivedFields) {
const attribute = this.initializeField(field, fields.name, allFields, fieldAttributes);
fields.set(field, attribute);
}
}
}
/**
* Retrieve a config value by name or id.
* @param {any} config The configuration.
* @param {string} name Configuration name.
* @param {number} id Configuration id.
* @returns {any} configuration value
*/
configValue(config, name, id) {
let value = null;
if (config != null) {
value = config[name];
if (!value) {
value = config[id];
}
}
return value;
}
/**
* Initialize the form field.
* @param {string} field The field.
* @param {string} formName The form name.
* @param {Set<string>} allFields Used to build and track all event fields.
* @param {any} fieldAttributes Field attributes override mappings
* @returns {string} attribute name
*/
initializeField(field, formName, allFields, fieldAttributes) {
let attribute = null;
if (fieldAttributes != null) {
attribute = fieldAttributes[field];
}
if (attribute == null) {
attribute = field;
if (allFields.has(attribute)) {
attribute = formName + '_' + attribute;
}
}
allFields.add(attribute);
return attribute;
}
/**
* Get the form fields for the form id.
* @param {number} id The form id.
* @returns {FormFields | undefined} The form fields.
*/
get(id) {
return this.formFields.get(id);
}
}
exports.EventTransform = EventTransform;
/**
* Mapping between form field titles and arc attributes.
*/
class FormFields {
/**
* Creates a new instance of FormFields
* @param {Form} form - The form.
*/
constructor(form) {
/**
* Form field mapping between titles and arc atrribute fields.
*/
this.fields = new Map();
/**
* Archived form fields
*/
this.archivedFields = new Set();
this.name = form.name;
this.id = form.id;
this.archived = form.archived;
}
/**
* Set the form field title to an arc attribute.
* @param {string} title The form field title.
* @param {string} attribute The arc attribute.
* @param {boolean} [archived] Archived field flag.
*/
set(title, attribute, archived) {
this.fields.set(title, attribute);
if (archived) {
this.archivedFields.add(title);
}
}
/**
* Get the arc attribute for the form field title.
* @param {string} title The form field title.
* @returns {string | undefined} The arc attribute.
*/
get(title) {
return this.fields.get(title);
}
/**
* Is the form field archived.
* @param {string} title The form field title.
* @returns {boolean} True if archived.
*/
isArchived(title) {
return this.fields.has(title);
}
}
exports.FormFields = FormFields;
//# sourceMappingURL=EventTransform.js.map