All files / src/services initialization.service.ts

74.14% Statements 172/232
37.1% Branches 23/62
69.39% Functions 34/49
74.76% Lines 157/210

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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472                                7x           7x     7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x   7x         7x 92x   92x 92x     92x 92x                     92x 92x 92x 92x 92x 92x 92x 92x 92x 92x     7x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x     27x 27x 27x 27x 27x 27x 27x   27x                   27x 22x                     27x               27x           22x 22x 22x       5x 5x   5x 5x     18x         5x 5x 5x 4x 4x                 4x 4x             5x 5x   5x             23x 23x 23x 23x 23x       1x     1x                       7x 1x 1x 1x       1x     1x                         7x 15x               7x                   7x 5x 5x   1x   1x 1x 1x       2x 1x 1x 1x                                                                                     4x         7x                                                                                 7x                                                       7x 11x 11x 71x 71x 71x       11x       8x 4x       4x   8x 8x 8x 8x 52x 52x   8x 8x   8x       7x 22x 22x                 22x 22x 22x 22x     7x 8x 8x 8x     7x 36x 36x     7x 1x 1x 1x 1x 1x           7x 1x 1x 1x 1x         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 { DocumentType, IStringContainer } from '../contracts/common';
import {
  ErrorInfo,
  ErrorLevel,
  ErrorScope,
  ErrorType,
} from '../models/error.model';
import { Observable, Subject } from 'rxjs';
 
import { ADMDigest } from '../contracts/adm-digest';
import { CommonUtil } from '../utils/common-util';
import { ConfigModel } from '../models/config.model';
import { DocumentManagementService } from './document-management.service';
import { ErrorHandlerService } from './error-handler.service';
import { FieldActionService } from './field-action.service';
import { FileManagementService } from './file-management.service';
import { LookupTableUtil } from '../utils/lookup-table-util';
import { MappingDefinition } from '../models/mapping-definition.model';
import { MappingExpressionService } from './mapping-expression.service';
import { MappingManagementService } from './mapping-management.service';
import { MappingPreviewService } from './mapping-preview.service';
import { MappingSerializer } from '../utils/mapping-serializer';
import { MappingUtil } from '../utils/mapping-util';
import { first } from 'rxjs/operators';
import ky from 'ky';
import log from 'loglevel';
 
log.setDefaultLevel(log.levels.WARN);
 
/**
 * Initialize AtlasMap UI core library. It initializes {@link ConfigModel} and core services.
 */
export class InitializationService {
  cfg: ConfigModel = ConfigModel.getConfig();
 
  systemInitializedSource = new Subject<void>();
  systemInitialized$: Observable<void> =
    this.systemInitializedSource.asObservable();
 
  initializationStatusChangedSource = new Subject<void>();
  initializationStatusChanged$: Observable<void> =
    this.initializationStatusChangedSource.asObservable();
 
  private documentService: DocumentManagementService;
  private mappingService: MappingManagementService;
  private errorService: ErrorHandlerService;
  private fieldActionService: FieldActionService;
  private fileService: FileManagementService;
  private previewService: MappingPreviewService;
  private expressionService: MappingExpressionService;
 
  constructor(private api: typeof ky) {
    this.documentService = new DocumentManagementService(this.api);
    this.mappingService = new MappingManagementService(this.api);
    this.errorService = new ErrorHandlerService();
    this.fieldActionService = new FieldActionService(this.api);
    this.fileService = new FileManagementService(this.api);
    this.previewService = new MappingPreviewService(this.api);
    this.expressionService = new MappingExpressionService();
    this.resetConfig();
    this.documentService.initialize();
  }
 
