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';
21 export class BibRecordSummary {
22 id: number; // == record.id() for convenience
23 metabibId: number; // If present, this is a metabib summary
24 metabibRecords: number[]; // all constituent bib records
32 bibCallNumber: string;
34 displayHighlights: {[name: string]: string | string[]} = {};
36 constructor(record: IdlObject, orgId: number, orgDepth?: number) {
37 this.id = Number(record.id());
40 this.orgDepth = orgDepth;
43 this.bibCallNumber = null;
44 this.metabibRecords = [];
47 // Get -> Set -> Return bib-level call number
48 getBibCallNumber(): Promise<string> {
50 if (this.bibCallNumber !== null) {
51 return Promise.resolve(this.bibCallNumber);
54 return this.net.request(
56 'open-ils.cat.biblio.record.marc_cn.retrieve',
57 this.id, null, this.orgId
58 ).toPromise().then(cnArray => {
59 if (cnArray && cnArray.length > 0) {
60 const key1 = Object.keys(cnArray[0])[0];
61 this.bibCallNumber = cnArray[0][key1];
63 this.bibCallNumber = '';
65 return this.bibCallNumber;
71 export class BibRecordService {
73 // Cache of bib editor / creator objects
74 // Assumption is this list will be limited in size.
75 userCache: {[id: number]: IdlObject};
78 private idl: IdlService,
79 private net: NetService,
80 private org: OrgService,
81 private unapi: UnapiService,
82 private pcrud: PcrudService
87 getBibSummary(id: number,
88 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
89 return this.getBibSummaries([id], orgId, isStaff);
92 getBibSummaries(bibIds: number[],
93 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
95 if (bibIds.length === 0) { return from([]); }
96 if (!orgId) { orgId = this.org.root().id(); }
98 let method = 'open-ils.search.biblio.record.catalog_summary';
99 if (isStaff) { method += '.staff'; }
101 return this.net.request('open-ils.search', method, orgId, bibIds)
102 .pipe(map(bibSummary => {
103 const summary = new BibRecordSummary(bibSummary.record, orgId);
104 summary.net = this.net; // inject
105 summary.display = bibSummary.display;
106 summary.attributes = bibSummary.attributes;
107 summary.holdCount = bibSummary.hold_count;
108 summary.holdingsSummary = bibSummary.copy_counts;
113 getMetabibSummaries(metabibIds: number[],
114 orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
116 if (metabibIds.length === 0) { return from([]); }
117 if (!orgId) { orgId = this.org.root().id(); }
119 let method = 'open-ils.search.biblio.metabib.catalog_summary';
120 if (isStaff) { method += '.staff'; }
122 return this.net.request('open-ils.search', method, orgId, metabibIds)
123 .pipe(map(metabibSummary => {
124 const summary = new BibRecordSummary(metabibSummary.record, orgId);
125 summary.net = this.net; // inject
126 summary.metabibId = Number(metabibSummary.metabib_id);
127 summary.metabibRecords = metabibSummary.metabib_records;
128 summary.display = metabibSummary.display;
129 summary.attributes = metabibSummary.attributes;
130 summary.holdCount = metabibSummary.hold_count;
131 summary.holdingsSummary = metabibSummary.copy_counts;