<commit_before>import { Router, ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Entity } from '../entities';
import { EntitiesService } from '../entities/entities.service';
import { SearchService } from '../search/search.service';
import { MapService } from './map.service';
import { MdlDialogService } from '@angular-mdl/core';
import { MdlSnackbarService } from '@angular-mdl/core';

import * as _ from 'lodash';
import * as moment from 'moment';
import {Mapping} from "./mapping.model";

@Component({
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss'],
})
export class MapComponent implements OnInit {
  // Entity Model
  public chosenEntity: Entity;
  private entityPrimaryKey: string = '';

  // Source Document
  private currentDatabase: string = 'STAGING';
  private entitiesOnly: boolean = false;
  private searchText: string = null;
  private activeFacets: any = {};
  private currentPage: number = 1;
  private pageLength: number = 1;
  public sampleDocURI: string = null;
  private sampleDocSrc: any = null;
  private sampleDocSrcProps: Array<any> = [];

  // Connections
  public conns: Object = {};
  public connsOrig: Object = {};
  private mapPrefix: string = 'dhf-map-';

  private entityName: string;
  public mapName: string;
  public flowName: string;

  public mapping: Mapping = new Mapping();

  public filterFocus = {};
  public filterText = {};

  // Edit source URI
  public editURIVal;
  public editingURI = false;

  //edit source Context
  public editingSourceContext = false;

  //edit description
  public editDescVal;
  public editingDesc = false;
  /**
   * Load chosen entity to use as harmonized model.
   */
  loadEntity(): void {
    let self = this;
    this.entitiesService.entitiesChange.subscribe(entities => {
      this.chosenEntity = _.find(entities, (e: Entity) => {
        return e.name === this.entityName;
      });
      this.entityPrimaryKey = this.chosenEntity.definition.primaryKey;
    });
    this.entitiesService.getEntities();
  }

  /**
   * Search for a sample document by entity name and load that document by its URI.
   * @param entityName An entity name
   */
  loadSampleDoc(entityName): void {
    this.activeFacets = {
      Collection: {
        values: [entityName]
      }
    };
    this.searchService.getResults(
      this.currentDatabase,
      this.entitiesOnly,
      this.searchText,
      this.activeFacets,
      this.currentPage,
      this.pageLength
    ).subscribe(response => {
      this.sampleDocURI = response.results[0].uri;
      this.editURIVal = this.sampleDocURI;
      this.loadSampleDocByURI(this.sampleDocURI)
    },
      () => {},
      () => {}
    );
  }

  /**
   * Load a sample document by its URI.
   * @param uri A document URI
   */
  loadSampleDocByURI(uri): void {
    let self = this;
    this.searchService.getDoc(this.currentDatabase, uri).subscribe(doc => {
      this.sampleDocSrcProps = [];
      this.sampleDocSrc = doc;
      _.forEach(this.sampleDocSrc['envelope']['instance'], function(val, key) {
        let prop = {
          key: key,
          val: String(val),
          type: self.getType(val)
        };
        self.sampleDocSrcProps.push(prop);
      });
      this.sampleDocURI = this.editURIVal;
      this.editingURI = false;
    },
      (err) => {
        // URI not found; this can only occur when editing the URI
        let result = this.dialogService.alert(
          'Document URI not found: ' + uri,
          'OK'
        );
        result.subscribe( () => {
            this.editURIVal = this.sampleDocURI;
            this.editingURI = false;
          },
          () => {},
          () => {}
          )
        }
      );
  }

  /**
   * Update the sample document based on a URI.
   */
  updateSampleDoc() {
    if (Object.keys(this.conns).length > 0) {
      let result = this.dialogService.confirm(
          'Changing your source document will remove<br/>existing property selections. Proceed?',
          'Cancel', 'OK');
      result.subscribe( () => {
          this.conns = {};
          this.loadSampleDocByURI(this.editURIVal);
        },(err: any) => {
          console.log('source change aborted');
          this.editingURI = false;
        },
        () => {}
      );
    } else {
      this.loadSampleDocByURI(this.editURIVal);
    }
  }

  /**
   * Cancel the editing of the source document URI.
   */
  cancelEditURI() {
    this.editURIVal = this.sampleDocURI;
    this.editingURI = false;
  }

  /**
   * Cancel the editing of the source document URI.
   */
  cancelEditDesc() {
    this.editDescVal = this.mapping.description;
    this.editingDesc = false;
  }

  keyPressURI(event) {
    if (event.key === 'Enter') {
      this.updateSampleDoc();
    }
  }

