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 getFieldTags(): ContextMenuEntry[] {
193 if (!this.fieldTags) {
194 this.fieldTags = Object.keys(this.tagMap)
195 .filter(tag => Boolean(this.tagMap[tag]))
198 label: `${tag}: ${this.tagMap[tag].name}`
200 .sort((a, b) => a.label < b.label ? -1 : 1);
203 return this.fieldTags;
206 getSubfieldValues(tag: string, sfCode: string): ContextMenuEntry[] {
207 if (!tag || !this.tagMap[tag]) { return []; }
209 const cached = this.fromCache('sfvalues', tag, sfCode);
210 if (cached) { return cached; }
212 const list: ContextMenuEntry[] = [];
214 this.tagMap[tag].subfields
216 sf.code === sfCode && sf.hasOwnProperty('value_list'))
218 sf.value_list.forEach(value => {
220 let label = value.description || value.code;
221 const code = value.code || label;
222 if (code !== label) { label = `${code}: ${label}`; }
224 list.push({value: code, label: label});
228 return this.toCache('sfvalues', tag, sfCode, list);
231 getIndicatorValues(tag: string, which: 'ind1' | 'ind2'): ContextMenuEntry[] {
232 if (!tag || !this.tagMap[tag]) { return; }
234 const cached = this.fromCache('indicators', tag, which);
235 if (cached) { return cached; }
237 let values = this.tagMap[tag][which];
238 if (!values) { return; }
240 values = values.map(value => ({
242 label: `${value.code}: ${value.description}`
244 .sort((a, b) => a.label < b.label ? -1 : 1);
246 return this.toCache('indicators', tag, which, values);
250 getFfFieldMeta(fieldCode: string): Promise<IdlObject> {
251 return this.getFfPosTable().then(table => {
253 // Best I can tell, the AngJS MARC editor stores the
254 // full POS table for all record types in every copy of
255 // the table, hence the seemingly extraneous check in ffType.
258 field.fixed_field === fieldCode
259 && field.rec_type === this.selector.ffType
265 // Assumes getFfPosTable and getFfValueTable have already been
266 // invoked for the requested record type.
267 getFfValues(fieldCode: string): ContextMenuEntry[] {
269 const cached = this.fromCache('ffvalues', fieldCode);
270 if (cached) { return cached; }
272 let values = this.ffValueTable;
274 if (!values || !values[fieldCode]) { return null; }
276 // extract the canned set of possible values for our
277 // fixed field. Ignore those whose value exceeds the
278 // specified field length.
279 values = values[fieldCode]
280 .filter(val => val[0].length <= val[2])
281 .map(val => ({value: val[0], label: `${val[0]}: ${val[1]}`}))
282 .sort((a, b) => a.label < b.label ? -1 : 1);
284 return this.toCache('ffvalues', fieldCode, null, values);
290 export class TagTableService {
292 tagTables: {[marcRecordType: string]: TagTable} = {};
293 controlledBibTags: string[];
296 private store: StoreService,
297 private auth: AuthService,
298 private net: NetService,
299 private pcrud: PcrudService,
302 loadTags(selector: TagTableSelector): Promise<TagTable> {
303 if (!selector.marcFormat) {
304 selector.marcFormat = DEFAULT_MARC_FORMAT;
307 // Tag tables of a given marc record type are identical.
308 if (this.tagTables[selector.marcRecordType]) {
309 return Promise.resolve(this.tagTables[selector.marcRecordType]);
312 const tt = new TagTable(
313 this.store, this.auth, this.net, this.pcrud, selector);
315 this.tagTables[selector.marcRecordType] = tt;
317 return tt.load().then(_ => tt);
320 getControlledBibTags(): Promise<string[]> {
321 if (this.controlledBibTags) {
322 return Promise.resolve(this.controlledBibTags);
325 this.controlledBibTags = [];
326 return this.pcrud.retrieveAll('acsbf', {select: ['tag']})
328 map(field => field.tag()),
330 map(tag => this.controlledBibTags.push(tag))
331 ).toPromise().then(_ => this.controlledBibTags);