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) {
144 if (!openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()])
145 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()] = {};
147 dojo.forEach( bf.authority_field().sf_list().split(''), function (sf_code) {
149 if (!openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code])
150 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code] = {};
152 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code][bf.authority_field().tag()] = sf_code;
158 if (this.controlSetList().length > 0)
159 delete openils.AuthorityControlSet._controlsets['-1'];
161 openils.AuthorityControlSet._remote_loaded = true;
164 if (kwargs.controlSet) {
165 this.controlSetId( kwargs.controlSet );
167 this.controlSetId( this.controlSetList().sort(function(a,b){return (a - b)}) );
171 _preFetchWithFielder: function(cmap) {
172 for (var hint in cmap) {
173 var cache_key = cmap[hint];
174 var method = "open-ils.fielder." + hint + ".atomic";
175 var pkey = fieldmapper.IDL.fmclasses[hint].pkey;
178 query[pkey] = {"!=": null};
180 openils.AuthorityControlSet[cache_key] = dojo.map(
181 fieldmapper.standardRequest(
182 ["open-ils.fielder", method],
183 [{"cache": 1, "query" : query}]
185 function(h) { return new fieldmapper[hint]().fromHash(h); }
190 controlSetId: function (x) {
191 if (x) this._controlset = ''+x;
192 return this._controlset;
195 controlSet: function (x) {
196 return openils.AuthorityControlSet._controlsets[''+this.controlSetId(x)];
199 controlSetByThesaurusCode: function (x) {
200 var thes = dojo.filter(
201 openils.AuthorityControlSet._thesaurus_list,
202 function (at) { return at.code() == x }
205 return this.controlSet(thes.control_set());
208 browseAxisByCode: function(code) {
209 return openils.AuthorityControlSet._browse_axis_by_code[code];
212 bibFieldByTag: function (x) {
215 me.controlSet().bib_fields,
216 function (bf) { if (bf.tag() == x) return true }
220 bibFields: function (x) {
221 return this.controlSet(x).bib_fields;
224 bibFieldBrowseAxes : function (t) {
226 for (var bcode in openils.AuthorityControlSet._browse_axis_by_code) {
228 openils.AuthorityControlSet._browse_axis_by_code[bcode].maps(),
231 m.field().bib_fields(),
232 function (b) { return b.tag() == t }
241 authorityFields: function (x) {
242 return this.controlSet(x).raw.authority_fields();
245 thesauri: function (x) {
246 return this.controlSet(x).raw.thesauri();
249 controlSetList : function () {
251 for (var i in openils.AuthorityControlSet._controlsets) {
257 findControlSetsForTag : function (tag) {
259 var old_acs = this.controlSetId();
260 var acs_list = dojo.filter(
262 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
264 this.controlSetId(old_acs);
268 findControlSetsForAuthorityTag : function (tag) {
270 var old_acs = this.controlSetId();
272 var acs_list = dojo.filter(
275 var a = me.controlSet(acs_id);
276 for (var btag in a.control_map) {
277 for (var sf in a.control_map[btag]) {
278 if (a.control_map[btag][sf][tag]) return true;
284 this.controlSetId(old_acs);
288 bibToAuthority : function (field) {
289 var b_field = this.bibFieldByTag(field.tag);
291 if (b_field) { // construct an marc authority record
292 af = b_field.authority_field();
293 var m = new MARC.Record ({rtype:'AUT'});
299 subfields: [dojo.filter(
301 function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
306 return m.toXmlString();
312 bibToAuthorities : function (field) {
316 var old_acs = this.controlSetId();
320 var acs = me.controlSet(acs_id);
321 var x = me.bibToAuthority(field);
322 if (x) { var foo = {}; foo[acs_id] = x; auth_list.push(foo); }
325 this.controlSetId(old_acs);
330 findMatchingAuthorities : function (field) {
331 return fieldmapper.standardRequest(
332 [ 'open-ils.search', 'open-ils.search.authority.simple_heading.from_xml.batch.atomic' ],
333 this.bibToAuthorities(field)
339 openils.AuthorityControlSet._remote_loaded = false;
341 openils.AuthorityControlSet._controlsets = {
342 // static sorta-LoC setup ... to be overwritten with server data
345 name : 'Static LoC legacy mapping',
346 description : 'Legacy mapping provided as a default',