]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/catalog/bib-record.service.ts
LP1889694 Staff catalog record summary API
[Evergreen.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
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
25     orgId: number;
26     orgDepth: number;
27     record: IdlObject;
28     display: any;
29     attributes: any;
30     holdingsSummary: any;
31     holdCount: number;
32     bibCallNumber: string;
33     net: NetService;
34     displayHighlights: {[name: string]: string | string[]} = {};
35
36     constructor(record: IdlObject, orgId: number, orgDepth?: number) {
37         this.id = Number(record.id());
38         this.record = record;
39         this.orgId = orgId;
40         this.orgDepth = orgDepth;
41         this.display = {};
42         this.attributes = {};
43         this.bibCallNumber = null;
44         this.metabibRecords = [];
45     }
46
47     // Get -> Set -> Return bib-level call number
48     getBibCallNumber(): Promise<string> {
49
50         if (this.bibCallNumber !== null) {
51             return Promise.resolve(this.bibCallNumber);
52         }
53
54         return this.net.request(
55             'open-ils.cat',
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];
62             } else {
63                 this.bibCallNumber = '';
64             }
65             return this.bibCallNumber;
66         });
67     }
68 }
69
70 @Injectable()
71 export class BibRecordService {
72
73     // Cache of bib editor / creator objects
74     // Assumption is this list will be limited in size.
75     userCache: {[id: number]: IdlObject};
76
77     constructor(
78         private idl: IdlService,
79         private net: NetService,
80         private org: OrgService,
81         private unapi: UnapiService,
82         private pcrud: PcrudService
83     ) {
84         this.userCache = {};
85     }
86
87     getBibSummary(id: number,
88         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
89         return this.getBibSummaries([id], orgId, isStaff);
90     }
91
92     getBibSummaries(bibIds: number[],
93         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
94
95         if (bibIds.length === 0) { return from([]); }
96         if (!orgId) { orgId = this.org.root().id(); }
97
98         let method = 'open-ils.search.biblio.record.catalog_summary';
99         if (isStaff) { method += '.staff'; }
100
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;
109             return summary;
110         }));
111     }
112
113     getMetabibSummaries(metabibIds: number[],
114         orgId?: number, isStaff?: boolean): Observable<BibRecordSummary> {
115
116         if (metabibIds.length === 0) { return from([]); }
117         if (!orgId) { orgId = this.org.root().id(); }
118
119         let method = 'open-ils.search.biblio.metabib.catalog_summary';
120         if (isStaff) { method += '.staff'; }
121
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;
132             return summary;
133         }));
134     }
135 }
136
137