2 * ---------------------------------------------------------------------------
3 * Copyright (C) 2011 Equinox Software, Inc.
4 * Mike Rylander <miker@esilibrary.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * ---------------------------------------------------------------------------
18 if(!dojo._hasResource["openils.AuthorityControlSet"]) {
19 dojo.require('MARC.FixedFields');
21 dojo._hasResource["openils.AuthorityControlSet"] = true;
22 dojo.provide("openils.AuthorityControlSet");
23 dojo.declare('openils.AuthorityControlSet', null, {
27 constructor : function(kwargs) {
29 kwargs = kwargs || {};
31 if (!openils.AuthorityControlSet._remote_loaded) {
33 // TODO -- push the raw tree into the oils cache for later reuse
35 // fetch everything up front...
36 this._preFetchWithFielder({
37 "acs": "_control_set_list",
38 "at": "_thesaurus_list",
39 "acsaf": "_authority_field_list",
40 "acsbf": "_bib_field_list",
41 "aba": "_browse_axis_list",
42 "abaafm": "_browse_field_map_list"
45 openils.AuthorityControlSet._browse_axis_by_code = {};
46 dojo.forEach( openils.AuthorityControlSet._browse_axis_list, function (ba) {
49 openils.AuthorityControlSet._browse_field_map_list,
50 function (m) { return m.axis() == ba.code() }
53 openils.AuthorityControlSet._browse_axis_by_code[ba.code()] = ba;
57 dojo.forEach( openils.AuthorityControlSet._control_set_list, function (cs) {
58 openils.AuthorityControlSet._controlsets[''+cs.id()] = {
61 description : cs.description(),
62 authority_tag_map : {},
68 // grab the authority fields
69 var acsaf_list = dojo.filter(
70 openils.AuthorityControlSet._authority_field_list,
71 function (af) { return af.control_set() == cs.id() }
74 var at_list = dojo.filter(
75 openils.AuthorityControlSet._thesaurus_list,
76 function (at) { return at.control_set() == cs.id() }
79 openils.AuthorityControlSet._controlsets[''+cs.id()].raw.authority_fields( acsaf_list );
80 openils.AuthorityControlSet._controlsets[''+cs.id()].raw.thesauri( at_list );
83 dojo.forEach( acsaf_list, function (csaf) {
86 // link the main entry if we're subordinate
87 if (csaf.main_entry()) {
89 dojo.filter(acsaf_list, function (x) {
90 return x.id() == csaf.main_entry();
95 // link the sub entries if we're main
97 dojo.filter(acsaf_list, function (x) {
98 return x.main_entry() == csaf.id();
103 var acsbf_list = dojo.filter(
104 openils.AuthorityControlSet._bib_field_list,
105 function (b) { return b.authority_field() == csaf.id() }
107 csaf.bib_fields( acsbf_list );
109 openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields = [].concat(
110 openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields,
114 dojo.forEach( acsbf_list, function (csbf) {
115 // link the authority field to the bib field
116 if (csbf.authority_field()) {
117 csbf.authority_field(
118 dojo.filter(acsaf_list, function (x) {
119 return x.id() == csbf.authority_field();
126 dojo.forEach( // for each axis
127 openils.AuthorityControlSet._browse_axis_list,
129 dojo.forEach( // loop over the maps
130 dojo.filter( // filtering to just this field's mapps
132 function (m) { return m.field() == csaf.id() }
134 function (fm) { fm.field( csaf ); csaf.axis_maps().push( fm ) } // and set the field
141 // build the authority_tag_map
142 dojo.forEach( openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields, function (bf) {
143 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()] = {};
144 dojo.forEach( bf.authority_field().sf_list().split(''), function (sf_code) {
145 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code] = {};
146 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code][bf.authority_field().tag()] = sf_code;
152 if (this.controlSetList().length > 0)
153 delete openils.AuthorityControlSet._controlsets['-1'];
155 openils.AuthorityControlSet._remote_loaded = true;
158 if (kwargs.controlSet) {
159 this.controlSetId( kwargs.controlSet );
161 this.controlSetId( this.controlSetList().sort(function(a,b){return (a - b)}) );
165 _preFetchWithFielder: function(cmap) {
166 for (var hint in cmap) {
167 var cache_key = cmap[hint];
168 var method = "open-ils.fielder." + hint + ".atomic";
169 var pkey = fieldmapper.IDL.fmclasses[hint].pkey;
172 query[pkey] = {"!=": null};
174 openils.AuthorityControlSet[cache_key] = dojo.map(
175 fieldmapper.standardRequest(
176 ["open-ils.fielder", method],
177 [{"cache": 1, "query" : query}]
179 function(h) { return new fieldmapper[hint]().fromHash(h); }
184 controlSetId: function (x) {
185 if (x) this._controlset = ''+x;
186 return this._controlset;
189 controlSet: function (x) {
190 return openils.AuthorityControlSet._controlsets[''+this.controlSetId(x)];
193 controlSetByThesaurusCode: function (x) {
194 var thes = dojo.filter(
195 openils.AuthorityControlSet._thesaurus_list,
196 function (at) { return at.code() == x }
199 return this.controlSet(thes.control_set());
202 browseAxisByCode: function(code) {
203 return openils.AuthorityControlSet._browse_axis_by_code[code];
206 bibFieldByTag: function (x) {
209 me.controlSet().bib_fields,
210 function (bf) { if (bf.tag() == x) return true }
214 bibFields: function (x) {
215 return this.controlSet(x).bib_fields;
218 bibFieldBrowseAxes : function (t) {
220 for (var bcode in openils.AuthorityControlSet._browse_axis_by_code) {
222 openils.AuthorityControlSet._browse_axis_by_code[bcode].maps(),
225 m.field().bib_fields(),
226 function (b) { return b.tag() == t }
235 authorityFields: function (x) {
236 return this.controlSet(x).raw.authority_fields();
239 thesauri: function (x) {
240 return this.controlSet(x).raw.thesauri();
243 controlSetList : function () {
245 for (var i in openils.AuthorityControlSet._controlsets) {
251 findControlSetsForTag : function (tag) {
253 var old_acs = this.controlSetId();
254 var acs_list = dojo.filter(
256 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
258 this.controlSetId(old_acs);
262 bibToAuthority : function (field) {
263 var b_field = this.bibFieldByTag(field.tag);
265 if (b_field) { // construct an marc authority record
266 af = b_field.authority_field();
267 var m = new MARC.Record ({rtype:'AUT'});
273 subfields: [dojo.filter(
275 function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
280 return m.toXmlString();
286 bibToAuthorities : function (field) {
290 var old_acs = this.controlSetId();
294 var acs = me.controlSet(acs_id);
295 var x = me.bibToAuthority(field);
296 if (x) { var foo = {}; foo[acs_id] = x; auth_list.push(foo); }
299 this.controlSetId(old_acs);
304 findMatchingAuthorities : function (field) {
305 return fieldmapper.standardRequest(
306 [ 'open-ils.search', 'open-ils.search.authority.simple_heading.from_xml.batch.atomic' ],
307 this.bibToAuthorities(field)
313 openils.AuthorityControlSet._remote_loaded = false;
315 openils.AuthorityControlSet._controlsets = {
316 // static sorta-LoC setup ... to be overwritten with server data
319 name : 'Static LoC legacy mapping',
320 description : 'Legacy mapping provided as a default',