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