56376dfeebbe805c4a1fef9b414fc5a490390813
[holmes/rule-management.git] / rulemgt-frontend / src / app / correlation-ruleInfo / ruleInfo.component.ts
1 /*
2  Copyright 2018 ZTE Corporation.
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 { Component, OnInit, Input } from '@angular/core';
17 import { RuleModel } from '../correlation-ruleList/alarmRule';
18 import { ActivatedRoute, Params, Router } from '@angular/router';
19 import { AlarmRuleService } from '../correlation-ruleList/alarmRule.service';
20 import { Http, Response, Headers } from '@angular/http';
21 import { ModalService } from '../correlation-modal/modal.service';
22 import { NgModel } from '@angular/forms';
23 declare var $: any;
24 @Component({
25     selector: 'RuleInfo',
26     templateUrl: './ruleInfo.component.html'
27 })
28 export class RuleInfo implements OnInit {
29     formModel: RuleModel;
30     queryRule: RuleModel;
31     id: number;
32     addBottonStatus: boolean;
33     constructor(private modalService: ModalService, private route: ActivatedRoute, private router: Router, private alarmRuleService: AlarmRuleService, http: Http) { };
34
35     update() {
36         this.queryRule.enabled = $('input:radio:checked').val();
37         if (!this.queryRule.content.startsWith('package ')) {
38             let msg = { title: 'exception_content_error', message: 'exception_package_error' };
39             this.modalService.getmodalObservable.next(msg);
40         } else {
41             this.alarmRuleService.checkContent(this.queryRule.content)
42                 .then(res => {
43                     if (res.status == 200) {
44                         this.alarmRuleService.updateRule(this.queryRule)
45                             .then(res => {
46                                 if (res.status == 200) {
47                                     let msg = { title: 'modalTitleUpdate', message: 'message_update_rule_success' };
48                                     this.modalService.getmodalObservable.next(msg);
49                                     this.router.navigate(['alarmRule']);
50                                 } else if (res.status == 499) {
51                                     let msg = { title: 'modalTitleUpdate', message: 'message_exception_rule_fail' };
52                                     this.modalService.getmodalObservable.next(msg);
53                                 } else {
54                                     let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
55                                     this.modalService.getmodalObservable.next(msg);
56                                 }
57                             }).catch(
58                             res => {
59                                 let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
60                                 this.modalService.getmodalObservable.next(msg);
61                             }
62                             );
63
64                     } else if (res.status == 499) {
65                         let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
66                         this.modalService.getmodalObservable.next(msg);
67                         return false;
68                     } else {
69                         let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
70                         this.modalService.getmodalObservable.next(msg);
71                         return false;
72                     }
73                 })
74         }
75     }
76
77     onSubmit(obj: NgModel) {
78         this.save();
79     }
80
81     save() {
82         if (!this.queryRule.content.startsWith('package ')) {
83             let msg = { title: 'exception_content_error', message: 'exception_package_error' };
84             this.modalService.getmodalObservable.next(msg);
85         } else {
86             this.alarmRuleService.checkContent(this.queryRule.content).then(res => {
87                 if (res.status == 200) {
88                     this.alarmRuleService.save(this.queryRule).then(res => {
89                         if (res.status == 200) {
90                             let msg = { title: 'modalTitleDefault', message: 'message_add_rule_success' };
91                             this.modalService.getmodalObservable.next(msg);
92                             this.router.navigate(['alarmRule']);
93                         } else if (res.status == 499) {
94                             let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
95                             this.modalService.getmodalObservable.next(msg);
96                         } else {
97                             let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
98                             this.modalService.getmodalObservable.next(msg);
99                         }
100
101                     }).catch(error => {
102                         let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
103                         this.modalService.getmodalObservable.next(msg);
104                     });
105                     return true;
106                 } else if (res.status == 499) {
107                     let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
108                     this.modalService.getmodalObservable.next(msg);
109                     return false;
110                 } else {
111                     let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
112                     this.modalService.getmodalObservable.next(msg);
113                     return false;
114                 }
115             })
116         }
117     }
118
119     getRuleInfo(id: string): void {
120         this.route.params.switchMap((params: Params) => this.alarmRuleService.search(id))
121             .subscribe(rule => {
122                 rule[0].enabled = '' + rule[0].enabled;
123                 this.queryRule = rule[0]
124             })
125     }
126
127     checkContent(judge: string): void {
128         if (!this.queryRule.content.startsWith('package ')) {
129             let msg = { title: 'exception_content_error', message: 'exception_package_error' };
130             this.modalService.getmodalObservable.next(msg);
131         } else {
132             this.alarmRuleService.checkContent(this.queryRule.content)
133                 .then(res => {
134                     if (res.status == 200) {
135                         let msg = { 'title': 'modalTitleCheck', message: 'message_checkContent_rule_success' };
136                         this.modalService.getmodalObservable.next(msg);
137                     } else if (res.status == 499) {
138                         let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
139                         this.modalService.getmodalObservable.next(msg);
140                         return false;
141                     } else {
142                         let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
143                         this.modalService.getmodalObservable.next(msg);
144                         return false;
145                     }
146                 });
147         }
148     }
149     switch(): void {
150         // alert(this.queryRule.enabled);
151         this.queryRule.enabled == '0' ? this.queryRule.enabled = '1' : this.queryRule.enabled = '0';
152     }
153
154
155     initUpload(queryRule: RuleModel): void {
156         $('#fileName').text('');
157         $('#importFailTip').addClass('hide_panel');
158         $('#fileupload').fileupload({
159
160             dropZone: $('#dropzone'),
161             maxNumberOfFiles: 1,
162             maxChunkSize: 20000000,
163             autoUpload: false,
164             add: function (e, data) {
165                 let fileName = data.files[0].name;
166
167                 $('#importFailTip').addClass('hide_panel');
168                 let suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length)
169
170                 if (suffix != '.txt') {
171                     $('#importFailTip').removeClass('hide_panel');
172                     return suffix;
173                 }
174
175                 $('#bar').css('width', '0%');
176                 $('#persent').text('0%');
177                 $('#fileName').text(fileName);
178                 $('#fileremove').attr('disabled', false);
179                 $('#filesubmit').attr('disabled', false);
180                 $('#filesubmit').click(function () {
181                     this.file = data.files[0]
182                     var reader = new FileReader();
183                     reader.readAsText(this.file);
184                     reader.onload = function (data) {
185                         queryRule.content = this.result;
186                     }
187                     $('#fileremove').click();
188                     $('#importDiv').hide();
189                     e.stopPropagation();
190                 });
191
192                 $('#fileremove').click(function () {
193                     $('#bar').css('width', '0%');
194                     $('#persent').text('0%');
195                     $('#fileName').text('');
196                     $('#filesubmit').attr('disabled', true);
197                     $('#fileremove').attr('disabled', true);
198                 });
199             },
200             done: function (e, data) {
201
202             },
203             fail: function (e, resp) {
204             },
205             always: function (e, data) {
206                 $('.progress').removeClass('active');
207                 $('#bar').css('width', '100%');
208                 $('#persent').text('100%');
209             },
210             progressall: function (e, data) {
211             }
212         });
213     }
214
215     initImportDiv(queryRule: RuleModel): void {
216         this.initUpload(queryRule);
217         let importDiv = $('#importDiv');
218         $(function (arg) {
219             $('#importBtn').click(function (e) {
220                 e.preventDefault();
221                 $('[data-toggle="tooltip"]').tooltip();
222                 $('#importFailTip').addClass('hide_panel');
223                 showDiv();
224                 $(document).one('click', function (e) {
225                     $(importDiv).hide();
226                 });
227                 e.stopPropagation();
228             });
229             $(importDiv).click(function (e) {
230                 e.stopPropagation();
231             });
232             $('#filesubmit').attr('disabled', true);
233             $('#fileremove').attr('disabled', true);
234         });
235         function showDiv() {
236             $('#bar').css('width', '0%');
237             $('#persent').text('0%');
238             $(importDiv).fadeIn();
239         }
240     }
241     ngOnInit() {
242         this.formModel = {
243             ruleId: null,
244             ruleName: null,
245             description: null,
246             content: null,
247             createTime: null,
248             creator: null,
249             updateTime: null,
250             modifier: null,
251             enabled: null,
252             loopControlName: null
253         };
254         this.queryRule = {
255             ruleId: null,
256             ruleName: null,
257             description: null,
258             content: null,
259             createTime: null,
260             creator: null,
261             updateTime: null,
262             modifier: null,
263             enabled: 0,
264             loopControlName: ''
265         };
266
267         this.route.params.subscribe((params) => {
268
269             let id = params['id'];
270             if (typeof (id) == 'string') {
271                 if (id.indexOf('&') == -1) {
272                     $('#ruleNameInput').attr('disabled', true);
273                     $('#saveLabel').hide();
274                 } else {
275                     this.addBottonStatus = false;
276                     $('#saveLabel').hide();
277                     $('#checkLabel').hide();
278                     $('#updateLabel').hide();
279                 }
280
281                 let str = id.split('&');
282                 this.getRuleInfo(str[0]);
283             } else if (typeof (id) != 'undefined') {
284                 this.getRuleInfo(id);
285             } else {
286                 $('#updateLabel').hide();
287             }
288             if (typeof (this.queryRule.enabled) === 'number') {
289                 this.queryRule.enabled = '' + this.queryRule.enabled;
290             }
291         });
292
293         this.initImportDiv(this.queryRule);
294     }
295
296 }