All files / src/utils lookup-table-util.ts

64% Statements 64/100
33.87% Branches 21/62
77.78% Functions 7/9
59.52% Lines 50/84

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                              7x 7x           7x   7x         7x       7x         7x 7x       4x               4x                                                           7x 9x 1x     1x       1x 1x 1x 1x 1x     1x 1x 1x 1x         9x 3x       7x         1x 1x       7x                           7x       1x     1x 1x 1x 3x 3x     1x     1x         1x 1x 3x 3x 3x 3x 3x       3x   3x   1x     7x                                                   7x  
/*
    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 { ErrorInfo, ErrorScope, ErrorType } from '../models/error.model';
import { LookupTable, LookupTableEntry } from '../models/lookup-table.model';
 
import { ConfigModel } from '../models/config.model';
import { Field } from '../models/field.model';
import { MappingDefinition } from '../models/mapping-definition.model';
import { MappingModel } from '../models/mapping.model';
import { TransitionMode } from '../models/transition.model';
 
const EnumerationUnspecified = '[ None ]';
 
/**
 * Lookup table structure.
 */
export class LookupTableData {
  sourceEnumValue: string;
  targetEnumValues: string[];
  selectedTargetEnumValue: string;
}
 
/**
 * Static lookup table utility methods.
 */
export class LookupTableUtil {
  static populateMappingLookupTable(
    mappingDefinition: MappingDefinition,
    m: MappingModel
  ): void {
    Eif (
      !(
        m.transition.mode === TransitionMode.ENUM &&
        !m.transition.lookupTableName &&
        m.getFields(true).length === 1 &&
        m.getFields(false).length === 1
      )
    ) {
      return;
    }
    let inputIdentifier: string | undefined;
    let outputIdentifier: string | undefined;
 
    const inputField: Field = m.getFields(true)[0];
    if (inputField) {
      inputIdentifier = inputField.name + '-' + inputField.docDef.id;
    }
    const outputField: Field = m.getFields(true)[0];
    if (outputField) {
      outputIdentifier = outputField.name + '-' + outputField.docDef.id;
    }
    if (inputIdentifier && outputIdentifier) {
      let table: LookupTable = mappingDefinition.getTableBySourceTarget(
        inputIdentifier,
        outputIdentifier
      );
      if (!table) {
        table = new LookupTable();
        table.sourceIdentifier = inputIdentifier;
        table.targetIdentifier = outputIdentifier;
        mappingDefinition.addTable(table);
        m.transition.lookupTableName = table.name;
      } else {
        m.transition.lookupTableName = table.name;
      }
    }
  }
 
  static updateLookupTables(mappingDefinition: MappingDefinition) {
    for (const t of mappingDefinition.getTables()) {
      Iif (t.sourceIdentifier && t.targetIdentifier) {
        continue;
      }
      const m: MappingModel = LookupTableUtil.getFirstMappingForLookupTable(
        mappingDefinition,
        t.name
      );
      Eif (m && m.transition.lookupTableName) {
        Eif (!t.sourceIdentifier) {
          const inputField: Field = m.getFields(true)[0];
          Eif (inputField) {
            t.sourceIdentifier = inputField.classIdentifier;
          }
        }
        Eif (!t.targetIdentifier) {
          const outputField: Field = m.getFields(false)[0];
          Eif (outputField) {
            t.targetIdentifier = outputField.classIdentifier;
          }
        }
      }
    }
    for (const m of mappingDefinition.mappings) {
      LookupTableUtil.populateMappingLookupTable(mappingDefinition, m);
    }
  }
 
  private static getFirstMappingForLookupTable(
    mappingDefinition: MappingDefinition,
    lookupTableName: string
  ): MappingModel {
    // TODO: check this non null operator
    return mappingDefinition.mappings.find(
      (m) => m.transition.lookupTableName === lookupTableName
    )!;
  }
 
  private static errorNoTable(cfg: ConfigModel, mapping: MappingModel) {
    cfg.errorService.addError(
      new ErrorInfo({
        message:
          'Could not find enumeration lookup table ' +
          mapping.transition?.lookupTableName +
          ' for mapping.',
        scope: ErrorScope.MAPPING,
        type: ErrorType.INTERNAL,
        mapping: mapping,
      })
    );
  }
 
  static getEnumerationValues(
    cfg: ConfigModel,
    mapping: MappingModel
  ): LookupTableData[] {
    Iif (!cfg || !cfg.mappings || !mapping) {
      return [];
    }
    const targetField: Field = mapping.getFields(false)[0];
    const targetValues: string[] = [];
    targetValues.push('[ None ]');
    for (const e of targetField.enumValues) {
      targetValues.push(e.name);
    }
 
    const table = cfg.mappings.getTableByName(
      mapping.transition?.lookupTableName!
    );
    Iif (!table) {
      LookupTableUtil.errorNoTable(cfg, mapping);
      return [];
    }
 
    const enumVals: LookupTableData[] = [];
    const sourceField: Field = mapping.getFields(true)[0];
    for (const sourceEnum of sourceField.enumValues) {
      const tableData: LookupTableData = new LookupTableData();
      tableData.sourceEnumValue = sourceEnum.name;
      tableData.targetEnumValues = targetValues;
      const selected: LookupTableEntry | null = table.getEntryForSource(
        tableData.sourceEnumValue,
        false
      );
      tableData.selectedTargetEnumValue =
        selected == null ? EnumerationUnspecified : selected.targetValue;
      enumVals.push(tableData);
    }
    return enumVals;
  }
 
  static updateEnumerationValues(
    cfg: ConfigModel,
    mapping: MappingModel,
    enumerationValues: LookupTableData[]
  ) {
    if (!cfg || !cfg.mappings || !mapping) {
      return;
    }
    const table = cfg.mappings.getTableByName(
      mapping.transition?.lookupTableName!
    );
    if (!table) {
      LookupTableUtil.errorNoTable(cfg, mapping);
      return;
    }
    table.lookupEntry = [];
    for (const enumValue of enumerationValues) {
      if (enumValue.selectedTargetEnumValue === EnumerationUnspecified) {
        continue;
      }
      const lte: LookupTableEntry = new LookupTableEntry();
      lte.sourceValue = enumValue.sourceEnumValue;
      lte.targetValue = enumValue.selectedTargetEnumValue;
      table.lookupEntry.push(lte);
    }
  }
}