1 import {Injectable, EventEmitter} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, tap, distinct} from 'rxjs/operators';
4 import {StoreService} from '@eg/core/store.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {NetService} from '@eg/core/net.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {ContextMenuEntry} from '@eg/share/context-menu/context-menu.service';
11 const DEFAULT_MARC_FORMAT = 'marc21';
13 interface TagTableSelector {
15 marcRecordType: 'biblio' | 'authority' | 'serial';
17 // MARC record fixed field "Type" value.
21 export class TagTable {
28 selector: TagTableSelector;
30 // Current set of tags in list and map form.
31 tagMap: {[tag: string]: any};
34 fieldTags: ContextMenuEntry[];
36 // Cache of compiled, sorted, munged data. Stuff the UI requests
37 // frequently for selectors, etc.
38 cache: {[valueType: string]: {[which: string]: any}} = {
50 selector: TagTableSelector
56 this.selector = selector;
60 load(): Promise<any> {
64 this.getFfValueTable(),
68 // Various data needs munging for display. Cached the modified
69 // values since they are refernced repeatedly by the UI code.
70 fromCache(dataType: string, which?: string, which2?: string): ContextMenuEntry[] {
71 const part1 = this.cache[dataType][which];
81 toCache(dataType: string, which: string,
82 which2: string, values: ContextMenuEntry[]): ContextMenuEntry[] {
83 const base = this.cache[dataType];
84 const part1 = base[which];
87 if (!base[which]) { base[which] = {}; }
88 base[which][which2] = values;
96 getFfPosTable(): Promise<any> {
97 const storeKey = 'FFPosTable_' + this.selector.ffType;
99 if (this.ffPosTable) {
100 return Promise.resolve(this.ffPosTable);
103 this.ffPosTable = this.store.getLocalItem(storeKey);
105 if (this.ffPosTable) {
106 return Promise.resolve(this.ffPosTable);
109 return this.net.request(
110 'open-ils.fielder', 'open-ils.fielder.cmfpm.atomic',
111 {query: {tag: {'!=' : '006'}, rec_type: this.selector.ffType}}
113 ).toPromise().then(table => {
114 this.store.setLocalItem(storeKey, table);
115 return this.ffPosTable = table;
119 // ffType is the fixed field Type value. BKS, AUT, etc.
120 // See config.marc21_rec_type_map
121 getFfValueTable(): Promise<any> {
123 const storeKey = 'FFValueTable_' + this.selector.ffType;
125 if (this.ffValueTable) {
126 return Promise.resolve(this.ffValueTable);
129 this.ffValueTable = this.store.getLocalItem(storeKey);
131 if (this.ffValueTable) {
132 return Promise.resolve(this.ffValueTable);
135 return this.net.request(
137 'open-ils.cat.biblio.fixed_field_values.by_rec_type',
140 ).toPromise().then(table => {
141 this.store.setLocalItem(storeKey, table);
142 return this.ffValueTable = table;
146 loadTagTable(): Promise<any> {
148 const sel = this.selector;
151 `current_tag_table_${sel.marcFormat}_${sel.marcRecordType}`;
153 this.tagMap = this.store.getLocalItem(cacheKey);
156 return Promise.resolve(this.tagMap);
159 return this.fetchTagTable().then(_ => {
160 this.store.setLocalItem(cacheKey, this.tagMap);
165 fetchTagTable(): Promise<any> {
167 return this.net.request(
169 'open-ils.cat.tag_table.all.retrieve.local',
170 this.auth.token(), this.selector.marcFormat,
171 this.selector.marcRecordType
172 ).pipe(tap(tagData => {
173 this.tagMap[tagData.tag] = tagData;
177 getSubfieldCodes(tag: string): ContextMenuEntry[] {
178 if (!tag || !this.tagMap[tag]) { return null; }
180 const cached = this.fromCache('sfcodes', tag);
182 const list = this.tagMap[tag].subfields.map(sf => ({
184 label: `${sf.code}: ${sf.description}`
186 .sort((a, b) => a.label < b.label ? -1 : 1);
188 return this.toCache('sfcodes', tag, null, list);
191 getSubfieldLabel(tag: string, sfCode: string): string {
192 if (!tag || !this.tagMap[tag]) { return null; }
193 const subfieldResults = this.tagMap[tag].subfields.filter(sf => sf.code === sfCode);
194 return subfieldResults.length ? subfieldResults[0].description : null;
198 getFieldTags(): ContextMenuEntry[] {
200 if (!this.fieldTags) {
201 this.fieldTags = Object.keys(this.tagMap)
202 .filter(tag => Boolean(this.tagMap[tag]))
205 label: `${tag}: ${this.tagMap[tag].name}`
207 .sort((a, b) => a.label < b.label ? -1 : 1);
210 return this.fieldTags;
213 getSubfieldValues(tag: string, sfCode: string): ContextMenuEntry[] {
214 if (!tag || !this.tagMap[tag]) { return []; }
216 const cached = this.fromCache('sfvalues', tag, sfCode);
217 if (cached) { return cached; }
219 const list: ContextMenuEntry[] = [];
221 this.tagMap[tag].subfields
223 sf.code === sfCode && sf.hasOwnProperty('value_list'))
225 sf.value_list.forEach(value => {
227 let label = value.description || value.code;
228 const code = value.code || label;
229 if (code !== label) { label = `${code}: ${label}`; }
231 list.push({value: code, label: label});
235 return this.toCache('sfvalues', tag, sfCode, list);
238 getIndicatorValues(tag: string, which: 'ind1' | 'ind2'): ContextMenuEntry[] {
239 if (!tag || !this.tagMap[tag]) { return; }
241 const cached = this.fromCache('indicators', tag, which);
242 if (cached) { return cached; }
244 let values = this.tagMap[tag][which];
245 if (!values) { return; }
247 values = values.map(value => ({
249 label: `${value.code}: ${value.description}`
251 .sort((a, b) => a.label < b.label ? -1 : 1);
253 return this.toCache('indicators', tag, which, values);
257 getFfFieldMeta(fieldCode: string): Promise<IdlObject> {
258 return this.getFfPosTable().then(table => {
260 // Best I can tell, the AngJS MARC editor stores the
261 // full POS table for all record types in every copy of
262 // the table, hence the seemingly extraneous check in ffType.
265 field.fixed_field === fieldCode
266 && field.rec_type === this.selector.ffType
272 // Assumes getFfPosTable and getFfValueTable have already been
273 // invoked for the requested record type.
274 getFfValues(fieldCode: string): ContextMenuEntry[] {
276 const cached = this.fromCache('ffvalues', fieldCode);
277 if (cached) { return cached; }
279 let values = this.ffValueTable;
281 if (!values || !values[fieldCode]) { return null; }
283 // extract the canned set of possible values for our
284 // fixed field. Ignore those whose value exceeds the
285 // specified field length.
286 values = values[fieldCode]
287 .filter(val => val[0].length <= val[2])
288 .map(val => ({value: val[0], label: `${val[0]}: ${val[1]}`}))
289 .sort((a, b) => a.label < b.label ? -1 : 1);
291 return this.toCache('ffvalues', fieldCode, null, values);
297 export class TagTableService {
299 tagTables: {[marcRecordType: string]: TagTable} = {};
300 controlledBibTags: string[];
303 private store: StoreService,
304 private auth: AuthService,
305 private net: NetService,
306 private pcrud: PcrudService,
309 loadTags(selector: TagTableSelector): Promise<TagTable> {
310 if (!selector.marcFormat) {
311 selector.marcFormat = DEFAULT_MARC_FORMAT;
314 // Tag tables of a given marc record type are identical.
315 if (this.tagTables[selector.marcRecordType]) {
316 return Promise.resolve(this.tagTables[selector.marcRecordType]);
319 const tt = new TagTable(
320 this.store, this.auth, this.net, this.pcrud, selector);
322 this.tagTables[selector.marcRecordType] = tt;
324 return tt.load().then(_ => tt);
327 getControlledBibTags(): Promise<string[]> {
328 if (this.controlledBibTags) {
329 return Promise.resolve(this.controlledBibTags);
332 this.controlledBibTags = [];
333 return this.pcrud.retrieveAll('acsbf', {select: ['tag']})
335 map(field => field.tag()),
337 map(tag => this.controlledBibTags.push(tag))
338 ).toPromise().then(_ => this.controlledBibTags);