]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/queue.component.ts
1b75f563ce0b092fed9271edb96ee74a30334ff3
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / queue.component.ts
1 import {Component, OnInit, AfterViewInit, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs/Observable';
3 import 'rxjs/add/observable/of';
4 import {map} from 'rxjs/operators/map';
5 import {filter} from 'rxjs/operators/filter';
6 import {Router, ActivatedRoute, ParamMap} from '@angular/router';              
7 import {Pager} from '@eg/share/util/pager';                                    
8 import {IdlObject} from '@eg/core/idl.service';
9 import {EventService} from '@eg/core/event.service';
10 import {NetService} from '@eg/core/net.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
13 import {ProgressDialogComponent} from '@eg/share/dialog/progress.component';
14 import {GridComponent} from '@eg/share/grid/grid.component';
15 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
16 import {VandelayService, VandelayImportSelection,
17     VANDELAY_EXPORT_PATH} from './vandelay.service';
18
19 @Component({
20   templateUrl: 'queue.component.html'
21 })
22 export class QueueComponent implements OnInit, AfterViewInit {
23
24     queueId: number;
25     queueType: string; // bib / authority
26     queueSource: GridDataSource;
27     queuedRecClass: string;
28     queueSummary: any;
29
30     filters = {
31         matches: false,
32         nonImported: false,
33         withErrors: false
34     };
35
36     limitToMatches: (checked: boolean) => void;
37     limitToNonImported: (checked: boolean) => void;
38     limitToImportErrors: (checked: boolean) => void;
39
40     // keep a local copy for convenience
41     attrDefs: IdlObject[];
42
43     @ViewChild('queueGrid') queueGrid: GridComponent;
44     @ViewChild('confirmDelDlg') confirmDelDlg: ConfirmDialogComponent;
45     @ViewChild('progressDlg') progressDlg: ProgressDialogComponent;
46
47     constructor(
48         private router: Router,
49         private route: ActivatedRoute,
50         private evt: EventService,
51         private net: NetService,
52         private auth: AuthService,
53         private vandelay: VandelayService) {
54
55         this.route.paramMap.subscribe((params: ParamMap) => {                  
56             this.queueType = params.get('qtype');
57             this.queueId = +params.get('id');
58         });
59
60         this.queueSource = new GridDataSource();
61         this.queueSource.getRows = (pager: Pager) => {
62             this.vandelay.queuePageOffset = pager.offset;
63             return this.loadQueueRecords(pager);
64         };
65
66         this.limitToMatches = (checked: boolean) => {
67             this.filters.matches = checked;
68             this.queueGrid.reload();
69         };
70
71         this.limitToNonImported = (checked: boolean) => {
72             this.filters.nonImported = checked;
73             this.queueGrid.reload();
74         };
75
76         this.limitToImportErrors = (checked: boolean) => {
77             this.filters.withErrors = checked;
78             this.queueGrid.reload();
79         };
80     }
81
82     ngOnInit() {
83     }
84
85     queuePageOffset(): number {
86         return this.vandelay.queuePageOffset;
87     }
88
89     ngAfterViewInit() {
90         if (this.queueType) { 
91             this.applyQueueType(); 
92             if (this.queueId) {
93                 this.loadQueueSummary();
94             }
95         }
96     }
97
98     openRecord(row: any) {
99         if (this.queueType == 'auth') {
100             this.queueType = 'authority';
101         }
102         const url = 
103           `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
104         this.router.navigate([url]);
105     }
106
107     applyQueueType() {
108         this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
109         this.vandelay.getAttrDefs(this.queueType).then(
110             attrs => {
111                 this.attrDefs = attrs;
112                 // Add grid columns for record attributes
113                 attrs.forEach(attr => {
114                     const col = new GridColumn();
115                     col.name = attr.code(),
116                     col.label = attr.description(),
117                     col.datatype = 'string';
118                     this.queueGrid.context.columnSet.add(col);
119                 });
120
121                 // Reapply the grid configuration now that we've
122                 // dynamically added columns.
123                 this.queueGrid.context.applyGridConfig();
124             }
125         );
126     }
127
128     qtypeShort(): string {
129         return this.queueType === 'bib' ? 'bib' : 'auth';    
130     }
131
132     loadQueueSummary(): Promise<any> {
133         const method = 
134             `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
135
136         return this.net.request(
137             'open-ils.vandelay', method, this.auth.token(), this.queueId)
138         .toPromise().then(sum => this.queueSummary = sum);
139     }
140
141     loadQueueRecords(pager: Pager): Observable<any> {
142
143         const options = {
144             clear_marc: true,
145             offset: pager.offset,
146             limit: pager.limit, 
147             flesh_import_items: true,
148             non_imported: this.filters.nonImported,
149             with_import_error: this.filters.withErrors
150         }
151
152         return this.vandelay.getQueuedRecords(
153             this.queueId, this.queueType, options, this.filters.matches).pipe(
154         filter(rec => {
155             // avoid sending mishapen data to the grid
156             // this happens (among other reasons) when the grid
157             // no longer exists
158             const e = this.evt.parse(rec);
159             if (e) { console.error(e); return false; }
160             return true;
161         }), 
162         map(rec => {
163             const recHash: any = {
164                 id: rec.id(),
165                 import_error: rec.import_error(),
166                 error_detail: rec.error_detail(),
167                 import_time: rec.import_time(),
168                 imported_as: rec.imported_as(),
169                 import_items: [],
170                 error_items: [],
171                 matches: rec.matches()
172             };
173
174             if (this.queueType === 'bib') {
175                 recHash.import_items = rec.import_items();
176                 recHash.error_items = rec.import_items().filter(i => i.import_error());
177             }
178
179             // Link the record attribute values to the root record 
180             // object so the grid can find them.
181             rec.attributes().forEach(attr => {
182                 const def = 
183                     this.attrDefs.filter(d => d.id() === attr.field())[0];
184                 recHash[def.code()] = attr.attr_value();
185             });
186
187             return recHash;
188         }));
189     }
190
191     findOrCreateImportSelection() {
192         let selection = this.vandelay.importSelection;
193         if (!selection) {
194             selection = new VandelayImportSelection();
195             this.vandelay.importSelection = selection;
196         }
197         selection.queue = this.queueSummary.queue;
198         return selection;
199     }
200
201     hasOverlayTarget(rid: number): boolean {
202         return this.vandelay.importSelection &&
203             Boolean(this.vandelay.importSelection.overlayMap[rid]);
204     }
205
206     importSelected() {
207         const rows = this.queueGrid.context.getSelectedRows();
208         if (rows.length) {
209             const selection = this.findOrCreateImportSelection();
210             selection.recordIds = rows.map(row => row.id);
211             console.log('importing: ', this.vandelay.importSelection);
212             this.router.navigate(['/staff/cat/vandelay/import']);
213         }
214     }
215
216     importAll() {
217         const selection = this.findOrCreateImportSelection();
218         selection.importQueue = true;
219         this.router.navigate(['/staff/cat/vandelay/import']);
220     }
221
222     deleteQueue() {
223         this.confirmDelDlg.open().then(
224             yes => {
225                 this.progressDlg.open();
226                 return this.net.request(
227                     'open-ils.vandelay',
228                     `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
229                     this.auth.token(), this.queueId
230                 ).toPromise();
231             },
232             no => {
233                 this.progressDlg.close();
234                 return Promise.reject('delete failed');
235             }
236         ).then(
237             resp => {
238                 this.progressDlg.close();
239                 const e = this.evt.parse(resp);
240                 if (e) {
241                     console.error(e);
242                     alert(e);
243                 } else {
244                     // Jump back to the main queue page.
245                     this.router.navigate(['/staff/cat/vandelay/queue']);
246                 }
247             },
248             err => {
249                 this.progressDlg.close();
250             }
251         );
252     }
253
254     exportNonImported() {
255         this.vandelay.exportQueue(this.queueSummary.queue, true);
256     }
257 }
258