]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/AuthorityControlSet.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / AuthorityControlSet.js
1 /* vim: et:sw=4:ts=4:
2  * ---------------------------------------------------------------------------
3  * Copyright (C) 2011  Equinox Software, Inc.
4  * Mike Rylander <miker@esilibrary.com>
5  *
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.
10  *
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  * ---------------------------------------------------------------------------
16  */
17
18 if(!dojo._hasResource["openils.AuthorityControlSet"]) {
19     dojo.require('MARC.FixedFields');
20
21     dojo._hasResource["openils.AuthorityControlSet"] = true;
22     dojo.provide("openils.AuthorityControlSet");
23     dojo.declare('openils.AuthorityControlSet', null, {
24
25         _controlset : null,
26
27         constructor : function(kwargs) {
28
29             kwargs = kwargs || {};
30
31             if (!openils.AuthorityControlSet._remote_loaded) {
32
33                 // TODO -- push the raw tree into the oils cache for later reuse
34
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"
43                 });
44
45                 openils.AuthorityControlSet._browse_axis_by_code = {};
46                 dojo.forEach( openils.AuthorityControlSet._browse_axis_list, function (ba) {
47                     ba.maps(
48                         dojo.filter(
49                             openils.AuthorityControlSet._browse_field_map_list,
50                             function (m) { return m.axis() == ba.code() }
51                         )
52                     );
53                     openils.AuthorityControlSet._browse_axis_by_code[ba.code()] = ba;
54                 });
55
56                 // loop over each acs
57                 dojo.forEach( openils.AuthorityControlSet._control_set_list, function (cs) {
58                     openils.AuthorityControlSet._controlsets[''+cs.id()] = {
59                         id : cs.id(),
60                         name : cs.name(),
61                         description : cs.description(),
62                         authority_tag_map : {},
63                         control_map : {},
64                         bib_fields : [],
65                         raw : cs
66                     };
67
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() }
72                     );
73
74                     var at_list = dojo.filter(
75                         openils.AuthorityControlSet._thesaurus_list,
76                         function (at) { return at.control_set() == cs.id() }
77                     );
78
79                     openils.AuthorityControlSet._controlsets[''+cs.id()].raw.authority_fields( acsaf_list );
80                     openils.AuthorityControlSet._controlsets[''+cs.id()].raw.thesauri( at_list );
81
82                     // and loop over each
83                     dojo.forEach( acsaf_list, function (csaf) {
84                         csaf.axis_maps([]);
85
86                         // link the main entry if we're subordinate
87                         if (csaf.main_entry()) {
88                             csaf.main_entry(
89                                 dojo.filter(acsaf_list, function (x) {
90                                     return x.id() == csaf.main_entry();
91                                 })[0]
92                             );
93                         }
94
95                         // link the sub entries if we're main
96                         csaf.sub_entries(
97                             dojo.filter(acsaf_list, function (x) {
98                                 return x.main_entry() == csaf.id();
99                             })
100                         );
101
102                         // now, bib fields
103                         var acsbf_list = dojo.filter(
104                             openils.AuthorityControlSet._bib_field_list,
105                             function (b) { return b.authority_field() == csaf.id() }
106                         );
107                         csaf.bib_fields( acsbf_list );
108
109                         openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields = [].concat(
110                             openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields,
111                             acsbf_list
112                         );
113
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();
120                                     })[0]
121                                 );
122                             }
123     
124                         });
125
126                         dojo.forEach( // for each axis
127                             openils.AuthorityControlSet._browse_axis_list,
128                             function (ba) {
129                                 dojo.forEach( // loop over the maps
130                                     dojo.filter( // filtering to just this field's mapps
131                                         ba.maps(),
132                                         function (m) { return m.field() == csaf.id() }
133                                     ),
134                                     function (fm) { fm.field( csaf ); csaf.axis_maps().push( fm ) } // and set the field
135                                 )
136                             }
137                         );
138
139                     });
140
141                     // build the authority_tag_map
142                     dojo.forEach( openils.AuthorityControlSet._controlsets[''+cs.id()].bib_fields, function (bf) {
143
144                         if (!openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()])
145                             openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()] = {};
146
147                         dojo.forEach( bf.authority_field().sf_list().split(''), function (sf_code) {
148
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] = {};
151
152                             openils.AuthorityControlSet._controlsets[''+cs.id()].control_map[bf.tag()][sf_code][bf.authority_field().tag()] = sf_code;
153                         });
154                     });
155
156                 });
157
158                 if (this.controlSetList().length > 0)
159                     delete openils.AuthorityControlSet._controlsets['-1'];
160
161                 openils.AuthorityControlSet._remote_loaded = true;
162             }
163
164             if (kwargs.controlSet) {
165                 this.controlSetId( kwargs.controlSet );
166             } else {
167                 this.controlSetId( this.controlSetList().sort(function(a,b){return (a - b)}) );
168             }
169         },
170
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;
176
177                 var query = {};
178                 query[pkey] = {"!=": null};
179
180                 openils.AuthorityControlSet[cache_key] = dojo.map(
181                     fieldmapper.standardRequest(
182                         ["open-ils.fielder", method],
183                         [{"cache": 1, "query" : query}]
184                     ),
185                     function(h) { return new fieldmapper[hint]().fromHash(h); }
186                 );
187             }
188         },
189
190         controlSetId: function (x) {
191             if (x) this._controlset = ''+x;
192             return this._controlset;
193         },
194
195         controlSet: function (x) {
196             return openils.AuthorityControlSet._controlsets[''+this.controlSetId(x)];
197         },
198
199         controlSetByThesaurusCode: function (x) {
200             var thes = dojo.filter(
201                 openils.AuthorityControlSet._thesaurus_list,
202                 function (at) { return at.code() == x }
203             )[0];
204
205             return this.controlSet(thes.control_set());
206         },
207
208         browseAxisByCode: function(code) {
209             return openils.AuthorityControlSet._browse_axis_by_code[code];
210         },
211
212         bibFieldByTag: function (x) {
213             var me = this;
214             return dojo.filter(
215                 me.controlSet().bib_fields,
216                 function (bf) { if (bf.tag() == x) return true }
217             )[0];
218         },
219
220         bibFields: function (x) {
221             return this.controlSet(x).bib_fields;
222         },
223
224         bibFieldBrowseAxes : function (t) {
225             var blist = [];
226             for (var bcode in openils.AuthorityControlSet._browse_axis_by_code) {
227                 dojo.forEach(
228                     openils.AuthorityControlSet._browse_axis_by_code[bcode].maps(),
229                     function (m) {
230                         if (dojo.filter(
231                                 m.field().bib_fields(),
232                                 function (b) { return b.tag() == t }
233                             ).length > 0
234                         ) blist.push(bcode);
235                     }
236                 );
237             }
238             return blist;
239         },
240
241         authorityFields: function (x) {
242             return this.controlSet(x).raw.authority_fields();
243         },
244
245         thesauri: function (x) {
246             return this.controlSet(x).raw.thesauri();
247         },
248
249         controlSetList : function () {
250             var l = [];
251             for (var i in openils.AuthorityControlSet._controlsets) {
252                 l.push(i);
253             }
254             return l;
255         },
256
257         findControlSetsForTag : function (tag) {
258             var me = this;
259             var old_acs = this.controlSetId();
260             var acs_list = dojo.filter(
261                 me.controlSetList(),
262                 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
263             );
264             this.controlSetId(old_acs);
265             return acs_list;
266         },
267
268         findControlSetsForAuthorityTag : function (tag) {
269             var me = this;
270             var old_acs = this.controlSetId();
271
272             var acs_list = dojo.filter(
273                 me.controlSetList(),
274                 function(acs_id) {
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;
279                         }
280                     }
281                     return false;
282                 }
283             );
284             this.controlSetId(old_acs);
285             return acs_list;
286         },
287
288         bibToAuthority : function (field) {
289             var b_field = this.bibFieldByTag(field.tag);
290
291             if (b_field) { // construct an marc authority record
292                 af = b_field.authority_field();
293                 var m = new MARC.Record ({rtype:'AUT'});
294                 m.appendFields(
295                     new MARC.Field ({
296                         tag : af.tag(),
297                         ind1: field.ind1,
298                         ind2: field.ind2,
299                         subfields: [dojo.filter(
300                             field.subfields,
301                             function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
302                         )]
303                     })
304                 );
305
306                 return m.toXmlString();
307             }
308
309             return null;
310         },
311
312         bibToAuthorities : function (field) {
313             var auth_list = [];
314             var me = this;
315
316             var old_acs = this.controlSetId();
317             dojo.forEach(
318                 me.controlSetList(),
319                 function (acs_id) {
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); }
323                 }
324             );
325             this.controlSetId(old_acs);
326
327             return auth_list;
328         },
329
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)
334             );
335         }
336
337     });
338
339     openils.AuthorityControlSet._remote_loaded = false;
340
341     openils.AuthorityControlSet._controlsets = {
342         // static sorta-LoC setup ... to be overwritten with server data 
343         '-1' : {
344             id : -1,
345             name : 'Static LoC legacy mapping',
346             description : 'Legacy mapping provided as a default',
347             control_map : {
348                 100 : {
349                     'a' : { 100 : 'a' },
350                     'd' : { 100 : 'd' },
351                     'e' : { 100 : 'e' },
352                     'q' : { 100 : 'q' }
353                 },
354                 110 : {
355                     'a' : { 110 : 'a' },
356                     'd' : { 110 : 'd' }
357                 },
358                 111 : {
359                     'a' : { 111 : 'a' },
360                     'd' : { 111 : 'd' }
361                 },
362                 130 : {
363                     'a' : { 130 : 'a' },
364                     'd' : { 130 : 'd' }
365                 },
366                 240 : {
367                     'a' : { 130 : 'a' },
368                     'd' : { 130 : 'd' }
369                 },
370                 400 : {
371                     'a' : { 100 : 'a' },
372                     'd' : { 100 : 'd' }
373                 },
374                 410 : {
375                     'a' : { 110 : 'a' },
376                     'd' : { 110 : 'd' }
377                 },
378                 411 : {
379                     'a' : { 111 : 'a' },
380                     'd' : { 111 : 'd' }
381                 },
382                 440 : {
383                     'a' : { 130 : 'a' },
384                     'n' : { 130 : 'n' },
385                     'p' : { 130 : 'p' }
386                 },
387                 700 : {
388                     'a' : { 100 : 'a' },
389                     'd' : { 100 : 'd' },
390                     'q' : { 100 : 'q' },
391                     't' : { 100 : 't' }
392                 },
393                 710 : {
394                     'a' : { 110 : 'a' },
395                     'd' : { 110 : 'd' }
396                 },
397                 711 : {
398                     'a' : { 111 : 'a' },
399                     'c' : { 111 : 'c' },
400                     'd' : { 111 : 'd' }
401                 },
402                 730 : {
403                     'a' : { 130 : 'a' },
404                     'd' : { 130 : 'd' }
405                 },
406                 800 : {
407                     'a' : { 100 : 'a' },
408                     'd' : { 100 : 'd' }
409                 },
410                 810 : {
411                     'a' : { 110 : 'a' },
412                     'd' : { 110 : 'd' }
413                 },
414                 811 : {
415                     'a' : { 111 : 'a' },
416                     'd' : { 111 : 'd' }
417                 },
418                 830 : {
419                     'a' : { 130 : 'a' },
420                     'd' : { 130 : 'd' }
421                 },
422                 600 : {
423                     'a' : { 100 : 'a' },
424                     'd' : { 100 : 'd' },
425                     'q' : { 100 : 'q' },
426                     't' : { 100 : 't' },
427                     'v' : { 180 : 'v',
428                         100 : 'v',
429                         181 : 'v',
430                         182 : 'v',
431                         185 : 'v'
432                     },
433                     'x' : { 180 : 'x',
434                         100 : 'x',
435                         181 : 'x',
436                         182 : 'x',
437                         185 : 'x'
438                     },
439                     'y' : { 180 : 'y',
440                         100 : 'y',
441                         181 : 'y',
442                         182 : 'y',
443                         185 : 'y'
444                     },
445                     'z' : { 180 : 'z',
446                         100 : 'z',
447                         181 : 'z',
448                         182 : 'z',
449                         185 : 'z'
450                     }
451                 },
452                 610 : {
453                     'a' : { 110 : 'a' },
454                     'd' : { 110 : 'd' },
455                     't' : { 110 : 't' },
456                     'v' : { 180 : 'v',
457                         110 : 'v',
458                         181 : 'v',
459                         182 : 'v',
460                         185 : 'v'
461                     },
462                     'x' : { 180 : 'x',
463                         110 : 'x',
464                         181 : 'x',
465                         182 : 'x',
466                         185 : 'x'
467                     },
468                     'y' : { 180 : 'y',
469                         110 : 'y',
470                         181 : 'y',
471                         182 : 'y',
472                         185 : 'y'
473                     },
474                     'z' : { 180 : 'z',
475                         110 : 'z',
476                         181 : 'z',
477                         182 : 'z',
478                         185 : 'z'
479                     }
480                 },
481                 611 : {
482                     'a' : { 111 : 'a' },
483                     'd' : { 111 : 'd' },
484                     't' : { 111 : 't' },
485                     'v' : { 180 : 'v',
486                         111 : 'v',
487                         181 : 'v',
488                         182 : 'v',
489                         185 : 'v'
490                     },
491                     'x' : { 180 : 'x',
492                         111 : 'x',
493                         181 : 'x',
494                         182 : 'x',
495                         185 : 'x'
496                     },
497                     'y' : { 180 : 'y',
498                         111 : 'y',
499                         181 : 'y',
500                         182 : 'y',
501                         185 : 'y'
502                     },
503                     'z' : { 180 : 'z',
504                         111 : 'z',
505                         181 : 'z',
506                         182 : 'z',
507                         185 : 'z'
508                     }
509                 },
510                 630 : {
511                     'a' : { 130 : 'a' },
512                     'd' : { 130 : 'd' }
513                 },
514                 648 : {
515                     'a' : { 148 : 'a' },
516                     'v' : { 148 : 'v' },
517                     'x' : { 148 : 'x' },
518                     'y' : { 148 : 'y' },
519                     'z' : { 148 : 'z' }
520                 },
521                 650 : {
522                     'a' : { 150 : 'a' },
523                     'b' : { 150 : 'b' },
524                     'v' : { 180 : 'v',
525                         150 : 'v',
526                         181 : 'v',
527                         182 : 'v',
528                         185 : 'v'
529                     },
530                     'x' : { 180 : 'x',
531                         150 : 'x',
532                         181 : 'x',
533                         182 : 'x',
534                         185 : 'x'
535                     },
536                     'y' : { 180 : 'y',
537                         150 : 'y',
538                         181 : 'y',
539                         182 : 'y',
540                         185 : 'y'
541                     },
542                     'z' : { 180 : 'z',
543                         150 : 'z',
544                         181 : 'z',
545                         182 : 'z',
546                         185 : 'z'
547                     }
548                 },
549                 651 : {
550                     'a' : { 151 : 'a' },
551                     'v' : { 180 : 'v',
552                         151 : 'v',
553                         181 : 'v',
554                         182 : 'v',
555                         185 : 'v'
556                     },
557                     'x' : { 180 : 'x',
558                         151 : 'x',
559                         181 : 'x',
560                         182 : 'x',
561                         185 : 'x'
562                     },
563                     'y' : { 180 : 'y',
564                         151 : 'y',
565                         181 : 'y',
566                         182 : 'y',
567                         185 : 'y'
568                     },
569                     'z' : { 180 : 'z',
570                         151 : 'z',
571                         181 : 'z',
572                         182 : 'z',
573                         185 : 'z'
574                     }
575                 },
576                 655 : {
577                     'a' : { 155 : 'a' },
578                     'v' : { 180 : 'v',
579                         155 : 'v',
580                         181 : 'v',
581                         182 : 'v',
582                         185 : 'v'
583                     },
584                     'x' : { 180 : 'x',
585                         155 : 'x',
586                         181 : 'x',
587                         182 : 'x',
588                         185 : 'x'
589                     },
590                     'y' : { 180 : 'y',
591                         155 : 'y',
592                         181 : 'y',
593                         182 : 'y',
594                         185 : 'y'
595                     },
596                     'z' : { 180 : 'z',
597                         155 : 'z',
598                         181 : 'z',
599                         182 : 'z',
600                         185 : 'z'
601                     }
602                 }
603             }
604         }
605      };
606
607 }