LP1881607 Angular catalog e-resource links display
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / share / catalog / bib-record.service.ts
1 import {Injectable} from '@angular/core';
2 import {Observable, from} from 'rxjs';
3 import {mergeMap, map, tap} from 'rxjs/operators';
4 import {OrgService} from '@eg/core/org.service';
5 import {UnapiService} from '@eg/share/catalog/unapi.service';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {NetService} from '@eg/core/net.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9
10 export const NAMESPACE_MAPS = {
11     'mods':     'http://www.loc.gov/mods/v3',
12     'biblio':   'http://open-ils.org/spec/biblio/v1',
13     'holdings': 'http://open-ils.org/spec/holdings/v1',
14     'indexing': 'http://open-ils.org/spec/indexing/v1'
15 };
16
17 export const HOLDINGS_XPATH =
18     '/holdings:holdings/holdings:counts/holdings:count';
19
20 interface EResourceUrl {
21     href: string;
22     note: string;
23     label: string;
24 }
25
26 export class BibRecordSummary {
27     id: number; // == record.id() for convenience
28     metabibId: number; // If present, this is a metabib summary
29     metabibRecords: number[]; // all constituent bib records
30     orgId: number;
31     orgDepth: number;
32     record: IdlObject;
33     display: any;
34     attributes: any;
35     holdingsSummary: any;
36     holdCount: number;
37     bibCallNumber: string;
38     net: NetService;
39     displayHighlights: {[name: string]: string | string[]} = {};
40     eResourceUrls: EResourceUrl[] = [];
41
42     constructor(record: IdlObject, orgId: number, orgDepth?: number) {
43         this.id = Number(record.id());
44         this.record = record;
45         this.orgId = orgId;
46         this.orgDepth = orgDepth;
47         this.display = {};
48         this.attributes = {};
49         this.bibCallNumber = null;
50         this.metabibRecords = [];
51     }
52
53     // Get -> Set -> Return bib-level call number
54     getBibCallNumber(): Promise<string> {
55
56         if (this.bibCallNumber !== null) {
57             return Promise.resolve(this.bibCallNumber);
58         }
59
60         return this.net.request(
61             'open-ils.cat',
62             'open-ils.cat.biblio.record.marc_cn.retrieve',
63             this.id, null, this.orgId
64         ).toPromise().then(cnArray => {
65             if (cnArray && cnArray.length > 0) {
66                 const key1 = Object.keys(cnArray[0])[0];
67                 this.bibCallNumber = cnArray[0][key1];
68             } else {
69                 this.bibCallNumber = '';
70             }
71             return this.bibCallNumber;
72         });
73     }
74 }
75
76 @Injectable()
77 export class BibRecordService {
78
79     // Cache of bib editor / creator objects
80     // Assumption is this list will be limited in size.
81     userCache: {[id: number]: IdlObject};
82
83     constructor(
84         private idl: IdlService,
85         private net: NetService,
86         private org: OrgService,
87         private unapi: UnapiService,
88         private pcrud: PcrudService
89     ) {
90         this.userCache = {};
91     }
92
93     getBibSummary(id: number,
94         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
95         return this.getBibSummaries([id], orgId, isStaff);
96     }
97
98     getBibSummaries(bibIds: number[],
99         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
100
101         if (bibIds.length === 0) { return from([]); }
102         if (!orgId) { orgId = this.org.root().id(); }
103
104         let method = 'open-ils.search.biblio.record.catalog_summary';
105         if (isStaff) { method += '.staff'; }
106
107         return this.net.request('open-ils.search', method, orgId, bibIds)
108         .pipe(map(bibSummary => {
109             const summary = new BibRecordSummary(bibSummary.record, orgId);
110             summary.net = this.net; // inject
111             summary.display = bibSummary.display;
112             summary.attributes = bibSummary.attributes;
113             summary.holdCount = bibSummary.hold_count;
114             summary.holdingsSummary = bibSummary.copy_counts;
115             summary.eResourceUrls = bibSummary.urls;
116             return summary;
117         }));
118     }
119
120     getMetabibSummaries(metabibIds: number[],
121         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
122
123         if (metabibIds.length === 0) { return from([]); }
124         if (!orgId) { orgId = this.org.root().id(); }
125
126         let method = 'open-ils.search.biblio.metabib.catalog_summary';
127         if (isStaff) { method += '.staff'; }
128
129         return this.net.request('open-ils.search', method, orgId, metabibIds)
130         .pipe(map(metabibSummary => {
131             const summary = new BibRecordSummary(metabibSummary.record, orgId);
132             summary.net = this.net; // inject
133             summary.metabibId = Number(metabibSummary.metabib_id);
134             summary.metabibRecords = metabibSummary.metabib_records;
135             summary.display = metabibSummary.display;
136             summary.attributes = metabibSummary.attributes;
137             summary.holdCount = metabibSummary.hold_count;
138             summary.holdingsSummary = metabibSummary.copy_counts;
139             return summary;
140         }));
141     }
142 }
143
144