LP#1759343 Clean up data seed values
[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') bibGrid: GridComponent;
25     @ViewChild('authGrid') 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     constructor(
35         private router: Router,
36         private route: ActivatedRoute,
37         private evt: EventService,
38         private net: NetService,
39         private auth: AuthService,
40         private pcrud: PcrudService,
41         private bib: BibRecordService,
42         private vandelay: VandelayService) {
43
44         this.bibDataSource = new GridDataSource();
45         this.authDataSource = new GridDataSource();
46
47         this.bibDataSource.getRows = (pager: Pager) => {
48             return this.getBibMatchRows(pager);
49         };
50
51         /* TODO
52         this.authDataSource.getRows = (pager: Pager) => {
53         }
54         */
55
56         // Mark or un-mark as row as the merge target on row click
57         this.matchRowClick = (row: any) => {
58             this.toggleMergeTarget(row.id);
59         };
60     }
61
62     toggleMergeTarget(matchId: number) {
63
64         if (this.isOverlayTarget(matchId)) {
65
66             // clear selection on secondary click;
67             delete this.vandelay.importSelection.overlayMap[this.recordId];
68
69         } else {
70             // Add to selection.
71             // Start a new one if necessary, which will be adopted
72             // and completed by the queue UI before import.
73
74             let selection = this.vandelay.importSelection;
75             if (!selection) {
76                 selection = new VandelayImportSelection();
77                 this.vandelay.importSelection = selection;
78             }
79             const match = this.matchMap[matchId];
80             selection.overlayMap[this.recordId] = match.eg_record();
81         }
82     }
83
84     isOverlayTarget(matchId: number): boolean {
85         const selection = this.vandelay.importSelection;
86         if (selection) {
87             const match = this.matchMap[matchId];
88             return selection.overlayMap[this.recordId] === match.eg_record();
89         }
90         return false;
91     }
92
93     ngOnInit() {}
94
95     // This thing is a nesty beast -- clean it up
96     getBibMatchRows(pager: Pager): Observable<any> {
97
98         return new Observable(observer => {
99
100             this.getQueuedRecord().then(() => {
101
102                 const matches = this.queuedRecord.matches();
103                 const recIds = [];
104                 this.matchMap = {};
105                 matches.forEach(m => {
106                     this.matchMap[m.id()] = m;
107                     if (!recIds.includes(m.eg_record())) {
108                         recIds.push(m.eg_record());
109                     }
110                 });
111
112                 const bibSummaries: {[id: number]: BibRecordSummary} = {};
113                 this.bib.getBibSummary(recIds).subscribe(
114                     summary => bibSummaries[summary.id] = summary,
115                     err => {},
116                     ()  => {
117                         this.bib.fleshBibUsers(
118                             Object.values(bibSummaries).map(sum => sum.record)
119                         ).then(() => {
120                             matches.forEach(match => {
121                                 const row = {
122                                     id: match.id(),
123                                     eg_record: match.eg_record(),
124                                     bre_quality: match.quality(),
125                                     vqbr_quality: this.queuedRecord.quality(),
126                                     match_score: match.match_score(),
127                                     bib_summary: bibSummaries[match.eg_record()]
128                                 };
129
130                                 observer.next(row);
131                             });
132
133                             observer.complete();
134                         });
135                     }
136                 );
137             });
138         });
139     }
140
141     getQueuedRecord(): Promise<any> {
142         if (this.queuedRecord) {
143             return Promise.resolve('');
144         }
145         const idlClass = this.queueType === 'bib' ? 'vqbr' : 'vqar';
146         const flesh = {flesh: 1, flesh_fields: {}};
147         flesh.flesh_fields[idlClass] = ['matches'];
148         return this.pcrud.retrieve(idlClass, this.recordId, flesh)
149             .toPromise().then(rec => this.queuedRecord = rec);
150     }
151 }
152