]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/triggers/triggers.component.ts
84acf4e5fec68586273f134fc3ff5f6172e35cbd
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / triggers / triggers.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {Router} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
9 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {ToastService} from '@eg/share/toast/toast.service';
12 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
13
14 @Component({
15     templateUrl: './triggers.component.html'
16 })
17
18 export class TriggersComponent implements OnInit {
19
20     eventsDataSource: GridDataSource = new GridDataSource();
21     hooksDataSource: GridDataSource = new GridDataSource();
22     reactorsDataSource: GridDataSource = new GridDataSource();
23     validatorsDataSource: GridDataSource = new GridDataSource();
24     triggerTab: 'eventDefinitions' | 'hooks' | 'reactors' | 'validators' = 'eventDefinitions';
25     idlClass: string;
26
27     @ViewChild('eventDialog', {static: false}) eventDialog: FmRecordEditorComponent;
28     @ViewChild('hookDialog', {static: false}) hookDialog: FmRecordEditorComponent;
29     @ViewChild('reactorDialog', {static: false}) reactorDialog: FmRecordEditorComponent;
30     @ViewChild('validatorDialog', {static: false}) validatorDialog: FmRecordEditorComponent;
31
32     @ViewChild('confirmDialog', {static: false}) private confirmDialog: ConfirmDialogComponent;
33
34     @ViewChild('eventsGrid', {static: false}) eventsGrid: GridComponent;
35     @ViewChild('hooksGrid', {static: false}) hooksGrid: GridComponent;
36     @ViewChild('reactorsGrid', {static: false}) reactorsGrid: GridComponent;
37     @ViewChild('validatorsGrid', {static: false}) validatorsGrid: GridComponent;
38
39     @ViewChild('updateSuccessString', {static: false}) updateSuccessString: StringComponent;
40     @ViewChild('updateFailedString', {static: false}) updateFailedString: StringComponent;
41     @ViewChild('cloneSuccessString', {static: false}) cloneSuccessString: StringComponent;
42     @ViewChild('cloneFailedString', {static: false}) cloneFailedString: StringComponent;
43     @ViewChild('deleteFailedString', {static: false}) deleteFailedString: StringComponent;
44     @ViewChild('deleteSuccessString', {static: false}) deleteSuccessString: StringComponent;
45     @ViewChild('createSuccessString', {static: false}) createSuccessString: StringComponent;
46     @ViewChild('createErrString', {static: false}) createErrString: StringComponent;
47
48     constructor(
49         private idl: IdlService,
50         private pcrud: PcrudService,
51         private toast: ToastService,
52         private router: Router,
53     ) {
54     }
55
56     ngOnInit() {
57         this.eventsDataSource.getRows = (pager: Pager, sort: any[]) => {
58             const orderEventsBy: any = {atevdef: 'name'};
59             if (sort.length) {
60                 orderEventsBy.atevdef = sort[0].name + ' ' + sort[0].dir;
61             }
62             return this.getData('atevdef', orderEventsBy, this.eventsDataSource, pager);
63         };
64
65         this.hooksDataSource.getRows = (pager: Pager, sort: any[]) => {
66             const orderHooksBy: any = {ath: 'key'};
67             if (sort.length) {
68                 orderHooksBy.ath = sort[0].name + ' ' + sort[0].dir;
69             }
70             return this.getData('ath', orderHooksBy, this.hooksDataSource, pager);
71         };
72
73         this.reactorsDataSource.getRows = (pager: Pager, sort: any[]) => {
74             const orderReactorsBy: any = {atreact: 'module'};
75             if (sort.length) {
76                 orderReactorsBy.atreact = sort[0].name + ' ' + sort[0].dir;
77             }
78             return this.getData('atreact', orderReactorsBy, this.reactorsDataSource, pager);
79         };
80
81         this.validatorsDataSource.getRows = (pager: Pager, sort: any[]) => {
82             const orderValidatorsBy: any = {atval: 'module'};
83             if (sort.length) {
84                 orderValidatorsBy.atval = sort[0].name + ' ' + sort[0].dir;
85             }
86             return this.getData('atval', orderValidatorsBy, this.validatorsDataSource, pager);
87         };
88     }
89
90     getData(idlString: any, currentOrderBy: any, currentDataSource: any, pager: Pager) {
91         const base: Object = {};
92         base[this.idl.classes[idlString].pkey] = {'!=' : null};
93         const query: any = new Array();
94         query.push(base);
95         Object.keys(currentDataSource.filters).forEach(key => {
96             Object.keys(currentDataSource.filters[key]).forEach(key2 => {
97                 query.push(currentDataSource.filters[key][key2]);
98             });
99         });
100         return this.pcrud.search(idlString,
101             query, {
102             offset: pager.offset,
103             limit: pager.limit,
104             order_by: currentOrderBy
105         });
106     }
107
108     onTabChange(event: NgbNavChangeEvent) {
109         this.triggerTab = event.nextId;
110     }
111
112     createNewEvent = () => {
113         this.createNewThing(this.eventDialog, this.eventsGrid);
114     }
115
116     createNewHook = () => {
117         this.createNewThing(this.hookDialog, this.hooksGrid);
118     }
119
120     createNewReactor = () => {
121         this.createNewThing(this.reactorDialog, this.reactorsGrid);
122     }
123
124     createNewValidator = () => {
125         this.createNewThing(this.validatorDialog, this.validatorsGrid);
126     }
127
128     createNewThing = (currentDialog: any, currentGrid: any) => {
129         currentDialog.mode = 'create';
130         currentDialog.recordId = null;
131         currentDialog.record = null;
132         currentDialog.open({size: 'lg'}).subscribe(
133             ok => {
134                 this.createSuccessString.current()
135                     .then(str => this.toast.success(str));
136                 currentGrid.reload();
137             },
138             rejection => {
139                 if (!rejection.dismissed) {
140                     this.createErrString.current()
141                         .then(str => this.toast.danger(str));
142                 }
143             }
144         );
145     }
146
147     editSelected = (selectedRecords: IdlObject[]) => {
148         if (this.triggerTab === 'eventDefinitions') {
149             this.editEventDefinition(selectedRecords);
150             return;
151         }
152         const editOneThing = (record: IdlObject) => {
153             if (!record) { return; }
154             this.showEditDialog(record).then(
155                 () => editOneThing(selectedRecords.shift()));
156         };
157         editOneThing(selectedRecords.shift());
158     }
159
160     editEventDefinition = (selectedRecords: IdlObject[]) => {
161         const id = selectedRecords[0].id();
162         this.router.navigate(['/staff/admin/local/action_trigger/event_definition/' + id]);
163     }
164
165     lookUpIdl (idl: string) {
166         let currentDialog;
167         let currentGrid;
168         switch (idl) {
169             case 'atevdef':
170                 currentDialog = this.eventDialog;
171                 currentGrid = this.eventsGrid;
172                 break;
173             case 'ath':
174                 currentDialog = this.hookDialog;
175                 currentGrid = this.hooksGrid;
176                 break;
177             case 'atreact':
178                 currentDialog = this.reactorDialog;
179                 currentGrid = this.reactorsGrid;
180                 break;
181             case 'atval':
182                 currentDialog = this.validatorDialog;
183                 currentGrid = this.validatorsGrid;
184                 break;
185             default:
186                 console.debug('Unknown class name');
187         }
188         return {currentDialog: currentDialog, currentGrid: currentGrid};
189     }
190
191     showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
192         const idl = selectedRecord.classname;
193         const lookupResults = this.lookUpIdl(idl);
194         const currentDialog = lookupResults.currentDialog;
195         const currentGrid = lookupResults.currentGrid;
196         currentDialog.mode = 'update';
197         const clone = this.idl.clone(selectedRecord);
198         currentDialog.record = clone;
199         return new Promise((resolve, reject) => {
200             currentDialog.open({size: 'lg'}).subscribe(
201                 result => {
202                     this.updateSuccessString.current()
203                         .then(str => this.toast.success(str));
204                     currentGrid.reload();
205                     resolve(result);
206                 },
207                 error => {
208                     this.updateFailedString.current()
209                         .then(str => this.toast.danger(str));
210                     reject(error);
211                }
212            );
213        });
214     }
215
216     deleteSelected = (idlThings: IdlObject[]) => {
217         const idl = idlThings[0].classname;
218         const currentGrid = this.lookUpIdl(idl).currentGrid;
219         idlThings.forEach(idlThing => idlThing.isdeleted(true));
220         this.pcrud.autoApply(idlThings).subscribe(
221             val => {
222                 console.debug('deleted: ' + val);
223                 this.deleteSuccessString.current()
224                     .then(str => this.toast.success(str));
225                 currentGrid.reload();
226             },
227             err => {
228                 this.deleteFailedString.current()
229                     .then(str => this.toast.danger(str));
230             }
231         );
232     }
233
234     cloneSelected = (selectedRecords: IdlObject[]) => {
235         const clone = this.idl.clone(selectedRecords[0]);
236         // look for existing environments
237         this.pcrud.search('atenv', {event_def: selectedRecords[0].id()}, {}, {atomic: true})
238             .toPromise().then(envs => {
239             if (envs) {
240                 // if environments found, ask user if they want to clone them
241                 this.confirmDialog.open().toPromise().then(ok => {
242                     if (ok) {
243                         this.doClone(clone, envs);
244                     } else {
245                         this.doClone(clone, []);
246                     }
247                 });
248             } else {
249                 this.doClone(clone, []);
250             }
251         });
252     }
253
254     doClone(eventDef, env_list) {
255         eventDef.id(null);
256         this.eventDialog.mode = 'create';
257         this.eventDialog.recordId = null;
258         this.eventDialog.record = eventDef;
259         this.eventDialog.open({size: 'lg'}).subscribe(
260             response => {
261                 this.cloneSuccessString.current()
262                     .then(str => this.toast.success(str));
263                 this.eventsGrid.reload();
264                 // clone environments also if user previously confirmed
265                 if (env_list.length) {
266                     this.cloneEnvs(response.id(), env_list);
267                 }
268             },
269             rejection => {
270                 if (!rejection.dismissed) {
271                     this.cloneFailedString.current()
272                         .then(str => this.toast.danger(str));
273                 }
274             }
275         );
276     }
277
278     cloneEnvs(cloneId, env_list) {
279         env_list.forEach(env => {
280             env.event_def(cloneId);
281             env.id(null);
282         });
283         this.pcrud.create(env_list).toPromise().then(
284             ok => {
285                 console.debug(ok);
286             },
287             err => {
288                 console.debug(err);
289             }
290         );
291     }
292
293 }