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';
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'
17 export const HOLDINGS_XPATH =
18 '/holdings:holdings/holdings:counts/holdings:count';
20 interface EResourceUrl {
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
37 bibCallNumber: string;
39 displayHighlights: {[name: string]: string | string[]} = {};
40 eResourceUrls: EResourceUrl[] = [];
42 constructor(record: IdlObject, orgId: number, orgDepth?: number) {
43 this.id = Number(record.id());
46 this.orgDepth = orgDepth;
49 this.bibCallNumber = null;
50 this.metabibRecords = [];
53 // Get -> Set -> Return bib-level call number
54 getBibCallNumber(): Promise<string> {
56 if (this.bibCallNumber !== null) {
57 return Promise.resolve(this.bibCallNumber);
60 return this.net.request(
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];
69 this.bibCallNumber = '';
71 return this.bibCallNumber;
77 export class BibRecordService {
79 // Cache of bib editor / creator objects
80 // Assumption is this list will be limited in size.
81 userCache: {[id: number]: IdlObject};
84 private idl: IdlService,
85 private net: NetService,
86 private org: OrgService,
87 private unapi: UnapiService,
88 private pcrud: PcrudService
93 getBibSummary(id: number,
94 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
95 return this.getBibSummaries([id], orgId, isStaff);
98 getBibSummaries(bibIds: number[],
99 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
101 if (bibIds.length === 0) { return from([]); }
102 if (!orgId) { orgId = this.org.root().id(); }
104 let method = 'open-ils.search.biblio.record.catalog_summary';
105 if (isStaff) { method += '.staff'; }
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;
120 getMetabibSummaries(metabibIds: number[],
121 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
123 if (metabibIds.length === 0) { return from([]); }
124 if (!orgId) { orgId = this.org.root().id(); }
126 let method = 'open-ils.search.biblio.metabib.catalog_summary';
127 if (isStaff) { method += '.staff'; }
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;