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 => {
66 ids: ids.map(id => [id])
69 this.applyResultData(ctx, result);
70 ctx.searchState = CatalogSearchState.COMPLETE;
71 this.onSearchComplete.emit(ctx);
75 // "Search" the basket by loading the IDs and treating
76 // them like a standard query search results set.
77 basketSearch(ctx: CatalogSearchContext): Promise<void> {
79 return this.basket.getRecordIds().then(ids => {
81 // Map our list of IDs into a search results object
82 // the search context can understand.
85 ids: ids.map(id => [id])
88 this.applyResultData(ctx, result);
89 ctx.searchState = CatalogSearchState.COMPLETE;
90 this.onSearchComplete.emit(ctx);
94 marcSearch(ctx: CatalogSearchContext): Promise<void> {
95 let method = 'open-ils.search.biblio.marc';
96 if (ctx.isStaff) { method += '.staff'; }
98 const queryStruct = ctx.compileMarcSearchArgs();
100 return this.net.request('open-ils.search', method, queryStruct)
101 .toPromise().then(result => {
102 // Match the query search return format
103 result.ids = result.ids.map(id => [id]);
105 this.applyResultData(ctx, result);
106 ctx.searchState = CatalogSearchState.COMPLETE;
107 this.onSearchComplete.emit(ctx);
111 termSearch(ctx: CatalogSearchContext): Promise<void> {
113 let method = 'open-ils.search.biblio.multiclass.query';
116 if (ctx.identSearch.isSearchable()) {
117 fullQuery = ctx.compileIdentSearchQuery();
120 fullQuery = ctx.compileTermSearchQuery();
122 if (ctx.termSearch.groupByMetarecord
123 && !ctx.termSearch.fromMetarecord) {
124 method = 'open-ils.search.metabib.multiclass.query';
127 if (ctx.termSearch.hasBrowseEntry) {
128 this.fetchBrowseEntry(ctx);
132 console.debug(`search query: ${fullQuery}`);
138 return this.net.request(
139 'open-ils.search', method, {
140 limit : ctx.pager.limit + 1,
141 offset : ctx.pager.offset
144 .then(result => this.applyResultData(ctx, result))
145 .then(_ => this.fetchFieldHighlights(ctx))
147 ctx.searchState = CatalogSearchState.COMPLETE;
148 this.onSearchComplete.emit(ctx);
152 // When showing titles linked to a browse entry, fetch
153 // the entry data as well so the UI can display it.
154 fetchBrowseEntry(ctx: CatalogSearchContext) {
155 const ts = ctx.termSearch;
157 const parts = ts.hasBrowseEntry.split(',');
158 const mbeId = parts[0];
159 const cmfId = parts[1];
161 this.pcrud.retrieve('mbe', mbeId)
162 .subscribe(mbe => ctx.termSearch.browseEntry = mbe);
165 applyResultData(ctx: CatalogSearchContext, result: any): void {
167 ctx.pager.resultCount = result.count;
169 // records[] tracks the current page of bib summaries.
172 // If this is a new search, reset the result IDs collection.
173 if (this.lastFacetKey !== result.facet_key) {
177 result.ids.forEach((blob, idx) => ctx.addResultId(blob[0], idx));
180 // Appends records to the search result set as they arrive.
181 // Returns a void promise once all records have been retrieved
182 fetchBibSummaries(ctx: CatalogSearchContext): Promise<void> {
184 const depth = ctx.global ?
185 ctx.org.root().ou_type().depth() :
186 ctx.searchOrg.ou_type().depth();
188 const isMeta = ctx.termSearch.isMetarecordSearch();
190 let observable: Observable<BibRecordSummary>;
193 observable = this.bibService.getMetabibSummary(
194 ctx.currentResultIds(), ctx.searchOrg.id(), depth);
196 observable = this.bibService.getBibSummary(
197 ctx.currentResultIds(), ctx.searchOrg.id(), depth);
200 return observable.pipe(map(summary => {
201 // Responses are not necessarily returned in request-ID order.
204 idx = ctx.currentResultIds().indexOf(summary.metabibId);
206 idx = ctx.currentResultIds().indexOf(summary.id);
209 if (ctx.result.records) {
210 // May be reset when quickly navigating results.
211 ctx.result.records[idx] = summary;
214 if (ctx.highlightData[summary.id]) {
215 summary.displayHighlights = ctx.highlightData[summary.id];
220 fetchFieldHighlights(ctx: CatalogSearchContext): Promise<any> {
224 // Extract the highlight map. Not all searches have them.
225 if ((hlMap = ctx.result) &&
226 (hlMap = hlMap.global_summary) &&
227 (hlMap = hlMap.query_struct) &&
228 (hlMap = hlMap.additional_data) &&
229 (hlMap = hlMap.highlight_map) &&
230 (Object.keys(hlMap).length > 0)) {
231 } else { return Promise.resolve(); }
234 if (ctx.getHighlightsFor) {
235 ids = [ctx.getHighlightsFor];
237 // ctx.currentResultIds() returns bib IDs or metabib IDs
238 // depending on the search type. If we have metabib IDs, map
239 // them to bib IDs for highlighting.
240 ids = ctx.currentResultIds();
241 if (ctx.termSearch.groupByMetarecord) {
242 ids = ids.map(mrId =>
243 ctx.result.records.filter(r => mrId === r.metabibId)[0].id
248 return this.net.requestWithParamList( // API is list-based
250 'open-ils.search.fetch.metabib.display_field.highlight',
252 ).pipe(map(fields => {
254 if (fields.length === 0) { return; }
256 // Each 'fields' collection is an array of display field
257 // values whose text is augmented with highlighting markup.
258 const highlights = ctx.highlightData[fields[0].source] = {};
260 fields.forEach(field => {
261 const dfMap = this.cmfMap[field.field].display_field_map();
262 if (!dfMap) { return; } // pretty sure this can't happen.
264 if (dfMap.multi() === 't') {
265 if (!highlights[dfMap.name()]) {
266 highlights[dfMap.name()] = [];
268 (highlights[dfMap.name()] as string[]).push(field.highlight);
270 highlights[dfMap.name()] = field.highlight;
277 fetchFacets(ctx: CatalogSearchContext): Promise<void> {
280 return Promise.reject('Cannot fetch facets without results');
283 if (!ctx.result.facet_key) {
284 return Promise.resolve();
287 if (this.lastFacetKey === ctx.result.facet_key) {
288 ctx.result.facetData = this.lastFacetData;
289 return Promise.resolve();
292 return new Promise((resolve, reject) => {
293 this.net.request('open-ils.search',
294 'open-ils.search.facet_cache.retrieve',
296 ).subscribe(facets => {
297 const facetData = {};
298 Object.keys(facets).forEach(cmfId => {
299 const facetHash = facets[cmfId];
300 const cmf = this.cmfMap[cmfId];
303 Object.keys(facetHash).forEach(value => {
304 const count = facetHash[value];
305 cmfData.push({value : value, count : count});
308 if (!facetData[cmf.field_class()]) {
309 facetData[cmf.field_class()] = {};
312 facetData[cmf.field_class()][cmf.name()] = {
313 cmfLabel : cmf.label(),
314 valueList : cmfData.sort((a, b) => {
315 if (a.count > b.count) { return -1; }
316 if (a.count < b.count) { return 1; }
317 // secondary alpha sort on display value
318 return a.value < b.value ? -1 : 1;
323 this.lastFacetKey = ctx.result.facet_key;
324 this.lastFacetData = ctx.result.facetData = facetData;
330 fetchCcvms(): Promise<void> {
332 if (Object.keys(this.ccvmMap).length) {
333 return Promise.resolve();
336 return new Promise((resolve, reject) => {
337 this.pcrud.search('ccvm',
338 {ctype : CATALOG_CCVM_FILTERS}, {},
339 {atomic: true, anonymous: true}
340 ).subscribe(list => {
341 this.compileCcvms(list);
347 compileCcvms(ccvms: IdlObject[]): void {
348 ccvms.forEach(ccvm => {
349 if (!this.ccvmMap[ccvm.ctype()]) {
350 this.ccvmMap[ccvm.ctype()] = [];
352 this.ccvmMap[ccvm.ctype()].push(ccvm);
355 Object.keys(this.ccvmMap).forEach(cType => {
356 this.ccvmMap[cType] =
357 this.ccvmMap[cType].sort((a, b) => {
358 return a.value() < b.value() ? -1 : 1;
363 iconFormatLabel(code: string): string {
365 const ccvm = this.ccvmMap.icon_format.filter(
366 format => format.code() === code)[0];
368 return ccvm.search_label();
373 fetchCmfs(): Promise<void> {
374 if (Object.keys(this.cmfMap).length) {
375 return Promise.resolve();
378 return new Promise((resolve, reject) => {
379 this.pcrud.search('cmf',
380 {'-or': [{facet_field : 't'}, {display_field: 't'}]},
381 {flesh: 1, flesh_fields: {cmf: ['display_field_map']}},
382 {atomic: true, anonymous: true}
385 cmfs.forEach(c => this.cmfMap[c.id()] = c);
392 fetchCopyLocations(contextOrg: number | IdlObject): Promise<any> {
393 const orgIds = this.org.fullPath(contextOrg, true);
394 this.copyLocations = [];
396 return this.pcrud.search('acpl',
397 {deleted: 'f', opac_visible: 't', owning_lib: orgIds},
398 {order_by: {acpl: 'name'}},
400 ).pipe(tap(loc => this.copyLocations.push(loc))).toPromise();
403 browse(ctx: CatalogSearchContext): Observable<any> {
404 ctx.searchState = CatalogSearchState.SEARCHING;
405 const bs = ctx.browseSearch;
407 let method = 'open-ils.search.browse';
412 return this.net.request(
414 'open-ils.search.browse.staff', {
415 browse_class: bs.fieldClass,
417 limit : ctx.pager.limit,
419 org_unit: ctx.searchOrg.id()
422 tap(result => ctx.searchState = CatalogSearchState.COMPLETE),
423 finalize(() => this.onSearchComplete.emit(ctx))
427 cnBrowse(ctx: CatalogSearchContext): Observable<any> {
428 ctx.searchState = CatalogSearchState.SEARCHING;
429 const cbs = ctx.cnBrowseSearch;
431 return this.net.request(
433 'open-ils.supercat.call_number.browse',
434 cbs.value, ctx.searchOrg.shortname(), ctx.pager.limit, cbs.offset
435 ).pipe(tap(result => ctx.searchState = CatalogSearchState.COMPLETE));