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';
20 templateUrl: 'queue.component.html'
22 export class QueueComponent implements OnInit, AfterViewInit {
25 queueType: string; // bib / authority
26 queueSource: GridDataSource;
27 queuedRecClass: string;
36 limitToMatches: (checked: boolean) => void;
37 limitToNonImported: (checked: boolean) => void;
38 limitToImportErrors: (checked: boolean) => void;
40 // keep a local copy for convenience
41 attrDefs: IdlObject[];
43 @ViewChild('queueGrid') queueGrid: GridComponent;
44 @ViewChild('confirmDelDlg') confirmDelDlg: ConfirmDialogComponent;
45 @ViewChild('progressDlg') progressDlg: ProgressDialogComponent;
48 private router: Router,
49 private route: ActivatedRoute,
50 private evt: EventService,
51 private net: NetService,
52 private auth: AuthService,
53 private vandelay: VandelayService) {
55 this.route.paramMap.subscribe((params: ParamMap) => {
56 this.queueType = params.get('qtype');
57 this.queueId = +params.get('id');
60 this.queueSource = new GridDataSource();
61 this.queueSource.getRows = (pager: Pager) => {
62 this.vandelay.queuePageOffset = pager.offset;
63 return this.loadQueueRecords(pager);
66 this.limitToMatches = (checked: boolean) => {
67 this.filters.matches = checked;
68 this.queueGrid.reload();
71 this.limitToNonImported = (checked: boolean) => {
72 this.filters.nonImported = checked;
73 this.queueGrid.reload();
76 this.limitToImportErrors = (checked: boolean) => {
77 this.filters.withErrors = checked;
78 this.queueGrid.reload();
85 queuePageOffset(): number {
86 return this.vandelay.queuePageOffset;
91 this.applyQueueType();
93 this.loadQueueSummary();
98 openRecord(row: any) {
100 `/staff/cat/vandelay/queue/${this.queueType}/${this.queueId}/record/${row.id}/marc`;
101 this.router.navigate([url]);
105 this.queuedRecClass = this.queueType.match(/bib/) ? 'vqbr' : 'vqar';
106 this.vandelay.getAttrDefs(this.queueType).then(
108 this.attrDefs = attrs;
109 // Add grid columns for record attributes
110 attrs.forEach(attr => {
111 const col = new GridColumn();
112 col.name = attr.code(),
113 col.label = attr.description(),
114 col.datatype = 'string';
115 this.queueGrid.context.columnSet.add(col);
118 // Reapply the grid configuration now that we've
119 // dynamically added columns.
120 this.queueGrid.context.applyGridConfig();
125 qtypeShort(): string {
126 return this.queueType === 'bib' ? 'bib' : 'auth';
129 loadQueueSummary(): Promise<any> {
131 `open-ils.vandelay.${this.qtypeShort()}_queue.summary.retrieve`;
133 return this.net.request(
134 'open-ils.vandelay', method, this.auth.token(), this.queueId)
135 .toPromise().then(sum => this.queueSummary = sum);
138 loadQueueRecords(pager: Pager): Observable<any> {
142 offset: pager.offset,
144 flesh_import_items: true,
145 non_imported: this.filters.nonImported,
146 with_import_error: this.filters.withErrors
149 return this.vandelay.getQueuedRecords(
150 this.queueId, this.queueType, options, this.filters.matches).pipe(
152 // avoid sending mishapen data to the grid
153 // this happens (among other reasons) when the grid
155 const e = this.evt.parse(rec);
156 if (e) { console.error(e); return false; }
160 const recHash: any = {
162 import_error: rec.import_error(),
163 error_detail: rec.error_detail(),
164 import_time: rec.import_time(),
165 imported_as: rec.imported_as(),
168 matches: rec.matches()
171 if (this.queueType === 'bib') {
172 recHash.import_items = rec.import_items();
173 recHash.error_items = rec.import_items().filter(i => i.import_error());
176 // Link the record attribute values to the root record
177 // object so the grid can find them.
178 rec.attributes().forEach(attr => {
180 this.attrDefs.filter(d => d.id() === attr.field())[0];
181 recHash[def.code()] = attr.attr_value();
188 findOrCreateImportSelection() {
189 let selection = this.vandelay.importSelection;
191 selection = new VandelayImportSelection();
192 this.vandelay.importSelection = selection;
194 selection.queue = this.queueSummary.queue;
198 hasOverlayTarget(rid: number): boolean {
199 return this.vandelay.importSelection &&
200 Boolean(this.vandelay.importSelection.overlayMap[rid]);
204 const rows = this.queueGrid.context.getSelectedRows();
206 const selection = this.findOrCreateImportSelection();
207 selection.recordIds = rows.map(row => row.id);
208 console.log('importing: ', this.vandelay.importSelection);
209 this.router.navigate(['/staff/cat/vandelay/import']);
214 const selection = this.findOrCreateImportSelection();
215 selection.importQueue = true;
216 this.router.navigate(['/staff/cat/vandelay/import']);
220 this.confirmDelDlg.open().then(
222 this.progressDlg.open();
223 return this.net.request(
225 `open-ils.vandelay.${this.qtypeShort()}_queue.delete`,
226 this.auth.token(), this.queueId
230 this.progressDlg.close();
231 return Promise.reject('delete failed');
235 this.progressDlg.close();
236 const e = this.evt.parse(resp);
241 // Jump back to the main queue page.
242 this.router.navigate(['/staff/cat/vandelay/queue']);
246 this.progressDlg.close();
251 exportNonImported() {
252 this.vandelay.exportQueue(this.queueSummary.queue, true);