]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/AuthorityControlSet.js
Merge remote branch 'working/user/shadowspar/ttopac-altcleanup' into template-toolkit...
[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                         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;
147                         });
148                     });
149
150                 });
151
152                 if (this.controlSetList().length > 0)
153                     delete openils.AuthorityControlSet._controlsets['-1'];
154
155                 openils.AuthorityControlSet._remote_loaded = true;
156             }
157
158             if (kwargs.controlSet) {
159                 this.controlSetId( kwargs.controlSet );
160             } else {
161                 this.controlSetId( this.controlSetList().sort(function(a,b){return (a - b)}) );
162             }
163         },
164
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;
170
171                 var query = {};
172                 query[pkey] = {"!=": null};
173
174                 openils.AuthorityControlSet[cache_key] = dojo.map(
175                     fieldmapper.standardRequest(
176                         ["open-ils.fielder", method],
177                         [{"cache": 1, "query" : query}]
178                     ),
179                     function(h) { return new fieldmapper[hint]().fromHash(h); }
180                 );
181             }
182         },
183
184         controlSetId: function (x) {
185             if (x) this._controlset = ''+x;
186             return this._controlset;
187         },
188
189         controlSet: function (x) {
190             return openils.AuthorityControlSet._controlsets[''+this.controlSetId(x)];
191         },
192
193         controlSetByThesaurusCode: function (x) {
194             var thes = dojo.filter(
195                 openils.AuthorityControlSet._thesaurus_list,
196                 function (at) { return at.code() == x }
197             )[0];
198
199             return this.controlSet(thes.control_set());
200         },
201
202         browseAxisByCode: function(code) {
203             return openils.AuthorityControlSet._browse_axis_by_code[code];
204         },
205
206         bibFieldByTag: function (x) {
207             var me = this;
208             return dojo.filter(
209                 me.controlSet().bib_fields,
210                 function (bf) { if (bf.tag() == x) return true }
211             )[0];
212         },
213
214         bibFields: function (x) {
215             return this.controlSet(x).bib_fields;
216         },
217
218         bibFieldBrowseAxes : function (t) {
219             var blist = [];
220             for (var bcode in openils.AuthorityControlSet._browse_axis_by_code) {
221                 dojo.forEach(
222                     openils.AuthorityControlSet._browse_axis_by_code[bcode].maps(),
223                     function (m) {
224                         if (dojo.filter(
225                                 m.field().bib_fields(),
226                                 function (b) { return b.tag() == t }
227                             ).length > 0
228                         ) blist.push(bcode);
229                     }
230                 );
231             }
232             return blist;
233         },
234
235         authorityFields: function (x) {
236             return this.controlSet(x).raw.authority_fields();
237         },
238
239         thesauri: function (x) {
240             return this.controlSet(x).raw.thesauri();
241         },
242
243         controlSetList : function () {
244             var l = [];
245             for (var i in openils.AuthorityControlSet._controlsets) {
246                 l.push(i);
247             }
248             return l;
249         },
250
251         findControlSetsForTag : function (tag) {
252             var me = this;
253             var old_acs = this.controlSetId();
254             var acs_list = dojo.filter(
255                 me.controlSetList(),
256                 function(acs_id) { return (me.controlSet(acs_id).control_map[tag]) }
257             );
258             this.controlSetId(old_acs);
259             return acs_list;
260         },
261
262         bibToAuthority : function (field) {
263             var b_field = this.bibFieldByTag(field.tag);
264
265             if (b_field) { // construct an marc authority record
266                 af = b_field.authority_field();
267                 var m = new MARC.Record ({rtype:'AUT'});
268                 m.appendFields(
269                     new MARC.Field ({
270                         tag : af.tag(),
271                         ind1: field.ind1,
272                         ind2: field.ind2,
273                         subfields: [dojo.filter(
274                             field.subfields,
275                             function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
276                         )]
277                     })
278                 );
279
280                 return m.toXmlString();
281             }
282
283             return null;
284         },
285
286         bibToAuthorities : function (field) {
287             var auth_list = [];
288             var me = this;
289
290             var old_acs = this.controlSetId();
291             dojo.forEach(
292                 me.controlSetList(),
293                 function (acs_id) {
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); }
297                 }
298             );
299             this.controlSetId(old_acs);
300
301             return auth_list;
302         },
303
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)
308             );
309         }
310
311     });
312
313     openils.AuthorityControlSet._remote_loaded = false;
314
315     openils.AuthorityControlSet._controlsets = {
316         // static sorta-LoC setup ... to be overwritten with server data 
317         '-1' : {
318             id : -1,
319             name : 'Static LoC legacy mapping',
320             description : 'Legacy mapping provided as a default',
321             control_map : {
322                 100 : {
323                     'a' : { 100 : 'a' },
324                     'd' : { 100 : 'd' },
325                     'e' : { 100 : 'e' },
326                     'q' : { 100 : 'q' }
327                 },
328                 110 : {
329                     'a' : { 110 : 'a' },
330                     'd' : { 110 : 'd' }
331                 },
332                 111 : {
333                     'a' : { 111 : 'a' },
334                     'd' : { 111 : 'd' }
335                 },
336                 130 : {
337                     'a' : { 130 : 'a' },
338                     'd' : { 130 : 'd' }
339                 },
340                 240 : {
341                     'a' : { 130 : 'a' },
342                     'd' : { 130 : 'd' }
343                 },
344                 400 : {
345                     'a' : { 100 : 'a' },
346                     'd' : { 100 : 'd' }
347                 },
348                 410 : {
349                     'a' : { 110 : 'a' },
350                     'd' : { 110 : 'd' }
351                 },
352                 411 : {
353                     'a' : { 111 : 'a' },
354                     'd' : { 111 : 'd' }
355                 },
356                 440 : {
357                     'a' : { 130 : 'a' },
358                     'n' : { 130 : 'n' },
359                     'p' : { 130 : 'p' }
360                 },
361                 700 : {
362                     'a' : { 100 : 'a' },
363                     'd' : { 100 : 'd' },
364                     'q' : { 100 : 'q' },
365                     't' : { 100 : 't' }
366                 },
367                 710 : {
368                     'a' : { 110 : 'a' },
369                     'd' : { 110 : 'd' }
370                 },
371                 711 : {
372                     'a' : { 111 : 'a' },
373                     'c' : { 111 : 'c' },
374                     'd' : { 111 : 'd' }
375                 },
376                 730 : {
377                     'a' : { 130 : 'a' },
378                     'd' : { 130 : 'd' }
379                 },
380                 800 : {
381                     'a' : { 100 : 'a' },
382                     'd' : { 100 : 'd' }
383                 },
384                 810 : {
385                     'a' : { 110 : 'a' },
386                     'd' : { 110 : 'd' }
387                 },
388                 811 : {
389                     'a' : { 111 : 'a' },
390                     'd' : { 111 : 'd' }
391                 },
392                 830 : {
393                     'a' : { 130 : 'a' },
394                     'd' : { 130 : 'd' }
395                 },
396                 600 : {
397                     'a' : { 100 : 'a' },
398                     'd' : { 100 : 'd' },
399                     'q' : { 100 : 'q' },
400                     't' : { 100 : 't' },
401                     'v' : { 180 : 'v',
402                         100 : 'v',
403                         181 : 'v',
404                         182 : 'v',
405                         185 : 'v'
406                     },
407                     'x' : { 180 : 'x',
408                         100 : 'x',
409                         181 : 'x',
410                         182 : 'x',
411                         185 : 'x'
412                     },
413                     'y' : { 180 : 'y',
414                         100 : 'y',
415                         181 : 'y',
416                         182 : 'y',
417                         185 : 'y'
418                     },
419                     'z' : { 180 : 'z',
420                         100 : 'z',
421                         181 : 'z',
422                         182 : 'z',
423                         185 : 'z'
424                     }
425                 },
426                 610 : {
427                     'a' : { 110 : 'a' },
428                     'd' : { 110 : 'd' },
429                     't' : { 110 : 't' },
430                     'v' : { 180 : 'v',
431                         110 : 'v',
432                         181 : 'v',
433                         182 : 'v',
434                         185 : 'v'
435                     },
436                     'x' : { 180 : 'x',
437                         110 : 'x',
438                         181 : 'x',
439                         182 : 'x',
440                         185 : 'x'
441                     },
442                     'y' : { 180 : 'y',
443                         110 : 'y',
444                         181 : 'y',
445                         182 : 'y',
446                         185 : 'y'
447                     },
448                     'z' : { 180 : 'z',
449                         110 : 'z',
450                         181 : 'z',
451                         182 : 'z',
452                         185 : 'z'
453                     }
454                 },
455                 611 : {
456                     'a' : { 111 : 'a' },
457                     'd' : { 111 : 'd' },
458                     't' : { 111 : 't' },
459                     'v' : { 180 : 'v',
460                         111 : 'v',
461                         181 : 'v',
462                         182 : 'v',
463                         185 : 'v'
464                     },
465                     'x' : { 180 : 'x',
466                         111 : 'x',
467                         181 : 'x',
468                         182 : 'x',
469                         185 : 'x'
470                     },
471                     'y' : { 180 : 'y',
472                         111 : 'y',
473                         181 : 'y',
474                         182 : 'y',
475                         185 : 'y'
476                     },
477                     'z' : { 180 : 'z',
478                         111 : 'z',
479                         181 : 'z',
480                         182 : 'z',
481                         185 : 'z'
482                     }
483                 },
484                 630 : {
485                     'a' : { 130 : 'a' },
486                     'd' : { 130 : 'd' }
487                 },
488                 648 : {
489                     'a' : { 148 : 'a' },
490                     'v' : { 148 : 'v' },
491                     'x' : { 148 : 'x' },
492                     'y' : { 148 : 'y' },
493                     'z' : { 148 : 'z' }
494                 },
495                 650 : {
496                     'a' : { 150 : 'a' },
497                     'b' : { 150 : 'b' },
498                     'v' : { 180 : 'v',
499                         150 : 'v',
500                         181 : 'v',
501                         182 : 'v',
502                         185 : 'v'
503                     },
504                     'x' : { 180 : 'x',
505                         150 : 'x',
506                         181 : 'x',
507                         182 : 'x',
508                         185 : 'x'
509                     },
510                     'y' : { 180 : 'y',
511                         150 : 'y',
512                         181 : 'y',
513                         182 : 'y',
514                         185 : 'y'
515                     },
516                     'z' : { 180 : 'z',
517                         150 : 'z',
518                         181 : 'z',
519                         182 : 'z',
520                         185 : 'z'
521                     }
522                 },
523                 651 : {
524                     'a' : { 151 : 'a' },
525                     'v' : { 180 : 'v',
526                         151 : 'v',
527                         181 : 'v',
528                         182 : 'v',
529                         185 : 'v'
530                     },
531                     'x' : { 180 : 'x',
532                         151 : 'x',
533                         181 : 'x',
534                         182 : 'x',
535                         185 : 'x'
536                     },
537                     'y' : { 180 : 'y',
538                         151 : 'y',
539                         181 : 'y',
540                         182 : 'y',
541                         185 : 'y'
542                     },
543                     'z' : { 180 : 'z',
544                         151 : 'z',
545                         181 : 'z',
546                         182 : 'z',
547                         185 : 'z'
548                     }
549                 },
550                 655 : {
551                     'a' : { 155 : 'a' },
552                     'v' : { 180 : 'v',
553                         155 : 'v',
554                         181 : 'v',
555                         182 : 'v',
556                         185 : 'v'
557                     },
558                     'x' : { 180 : 'x',
559                         155 : 'x',
560                         181 : 'x',
561                         182 : 'x',
562                         185 : 'x'
563                     },
564                     'y' : { 180 : 'y',
565                         155 : 'y',
566                         181 : 'y',
567                         182 : 'y',
568                         185 : 'y'
569                     },
570                     'z' : { 180 : 'z',
571                         155 : 'z',
572                         181 : 'z',
573                         182 : 'z',
574                         185 : 'z'
575                     }
576                 }
577             }
578         }
579      };
580
581 }