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