  resetConfig(): void {
    this.cfg = new ConfigModel();
    this.cfg.documentService = this.documentService;
    this.cfg.documentService.cfg = this.cfg;
    this.cfg.mappingService = this.mappingService;
    this.cfg.mappingService.cfg = this.cfg;
    this.cfg.errorService = this.errorService;
    this.cfg.fieldActionService = this.fieldActionService;
    this.cfg.fieldActionService.cfg = this.cfg;
    this.cfg.fileService = this.fileService;
    this.cfg.fileService.cfg = this.cfg;
    this.cfg.previewService = this.previewService;
    this.cfg.previewService.cfg = this.cfg;
    this.cfg.expressionService = this.expressionService;
    this.cfg.expressionService.cfg = this.cfg;
    this.cfg.initializationService = this;
    this.cfg.logger = log.getLogger('config');
    ConfigModel.setConfig(this.cfg);
  }
 
  initialize(): Promise<boolean> {
    return new Promise<boolean>(async (resolve) => {
      this.cfg.setConstantPropertyDocs();
      this.cfg.errorService.resetAll();
      this.cfg.fieldActionService.isInitialized = false;
      this.cfg.initCfg.initialized = false;
      this.cfg.initCfg.mappingInitialized = false;
 
      Iif (this.cfg.mappingService == null) {
        this.cfg.errorService.addError(
          new ErrorInfo({
            message:
              'Mapping service is not configured, validation service will not be used.',
            level: ErrorLevel.WARN,
            scope: ErrorScope.APPLICATION,
            type: ErrorType.INTERNAL,
          })
        );
      } else if (this.cfg.initCfg.baseMappingServiceUrl == null) {
        this.cfg.errorService.addError(
          new ErrorInfo({
            message:
              'Mapping service URL is not configured, validation service will not be used.',
            level: ErrorLevel.WARN,
            scope: ErrorScope.APPLICATION,
            type: ErrorType.INTERNAL,
          })
        );
      }
 
      Iif (!this.cfg.fieldActionService) {
        this.handleError('FieldActionService is not configured');
        resolve(false);
        return;
      }
 
      // Verify the runtime service is out there.
      try {
        Iif (!(await this.runtimeServiceActive())) {
          this.handleError('The AtlasMap runtime service is not available.');
          resolve(false);
          return;
        }
      } catch (error) {
        this.handleError('The AtlasMap runtime service is not available.');
        resolve(false);
        return;
      }
 
      // load documents from initialization parameters in embedded mode
      this.updateLoadingStatus('Loading source/target documents.');
      const allDocs = this.cfg.getAllDocs();
      // assumption is that there will be at least one document present
      const lastDoc = allDocs[allDocs.length - 1];
      this.cfg.documentService
        .inspectDocuments()
        // inspectedDocuments notifies for all documents, wait till the last document
        .pipe(first((d) => d === lastDoc))
        .subscribe({
          next: () => {
            // updateStatus() will nullify this.cfg.preloadedMappingJson
            // let's store it for comparisson below
            const preloadedMappingJson = this.cfg.preloadedMappingJson;
            this.updateStatus();
            this.systemInitializedSource.pipe(first()).subscribe(() => {
              Eif (preloadedMappingJson) {
                const maybeUpdatedMappingJson = JSON.stringify(
                  MappingSerializer.serializeMappings(this.cfg)
                );
                // inspection might change the mapping, for example the preloaded
                // mapping could have a document URI that is no longer the same
                // after inspection, e.g. when parameters change on the provided
                // documents and differ from the parameters embedded in the
                // provided mapping; in that case we need to notify that mapping
                // has been changed
                Eif (preloadedMappingJson !== maybeUpdatedMappingJson) {
                  this.cfg.mappingService.notifyMappingUpdated();
                }
              }
            });
          },
        });
 
      this.initializeWithMappingDigest().finally(() => {
        this.updateStatus();
      });
      resolve(true);
    });
  }
 
