All files / src/models field.model.ts

70.06% Statements 117/167
42.86% Branches 30/70
64% Functions 16/25
68.97% Lines 100/145

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                              16x               16x     16x   16x 16x                     1424x   1424x 1424x 1424x 1424x   1424x 1424x 1424x   1424x 1424x 1424x 1424x 1424x 1424x 1424x       16x 492x     492x 370x   122x 122x       122x           16x 52x 492x 492x   52x     16x               16x   9x     16x 936x 936x 2629x 2629x   2629x 90x   2539x 2539x   936x 936x 2539x 2539x     2539x 2539x 165x           1424x 1424x           16x                         16x             16x             16x       16x 2384x 4x   2380x 218x   2162x     16x 219x 219x     219x 219x 219x   219x 219x 183x     219x     16x                             16x 335x 335x 426x 96x   330x       16x 294x     16x                       16x 691x     16x       16x 10x 10x 10x                           10x   10x           10x     10x     16x       16x 317x         16x 584x       16x  
/*
    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 {
  DocumentType,
  FIELD_PATH_SEPARATOR,
  FieldType,
  IField,
} from '../contracts/common';
import { DocumentDefinition } from './document-definition.model';
 
export class EnumValue {
  name: string;
  ordinal: number;
}
 
export class Field {
  private static uuidCounter = 0;
 
  name: string;
  classIdentifier: string;
  displayName: string;
  path: string;
  type: FieldType;
  scope: string | undefined;
  value: string;
  column: number;
  // The field properties read from document inspection result.
  documentField: IField = { jsonType: '' };
  parentField: Field;
  partOfMapping = false;
  partOfTransformation = false;
  visibleInCurrentDocumentSearch = true;
  enumeration = false;
  enumIndexValue: number;
  enumValues: EnumValue[] = [];
  children: Field[] = [];
  fieldDepth = 0;
  uuid: string;
  collapsed = true;
  hasUnmappedChildren = false;
  isCollection = false;
  isArray = false;
  isAttribute = false;
  isPrimitive = false;
  userCreated = false;
  docDef: DocumentDefinition;
  namespaceAlias: string | null;
 
  static fieldHasUnmappedChild(field: Field): boolean {
    Iif (field == null) {
      return false;
    }
    if (field.isTerminal()) {
      return field.partOfMapping === false;
    }
    for (const childField of field.children) {
      Eif (
        childField.hasUnmappedChildren ||
        Field.fieldHasUnmappedChild(childField)
      ) {
        return true;
      }
    }
    return false;
  }
 
  static getFieldPaths(fields: Field[]): string[] {
    const paths: string[] = [];
    for (const field of fields) {
      paths.push(field.path);
    }
    return paths;
  }
 
  static getFieldNames(fields: Field[]): string[] {
    const paths: string[] = [];
    for (const field of fields) {
      paths.push(field.name);
    }
    return paths;
  }
 
  static getField(fieldPath: string, fields: Field[]): Field {
    // TODO: check this non null operator
    return fields.find((field) => fieldPath === field.path)!;
  }
 
  static alphabetizeFields(fields: Field[]): void {
    const fieldsByPath: { [key: string]: Field } = {};
    const fieldPaths: string[] = [];
    for (const field of fields) {
      let fieldKey = field.path;
      // Discard duplicate field keys, field names are repeatable.
      if (fieldsByPath[fieldKey] != null) {
        continue;
      }
      fieldsByPath[fieldKey] = field;
      fieldPaths.push(fieldKey);
    }
    fieldPaths.sort();
    fields.length = 0;
    for (const path of fieldPaths) {
      fields.push(fieldsByPath[path]);
    }
 
    for (const field of fields) {
      if (field.children && field.children.length) {
        this.alphabetizeFields(field.children);
      }
    }
  }
 
  constructor() {
    this.uuid = Field.uuidCounter.toString();
    Field.uuidCounter++;
  }
 
  /**
   * Expand all fields above the current field.
   */
  expandToRoot() {
    let parent: Field = this;
    while (parent != null) {
      parent.collapsed = false;
      if (parent.isPropertyOrConstant()) {
        if (parent.docDef) {
          parent.docDef.showFields = true;
        }
      }
      parent = parent.parentField;
    }
  }
 
  getNameWithNamespace(): string {
    if (!this.namespaceAlias) {
      return this.name;
    }
    return this.namespaceAlias + ':' + this.name;
  }
 
  isParentField(): boolean {
    if (this.isCollection && !this.isPrimitive) {
      return true;
    }
    return this.type === 'COMPLEX';
  }
 
  isStringField(): boolean {
    return this.type === 'STRING';
  }
 
  isTerminal(): boolean {
    if (this.enumeration) {
      return true;
    }
    if (this.isCollection && !this.isPrimitive) {
      return false;
    }
    return this.type !== 'COMPLEX';
  }
 
  copy(): Field {
    const copy: Field = new Field();
    Object.assign(copy, this);
 
    // make these pointers to the same object, not copies
    copy.documentField = this.documentField;
    copy.parentField = this.parentField;
    copy.docDef = this.docDef;
 
    copy.children = [];
    for (const childField of this.children) {
      copy.children.push(childField.copy());
    }
    // console.log("Copied: " + this.name, { "src": this, "target": copy });
    return copy;
  }
 
  copyFrom(that: Field): void {
    Object.assign(this, that);
 
    // make these pointers to the same object, not copies
    this.documentField = that.documentField;
    this.parentField = that.parentField;
    this.docDef = that.docDef;
 
    this.children = [];
    for (const childField of that.children) {
      this.children.push(childField.copy());
    }
  }
 
  // @ts-ignore
  getCollectionParentField(): Field {
    let parent: Field = this;
    while (parent != null) {
      if (parent.isCollection) {
        return parent;
      }
      parent = parent.parentField;
    }
  }
 
  isInCollection(): boolean {
    return this.getCollectionParentField() != null;
  }
 
  getCollectionCount(): number {
    let count = 0;
    let field: Field = this;
    while (field != null) {
      if (field.isCollection) {
        count++;
      }
      field = field.parentField;
    }
    return count;
  }
 
  isSource(): boolean {
    return this.docDef != null && this.docDef.isSource;
  }
 
  getCollectionType(): string | null {
    return this.isCollection ? (this.isArray ? 'ARRAY' : 'LIST') : null;
  }
 
  getFieldLabel(showTypes: boolean, includePath: boolean): string {
    let fieldPath = '';
    Eif (includePath) {
      fieldPath = this.path;
    } else {
      const pathComps = this.path.split(FIELD_PATH_SEPARATOR);
      // Check for a leaf path attribute field starting with '@'
      if (
        this.isAttribute &&
        pathComps.length > 0 &&
        pathComps[pathComps.length - 1].startsWith('@')
      ) {
        fieldPath = this.path.split('@')[1];
      } else {
        fieldPath = this.getNameWithNamespace();
      }
    }
    Iif (showTypes && this.type && !this.isPropertyOrConstant()) {
      fieldPath += ' (' + this.type + ')';
    } else Iif (this.isProperty() && this.value != null) {
      fieldPath += ' = ' + this.value;
      if (showTypes && this.type) {
        fieldPath += ' (' + this.type + ')';
      }
    }
    Iif (!fieldPath && !this.parentField) {
      fieldPath = '< Document Root >';
    }
    return fieldPath;
  }
 
  isPropertyOrConstant(): boolean {
    return this.docDef == null ? false : this.docDef.isPropertyOrConstant;
  }
 
  isProperty(): boolean {
    return this.docDef == null
      ? false
      : this.docDef.type === DocumentType.PROPERTY;
  }
 
  isConstant(): boolean {
    return this.docDef == null
      ? false
      : this.docDef.type === DocumentType.CONSTANT;
  }
}