All files / src/models mapping.model.ts

64.53% Statements 171/265
56.05% Branches 88/157
72.5% Functions 29/40
66.07% Lines 148/224

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491                              12x   12x 12x 12x   12x 12x   12x                       341x   12x                                           12x 350x     12x 1x     12x     12x   12x       124x 124x 124x 124x     124x 124x     12x 4x 4x 2x     2x     12x 35x 35x       19x                 12x 169x 169x 29x 29x         169x 169x 169x 120x   49x   169x               12x 1x 1x 1x                   12x                       12x           12x 7x     12x 205x     12x 134x     12x 5x     5x     5x       5x     12x 68x       68x 61x 39x     29x                         12x         50x     50x 50x 101x 44x 10x   34x 34x       6x     12x 8x     8x 8x     8x     12x 163x     163x           12x 6x 6x     6x 6x 6x   6x               12x 5x   5x     5x         5x 5x 5x   5x               12x                           12x 40x     40x 80x 13x         13x     27x                       12x 6x     6x 12x                 6x 6x 5x 5x   1x                   12x                         12x 46x     12x 1902x     12x               12x 138x 138x 120x 120x     138x     12x                   12x                   12x                 12x 50x     12x 19x     12x 42x     12x                               12x   19x   19x 8x   22x 11x 9x     2x   2x                   11x   12x  
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import { FieldType, IField } from '../contracts/common';
 
import { CommonUtil } from '../utils/common-util';
import { ConfigModel } from './config.model';
import { Field } from './field.model';
import { FieldAction } from './field-action.model';
import { PaddingField } from './document-definition.model';
import { TransitionModel } from './transition.model';
 
export class MappedField {
  /**
   * The field properties read from mapping definition. This is a temporary object
   * when it's deserialized from mapping definition and once {@link field} is populated
   * this should not be used.
   * @todo Identify document field at once when it deserializes and remove this temporary
   * object.
   */
  mappingField: IField;
  /** The field object created from document field */
  field: Field | null;
  /** The field actions read from mapping and enriched through {@link FieldActionService} */
  actions: FieldAction[] = [];
 
  static sortMappedFieldsByPath(mappedFields: MappedField[]): MappedField[] {
    if (mappedFields == null || mappedFields.length === 0) {
      return [];
    }
    const fieldsByPath: { [key: string]: MappedField } = {};
    const fieldPaths: string[] = [];
    for (const mappedField of mappedFields) {
      if (mappedField == null || mappedField.field == null) {
        continue;
      }
      const path: string = mappedField.field.path;
      fieldsByPath[path] = mappedField;
      fieldPaths.push(path);
    }
    fieldPaths.sort();
    const result: MappedField[] = [];
    for (const name of fieldPaths) {
      result.push(fieldsByPath[name]);
    }
    return result;
  }
 
  isPadField(): boolean {
    return this.field instanceof PaddingField;
  }
 
  isSource(): boolean {
    return this.field ? this.field.isSource() : false;
  }
 
  removeAction(action: FieldAction): void {
    CommonUtil.removeItemFromArray(action, this.actions);
  }
}
 
export class MappingModel {
  cfg: ConfigModel;
  uuid: string;
 
  sourceFields: MappedField[] = [];
  targetFields: MappedField[] = [];
  referenceFields: MappedField[] = [];
  transition: TransitionModel = new TransitionModel();
 
  constructor() {
    this.uuid = 'mapping.' + Math.floor(Math.random() * 1000000 + 1).toString();
    this.cfg = ConfigModel.getConfig();
  }
 
  getFirstCollectionField(isSource: boolean): Field | null {
    for (const f of isSource ? this.sourceFields : this.targetFields) {
      if (f.field && f.field.isInCollection()) {
        return f.field;
      }
    }
    return null;
  }
 
