]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/vandelay/queued-record-matches.component.ts
7f2eefbace231eafe8d427894aa54180b18c77de
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / queued-record-matches.component.ts
1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {Observable, of} from 'rxjs';
4 import {map} from 'rxjs/operators';
5 import {Pager} from '@eg/share/util/pager';
6 import {GridComponent} from '@eg/share/grid/grid.component';
7 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
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 {PcrudService} from '@eg/core/pcrud.service';
13 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
14 import {VandelayService, VandelayImportSelection} from './vandelay.service';
15
16 @Component({
17   selector: 'eg-queued-record-matches',
18   templateUrl: 'queued-record-matches.component.html'
19 })
20 export class QueuedRecordMatchesComponent implements OnInit {
21
22     @Input() queueType: string;
23     @Input() recordId: number;
24     @ViewChild('bibGrid', { static: false }) bibGrid: GridComponent;
25     @ViewChild('authGrid', { static: false }) authGrid: GridComponent;
26
27     queuedRecord: IdlObject;
28     bibDataSource: GridDataSource;
29     authDataSource: GridDataSource;
30     markOverlayTarget: (rows: any[]) => any;
31     matchRowClick: (row: any) => void;
32     matchMap: {[id: number]: IdlObject};
33
34     cellPrintValues: (row: any, cell: GridColumn) => string;
35
36     constructor(
37         private router: Router,
38         private route: ActivatedRoute,
39         private evt: EventService,
40         private net: NetService,
41         private auth: AuthService,
42         private pcrud: PcrudService,
43         private bib: BibRecordService,
44         private vandelay: VandelayService) {
45
46         this.bibDataSource = new GridDataSource();
47         this.authDataSource = new GridDataSource();
48
49         this.bibDataSource.getRows = (pager: Pager) => {
50             return this.getBibMatchRows(pager);
51         };
52
53         // Text-ify function for cells that use display templates.
54         this.cellPrintValues = (row: any, cell: GridColumn): string => {
55             return ({
56                 'selected': this.isOverlayTarget(row.id) + '',
57                 'eg_record': row.eg_record + ''
58             })[cell.name] || '';
59         };
60
61
62         /* TODO
63         this.authDataSource.getRows = (pager: Pager) => {
64         }
65         */
66
67         // Mark or un-mark as row as the merge target on row click
68         this.matchRowClick = (row: any) => {
69             this.toggleMergeTarget(row.id);
70         };
71     }
72
73     toggleMergeTarget(matchId: number) {
74
75         if (this.isOverlayTarget(matchId)) {
76
77             // clear selection on secondary click;
78             delete this.vandelay.importSelection.overlayMap[this.recordId];
79
80         } else {
81             // Add to selection.
82             // Start a new one if necessary, which will be adopted
83             // and completed by the queue UI before import.
84
85             let selection = this.vandelay.importSelection;
86             if (!selection) {
87                 selection = new VandelayImportSelection();
88                 this.vandelay.importSelection = selection;
89             }
90             const match = this.matchMap[matchId];
91             selection.overlayMap[this.recordId] = match.eg_record();
92         }
93     }
94
95     isOverlayTarget(matchId: number): boolean {
96         const selection = this.vandelay.importSelection;
97         if (selection) {
98             const match = this.matchMap[matchId];
99             return selection.overlayMap[this.recordId] === match.eg_record();
100         }
101         return false;
102     }
103
104     ngOnInit() {}
105
106     // This thing is a nesty beast -- clean it up
107     getBibMatchRows(pager: Pager): Observable<any> {
108
109         return new Observable(observer => {
110
111             this.getQueuedRecord().then(() => {
112
113                 const matches = this.queuedRecord.matches();
114                 const recIds = [];
115                 this.matchMap = {};
116                 matches.forEach(m => {
117                     this.matchMap[m.id()] = m;
118                     if (!recIds.includes(m.eg_record())) {
119                         recIds.push(m.eg_record());
120                     }
121                 });
122
123                 const bibSummaries: {[id: number]: BibRecordSummary} = {};
124                 this.bib.getBibSummary(recIds).subscribe(
125                     summary => bibSummaries[summary.id] = summary,
126                     err => {},
127                     ()  => {
128                         this.bib.fleshBibUsers(
129                             Object.values(bibSummaries).map(sum => sum.record)
130                         ).then(() => {
131                             matches.forEach(match => {
132                                 const row = {
133                                     id: match.id(),
134                                     eg_record: match.eg_record(),
135                                     bre_quality: match.quality(),
136                                     vqbr_quality: this.queuedRecord.quality(),
137                                     match_score: match.match_score(),
138                                     bib_summary: bibSummaries[match.eg_record()]
139                                 };
140
141                                 observer.next(row);
142                             });
143
144                             observer.complete();
145                         });
146                     }
147                 );
148             });
149         });
150     }
151
152     getQueuedRecord(): Promise<any> {
153         if (this.queuedRecord) {
154             return Promise.resolve('');
155         }
156         const idlClass = this.queueType === 'bib' ? 'vqbr' : 'vqar';
157         const flesh = {flesh: 1, flesh_fields: {}};
158         flesh.flesh_fields[idlClass] = ['matches'];
159         return this.pcrud.retrieve(idlClass, this.recordId, flesh)
160             .toPromise().then(rec => this.queuedRecord = rec);
161     }
162 }
163