1 import {Component, 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';
18 templateUrl: 'queue.component.html'
20 export class QueueComponent implements AfterViewInit {
23 queueType: string; // bib / authority
24 queueSource: GridDataSource;
25 queuedRecClass: string;
34 // keep a local copy for convenience
35 attrDefs: IdlObject[];
37 @ViewChild('queueGrid', { static: true }) queueGrid: GridComponent;
38 @ViewChild('confirmDelDlg', { static: false }) confirmDelDlg: ConfirmDialogComponent;
39 @ViewChild('progressDlg', { static: true }) progressDlg: ProgressDialogComponent;
41 cellTextGenerator: GridCellTextGenerator;
44 private router: Router,
45 private route: ActivatedRoute,
46 private evt: EventService,
47 private net: NetService,
48 private auth: AuthService,
49 private vandelay: VandelayService) {
51 this.route.paramMap.subscribe((params: ParamMap) => {
52 this.queueType = params.get('qtype');
53 this.queueId = +params.get('id');
56 this.queueSource = new GridDataSource();
57 this.queueSource.getRows = (pager: Pager) => {
58 this.vandelay.queuePageOffset = pager.offset;
59 return this.loadQueueRecords(pager);
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 + ''
69 limitToMatches(checked: boolean) {
70 this.filters.matches = checked;
71 this.queueGrid.reload();
74 limitToNonImported(checked: boolean) {
75 this.filters.nonImported = checked;
76 this.queueGrid.reload();
79 limitToImportErrors(checked: boolean) {
80 this.filters.withErrors = checked;
81 this.queueGrid.reload();
84 queuePageOffset(): number {
85 return this.vandelay.queuePageOffset;
90 this.applyQueueType();
92 this.loadQueueSummary();
97 openRecord(row: any) {
98 if (this.queueType === 'auth') {
99 this.queueType = 'authority';
102 `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
103 this.router.navigate([url]);
107 this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
108 this.vandelay.getAttrDefs(this.queueType).then(
110 this.attrDefs = attrs;
111 // Add grid columns for record attributes
112 attrs.forEach(attr => {
113 const col = new GridColumn();
114 col.name = attr.code(),
115 col.label = attr.description(),
116 col.datatype = 'string';
117 this.queueGrid.context.columnSet.add(col);
120 // Reapply the grid configuration now that we've
121 // dynamically added columns.
122 this.queueGrid.context.applyGridConfig();
127 qtypeShort(): string {
128 return this.queueType === 'bib' ? 'bib' : 'auth';
131 loadQueueSummary(): Promise<any> {
133 `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
135 return this.net.request(
136 'open-ils.vandelay', method, this.auth.token(), this.queueId)
137 .toPromise().then(sum => this.queueSummary = sum);
140 loadQueueRecords(pager: Pager): Observable<any> {
144 offset: pager.offset,
146 flesh_import_items: true,
147 non_imported: this.filters.nonImported,
148 with_import_error: this.filters.withErrors
151 return this.vandelay.getQueuedRecords(
152 this.queueId, this.queueType, options, this.filters.matches).pipe(
154 // avoid sending mishapen data to the grid
155 // this happens (among other reasons) when the grid
157 const e = this.evt.parse(rec);
158 if (e) { console.error(e); return false; }
162 const recHash: any = {
164 import_error: rec.import_error(),
165 error_detail: rec.error_detail(),
166 import_time: rec.import_time(),
167 imported_as: rec.imported_as(),
170 matches: rec.matches()
173 if (this.queueType === 'bib') {
174 recHash.import_items = rec.import_items();
175 recHash.error_items = rec.import_items().filter(i => i.import_error());
178 // Link the record attribute values to the root record
179 // object so the grid can find them.
180 rec.attributes().forEach(attr => {
182 this.attrDefs.filter(d => d.id() === attr.field())[0];
183 recHash[def.code()] = attr.attr_value();
190 findOrCreateImportSelection() {
191 let selection = this.vandelay.importSelection;
193 selection = new VandelayImportSelection();
194 this.vandelay.importSelection = selection;
196 selection.queue = this.queueSummary.queue;
200 hasOverlayTarget(rid: number): boolean {
201 return this.vandelay.importSelection &&
202 Boolean(this.vandelay.importSelection.overlayMap[rid]);
206 const rows = this.queueGrid.context.getSelectedRows();
208 const selection = this.findOrCreateImportSelection();
209 selection.recordIds = rows.map(row => row.id);
210 console.log('importing: ', this.vandelay.importSelection);
211 this.router.navigate(['/staff/cat/vandelay/import']);
216 const selection = this.findOrCreateImportSelection();
217 selection.importQueue = true;
218 this.router.navigate(['/staff/cat/vandelay/import']);
223 this.confirmDelDlg.open().subscribe(confirmed => {
224 if (!confirmed) { return; }
226 this.progressDlg.open();
229 `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
230 this.auth.token(), this.queueId
233 const e = this.evt.parse(resp);
234 if (e) { return new Error(e.toString()); }
236 // Jump back to the main queue page.
237 this.router.navigate(['/staff/cat/vandelay/queue']);
239 err => console.error('queue deletion failed!', err)
240 ).finally(() => this.progressDlg.close());
244 exportNonImported() {
245 this.vandelay.exportQueue(this.queueSummary.queue, true);