All files / src/models mapping-definition.model.ts

79.1% Statements 53/67
50% Branches 9/18
72.73% Functions 8/11
77.05% Lines 47/61

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                                10x         10x   10x 58x 58x 58x 58x 58x 58x   58x 58x 58x         58x     10x       10x 2x 2x 2x     10x 1x     10x               10x 93x 93x 4x     4x 4x   93x     10x                       10x 72x 72x 72x 71x   2x 2x 1x               10x 24x 30x 30x 9x     24x     10x 5x     5x   10x  
/*
    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 { CommonUtil } from '../utils/common-util';
import { DocumentDefinition } from './document-definition.model';
import { Field } from './field.model';
import { LookupTable } from './lookup-table.model';
import { MappingModel } from './mapping.model';
import { TransitionMode } from './transition.model';
 
export class MappingDefinition {
  name: string | null = null;
  version: string | null = null;
  mappings: MappingModel[] = [];
  activeMapping: MappingModel | null = null;
  parsedDocs: DocumentDefinition[] = [];
  templateText: string | null = null;
 
  private tables: LookupTable[] = [];
  private tablesBySourceTargetKey: { [key: string]: LookupTable } = {};
  private tablesByName: { [key: string]: LookupTable } = {};
 
  constructor() {
    // Mapping definition ID must be 0 until https://github.com/atlasmap/atlasmap/issues/1577
    // this.name = 'UI.' + Math.floor((Math.random() * 1000000) + 1).toString();
    this.name = 'UI.0';
  }
 
  templateExists(): boolean {
    return this.templateText != null && this.templateText !== '';
  }
 
  addTable(table: LookupTable): void {
    this.tablesBySourceTargetKey[table.getInputOutputKey()] = table;
    this.tablesByName[table.name] = table;
    this.tables.push(table);
  }
 
  getTableByName(name: string): LookupTable {
    return this.tablesByName[name];
  }
 
  getTableBySourceTarget(
    sourceIdentifier: string,
    targetIdentifier: string
  ): LookupTable {
    const key: string = sourceIdentifier + ':' + targetIdentifier;
    return this.tablesBySourceTargetKey[key];
  }
 
  getTables(): LookupTable[] {
    const tables: LookupTable[] = [];
    for (const key in this.tablesByName) {
      Iif (!this.tablesByName.hasOwnProperty(key)) {
        continue;
      }
      const table: LookupTable = this.tablesByName[key];
      tables.push(table);
    }
    return tables;
  }
 
  removeTableByName(name: string) {
    if (name) {
      const table = this.tablesByName[name];
      const iokey = table.getInputOutputKey();
      if (this.tablesByName[name]) {
        delete this.tables[this.tables.indexOf(table)];
        delete this.tablesByName[name];
        delete this.tablesBySourceTargetKey[iokey];
      }
    }
  }
 
  getAllMappings(includeActiveMapping: boolean): MappingModel[] {
    const mappings: MappingModel[] = [...this.mappings];
    Eif (includeActiveMapping) {
      if (this.activeMapping == null) {
        return mappings;
      }
      for (const mapping of mappings) {
        if (mapping === this.activeMapping) {
          return mappings;
        }
      }
      mappings.push(this.activeMapping);
    }
    return mappings;
  }
 
  findMappingsForField(field: Field): MappingModel[] {
    const mappingsForField: MappingModel[] = [];
    for (const m of this.mappings) {
      if (m.isFieldMapped(field)) {
        mappingsForField.push(m);
      }
    }
    return mappingsForField;
  }
 
  removeMapping(m: MappingModel): boolean {
    Iif (m.transition.mode === TransitionMode.ENUM) {
      this.removeTableByName(m.transition.lookupTableName!);
    }
    return CommonUtil.removeItemFromArray(m, this.mappings);
  }
}