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