All files / src/models config.model.ts

80.69% Statements 117/145
60% Branches 12/20
72.73% Functions 16/22
85.61% Lines 113/132

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                              13x         13x 13x                       13x 106x 106x 106x 106x 106x                         106x             106x 106x 106x 106x 106x 106x   106x     106x   106x 13x   13x           26x               13x           13x 13x   106x     106x 106x 106x 106x 106x 106x 106x 106x 106x                     106x 106x 106x 106x 106x 106x 106x 106x   106x         106x     13x 323x     13x 92x     13x 266x 266x 266x 266x         266x 266x 266x     13x 133x 133x 133x 133x 133x 133x         133x 133x 133x     13x 2x             13x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x   25x   25x 5x 5x                     20x     25x 11x   14x   25x     13x               13x 289x     13x 289x 289x               13x 12x 12x 12x 12x 12x 12x     13x                 13x         1061x     13x           13x 149x           13x               13x  
/*
    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 {
  CollectionType,
  DocumentType,
  InspectionType,
} from '../contracts/common';
import { DocumentDefaultName } from '../common/config.types';
import { DocumentDefinition } from './document-definition.model';
import { DocumentManagementService } from '../services/document-management.service';
import { ErrorHandlerService } from '../services/error-handler.service';
import { FieldActionService } from '../services/field-action.service';
import { FileManagementService } from '../services/file-management.service';
import { InitializationService } from '../services/initialization.service';
import { Logger } from 'loglevel';
import { MappingDefinition } from './mapping-definition.model';
import { MappingExpressionService } from '../services/mapping-expression.service';
import { MappingManagementService } from '../services/mapping-management.service';
import { MappingPreviewService } from '../services/mapping-preview.service';
 
export class DataMapperInitializationModel {
  dataMapperVersion = '';
  initialized = false;
  loadingStatus = 'Loading.';
  admHttpTimeout = 30000; // 30 seconds
  initializationErrorOccurred = false;
 
  baseJavaInspectionServiceUrl?: string;
  baseXMLInspectionServiceUrl?: string;
  baseJSONInspectionServiceUrl?: string;
  baseCSVInspectionServiceUrl?: string;
  baseMappingServiceUrl?: string;
 
  xsrfHeaderName?: string;
  xsrfCookieName?: string;
  xsrfDefaultTokenValue?: string;
 
  /* class path fetching configuration */
  classPathFetchTimeoutInMilliseconds = 30000;
  // if classPath is specified, maven call to resolve pom will be skipped
  pomPayload?: string;
 
  classPath?: string;
 
  /* inspection service filtering flags */
  fieldNameExclusions: string[] = [];
  classNameExclusions: string[] = [];
  disablePrivateOnlyFields = false;
  disableProtectedOnlyFields = false;
  disablePublicOnlyFields = false;
  disablePublicGetterSetterFields = false;
 
  disableMappingPreviewMode = false;
 
  /* enable the navigation bar and import/export */
  disableNavbar = true;
 
  mappingInitialized = false;
}
 
export class DocumentInitializationModel {
  id: string;
  type: DocumentType;
  name: string;
  description: string;
  isSource: boolean;
  showFields = true;
  inspectionType: InspectionType;
  inspectionSource: string;
  inspectionParameters: { [key: string]: string };
  inspectionResult: string;
  selectedRoot: string;
  collectionType?: CollectionType;
  collectionClassName?: string;
}
 
/**
 * The central store of the AtlasMap UI core library. It is expected to be initialized through
 * {@link InitializationService}.
 */
export class ConfigModel {
  private static cfg: ConfigModel = new ConfigModel();
 
  initCfg: DataMapperInitializationModel = new DataMapperInitializationModel();
 
  /* current ui state config */
  showMappingDetailTray = false;
  showMappingTable = false;
  showNamespaceTable = false;
  showLinesAlways = true;
  showTypes = false;
  showMappedFields = true;
  showUnmappedFields = true;
  _showMappingPreview = false;
  currentDraggedField: any = null;
 
  documentService: DocumentManagementService;
  mappingService: MappingManagementService;
  errorService: ErrorHandlerService;
  initializationService: InitializationService;
  fieldActionService: FieldActionService;
  fileService: FileManagementService;
  previewService: MappingPreviewService;
  expressionService: MappingExpressionService;
 
  sourceDocs: DocumentDefinition[] = [];
  targetDocs: DocumentDefinition[] = [];
  sourcePropertyDoc: DocumentDefinition = new DocumentDefinition();
  targetPropertyDoc: DocumentDefinition = new DocumentDefinition();
  constantDoc: DocumentDefinition = new DocumentDefinition();
  mappingFiles: string[] = [];
  mappingDefinitionId = 0;
  mappings: MappingDefinition | null = null;
 
  preloadedMappingJson: string | null = null;
  preloadedFieldActionMetadata: any;
  logger?: Logger;
 
  constructor() {
    this.setConstantPropertyDocs();
  }
 
  static getConfig(): ConfigModel {
    return ConfigModel.cfg;
  }
 
  static setConfig(cfg: ConfigModel): void {
    ConfigModel.cfg = cfg;
  }
 
