91be8d0a41e9750c204467d195a9282b98b770c2
[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       enabled: false,
66       saveRaw: false,
67       dataFormat: this.dataFormats[0],
68       ttl: null,
69       correlateClearedMessage: false,
70       messageIdPath: null,
71       type:null
72     };
73     this.TopicInput = new Topic();
74     const feeds = {
75         name: this.newTopic.name,
76         login: this.newTopic.login,
77         password:this.newTopic.password,
78         sinkdbs: this.newTopic.sinkdbs,
79         enabled: this.newTopic.enabled,
80         saveRaw: this.newTopic.saveRaw,
81         dataFormat: this.newTopic.dataFormat,
82         ttl: this.newTopic.ttl,
83         correlateClearedMessage: this.newTopic.correlateClearedMessage,
84         messageIdPath: this.newTopic.messageIdPath,
85         type:null
86       };
87     this.TopicInput = feeds;
88     this. idExFields = [];
89     if (this.TopicInput.messageIdPath != null) {
90       var feed = this.TopicInput.messageIdPath.split(",");
91       for (var i = 0; i < feed.length; i++) {
92         var data = { item: feed[i] };
93         this.idExFields.push(data);
94       }
95     } else {
96       this.idExFields.push([]);
97     }
98   }
99
100   getDbs() {
101     this.dbs = [];
102     this.restApiService.getDbList().subscribe((data: {}) => {
103       this.dbs = data;
104     });
105   }
106
107   updateSelectedDB(event: any, name: string) {
108     if (event.target.checked) {
109       if (!this.tempSeletedDbs.find(db => db === name)) {
110         this.tempSeletedDbs.push(name);
111       }
112     } else {
113       const index = this.tempSeletedDbs.indexOf(name, 0);
114       if (index > -1) {
115         this.tempSeletedDbs.splice(index, 1);
116       }
117     }
118   }
119
120   addIdField() {
121     this.idExFields.push(this.idExNewField);
122     this.idExNewField = {};
123   }
124
125   deleteIdField(index: number) {
126     if (this.idExFields.length > 1) {
127       this.idExFields.splice(index, 1);
128     }
129   }
130
131   passBack() {
132     this.newTopic = this.TopicInput;
133     this.newTopic.name = this.t_topicname.nativeElement.value;
134     this.newTopic.login = this.t_login.nativeElement.value;
135     this.newTopic.password = this.t_password.nativeElement.value;
136     this.newTopic.sinkdbs = this.tempSeletedDbs;
137     this.newTopic.dataFormat = this.t_dataFormat.nativeElement.value;
138     this.newTopic.ttl = this.t_ttl.nativeElement.value;
139     this.newTopic.messageIdPath = "";
140     for (var i = 0; i < this.idExFields.length; i++) {
141       let item = "/"+this.idExFields[i].item;
142       if (i == 0) {
143         this.newTopic.messageIdPath = item;
144       } else {
145         this.newTopic.messageIdPath =
146           this.newTopic.messageIdPath + "," + item;
147       }
148     }
149     // Reset to default
150     if (this.newTopic.sinkdbs.length == 0) {
151       return false;
152     }
153     console.log(this.newTopic);
154     this.passEntry.emit(this.newTopic);
155   }
156
157 }