c1c61f0c2b803a56b21bd8dd23f4e8c707c1d741
[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 { Topic } from "src/app/core/models/topic.model";
39
40 @Component({
41   selector: "app-topic-detail-modal",
42   templateUrl: "./topic-detail-modal.component.html",
43   styleUrls: ["./topic-detail-modal.component.css"]
44 })
45 export class TopicDetailModalComponent implements OnInit {
46   @Input() topic: Topic;
47   @Output() passEntry: EventEmitter<any> = new EventEmitter();
48
49   // page elements
50   dbs: any = [];
51   dataFormats: Array<string> = ["JSON", "XML"];
52   tempSeletedDbs: any = [];
53   tempEnabled: boolean;
54   tempSaveRaw: boolean;
55   tempMsg: boolean;
56   idExFields: Array<any> = [];
57   idExNewField: any = {};
58   @ViewChild("t_login") t_login: ElementRef;
59   @ViewChild("t_password") t_password: ElementRef;
60   @ViewChild("t_dataFormat") t_dataFormat: ElementRef;
61   @ViewChild("t_ttl") t_ttl: ElementRef;
62
63   constructor(
64     public activeModal: NgbActiveModal,
65     private restApiService: RestApiService
66   ) {
67     this.getDbs();
68   }
69   ngOnInit() {
70     // for display
71     this.topic.sinkdbs.forEach(item => {
72       this.tempSeletedDbs.push(item);
73     });
74     this.tempEnabled = this.topic.enabled;
75     this.tempSaveRaw = this.topic.saveRaw;
76     this.tempMsg = this.topic.correlateClearedMessage;
77     this.idExFields = [];
78
79     if (this.topic.messageIdPath != null) {
80       var feed = this.topic.messageIdPath.split(",");
81       for (var i = 0; i < feed.length; i++) {
82         var data = { item: feed[i] };
83         this.idExFields.push(data);
84       }
85     } else {
86       this.idExFields.push([]);
87     }
88   }
89
90   getDbs() {
91     this.dbs = [];
92     this.restApiService.getDbList().subscribe((data: {}) => {
93       //console.log(data);
94       this.dbs = data;
95     });
96   }
97
98   updateSelectedDB(event: any, name: string) {
99     if (event.target.checked) {
100       console.log("checked");
101       if (!this.tempSeletedDbs.find(db => db === name)) {
102         this.tempSeletedDbs.push(name);
103       }
104     } else {
105       console.log("unchecked");
106       const index = this.tempSeletedDbs.indexOf(name, 0);
107       if (index > -1) {
108         this.tempSeletedDbs.splice(index, 1);
109       }
110     }
111   }
112
113   addIdField() {
114     this.idExFields.push(this.idExNewField);
115     this.idExNewField = {};
116   }
117
118   deleteIdField(index: number) {
119     if (this.idExFields.length > 1) {
120       this.idExFields.splice(index, 1);
121     }
122   }
123
124   passBack() {
125     this.topic.enabled = this.tempEnabled;
126     this.topic.login = this.t_login.nativeElement.value;
127     this.topic.password = this.t_password.nativeElement.value;
128
129     this.topic.sinkdbs = this.tempSeletedDbs;
130
131     this.topic.dataFormat = this.t_dataFormat.nativeElement.value;
132     this.topic.ttl = this.t_ttl.nativeElement.value;
133     this.topic.saveRaw = this.tempSaveRaw;
134     this.topic.correlateClearedMessage = this.tempMsg;
135     this.topic.messageIdPath = "";
136     for (var i = 0; i < this.idExFields.length; i++) {
137       if (i == 0) {
138         this.topic.messageIdPath = this.idExFields[i].item;
139       } else {
140         this.topic.messageIdPath =
141           this.topic.messageIdPath + "," + this.idExFields[i].item;
142       }
143     }
144
145     console.log("==================================");
146     console.log("Update topic name: " + this.topic.name);
147     console.log("Update topic login: " + this.topic.login);
148     console.log("Update topic password: " + this.topic.password);
149     console.log("Update topic sinkdbs: " + this.topic.sinkdbs);
150     console.log("Update topic enabled: " + this.topic.enabled);
151     console.log("Update topic saveRaw: " + this.topic.saveRaw);
152     console.log("Update topic dataFormat: " + this.topic.dataFormat);
153     console.log("Update topic ttl: " + this.topic.ttl);
154     console.log(
155       "Update topic correlateClearedMessage: " +
156         this.topic.correlateClearedMessage
157     );
158     console.log("Update topic messageIdPath: " + this.topic.messageIdPath);
159
160     // Reset to default
161     if (this.topic.sinkdbs.length == 0) {
162       this.topic.type = false;
163     } else {
164       this.topic.type = true;
165     }
166     console.log("Update topic type: " + this.topic.type);
167     console.log("==================================");
168     this.passEntry.emit(this.topic);
169   }
170 }