  updateDesc() {
    this.mapping.description = this.editDescVal;
    this.mapService.saveMap(this.mapping.name, JSON.stringify(this.mapping)).subscribe((res: any) => {
      console.log('map saved with edited description');<commit_msg>DHFPROD-1125 rollback of URI conns<commit_after>import { Router, ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Entity } from '../entities';
import { EntitiesService } from '../entities/entities.service';
import { SearchService } from '../search/search.service';
import { MapService } from './map.service';
import { MdlDialogService } from '@angular-mdl/core';
import { MdlSnackbarService } from '@angular-mdl/core';

import * as _ from 'lodash';
import * as moment from 'moment';
import {Mapping} from "./mapping.model";

@Component({
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss'],
})
export class MapComponent implements OnInit {
  // Entity Model
  public chosenEntity: Entity;
  private entityPrimaryKey: string = '';

  // Source Document
  private currentDatabase: string = 'STAGING';
  private entitiesOnly: boolean = false;
  private searchText: string = null;
  private activeFacets: any = {};
  private currentPage: number = 1;
  private pageLength: number = 1;
  public sampleDocURI: string = null;
  private sampleDocSrc: any = null;
  private sampleDocSrcProps: Array<any> = [];

  // Connections
  public conns: Object = {};
  public connsOrig: Object = {};
  private mapPrefix: string = 'dhf-map-';

  private entityName: string;
  public mapName: string;
  public flowName: string;

  public mapping: Mapping = new Mapping();

  public filterFocus = {};
  public filterText = {};

  // Edit source URI
  public editURIVal;
  public editingURI = false;

  //edit source Context
  public editingSourceContext = false;

  //edit description
  public editDescVal;
  public editingDesc = false;
  /**
   * Load chosen entity to use as harmonized model.
   */
  loadEntity(): void {
    let self = this;
    this.entitiesService.entitiesChange.subscribe(entities => {
      this.chosenEntity = _.find(entities, (e: Entity) => {
        return e.name === this.entityName;
      });
      this.entityPrimaryKey = this.chosenEntity.definition.primaryKey;
    });
    this.entitiesService.getEntities();
  }

  /**
   * Search for a sample document by entity name and load that document by its URI.
   * @param entityName An entity name
   */
  loadSampleDoc(entityName): void {
    this.activeFacets = {
      Collection: {
        values: [entityName]
      }
    };
    this.searchService.getResults(
      this.currentDatabase,
      this.entitiesOnly,
      this.searchText,
      this.activeFacets,
      this.currentPage,
      this.pageLength
    ).subscribe(response => {
      this.sampleDocURI = response.results[0].uri;
      this.editURIVal = this.sampleDocURI;
      this.loadSampleDocByURI(this.sampleDocURI, {})
    },
      () => {},
      () => {}
    );
  }

  /**
   * Load a sample document by its URI.
   * @param uri A document URI
   * @param conns A connections object in case rollback is required
   */
  loadSampleDocByURI(uri: string, conns: Object): void {
    let self = this;
    this.searchService.getDoc(this.currentDatabase, uri).subscribe(doc => {
      this.sampleDocSrcProps = [];
      this.sampleDocSrc = doc;
      _.forEach(this.sampleDocSrc['envelope']['instance'], function(val, key) {
        let prop = {
          key: key,
          val: String(val),
          type: self.getType(val)
        };
        self.sampleDocSrcProps.push(prop);
      });
      this.sampleDocURI = this.editURIVal;
      this.editingURI = false;
    },
      (err) => {
        // URI not found; this can only occur when editing the URI
        let result = this.dialogService.alert(
          'Document URI not found: ' + uri,
          'OK'
        );
        result.subscribe( () => {
            this.editURIVal = this.sampleDocURI;
            this.editingURI = false;
            // rollback to conns from previous URI
            if (!_.isEmpty(conns)) {
              this.conns = conns;
            }
          },
          () => {},
          () => {}
          )
        }
      );
  }

  /**
   * Update the sample document based on a URI.
   */
  updateSampleDoc() {
    if (Object.keys(this.conns).length > 0) {
      let result = this.dialogService.confirm(
          'Changing your source document will remove<br/>existing property selections. Proceed?',
          'Cancel', 'OK');
      result.subscribe( () => {
          let connsOrig = _.cloneDeep(this.conns);
          this.conns = {};
          // provide connsOrig for rollback purposes if needed
          this.loadSampleDocByURI(this.editURIVal, connsOrig);
        },(err: any) => {
          console.log('source change aborted');
          this.editingURI = false;
        },
        () => {}
      );
    } else {
      this.loadSampleDocByURI(this.editURIVal, {});
    }
  }

  /**
   * Cancel the editing of the source document URI.
   */
  cancelEditURI() {
    this.editURIVal = this.sampleDocURI;
    this.editingURI = false;
  }

  /**
   * Cancel the editing of the source document URI.
   */
  cancelEditDesc() {
    this.editDescVal = this.mapping.description;
    this.editingDesc = false;
  }

  keyPressURI(event) {
    if (event.key === 'Enter') {
      this.updateSampleDoc();
    }
  }

  updateDesc() {
    this.mapping.description = this.editDescVal;
    this.mapService.saveMap(this.mapping.name, JSON.stringify(this.mapping)).subscribe((res: any) => {
      console.log('map saved with edited description');
{'fraction_non_alphanumeric': 0.1286873508353222, 'fraction_numerical': 0.0011455847255369927, 'mean_word_length': 2.55359565807327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4735612', 'n_tokens_mistral': 3111, 'n_tokens_neox': 3092, 'n_words': 755}