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