1 import {Injectable} from '@angular/core';
2 import {map, tap, distinct} from 'rxjs/operators';
3 import {StoreService} from '@eg/core/store.service';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {ContextMenuEntry} from '@eg/share/context-menu/context-menu.service';
9 import {MARC_RECORD_TYPE} from './editor-context';
11 const DEFAULT_MARC_FORMAT = 'marc21';
13 interface TagTableSelector {
15 marcRecordType: MARC_RECORD_TYPE;
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 // eslint-disable-next-line no-prototype-builtins
224 sf.code === sfCode && sf.hasOwnProperty('value_list'))
226 sf.value_list.forEach(value => {
228 let label = value.description || value.code;
229 const code = value.code || label;
230 if (code !== label) { label = `${code}: ${label}`; }
232 list.push({value: code, label: label});
236 return this.toCache('sfvalues', tag, sfCode, list);
239 getIndicatorValues(tag: string, which: 'ind1' | 'ind2'): ContextMenuEntry[] {
240 if (!tag || !this.tagMap[tag]) { return; }
242 const cached = this.fromCache('indicators', tag, which);
243 if (cached) { return cached; }
245 let values = this.tagMap[tag][which];
246 if (!values) { return; }
248 values = values.map(value => ({
250 label: `${value.code}: ${value.description}`
252 .sort((a, b) => a.label < b.label ? -1 : 1);
254 return this.toCache('indicators', tag, which, values);
258 getFfFieldMeta(fieldCode: string): Promise<IdlObject> {
259 return this.getFfPosTable().then(table => {
261 // Best I can tell, the AngJS MARC editor stores the
262 // full POS table for all record types in every copy of
263 // the table, hence the seemingly extraneous check in ffType.
266 field.fixed_field === fieldCode
267 && field.rec_type === this.selector.ffType
273 // Assumes getFfPosTable and getFfValueTable have already been
274 // invoked for the requested record type.
275 getFfValues(fieldCode: string): ContextMenuEntry[] {
277 const cached = this.fromCache('ffvalues', fieldCode);
278 if (cached) { return cached; }
280 let values = this.ffValueTable;
282 if (!values || !values[fieldCode]) { return null; }
284 // extract the canned set of possible values for our
285 // fixed field. Ignore those whose value exceeds the
286 // specified field length.
287 values = values[fieldCode]
288 .filter(val => val[0].length <= val[2])
289 .map(val => ({value: val[0], label: `${val[0]}: ${val[1]}`}))
290 .sort((a, b) => a.label < b.label ? -1 : 1);
292 return this.toCache('ffvalues', fieldCode, null, values);
298 export class TagTableService {
300 tagTables: {[marcRecordType: string]: TagTable} = {};
301 controlledBibTags: string[];
304 private store: StoreService,
305 private auth: AuthService,
306 private net: NetService,
307 private pcrud: PcrudService,
310 loadTags(selector: TagTableSelector): Promise<TagTable> {
311 if (!selector.marcFormat) {
312 selector.marcFormat = DEFAULT_MARC_FORMAT;
315 const tt = new TagTable(
316 this.store, this.auth, this.net, this.pcrud, selector);
318 this.tagTables[selector.marcRecordType] = tt;
320 return tt.load().then(_ => tt);
323 getControlledBibTags(): Promise<string[]> {
324 if (this.controlledBibTags) {
325 return Promise.resolve(this.controlledBibTags);
328 this.controlledBibTags = [];
329 return this.pcrud.retrieveAll('acsbf', {select: ['tag']})
331 map(field => field.tag()),
333 map(tag => this.controlledBibTags.push(tag))
334 ).toPromise().then(_ => this.controlledBibTags);