summaryrefslogtreecommitdiffstats
path: root/public/src/app/import-rules/import-rules.component.ts
blob: 7403518bdaae1db99142757b8b7ba46bbf884f65 (plain)
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
import {
  Component,
  EventEmitter,
  Output,
  ViewChild,
  ElementRef
} from '@angular/core';
import { Store } from '../store/store';
import { RuleEngineApiService } from '../rule-engine/api/rule-engine-api.service';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-import-rules',
  templateUrl: './import-rules.component.html',
  styleUrls: ['./import-rules.component.scss']
})
export class ImportRulesComponent {
  fileToUpload: File = null;
  fileName = '';
  mappingTarget: string;
  tabName: string;
  isGroup = false;
  @Output() refrashRuleList = new EventEmitter();
  @ViewChild('fileInput') fileInput: ElementRef;

  constructor(
    public _ruleApi: RuleEngineApiService,
    private toastr: ToastrService,
    public store: Store
  ) {
    this._ruleApi.tabIndex
      // .filter(index => {   if (index >= 0) {     const tabName =
      // this.store.cdump.nodes[index].name;     console.log('tab name:', tabName); if
      // (tabName.toLowerCase().includes('map')) {       return index;     }   } })
      .subscribe(index => {
        if (index >= 0) {
          this.tabName = this.store.cdump.nodes[index].name;
          console.log('tab name:', this.tabName);
          if (
            this.tabName.toLowerCase().includes('map') ||
            this.tabName.toLowerCase().includes('highlandpark') ||
            this.tabName.toLowerCase().includes('hp')
          ) {
            this.store.advancedSetting = this.store.tabsProperties[
              index
            ].filter(item => {
              if (
                !(
                  item.hasOwnProperty('constraints') &&
                  item.value !== undefined &&
                  !item.value.includes('get_input')
                )
              ) {
                return item;
              }
            });
            this.mappingTarget = this.store.advancedSetting[0].name;

            this._ruleApi.setParams({
              userId: this.store.sdcParmas.userId,
              nodeName: this.store.tabParmasForRule[0].name,
              nodeId: this.store.tabParmasForRule[0].nid,
              vfcmtUuid: this.store.mcUuid,
              fieldName: this.mappingTarget,
              flowType: this.store.cdump.flowType
            });

            this._ruleApi
              .generateMappingRulesFileName(
                this.store.tabParmasForRule[0].name,
                this.store.tabParmasForRule[0].nid,
                this.store.mcUuid
              )
              .subscribe(response => {
                console.log(
                  'generateMappingRulesFileName response: ',
                  response
                );
                this.store.advancedSetting.forEach(element => {
                  if (response.includes(element.name)) {
                    element.isExist = true;
                  } else {
                    element.isExist = false;
                  }
                });
              });
            console.log('advancedSetting', this.store.advancedSetting);
          }
        }
      });
  }

  onChangeMapping(configurationKey) {
    console.log('changing propertiy key:', configurationKey);
    this._ruleApi.setFieldName(configurationKey);
    this.refrashRuleList.next();
  }

  private notifyError(error: any) {
    this.store.loader = false;
    console.log(error.notes);
    this.store.ErrorContent = Object.values(error.requestError);
    this.store.displayErrorDialog = true;
  }

  handleFileInput(files: FileList) {
    const reader = new FileReader();
    if (files && files.length > 0) {
      this.store.loader = true;
      this.fileToUpload = files.item(0);
      console.log('file to load:', this.fileToUpload);
      this.fileName = this.fileToUpload !== null ? this.fileToUpload.name : '';
      reader.readAsText(this.fileToUpload, 'UTF-8');
      reader.onload = () => {
        console.log(reader.result);
        this._ruleApi
          .getLatestMcUuid({
            contextType: this.store.sdcParmas.contextType,
            serviceUuid: this.store.sdcParmas.uuid,
            vfiName: this.store.vfiName,
            vfcmtUuid: this.store.mcUuid
          })
          .subscribe(
            res => {
              this.store.mcUuid = res.uuid;
              if (
                this.tabName.toLowerCase().includes('highlandpark') ||
                this.tabName.toLowerCase().includes('hp')
              ) {
                this.isGroup = true;
              }
              this._ruleApi
                .importRules(reader.result, res.uuid, this.isGroup)
                .subscribe(
                  response => {
                    console.log('success import', response);
                    this.toastr.success('', 'success import');
                    this.store.expandImports[this.store.tabIndex] = false;
                    this.clearFile();
                    this.store.loader = false;
                    this._ruleApi.callUpdateTabIndex(this.store.tabIndex);
                  },
                  error => {
                    this.notifyError(error);
                    this.clearFile();
                  }
                );
            },
            error => {
              this.notifyError(error);
              this.clearFile();
            }
          );
      };
    } else {
      this.clearFile();
    }
  }

  clearFile() {
    this.fileInput.nativeElement.value = '';
    this.fileName = '';
  }
}