  isLookupMode(): boolean {
    for (const f of this.sourceFields.concat(this.targetFields)) {
      Iif (f.field && f.field.enumeration) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * Add the specified field to this field mapping.
   *
   * @param field - field to add to the mapping
   * @param first - if true add the field to the beginning of the array, last otherwise.
   */
  addField(field: Field, first: boolean): MappedField {
    const mappedFields = this.getMappedFields(field.isSource());
    if (mappedFields.length === 1) {
      const mappedField: MappedField = mappedFields[0];
      Iif (!mappedField.field) {
        mappedField.field = field;
        return mappedField;
      }
    }
    const mappedField: MappedField = new MappedField();
    mappedField.field = field;
    if (first) {
      mappedFields.unshift(mappedField);
    } else {
      mappedFields.push(mappedField);
    }
    return mappedField;
  }
 
  /**
   * Remove the specified field from this field mapping.
   *
   * @param field
   */
  removeField(field: Field) {
    const mappedFields = this.getMappedFields(field.isSource());
    CommonUtil.removeItemFromArray(
      mappedFields.find((mf) => mf.field === field),
      mappedFields
    );
  }
 
  /**
   * Return the number of user-defined (non-padding) fields in this mapping.
   *
   * @param field
   */
  getUserFieldCount(field: Field): number {
    const mappedFields = this.getMappedFields(field.isSource());
    let userFieldCount = 0;
 
    for (const mappedField of mappedFields) {
      if (!mappedField.isPadField()) {
        userFieldCount++;
      }
    }
    return userFieldCount;
  }
 
  hasMappedField(isSource: boolean) {
    return isSource
      ? this.sourceFields.length > 0
      : this.targetFields.length > 0;
  }
 
  isEmpty() {
    return this.sourceFields.length === 0 && this.targetFields.length === 0;
  }
 
  isFullyMapped(): boolean {
    return this.sourceFields.length > 0 && this.targetFields.length > 0;
  }
 
  addMappedField(mappedField: MappedField, isSource: boolean): void {
    this.getMappedFields(isSource).push(mappedField);
  }
 
  removeMappedField(mappedField: MappedField): void {
    Iif (!mappedField || !mappedField.field) {
      return;
    }
    Iif (mappedField.field.isCollection) {
      this.removeReferenceField(mappedField);
    }
    CommonUtil.removeItemFromArray(
      mappedField,
      this.getMappedFields(mappedField.field!.isSource())
    );
    this.cfg.mappingService.notifyMappingUpdated();
  }
 
  getMappedFieldForField(field: Field): MappedField | null {
    Iif (!field || !field.isSource) {
      return null;
    }
 
    for (const mappedField of this.getMappedFields(field.isSource())) {
      if (mappedField.field === field) {
        return mappedField;
      }
    }
    return null;
  }
 
  /**
   * Return the MappedField associated with the specified field path and panel. The
   * document ID is optional identifier parameters used to distinguish the fields
   * with the same path in a different document. The first match will be returned
   * if not specified.
   *
   * @param fieldPath
   * @param isSource
   * @param identifier
   */
  getMappedFieldByPath(
    fieldPath: string,
    isSource: boolean,
    docId?: string
  ): MappedField | null {
    Iif (!fieldPath) {
      return null;
    }
    const mappedFields = this.getMappedFields(isSource);
    for (let i = 0; i < mappedFields.length; i++) {
      if (mappedFields[i].field?.path === fieldPath) {
        if (!docId) {
          return mappedFields[i];
        }
        Eif (docId && mappedFields[i].field?.docDef.id === docId) {
          return mappedFields[i];
        }
      }
    }
    return null;
  }
 
  getMappedFieldForIndex(index: string, isSource: boolean): MappedField | null {
    Iif (!index || index.length === 0) {
      return null;
    }
    const mappedFields = this.getMappedFields(isSource);
    Iif (+index - 1 > mappedFields.length - 1) {
      return null;
    }
    return mappedFields[+index - 1];
  }
 
  getIndexForMappedField(mappedField: MappedField): number | null {
    Iif (!mappedField || !mappedField.field) {
      return null;
    }
    return (
      this.getMappedFields(mappedField.field.isSource()).indexOf(mappedField) +
      1
    );
  }
 
  getField(docId: string, fieldPath: string): Field | null {
    let docDef = this.cfg.getDocForIdentifier(docId, true);
    Iif (!docDef) {
      docDef = this.cfg.getDocForIdentifier(docId, false);
    }
    let field: Field | null = null;
    Eif (docDef) {
      field = Field.getField(fieldPath, docDef.getAllFields());
    }
    return field;
  }
 
  /**
   * Create a reference field in the reference fields array for this mapping.
   *
   * @param field
   */
  createReferenceField(field: Field): MappedField | null {
    let mappedField: MappedField | null = null;
 
    Iif (!field) {
      return null;
    }
    Eif (
      !field.documentField?.status ||
      field.documentField?.status === 'SUPPORTED' ||
      field.documentField?.status === 'CACHED'
    ) {
      mappedField = new MappedField();
      mappedField.field = field;
      this.referenceFields.push(mappedField);
    }
    return mappedField;
  }
 
  /**
   * Remove the specified reference field from the reference fields array.
   *
   * @param field
   */
  removeReferenceField(mappedField: MappedField) {
    if (!mappedField) {
      return;
    }
    CommonUtil.removeItemFromArray(mappedField, this.referenceFields);
  }
 
  /**
   * Return true if a reference field exists in this mapping with the specified
   * document ID and field path, false otherwise.
   *
   * @param docId
   * @param fieldPath
   */
  referenceFieldExists(docId: string, fieldPath: string): boolean {
    Iif (!docId || !fieldPath) {
      return false;
    }
    const referenceFields = this.getReferenceMappedFields();
    for (let referenceField of referenceFields) {
      Eif (
        referenceField.field &&
        referenceField.field.docDef.id === docId &&
        referenceField.field.path === fieldPath
      ) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * A reference field is a complex field which is referenced in a conditional
   * expression but does not exist as an explicit part of the mapping.  It is
   * typically used as a parameter to conditional functions/ constructs. If the
   * field already exists return it otherwise create it.
   *
   * @param docId
   * @param fieldPath
   */
  getReferenceField(docId: string, fieldPath: string): MappedField | null {
    Iif (!docId || !fieldPath) {
      return null;
    }
    const referenceFields = this.getReferenceMappedFields();
    for (let referenceField of referenceFields) {
      if (
        referenceField.field &&
        referenceField.field.docDef.id === docId &&
        referenceField.field.path === fieldPath
      ) {
        return referenceField;
      }
    }
    const field = this.getField(docId, fieldPath);
    if (field?.type === FieldType.COMPLEX) {
      this.transition.expression.hasComplexField = true;
      return this.createReferenceField(field);
    }
    return null;
  }
 
  /**
   * Return an array of user mapped fields for the specified panel in this
   * field pair instance.  No data-mapper generated padding fields will be
   * included.
   *
   * @param isSource - true source panel, false target panel
   */
  getUserMappedFields(isSource: boolean): MappedField[] {
    const workingFields = isSource ? this.sourceFields : this.targetFields;
    const resultFields: MappedField[] = [new MappedField()];
 
    for (const mappedField of workingFields) {
      if (!mappedField.isPadField()) {
        resultFields.push(mappedField);
      }
    }
    resultFields.shift();
    return resultFields;
  }
 
  getReferenceMappedFields(): MappedField[] {
    return this.referenceFields;
  }
 
  getMappedFields(isSource: boolean): MappedField[] {
    return isSource ? this.sourceFields : this.targetFields;
  }
 
  getLastMappedField(isSource: boolean): MappedField | null {
    const fields: MappedField[] = this.getMappedFields(isSource);
    if (fields != null && fields.length > 0) {
      return fields[fields.length - 1];
    }
    return null;
  }
 
  getFields(isSource: boolean): Field[] {
    const fields: Field[] = [];
    for (const mappedField of this.getMappedFields(isSource)) {
      Eif (mappedField.field != null) {
        fields.push(mappedField.field);
      }
    }
    return fields;
  }
 
  getFieldNames(isSource: boolean): string[] {
    const fields: Field[] = this.getFields(isSource);
    Field.alphabetizeFields(fields);
    const names: string[] = [];
    for (const field of fields) {
      names.push(field.name);
    }
    return names;
  }
 
  getFieldPaths(isSource: boolean): string[] {
    const fields: Field[] = this.getFields(isSource);
    Field.alphabetizeFields(fields);
    const paths: string[] = [];
    for (const field of fields) {
      paths.push(field.path);
    }
    return paths;
  }
 
  hasFieldActions(): boolean {
    for (const mappedField of this.getAllMappedFields()) {
      if (mappedField.actions.length > 0) {
        return true;
      }
    }
    return false;
  }
 
  getAllFields(): Field[] {
    return this.getFields(true).concat(this.getFields(false));
  }
 
  getAllMappedFields(): MappedField[] {
    return this.getMappedFields(true).concat(this.getMappedFields(false));
  }
 
  isFieldMapped(field: Field): boolean {
    return this.getMappedFieldForField(field) != null;
  }
 
  hasTransformation(): boolean {
    const mappedFields: MappedField[] = this.getAllMappedFields();
    for (const mappedField of mappedFields) {
      if (mappedField.actions.length > 0) {
        return true;
      }
    }
    return false;
  }
 
  /**
   * Walk all target field mappings and return one of corresponding source field name
   * if the specified field is already the target of a previous mapping, null otherwise.
   *
   * @param field
   */
  public getMappedTarget(field: Field): string | null {
    // TODO: check this non null operator
    const mappings: MappingModel[] = this.cfg.mappings!.mappings;
 
    if (field.isSource()) {
      return null;
    }
    for (const m of mappings) {
      if (m.targetFields.length === 0) {
        continue;
      }
 
      for (const mappedOutputField of m.targetFields) {
        // TODO: check this non null operator
        Iif (
          mappedOutputField.field?.docDef === field.docDef &&
          mappedOutputField.field!.path === field.path
        ) {
          if (m.isFieldMapped(field)) {
            return m.sourceFields[0]?.field!.name;
          }
        }
      }
    }
    return null;
  }
}