1 import {Injectable, EventEmitter} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, tap, finalize} 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 import {CatalogSearchContext, CatalogSearchState} from './search-context';
10 import {BibRecordService, BibRecordSummary} from './bib-record.service';
11 import {BasketService} from './basket.service';
12 import {CATALOG_CCVM_FILTERS} from './search-context';
15 export class CatalogService {
17 ccvmMap: {[ccvm: string]: IdlObject[]} = {};
18 cmfMap: {[cmf: string]: IdlObject} = {};
19 copyLocations: IdlObject[];
21 // Keep a reference to the most recently retrieved facet data,
22 // since facet data is consistent across a given search.
23 // No need to re-fetch with every page of search data.
27 // Allow anyone to watch for completed searches.
28 onSearchComplete: EventEmitter<CatalogSearchContext>;
31 private idl: IdlService,
32 private net: NetService,
33 private org: OrgService,
34 private unapi: UnapiService,
35 private pcrud: PcrudService,
36 private bibService: BibRecordService,
37 private basket: BasketService
39 this.onSearchComplete = new EventEmitter<CatalogSearchContext>();
43 search(ctx: CatalogSearchContext): Promise<void> {
44 ctx.searchState = CatalogSearchState.SEARCHING;
47 return this.basketSearch(ctx);
48 } else if (ctx.marcSearch.isSearchable()) {
49 return this.marcSearch(ctx);
50 } else if (ctx.identSearch.isSearchable() &&
51 ctx.identSearch.queryType === 'item_barcode') {
52 return this.barcodeSearch(ctx);
54 return this.termSearch(ctx);
58 barcodeSearch(ctx: CatalogSearchContext): Promise<void> {
59 return this.net.request(
61 'open-ils.search.multi_home.bib_ids.by_barcode',
63 ).toPromise().then(ids => {
64 // API returns an event for not-found barcodes
65 if (!Array.isArray(ids)) { ids = []; }
68 ids: ids.map(id => [id])
71 this.applyResultData(ctx, result);
72 ctx.searchState = CatalogSearchState.COMPLETE;
73 this.onSearchComplete.emit(ctx);
77 // "Search" the basket by loading the IDs and treating
78 // them like a standard query search results set.
79 basketSearch(ctx: CatalogSearchContext): Promise<void> {
81 return this.basket.getRecordIds().then(ids => {
83 // Map our list of IDs into a search results object
84 // the search context can understand.
87 ids: ids.map(id => [id])
90 this.applyResultData(ctx, result);
91 ctx.searchState = CatalogSearchState.COMPLETE;
92 this.onSearchComplete.emit(ctx);
96 marcSearch(ctx: CatalogSearchContext): Promise<void> {
97 let method = 'open-ils.search.biblio.marc';
98 if (ctx.isStaff) { method += '.staff'; }
100 const queryStruct = ctx.compileMarcSearchArgs();
102 return this.net.request('open-ils.search', method, queryStruct)
103 .toPromise().then(result => {
104 // Match the query search return format
105 result.ids = result.ids.map(id => [id]);
107 this.applyResultData(ctx, result);
108 ctx.searchState = CatalogSearchState.COMPLETE;
109 this.onSearchComplete.emit(ctx);
113 termSearch(ctx: CatalogSearchContext): Promise<void> {
115 let method = 'open-ils.search.biblio.multiclass.query';
118 if (ctx.identSearch.isSearchable()) {
119 fullQuery = ctx.compileIdentSearchQuery();
122 fullQuery = ctx.compileTermSearchQuery();
124 if (ctx.termSearch.groupByMetarecord
125 && !ctx.termSearch.fromMetarecord) {
126 method = 'open-ils.search.metabib.multiclass.query';
129 if (ctx.termSearch.hasBrowseEntry) {
130 this.fetchBrowseEntry(ctx);
134 console.debug(`search query: ${fullQuery}`);
140 return this.net.request(
141 'open-ils.search', method, {
142 limit : ctx.pager.limit + 1,
143 offset : ctx.pager.offset
146 .then(result => this.applyResultData(ctx, result))
147 .then(_ => this.fetchFieldHighlights(ctx))
149 ctx.searchState = CatalogSearchState.COMPLETE;
150 this.onSearchComplete.emit(ctx);
154 // When showing titles linked to a browse entry, fetch
155 // the entry data as well so the UI can display it.
156 fetchBrowseEntry(ctx: CatalogSearchContext) {
157 const ts = ctx.termSearch;
159 const parts = ts.hasBrowseEntry.split(',');
160 const mbeId = parts[0];
161 const cmfId = parts[1];
163 this.pcrud.retrieve('mbe', mbeId)
164 .subscribe(mbe => ctx.termSearch.browseEntry = mbe);
167 applyResultData(ctx: CatalogSearchContext, result: any): void {
169 ctx.pager.resultCount = result.count;
171 // records[] tracks the current page of bib summaries.
174 // If this is a new search, reset the result IDs collection.
175 if (this.lastFacetKey !== result.facet_key) {
179 result.ids.forEach((blob, idx) => ctx.addResultId(blob[0], idx));
182 // Appends records to the search result set as they arrive.
183 // Returns a void promise once all records have been retrieved
184 fetchBibSummaries(ctx: CatalogSearchContext): Promise<void> {
186 const depth = ctx.global ?
187 ctx.org.root().ou_type().depth() :
188 ctx.searchOrg.ou_type().depth();
190 const isMeta = ctx.termSearch.isMetarecordSearch();
192 let observable: Observable<BibRecordSummary>;
195 observable = this.bibService.getMetabibSummaries(
196 ctx.currentResultIds(), ctx.searchOrg.id(), ctx.isStaff);
198 observable = this.bibService.getBibSummaries(
199 ctx.currentResultIds(), ctx.searchOrg.id(), ctx.isStaff);
202 return observable.pipe(map(summary => {
203 // Responses are not necessarily returned in request-ID order.
206 idx = ctx.currentResultIds().indexOf(summary.metabibId);
208 idx = ctx.currentResultIds().indexOf(summary.id);
211 if (ctx.result.records) {
212 // May be reset when quickly navigating results.
213 ctx.result.records[idx] = summary;
216 if (ctx.highlightData[summary.id]) {
217 summary.displayHighlights = ctx.highlightData[summary.id];
222 fetchFieldHighlights(ctx: CatalogSearchContext): Promise<any> {
226 // Extract the highlight map. Not all searches have them.
227 if ((hlMap = ctx.result) &&
228 (hlMap = hlMap.global_summary) &&
229 (hlMap = hlMap.query_struct) &&
230 (hlMap = hlMap.additional_data) &&
231 (hlMap = hlMap.highlight_map) &&
232 (Object.keys(hlMap).length > 0)) {
233 } else { return Promise.resolve(); }
236 if (ctx.getHighlightsFor) {
237 ids = [ctx.getHighlightsFor];
239 // ctx.currentResultIds() returns bib IDs or metabib IDs
240 // depending on the search type. If we have metabib IDs, map
241 // them to bib IDs for highlighting.
242 ids = ctx.currentResultIds();
243 if (ctx.termSearch.groupByMetarecord) {
244 // The 4th slot in the result ID reports the master record
245 // for the metarecord in question. Sometimes it's null?
246 ids = ctx.result.ids.map(id => id[4]).filter(id => id !== null);
250 return this.net.requestWithParamList( // API is list-based
252 'open-ils.search.fetch.metabib.display_field.highlight',
254 ).pipe(map(fields => {
256 if (fields.length === 0) { return; }
258 // Each 'fields' collection is an array of display field
259 // values whose text is augmented with highlighting markup.
260 const highlights = ctx.highlightData[fields[0].source] = {};
262 fields.forEach(field => {
263 const dfMap = this.cmfMap[field.field].display_field_map();
264 if (!dfMap) { return; } // pretty sure this can't happen.
266 if (dfMap.multi() === 't') {
267 if (!highlights[dfMap.name()]) {
268 highlights[dfMap.name()] = [];
270 (highlights[dfMap.name()] as string[]).push(field.highlight);
272 highlights[dfMap.name()] = field.highlight;
279 fetchFacets(ctx: CatalogSearchContext): Promise<void> {
282 return Promise.reject('Cannot fetch facets without results');
285 if (!ctx.result.facet_key) {
286 return Promise.resolve();
289 if (this.lastFacetKey === ctx.result.facet_key) {
290 ctx.result.facetData = this.lastFacetData;
291 return Promise.resolve();
294 return new Promise((resolve, reject) => {
295 this.net.request('open-ils.search',
296 'open-ils.search.facet_cache.retrieve',
298 ).subscribe(facets => {
299 const facetData = {};
300 Object.keys(facets).forEach(cmfId => {
301 const facetHash = facets[cmfId];
302 const cmf = this.cmfMap[cmfId];
305 Object.keys(facetHash).forEach(value => {
306 const count = facetHash[value];
307 cmfData.push({value : value, count : count});
310 if (!facetData[cmf.field_class()]) {
311 facetData[cmf.field_class()] = {};
314 facetData[cmf.field_class()][cmf.name()] = {
315 cmfLabel : cmf.label(),
316 valueList : cmfData.sort((a, b) => {
317 if (a.count > b.count) { return -1; }
318 if (a.count < b.count) { return 1; }
319 // secondary alpha sort on display value
320 return a.value < b.value ? -1 : 1;
325 this.lastFacetKey = ctx.result.facet_key;
326 this.lastFacetData = ctx.result.facetData = facetData;
332 fetchCcvms(): Promise<void> {
334 if (Object.keys(this.ccvmMap).length) {
335 return Promise.resolve();
338 return new Promise((resolve, reject) => {
339 this.pcrud.search('ccvm',
340 {ctype : CATALOG_CCVM_FILTERS}, {},
341 {atomic: true, anonymous: true}
342 ).subscribe(list => {
343 this.compileCcvms(list);
349 compileCcvms(ccvms: IdlObject[]): void {
350 ccvms.forEach(ccvm => {
351 if (!this.ccvmMap[ccvm.ctype()]) {
352 this.ccvmMap[ccvm.ctype()] = [];
354 this.ccvmMap[ccvm.ctype()].push(ccvm);
357 Object.keys(this.ccvmMap).forEach(cType => {
358 this.ccvmMap[cType] =
359 this.ccvmMap[cType].sort((a, b) => {
360 return a.value() < b.value() ? -1 : 1;
365 iconFormatLabel(code: string): string {
367 const ccvm = this.ccvmMap.icon_format.filter(
368 format => format.code() === code)[0];
370 return ccvm.search_label();
375 fetchCmfs(): Promise<void> {
376 if (Object.keys(this.cmfMap).length) {
377 return Promise.resolve();
380 return new Promise((resolve, reject) => {
381 this.pcrud.search('cmf',
382 {'-or': [{facet_field : 't'}, {display_field: 't'}]},
383 {flesh: 1, flesh_fields: {cmf: ['display_field_map']}},
384 {atomic: true, anonymous: true}
387 cmfs.forEach(c => this.cmfMap[c.id()] = c);
394 fetchCopyLocations(contextOrg: number | IdlObject): Promise<any> {
395 const orgIds = this.org.fullPath(contextOrg, true);
396 this.copyLocations = [];
398 return this.pcrud.search('acpl',
399 {deleted: 'f', opac_visible: 't', owning_lib: orgIds},
400 {order_by: {acpl: 'name'}},
402 ).pipe(tap(loc => this.copyLocations.push(loc))).toPromise();
405 browse(ctx: CatalogSearchContext): Observable<any> {
406 ctx.searchState = CatalogSearchState.SEARCHING;
407 const bs = ctx.browseSearch;
409 let method = 'open-ils.search.browse';
414 return this.net.request(
416 'open-ils.search.browse.staff', {
417 browse_class: bs.fieldClass,
419 limit : ctx.pager.limit,
421 org_unit: ctx.searchOrg.id()
424 tap(result => ctx.searchState = CatalogSearchState.COMPLETE),
425 finalize(() => this.onSearchComplete.emit(ctx))
429 cnBrowse(ctx: CatalogSearchContext): Observable<any> {
430 ctx.searchState = CatalogSearchState.SEARCHING;
431 const cbs = ctx.cnBrowseSearch;
433 return this.net.request(
435 'open-ils.supercat.call_number.browse',
436 cbs.value, ctx.searchOrg.shortname(), cbs.limit, cbs.offset
437 ).pipe(tap(result => ctx.searchState = CatalogSearchState.COMPLETE));