All files / src/models transition.model.ts

85.32% Statements 93/109
48.15% Branches 13/27
100% Functions 17/17
85.05% Lines 91/107

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                                    14x 14x 14x 14x 14x 14x 14x     14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x     14x 160x 160x 160x 160x               160x 160x 160x 160x   14x   14x 14x   125x 125x 125x 125x   125x 125x     125x     14x 125x 10x 10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x               10x       14x 10x 160x               14x     1x   1x 1x                                                     1x     14x 1x 1x     1x               1x   1x     1x     14x 62x     14x 2x     14x 112x     14x 84x     14x 96x     14x 229x   14x  
/*
    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 { ExpressionModel } from './expression.model';
import { FieldAction } from './field-action.model';
 
export enum TransitionMode {
  ONE_TO_ONE = 'ONE_TO_ONE',
  ONE_TO_MANY = 'ONE_TO_MANY',
  ENUM = 'ENUM',
  MANY_TO_ONE = 'MANY_TO_ONE',
  FOR_EACH = 'FOR_EACH',
  EXPRESSION = 'EXPRESSION',
}
 
export enum TransitionDelimiter {
  NONE,
  AMPERSAND,
  AT_SIGN,
  BACKSLASH,
  COLON,
  COMMA,
  DASH,
  EQUAL,
  HASH,
  PERIOD,
  PIPE,
  SEMICOLON,
  SLASH,
  SPACE,
  UNDERSCORE,
  USER_DEFINED,
}
 
export class TransitionDelimiterModel {
  delimiter: TransitionDelimiter = TransitionDelimiter.SPACE;
  serializedValue: string | null = null;
  prettyName: string | null = null;
  actualDelimiter = '';
 
  constructor(
    delimiter: TransitionDelimiter,
    serializedValue: string | null,
    prettyName: string,
    actualDelimiter: string
  ) {
    this.delimiter = delimiter;
    this.serializedValue = serializedValue;
    this.prettyName = prettyName;
    this.actualDelimiter = actualDelimiter;
  }
}
 
export class TransitionModel {
  static delimiterModels: TransitionDelimiterModel[] = [];
 
  mode: TransitionMode = TransitionMode.ONE_TO_ONE;
  delimiter: TransitionDelimiter = TransitionDelimiter.SPACE;
  userDelimiter = '';
  lookupTableName: string | null = null;
  expression!: ExpressionModel;
  enableExpression = false;
  transitionFieldAction: FieldAction | null = null;
 
  constructor() {
    TransitionModel.initialize();
  }
 
  static initialize() {
    if (TransitionModel.delimiterModels.length === 0) {
      const models: TransitionDelimiterModel[] = [];
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.NONE,
          null,
          '[None]',
          ''
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.AMPERSAND,
          'Ampersand',
          'Ampersand [&]',
          '&'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.AT_SIGN,
          'AtSign',
          'At Sign [@]',
          '@'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.BACKSLASH,
          'Backslash',
          'Backslash [\\]',
          '\\'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.COLON,
          'Colon',
          'Colon [:]',
          ':'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.COMMA,
          'Comma',
          'Comma [,]',
          ','
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.DASH,
          'Dash',
          'Dash [-]',
          '-'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.EQUAL,
          'Equal',
          'Equal [=]',
          '='
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.HASH,
          'Hash',
          'Hash [#]',
          '#'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.PERIOD,
          'Period',
          'Period [.]',
          '.'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.PIPE,
          'Pipe',
          'Pipe [|]',
          '|'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.SEMICOLON,
          'Semicolon',
          'Semicolon [;]',
          ';'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.SLASH,
          'Slash',
          'Slash [/]',
          '/'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.SPACE,
          'Space',
          'Space [ ]',
          ' '
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.UNDERSCORE,
          'Underscore',
          'Underscore [_]',
          '_'
        )
      );
      models.push(
        new TransitionDelimiterModel(
          TransitionDelimiter.USER_DEFINED,
          'User defined',
          'User defined',
          '\0'
        )
      );
      TransitionModel.delimiterModels = models;
    }
  }
 
  static delimiterToModel(delimiter: string): TransitionDelimiterModel {
    return TransitionModel.delimiterModels.filter(
      (model) => model.actualDelimiter === delimiter
    )[0];
  }
 
  /**
   * Translate a mapping mode number into a string.
   * @param mode
   */
  static getMappingModeName(mode: TransitionMode): string {
    let actionName: string;
 
    switch (mode) {
      case TransitionMode.ONE_TO_ONE: {
        actionName = 'One to One';
        break;
      }
      case TransitionMode.MANY_TO_ONE: {
        actionName = 'Many to One';
        break;
      }
      case TransitionMode.ONE_TO_MANY: {
        actionName = 'One to Many';
        break;
      }
      case TransitionMode.ENUM: {
        actionName = 'ENUM';
        break;
      }
      case TransitionMode.FOR_EACH: {
        actionName = 'For Each';
        break;
      }
      case TransitionMode.EXPRESSION: {
        actionName = 'Expression';
        break;
      }
      default: {
        actionName = '';
        break;
      }
    }
    return actionName;
  }
 
  getPrettyName() {
    let transitionFieldActionName = '';
    Iif (this.transitionFieldAction) {
      transitionFieldActionName = ' (' + this.transitionFieldAction.name + ')';
    }
    Iif (
      this.mode === TransitionMode.ONE_TO_MANY ||
      this.mode === TransitionMode.MANY_TO_ONE
    ) {
      return (
        TransitionModel.getMappingModeName(this.mode) +
        transitionFieldActionName
      );
    } else Iif (this.mode === TransitionMode.ENUM) {
      return 'Enum (table: ' + this.lookupTableName + ')';
    } else Iif (this.mode === TransitionMode.EXPRESSION) {
      return 'Expression (' + this.expression?.toText() + ')';
    }
    return TransitionModel.getMappingModeName(this.mode);
  }
 
  isOneToManyMode(): boolean {
    return this.mode === TransitionMode.ONE_TO_MANY;
  }
 
  isOneToOneMode(): boolean {
    return this.mode === TransitionMode.ONE_TO_ONE;
  }
 
  isManyToOneMode(): boolean {
    return this.mode === TransitionMode.MANY_TO_ONE;
  }
 
  isForEachMode(): boolean {
    return this.mode === TransitionMode.FOR_EACH;
  }
 
  isEnumerationMode(): boolean {
    return this.mode === TransitionMode.ENUM;
  }
 
  isExpressionMode(): boolean {
    return this.mode === TransitionMode.EXPRESSION;
  }
}