2 * Retrieve, cache, and query MARC tag tables
4 angular.module('egCoreMod')
6 ['$q', 'egCore', 'egAuth',
7 function($q, egCore, egAuth) {
10 defaultTagTableSelector : {
11 marcFormat : 'marc21',
12 marcRecordType : 'biblio',
17 authority_control_set : {
18 _remote_loaded : false,
21 _active_control_set : undefined
24 service.initialized = function() {
25 return service.authority_control_set._remote_loaded;
28 // allow 'bre' and 'biblio' to be synonyms, etc.
29 service.normalizeRecordType = function(recordType) {
30 if (recordType === 'sre') {
32 } else if (recordType === 'bre') {
34 } else if (recordType === 'are') {
41 service.loadTagTable = function(args) {
42 var fields = service.defaultTagTableSelector;
44 if (args.marcFormat) {
45 fields.marcFormat = args.marcFormat;
47 if (args.marcRecordType) {
48 fields.marcRecordType = service.normalizeRecordType(args.marcRecordType);
51 var tt_key = 'current_tag_table_' + fields.marcFormat + '_' +
52 fields.marcRecordType;
53 egCore.hatch.getItem(tt_key).then(function(tt) {
57 service.loadRemoteTagTable(fields, tt_key);
62 service.fetchFFPosTable = function(rtype) {
63 var deferred = $q.defer();
65 var hatch_pos_key = 'FFPosTable_'+rtype;
67 egCore.hatch.getItem(hatch_pos_key).then(function(cached_table) {
69 service.ff_pos_map[rtype] = cached_table;
70 deferred.resolve(cached_table);
74 egCore.net.request( // First, get the list of FFs (minus 006)
76 'open-ils.fielder.cmfpm.atomic',
77 { query : { tag : { '!=' : '006' } } }
78 ).then(function (data) {
79 service.ff_pos_map[rtype] = data;
80 egCore.hatch.setItem(hatch_pos_key, data);
81 deferred.resolve(data);
86 return deferred.promise;
89 service.fetchFFValueTable = function(rtype) {
90 var deferred = $q.defer();
92 var hatch_value_key = 'FFValueTable_'+rtype;
94 egCore.hatch.getItem(hatch_value_key).then(function(cached_table) {
96 service.ff_value_map[rtype] = cached_table;
97 deferred.resolve(cached_table);
103 'open-ils.cat.biblio.fixed_field_values.by_rec_type',
105 ).then(function (data) {
106 service.ff_value_map[rtype] = data;
107 egCore.hatch.setItem(hatch_value_key, data);
108 deferred.resolve(data);
113 return deferred.promise;
116 service.loadRemoteTagTable = function(fields, tt_key) {
119 'open-ils.cat.tag_table.all.retrieve.local',
120 egAuth.token(), fields.marcFormat, fields.marcRecordType
123 egCore.hatch.setItem(tt_key, service.fields);
125 function (err) { console.err('error fetch tag table: ' + err) },
128 service.fields[field.tag] = field;
133 service.getFieldTags = function() {
135 angular.forEach(service.fields, function(value, key) {
138 label: key + ': ' + value.name
144 service.getSubfieldCodes = function(tag) {
147 if (!service.fields[tag]) return;
148 angular.forEach(service.fields[tag].subfields, function(value) {
151 label: value.code + ': ' + value.description
157 service.getSubfieldValues = function(tag, sf_code) {
159 if (!tag) return list;
160 if (!service.fields[tag]) return;
161 if (!service.fields[tag]) return;
162 angular.forEach(service.fields[tag].subfields, function(sf) {
163 if (sf.code == sf_code && sf.hasOwnProperty('value_list')) {
164 angular.forEach(sf.value_list, function(value) {
165 var label = (value.code == value.description) ?
167 value.code + ': ' + value.description;
178 service.getIndicatorValues = function(tag, pos) {
180 if (!tag) return list;
181 if (!service.fields[tag]) return;
182 if (!service.fields[tag]["ind" + pos]) return;
183 angular.forEach(service.fields[tag]["ind" + pos], function(value) {
186 label: value.code + ': ' + value.description
192 service.authorityControlSet = function (kwargs) {
194 kwargs = kwargs || {};
196 this._fetch_class = function(hint, cache_key) {
197 return egCore.pcrud.retrieveAll(hint, {}, {atomic : true}).then(
199 egCore.env.absorbList(list, hint);
200 service.authority_control_set[cache_key] = list;
205 this._fetch = function(cmap) {
206 var deferred = $q.defer();
208 for (var hint in cmap) {
209 promises.push(this._fetch_class(hint, cmap[hint]));
211 $q.all(promises).then(function() {
214 return deferred.promise;
217 this._parse = function() {
218 service.authority_control_set._browse_axis_by_code = {};
219 service.authority_control_set._browse_axis_list.forEach(function (ba) {
221 service.authority_control_set._browse_field_map_list.filter(
222 function (m) { return m.axis() == ba.code() }
225 service.authority_control_set._browse_axis_by_code[ba.code()] = ba;
228 // loop over each acs
229 service.authority_control_set._control_set_list.forEach(function (cs) {
230 service.authority_control_set._controlsets[''+cs.id()] = {
233 description : cs.description(),
234 authority_tag_map : {},
240 // grab the authority fields
241 var acsaf_list = service.authority_control_set._authority_field_list.filter(
242 function (af) { return af.control_set() == cs.id() }
245 var at_list = service.authority_control_set._thesaurus_list.filter(
246 function (at) { return at.control_set() == cs.id() }
249 service.authority_control_set._controlsets[''+cs.id()].raw.authority_fields( acsaf_list );
250 service.authority_control_set._controlsets[''+cs.id()].raw.thesauri( at_list );
252 // and loop over each
253 acsaf_list.forEach(function (csaf) {
256 // link the main entry if we're subordinate
257 if (csaf.main_entry()) {
259 acsaf_list.filter(function (x) {
260 return x.id() == csaf.main_entry();
265 // link the sub entries if we're main
267 acsaf_list.filter(function (x) {
268 return x.main_entry() == csaf.id();
273 var acsbf_list = service.authority_control_set._bib_field_list.filter(
274 function (b) { return b.authority_field() == csaf.id() }
276 csaf.bib_fields( acsbf_list );
278 service.authority_control_set._controlsets[''+cs.id()].bib_fields = [].concat(
279 service.authority_control_set._controlsets[''+cs.id()].bib_fields,
283 acsbf_list.forEach(function (csbf) {
284 // link the authority field to the bib field
285 if (csbf.authority_field()) {
286 csbf.authority_field(
287 acsaf_list.filter(function (x) {
288 return x.id() == csbf.authority_field();
295 service.authority_control_set._browse_axis_list.forEach(
298 function (m) { return m.field() == csaf.id() }
300 function (fm) { fm.field( csaf ); csaf.axis_maps().push( fm ) } // and set the field
307 // build the authority_tag_map
308 service.authority_control_set._controlsets[''+cs.id()].bib_fields.forEach(function (bf) {
310 if (!service.authority_control_set._controlsets[''+cs.id()].control_map[bf.tag()])
311 service.authority_control_set._controlsets[''+cs.id()].control_map[bf.tag()] = {};
313 bf.authority_field().sf_list().split('').forEach(function (sf_code) {
315 if (!service.authority_control_set._controlsets[''+cs.id()].control_map[bf.tag()][sf_code])
316 service.authority_control_set._controlsets[''+cs.id()].control_map[bf.tag()][sf_code] = {};
318 service.authority_control_set._controlsets[''+cs.id()].control_map[bf.tag()][sf_code][bf.authority_field().tag()] = sf_code;
324 if (this.controlSetList().length > 0)
325 delete service.authority_control_set._controlsets['-1'];
329 this.controlSetId = function (x) {
330 if (x) this._controlset = ''+x;
331 return this._controlset;
334 this.controlSetList = function () {
336 for (var i in service.authority_control_set._controlsets) {
343 if (!service.authority_control_set._remote_loaded) {
345 // TODO -- push the raw tree into the oils cache for later reuse
347 // fetch everything up front...
350 "acs": "_control_set_list",
351 "at": "_thesaurus_list",
352 "acsaf": "_authority_field_list",
353 "acsbf": "_bib_field_list",
354 "aba": "_browse_axis_list",
355 "abaafm": "_browse_field_map_list"
357 service.authority_control_set._remote_loaded = true;
359 if (kwargs.controlSet) {
360 parent.controlSetId( kwargs.controlSet );
362 parent.controlSetId( parent.controlSetList().sort(function(a,b){return (a - b)}) );
367 this.controlSet = function (x) {
368 return service.authority_control_set._controlsets[''+this.controlSetId(x)];
371 this.controlSetByThesaurusCode = function (x) {
372 var thes = service.authority_control_set._thesaurus_list.filter(
373 function (at) { return at.code() == x }
376 return this.controlSet(thes.control_set());
379 this.browseAxisByCode = function(code) {
380 return service.authority_control_set._browse_axis_by_code[code];
383 this.bibFieldByTag = function (x) {
385 return me.controlSet().bib_fields.filter(
386 function (bf) { if (bf.tag() == x) return true }
390 this.bibFields = function (x) {
391 return this.controlSet(x).bib_fields;
394 this.bibFieldBrowseAxes = function (t) {
396 for (var bcode in service.authority_control_set._browse_axis_by_code) {
397 service.authority_control_set._browse_axis_by_code[bcode].maps().forEach(
399 if (m.field().bib_fields().filter(
400 function (b) { return b.tag() == t }
409 this.authorityFields = function (x) {
410 return this.controlSet(x).raw.authority_fields();
413 this.thesauri = function (x) {
414 return this.controlSet(x).raw.thesauri();
417 this.findControlSetsForTag = function (tag) {
419 var old_acs = this.controlSetId();
420 var acs_list = me.controlSetList().filter(
421 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
423 this.controlSetId(old_acs);
427 this.findControlSetsForAuthorityTag = function (tag) {
429 var old_acs = this.controlSetId();
431 var acs_list = me.controlSetList().filter(
433 var a = me.controlSet(acs_id);
434 for (var btag in a.control_map) {
435 for (var sf in a.control_map[btag]) {
436 if (a.control_map[btag][sf][tag]) return true;
442 this.controlSetId(old_acs);
446 this.bibToAuthority = function (field) {
447 var b_field = this.bibFieldByTag(field.tag);
449 if (b_field) { // construct an marc authority record
450 var af = b_field.authority_field();
453 for (var i = 0; i < field.subfields.length; i++) {
454 if (af.sf_list().indexOf(field.subfields[i][0]) > -1) {
455 sflist.push(field.subfields[i]);
459 var m = new MARC21.Record ({rtype:'AUT'});
469 return m.toXmlString();
475 this.bibToAuthorities = function (field) {
479 var old_acs = this.controlSetId();
480 me.controlSetList().forEach(
482 var acs = me.controlSet(acs_id);
483 var x = me.bibToAuthority(field);
484 if (x) { var foo = {}; foo[acs_id] = x; auth_list.push(foo); }
487 this.controlSetId(old_acs);
494 service.getAuthorityControlSet = function() {
495 if (!service._active_control_set) {
496 service.authority_control_set._remote_loaded = false;
497 service._active_control_set = new service.authorityControlSet();
499 return service._active_control_set;