  /**
   * Return true if the runtime service is available, false otherwise.
   */
  runtimeServiceActive(): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      const url: string = this.cfg.initCfg.baseMappingServiceUrl + 'ping';
      this.cfg.logger!.debug('Runtime Service Ping Request');
      this.api
        .get(url)
        .json<IStringContainer>()
        .then((body) => {
          this.cfg.logger!.debug(
            `Runtime Service Ping Response: ${body.String}`
          );
          resolve(body?.String === 'pong');
        })
        .catch((error: any) => {
          reject(error);
        });
    });
  }
 
  /**
   * Retrieve AtlasMap design time backend runtime version.
   * @returns
   */
  getRuntimeVersion(): Promise<string> {
    const url = this.cfg.initCfg.baseMappingServiceUrl + 'version';
    return new Promise<string>((resolve, reject) => {
      this.api
        .get(url)
        .json<IStringContainer>()
        .then((body) => {
          this.cfg.logger!.debug(
            `Runtime Service Version Response: ${body.String}`
          );
          resolve(body.String);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
 
  /**
   * Return the UI version as a string.
   *
   * @returns UI version
   */
  getUIVersion(): string {
    return this.cfg.initCfg.dataMapperVersion;
  }
 
  /**
   * Set the UI version.
   *
   * @param uiVersion - version to set
   */
  setUIVersion(uiVersion: string) {
    this.cfg.initCfg.dataMapperVersion = uiVersion;
  }
 
  /**
   * Initialize with the {@link ADMDigest} mapping digest from either an imported ADM archive
   * file or from the DM runtime digest file is presented to update the canvas.
   *
   * @param mappingDigest - {@link ADMDigest} mapping digest
   */
  private initializeWithMappingDigest(): Promise<boolean> {
    return new Promise<boolean>((resolve) => {
      this.cfg.fileService
        .getCurrentMappingDigest()
        .then(async (mappingDigest: ADMDigest | null) => {
          // If digest is null then no compressed mappings digest file is available on the server.
          if (I!mappingDigest) {
            Eif (this.cfg.mappings === null) {
              this.cfg.mappings = new MappingDefinition();
            }
 
            // load field actions - do this even with no documents so the default field actions are loaded.
            await this.cfg.fieldActionService.fetchFieldActions();
            this.updateStatus();
            resolve(true);
            return;
          }
 
          await this.addDocumentsFromMappingDigest(mappingDigest);
 
          if (!mappingDigest || !mappingDigest.exportMappings) {
            resolve(false);
            return;
          }
          // Reinitialize the model mappings.
          const digestMappingsName =
            MappingSerializer.deserializeAtlasMappingName(
              CommonUtil.objectize(mappingDigest.exportMappings.value)
            );
 
          // If the live UI mappings name does not match the UI mappings name extracted from the
          // catalog file then use the mappings from the catalog file. Otherwise use the live
          // UI file.
          this.cfg.fileService
            .findMappingFiles('UI')
            .then(async (files: string[]) => {
              await this.cfg.fileService.setMappingDigestToService(
                mappingDigest
              );
              await this.cfg.fieldActionService.fetchFieldActions();
              if (digestMappingsName !== files[0]) {
                await this.cfg.fileService.setMappingStringToService(
                  mappingDigest.exportMappings.value
                );
              }
              // load both default and custom field actions
              await this.cfg.fieldActionService.fetchFieldActions();
 
              // load mappings
              this.fetchMappings().then((value) => {
                resolve(value);
              });
            })
            .catch(() => {
              resolve(false);
            });
        })
        .catch(() => {
          resolve(false);
        });
    });
  }
 
  private addDocumentsFromMappingDigest(
    mappingDigest: ADMDigest
  ): Promise<boolean> {
    return new Promise<any>(async (resolve) => {
      this.cfg.errorService.resetAll();
 
      let fragIndex = 0;
 
      // Reinitialize the model documents.
      for (let metaFragment of mappingDigest.exportMeta) {
        const fragData = mappingDigest.exportBlockData[fragIndex].value;
        const docID = metaFragment.id ? metaFragment.id : metaFragment.name;
        const docType = metaFragment.dataSourceType
          ? (metaFragment.dataSourceType.toUpperCase() as DocumentType)
          : (metaFragment.documentType?.toUpperCase() as DocumentType);
        const isSource =
          typeof metaFragment.isSource === 'string'
            ? (metaFragment.isSource as string).toLowerCase() === 'true'
            : metaFragment.isSource;
        await this.cfg.documentService.addDocument(
          fragData,
          docID,
          metaFragment.name,
          docType,
          metaFragment.inspectionType,
          isSource,
          metaFragment.inspectionParameters
        );
        this.updateStatus();
        fragIndex++;
      }
      this.cfg.mappingService.notifyMappingUpdated();
      resolve(true);
    });
  }
 
  /**
   * Fetch mapping files and initialize user mappings in the canvas.
   *
   * @param mappingFiles
   */
  private fetchMappings(): Promise<boolean> {
    return new Promise<boolean>(async (resolve) => {
      if (this.cfg.mappings != null) {
        resolve(true);
        return;
      }
 
      this.cfg.mappings = new MappingDefinition();
      let mappingFiles = this.cfg.mappingFiles;
      if (!this.cfg.mappingFiles || this.cfg.mappingFiles.length === 0) {
        mappingFiles = await this.cfg.fileService.findMappingFiles('UI');
      }
      if (mappingFiles.length === 0) {
        resolve(false);
      }
 
      this.cfg.mappingService
        .fetchMappings(mappingFiles, this.cfg.mappings)
        .then(() => {
          this.cfg.initCfg.mappingInitialized = true;
          this.updateStatus();
          this.cfg.mappingService
            .notifyMappingUpdated()
            .then(() => resolve(true));
        });
    });
  }
 
  updateStatus(): void {
    const documentCount: number = this.cfg.getAllDocs().length;
    let finishedDocCount = 0;
    for (const docDef of this.cfg.getAllDocs()) {
      Eif (docDef.initialized || docDef.errorOccurred) {
        finishedDocCount++;
      }
    }
 
    if (
      documentCount === finishedDocCount &&
      this.cfg.fieldActionService.isInitialized
    ) {
      if (this.cfg.preloadedMappingJson) {
        MappingSerializer.deserializeMappingServiceJSON(
          JSON.parse(this.cfg.preloadedMappingJson),
          this.cfg
        );
        this.cfg.preloadedMappingJson = null;
      }
      Eif (this.cfg.mappings) {
        LookupTableUtil.updateLookupTables(this.cfg.mappings);
        MappingUtil.updateDocumentNamespacesFromMappings(this.cfg);
        MappingUtil.updateMappingsFromDocuments(this.cfg);
        for (const d of this.cfg.getAllDocs()) {
          d.updateFromMappings(this.cfg.mappings);
        }
        MappingUtil.removeStaleMappings(this.cfg);
        this.cfg.mappingService.updateMappingsTransition();
      }
      this.updateInitComplete();
    }
  }
 
  private handleError(message: string, error?: any) {
    message = 'Data Mapper UI Initialization Error: ' + message;
    this.cfg.errorService.addError(
      new ErrorInfo({
        message: message,
        scope: ErrorScope.APPLICATION,
        level: ErrorLevel.ERROR,
        type: ErrorType.INTERNAL,
        object: error,
      })
    );
    this.updateLoadingStatus(message);
    this.cfg.initCfg.initializationErrorOccurred = true;
    this.cfg.initCfg.initialized = true;
    this.systemInitializedSource.next();
  }
 
  updateInitComplete(): void {
    this.updateLoadingStatus('Initialization complete.');
    this.cfg.initCfg.initialized = true;
    this.systemInitializedSource.next();
  }
 
  updateLoadingStatus(status: string): void {
    this.cfg.initCfg.loadingStatus = status;
    this.initializationStatusChangedSource.next();
  }
 
  initializeWithADMArchiveFile(file: File): Promise<boolean> {
    return new Promise<boolean>((resolve) => {
      this.updateLoadingStatus('Importing ADM archive file');
      this.cfg.fileService.importADMArchive(file).then(() => {
        this.initialize().then((value) => {
          resolve(value);
        });
      });
    });
  }
 
  resetAtlasMap(): Promise<boolean> {
    return new Promise<boolean>((resolve) => {
      this.cfg.fileService.resetAll().then((value) => {
        this.initialize().then((value2) => {
          resolve(value && value2);
        });
      });
    });
  }
}