  initializePropertyDoc(propertyDoc: DocumentDefinition, isSource: boolean) {
    propertyDoc.clearFields();
    propertyDoc.type = DocumentType.PROPERTY;
    propertyDoc.name = DocumentDefaultName.PROPERTIES;
    propertyDoc.id =
      'DOC.' +
      propertyDoc.name +
      '.' +
      Math.floor(Math.random() * 1000000 + 1).toString();
    propertyDoc.isSource = isSource;
    propertyDoc.showFields = false;
    propertyDoc.isPropertyOrConstant = true;
  }
 
  setConstantPropertyDocs(): void {
    this.initializePropertyDoc(this.sourcePropertyDoc, true);
    this.initializePropertyDoc(this.targetPropertyDoc, false);
    this.constantDoc.clearFields();
    this.constantDoc.type = DocumentType.CONSTANT;
    this.constantDoc.name = DocumentDefaultName.CONSTANTS;
    this.constantDoc.id =
      'DOC.' +
      this.constantDoc.name +
      '.' +
      Math.floor(Math.random() * 1000000 + 1).toString();
    this.constantDoc.isSource = true;
    this.constantDoc.showFields = false;
    this.constantDoc.isPropertyOrConstant = true;
  }
 
  set showMappingPreview(show: boolean) {
    this._showMappingPreview = show;
  }
 
  get showMappingPreview(): boolean {
    return this._showMappingPreview;
  }
 
  addDocument(docInitModel: DocumentInitializationModel): DocumentDefinition {
    const docDef: DocumentDefinition = new DocumentDefinition();
    docDef.initModel = docInitModel;
    docDef.id = docInitModel.id;
    docDef.type = docInitModel.type;
    docDef.name = docInitModel.name;
    docDef.description = docInitModel.description;
    docDef.isSource = docInitModel.isSource;
    docDef.showFields = docInitModel.showFields;
    docDef.inspectionType = docInitModel.inspectionType;
    docDef.inspectionSource = docInitModel.inspectionSource;
    docDef.inspectionResult = docInitModel.inspectionResult;
    docDef.inspectionParameters = docInitModel.inspectionParameters;
    docDef.selectedRoot = docInitModel.selectedRoot;
 
    Iif (docDef.type === DocumentType.XSD) {
      docDef.uri = 'atlas:xml:' + docDef.id;
    } else if (docDef.type === DocumentType.JAVA) {
      docDef.uri = `atlas:java:${docDef.id}?className=${docDef.inspectionSource}`;
      Iif (
        docInitModel.collectionType &&
        docInitModel.collectionType !== CollectionType.NONE
      ) {
        docDef.uri += '&collectionType=' + docInitModel.collectionType;
        if (docInitModel.collectionClassName) {
          docDef.uri +=
            '&collectionClassName=' + docInitModel.collectionClassName;
        }
      }
    } else {
      docDef.uri = 'atlas:' + docDef.type.toLowerCase() + ':' + docDef.id;
    }
 
    if (docInitModel.isSource) {
      this.sourceDocs.push(docDef);
    } else {
      this.targetDocs.push(docDef);
    }
    return docDef;
  }
 
  addDocuments(docModels: DocumentInitializationModel[]): DocumentDefinition[] {
    const docDefs: DocumentDefinition[] = [];
    for (const docModel of docModels) {
      docDefs.push(this.addDocument(docModel));
    }
    return docDefs;
  }
 
  getDocsWithoutPropertyDoc(isSource: boolean): DocumentDefinition[] {
    return isSource ? [...this.sourceDocs] : [...this.targetDocs];
  }
 
  getDocs(isSource: boolean): DocumentDefinition[] {
    const docs: DocumentDefinition[] = this.getDocsWithoutPropertyDoc(isSource);
    return isSource
      ? [this.sourcePropertyDoc, this.constantDoc].concat(docs)
      : [this.targetPropertyDoc].concat(docs);
  }
 
  /**
   * Clear source/target/mapping documents from the model.  Reset constant and property document definitions.
   */
  clearDocs(): void {
    this.sourceDocs = [];
    this.targetDocs = [];
    this.sourcePropertyDoc.clearFields();
    this.targetPropertyDoc.clearFields();
    this.constantDoc.clearFields();
    this.mappingFiles = [];
  }
 
  hasJavaDocuments(): boolean {
    for (const doc of this.getAllDocs()) {
      if (doc.type === DocumentType.JAVA) {
        return true;
      }
    }
    return false;
  }
 
  getDocForIdentifier(
    documentId: string,
    isSource: boolean
  ): DocumentDefinition | null {
    // TODO: check this non null operator
    return this.getDocs(isSource).find((d) => d.id === documentId)!;
  }
 
  getFirstXmlDoc(isSource: boolean): DocumentDefinition {
    const docs: DocumentDefinition[] = this.getDocsWithoutPropertyDoc(isSource);
    // TODO: check this non null operator
    return docs.find((doc) => doc.type === DocumentType.XML)!;
  }
 
  getAllDocs(): DocumentDefinition[] {
    return [this.sourcePropertyDoc, this.constantDoc]
      .concat(this.sourceDocs)
      .concat(this.targetPropertyDoc)
      .concat(this.targetDocs);
  }
 
  documentsAreLoaded(): boolean {
    for (const doc of this.getAllDocs()) {
      if (!doc.initialized) {
        return false;
      }
    }
    return true;
  }
}