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';
18 templateUrl: 'queue.component.html'
20 export class QueueComponent implements OnInit, 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,
65 'imported_as': row => row.imported_as + ''
72 limitToMatches(checked: boolean) {
73 this.filters.matches = checked;
74 this.queueGrid.reload();
77 limitToNonImported(checked: boolean) {
78 this.filters.nonImported = checked;
79 this.queueGrid.reload();
82 limitToImportErrors(checked: boolean) {
83 this.filters.withErrors = checked;
84 this.queueGrid.reload();
87 queuePageOffset(): number {
88 return this.vandelay.queuePageOffset;
93 this.applyQueueType();
95 this.loadQueueSummary();
100 openRecord(row: any) {
101 if (this.queueType === 'auth') {
102 this.queueType = 'authority';
105 `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
106 this.router.navigate([url]);
110 this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
111 this.vandelay.getAttrDefs(this.queueType).then(
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);
123 // Reapply the grid configuration now that we've
124 // dynamically added columns.
125 this.queueGrid.context.applyGridConfig();
130 qtypeShort(): string {
131 return this.queueType === 'bib' ? 'bib' : 'auth';
134 loadQueueSummary(): Promise<any> {
136 `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
138 return this.net.request(
139 'open-ils.vandelay', method, this.auth.token(), this.queueId)
140 .toPromise().then(sum => this.queueSummary = sum);
143 loadQueueRecords(pager: Pager): Observable<any> {
147 offset: pager.offset,
149 flesh_import_items: true,
150 non_imported: this.filters.nonImported,
151 with_import_error: this.filters.withErrors
154 return this.vandelay.getQueuedRecords(
155 this.queueId, this.queueType, options, this.filters.matches).pipe(
157 // avoid sending mishapen data to the grid
158 // this happens (among other reasons) when the grid
160 const e = this.evt.parse(rec);
161 if (e) { console.error(e); return false; }
165 const recHash: any = {
167 import_error: rec.import_error(),
168 error_detail: rec.error_detail(),
169 import_time: rec.import_time(),
170 imported_as: rec.imported_as(),
173 matches: rec.matches()
176 if (this.queueType === 'bib') {
177 recHash.import_items = rec.import_items();
178 recHash.error_items = rec.import_items().filter(i => i.import_error());
181 // Link the record attribute values to the root record
182 // object so the grid can find them.
183 rec.attributes().forEach(attr => {
185 this.attrDefs.filter(d => d.id() === attr.field())[0];
186 recHash[def.code()] = attr.attr_value();
193 findOrCreateImportSelection() {
194 let selection = this.vandelay.importSelection;
196 selection = new VandelayImportSelection();
197 this.vandelay.importSelection = selection;
199 selection.queue = this.queueSummary.queue;
203 hasOverlayTarget(rid: number): boolean {
204 return this.vandelay.importSelection &&
205 Boolean(this.vandelay.importSelection.overlayMap[rid]);
209 const rows = this.queueGrid.context.getSelectedRows();
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']);
219 const selection = this.findOrCreateImportSelection();
220 selection.importQueue = true;
221 this.router.navigate(['/staff/cat/vandelay/import']);
226 this.confirmDelDlg.open().subscribe(confirmed => {
227 if (!confirmed) { return; }
229 this.progressDlg.open();
232 `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
233 this.auth.token(), this.queueId
236 const e = this.evt.parse(resp);
237 if (e) { return new Error(e.toString()); }
239 // Jump back to the main queue page.
240 this.router.navigate(['/staff/cat/vandelay/queue']);
242 err => console.error('queue deletion failed!', err)
243 ).finally(() => this.progressDlg.close());
247 exportNonImported() {
248 this.vandelay.exportQueue(this.queueSummary.queue, true);