17d0d036891e498fae7a3402d268fdd441592866
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 QCT
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  *
23  * @author Ekko Chang
24  *
25  */
26
27 import {
28   Component,
29   OnInit,
30   Input,
31   Output,
32   EventEmitter,
33   ViewChild,
34   ElementRef
35 } from "@angular/core";
36 import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
37 import { RestApiService } from "src/app/core/services/rest-api.service";
38 import { AdminService } from "src/app/core/services/admin.service";
39 import { Topic } from "src/app/core/models/topic.model";
40
41 @Component({
42   selector: "app-topic-detail-modal",
43   templateUrl: "./topic-detail-modal.component.html",
44   styleUrls: ["./topic-detail-modal.component.css"]
45 })
46 export class TopicDetailModalComponent implements OnInit {
47   @Input() topic: Topic;
48   @Output() passEntry: EventEmitter<any> = new EventEmitter();
49
50   // page elements
51   dbs: any = [];
52   dataFormats: Array<string> = ["JSON", "XML"];
53   tempSeletedDbs: any = [];
54   tempEnabled: boolean;
55   tempSaveRaw: boolean;
56   tempMsg: boolean;
57   idExFields: Array<any> = [];
58   idExNewField: any = {};
59   @ViewChild("t_login") t_login: ElementRef;
60   @ViewChild("t_password") t_password: ElementRef;
61   @ViewChild("t_dataFormat") t_dataFormat: ElementRef;
62   @ViewChild("t_ttl") t_ttl: ElementRef;
63
64   constructor(
65     public activeModal: NgbActiveModal,
66     public adminService: AdminService,
67     private restApiService: RestApiService
68   ) {
69     this.getDbs();
70   }
71   ngOnInit() {
72     // for display
73     this.topic.sinkdbs.forEach(item => {
74       this.tempSeletedDbs.push(item);
75     });
76     this.tempEnabled = this.topic.enabled;
77     this.tempSaveRaw = this.topic.saveRaw;
78     this.tempMsg = this.topic.correlateClearedMessage;
79     this.idExFields = [];
80
81     if (this.topic.messageIdPath != null) {
82       var feed = this.topic.messageIdPath.split(",");
83       for (var i = 0; i < feed.length; i++) {
84         var data = { item: feed[i] };
85         this.idExFields.push(data);
86       }
87     } else {
88       this.idExFields.push([]);
89     }
90   }
91
92   getDbs() {
93     this.dbs = [];
94     this.restApiService.getDbList().subscribe((data: {}) => {
95       this.dbs = data;
96     });
97   }
98
99   updateSelectedDB(event: any, name: string) {
100     if (event.target.checked) {
101       if (!this.tempSeletedDbs.find(db => db === name)) {
102         this.tempSeletedDbs.push(name);
103       }
104     } else {
105       const index = this.tempSeletedDbs.indexOf(name, 0);
106       if (index > -1) {
107         this.tempSeletedDbs.splice(index, 1);
108       }
109     }
110   }
111
112   addIdField() {
113     this.idExFields.push(this.idExNewField);
114     this.idExNewField = {};
115   }
116
117   deleteIdField(index: number) {
118     if (this.idExFields.length > 1) {
119       this.idExFields.splice(index, 1);
120     }
121   }
122
123   passBack() {
124     this.topic.enabled = this.tempEnabled;
125     this.topic.login = this.t_login.nativeElement.value;
126     this.topic.password = this.t_password.nativeElement.value;
127
128     this.topic.sinkdbs = this.tempSeletedDbs;
129
130     this.topic.dataFormat = this.t_dataFormat.nativeElement.value;
131     this.topic.ttl = this.t_ttl.nativeElement.value;
132     this.topic.saveRaw = this.tempSaveRaw;
133     this.topic.correlateClearedMessage = this.tempMsg;
134     this.topic.messageIdPath = "";
135     for (var i = 0; i < this.idExFields.length; i++) {
136       if (i == 0) {
137         this.topic.messageIdPath = this.idExFields[i].item;
138       } else {
139         this.topic.messageIdPath =
140           this.topic.messageIdPath + "," + this.idExFields[i].item;
141       }
142     }
143
144     // Reset to default
145     if (this.topic.sinkdbs.length == 0) {
146       this.topic.config = false;
147     } else {
148       this.topic.config = true;
149     }
150     this.passEntry.emit(this.topic);
151   }
152 }