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