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"]) {
20 dojo.require('openils.PermaCrud');
21 dojo.require('MARC.FixedFields');
23 dojo._hasResource["openils.AuthorityControlSet"] = true;
24 dojo.provide("openils.AuthorityControlSet");
25 dojo.declare('openils.AuthorityControlSet', null, {
29 constructor : function(kwargs) {
31 kwargs = kwargs || {};
33 if (!openils.AuthorityControlSet._remote_loaded) {
35 // TODO -- push the raw tree into the oils cache for later reuse
37 var pcrud = new openils.PermaCrud({authtoken : ses()});
39 // fetch everything up front...
40 openils.AuthorityControlSet._control_set_list = pcrud.retrieveAll('acs');
41 openils.AuthorityControlSet._thesaurus_list = pcrud.retrieveAll('at');
42 openils.AuthorityControlSet._authority_field_list = pcrud.retrieveAll('acsaf');
43 openils.AuthorityControlSet._bib_field_list = pcrud.retrieveAll('acsbf');
44 openils.AuthorityControlSet._browse_axis_list = pcrud.retrieveAll('aba');
45 openils.AuthorityControlSet._browse_field_map_list = pcrud.retrieveAll('abaafm');
47 openils.AuthorityControlSet._browse_axis_by_name = {};
48 dojo.forEach( openils.AuthorityControlSet._browse_axis_list, function (ba) {
51 openils.AuthorityControlSet._browse_field_map_list,
52 function (m) { return m.axis() == ba.code() }
55 openils.AuthorityControlSet._browse_axis_by_name[ba.code()] = ba;
59 dojo.forEach( openils.AuthorityControlSet._control_set_list, function (cs) {
60 openils.AuthorityControlSet._controlsets[''+cs.id()] = {
63 description : cs.description(),
64 authority_tag_map : {},
70 // grab the authority fields
71 var acsaf_list = dojo.filter(
72 openils.AuthorityControlSet._authority_field_list,
73 function (af) { return af.control_set() == cs.id() }
76 var at_list = dojo.filter(
77 openils.AuthorityControlSet._thesaurus_list,
78 function (at) { return at.control_set() == cs.id() }
81 openils.AuthorityControlSet._controlsets[''+cs.id()].raw.authority_fields( acsaf_list );
82 openils.AuthorityControlSet._controlsets[''+cs.id()].raw.thesauri( at_list );
85 dojo.forEach( acsaf_list, function (csaf) {
88 // link the main entry if we're subordinate
89 if (csaf.main_entry()) {
91 dojo.filter(acsaf_list, function (x) {
92 return x.id() == csaf.main_entry();
97 // link the sub entries if we're main
99 dojo.filter(acsaf_list, function (x) {
100 return x.main_entry() == csaf.id();
105 var acsbf_list = dojo.filter(
106 openils.AuthorityControlSet._bib_field_list,
107 function (b) { return b.authority_field() == csaf.id() }
109 csaf.bib_fields( acsbf_list );
111 openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields = [].concat(
112 openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields,
116 dojo.forEach( acsbf_list, function (csbf) {
117 // link the authority field to the bib field
118 if (csbf.authority_field()) {
119 csbf.authority_field(
120 dojo.filter(acsaf_list, function (x) {
121 return x.id() == csbf.authority_field();
128 dojo.forEach( // for each axis
129 openils.AuthorityControlSet._browse_axis_list,
131 dojo.forEach( // loop over the maps
132 dojo.filter( // filtering to just this field's mapps
134 function (m) { return m.field() == csaf.id() }
136 function (fm) { fm.field( csaf ); csaf.axis_maps().push( fm ) } // and set the field
143 // build the authority_tag_map
144 dojo.forEach( openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields, function (bf) {
145 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()] = {};
146 dojo.forEach( bf.authority_field().sf_list().split(''), function (sf_code) {
147 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code] = {};
148 openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code][bf.authority_field().tag()] = sf_code;
154 if (this.controlSetList().length > 0)
155 delete openils.AuthorityControlSet._controlsets['-1'];
157 openils.AuthorityControlSet._remote_loaded = true;
160 if (kwargs.controlSet) {
161 this.controlSetId( kwargs.controlSet );
163 this.controlSetId( this.controlSetList().sort(function(a,b){return (a - b)}) );
167 controlSetId: function (x) {
168 if (x) this._controlset = ''+x;
169 return this._controlset;
172 controlSet: function (x) {
173 return openils.AuthorityControlSet._controlsets[''+this.controlSetId(x)];
176 bibFieldByTag: function (x) {
179 me.controlSet().bib_fields,
180 function (bf) { if (bf.tag() == x) return true }
184 bibFields: function (x) {
185 return this.controlSet(x).bib_fields;
188 bibFieldBrowseAxes : function (t) {
190 for (var bname in openils.AuthorityControlSet._browse_axis_by_name) {
192 openils.AuthorityControlSet._browse_axis_by_name[bname].maps(),
195 m.field().bib_fields(),
196 function (b) { return b.tag() == t }
205 authorityFields: function (x) {
206 return this.controlSet(x).raw.authority_fields();
209 thesauri: function (x) {
210 return this.controlSet(x).raw.thesauri();
213 controlSetList : function () {
215 for (var i in openils.AuthorityControlSet._controlsets) {
221 findControlSetsForTag : function (tag) {
223 var old_acs = this.controlSetId();
224 var acs_list = dojo.filter(
226 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
228 this.controlSetId(old_acs);
232 bibToAuthority : function (field) {
233 var b_field = this.bibFieldByTag(field.tag);
235 if (b_field) { // construct an marc authority record
236 af = b_field.authority_field();
237 var m = new MARC.Record ({rtype:'AUT'});
243 subfields: [dojo.filter(
245 function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
250 return m.toXmlString();
256 bibToAuthorities : function (field) {
260 var old_acs = this.controlSetId();
264 var acs = me.controlSet(acs_id);
265 var x = me.bibToAuthority(field);
266 if (x) { var foo = {}; foo[acs_id] = x; auth_list.push(foo); }
269 this.controlSetId(old_acs);
274 findMatchingAuthorities : function (field) {
275 return fieldmapper.standardRequest(
276 [ 'open-ils.search', 'open-ils.search.authority.simple_heading.from_xml.batch.atomic' ],
277 this.bibToAuthorities(field)
283 openils.AuthorityControlSet._remote_loaded = false;
285 openils.AuthorityControlSet._controlsets = {
286 // static sorta-LoC setup ... to be overwritten with server data
289 name : 'Static LoC legacy mapping',
290 description : 'Legacy mapping provided as a default',