]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/AuthorityControlSet.js
Merge branch 'collab/miker/acs-ui-repairs' from working/Evergreen.git
[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                         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         findControlSetsForAuthorityTag : function (tag) {
263             var me = this;
264             var old_acs = this.controlSetId();
265
266             var acs_list = dojo.filter(
267                 me.controlSetList(),
268                 function(acs_id) {
269                     var a = me.controlSet(acs_id);
270                     for (var btag in a.control_map) {
271                         for (var sf in a.control_map[btag]) {
272                             if (a.control_map[btag][sf][tag]) return true;
273                         }
274                     }
275                     return false;
276                 }
277             );
278             this.controlSetId(old_acs);
279             return acs_list;
280         },
281
282         bibToAuthority : function (field) {
283             var b_field = this.bibFieldByTag(field.tag);
284
285             if (b_field) { // construct an marc authority record
286                 af = b_field.authority_field();
287                 var m = new MARC.Record ({rtype:'AUT'});
288                 m.appendFields(
289                     new MARC.Field ({
290                         tag : af.tag(),
291                         ind1: field.ind1,
292                         ind2: field.ind2,
293                         subfields: [dojo.filter(
294                             field.subfields,
295                             function (sf) { return (af.sf_list().indexOf(sf[0]) > -1) }
296                         )]
297                     })
298                 );
299
300                 return m.toXmlString();
301             }
302
303             return null;
304         },
305
306         bibToAuthorities : function (field) {
307             var auth_list = [];
308             var me = this;
309
310             var old_acs = this.controlSetId();
311             dojo.forEach(
312                 me.controlSetList(),
313                 function (acs_id) {
314                     var acs = me.controlSet(acs_id);
315                     var x = me.bibToAuthority(field);
316                     if (x) { var foo = {}; foo[acs_id] = x; auth_list.push(foo); }
317                 }
318             );
319             this.controlSetId(old_acs);
320
321             return auth_list;
322         },
323
324         findMatchingAuthorities : function (field) {
325             return fieldmapper.standardRequest(
326                 [ 'open-ils.search', 'open-ils.search.authority.simple_heading.from_xml.batch.atomic' ],
327                 this.bibToAuthorities(field)
328             );
329         }
330
331     });
332
333     openils.AuthorityControlSet._remote_loaded = false;
334
335     openils.AuthorityControlSet._controlsets = {
336         // static sorta-LoC setup ... to be overwritten with server data 
337         '-1' : {
338             id : -1,
339             name : 'Static LoC legacy mapping',
340             description : 'Legacy mapping provided as a default',
341             control_map : {
342                 100 : {
343                     'a' : { 100 : 'a' },
344                     'd' : { 100 : 'd' },
345                     'e' : { 100 : 'e' },
346                     'q' : { 100 : 'q' }
347                 },
348                 110 : {
349                     'a' : { 110 : 'a' },
350                     'd' : { 110 : 'd' }
351                 },
352                 111 : {
353                     'a' : { 111 : 'a' },
354                     'd' : { 111 : 'd' }
355                 },
356                 130 : {
357                     'a' : { 130 : 'a' },
358                     'd' : { 130 : 'd' }
359                 },
360                 240 : {
361                     'a' : { 130 : 'a' },
362                     'd' : { 130 : 'd' }
363                 },
364                 400 : {
365                     'a' : { 100 : 'a' },
366                     'd' : { 100 : 'd' }
367                 },
368                 410 : {
369                     'a' : { 110 : 'a' },
370                     'd' : { 110 : 'd' }
371                 },
372                 411 : {
373                     'a' : { 111 : 'a' },
374                     'd' : { 111 : 'd' }
375                 },
376                 440 : {
377                     'a' : { 130 : 'a' },
378                     'n' : { 130 : 'n' },
379                     'p' : { 130 : 'p' }
380                 },
381                 700 : {
382                     'a' : { 100 : 'a' },
383                     'd' : { 100 : 'd' },
384                     'q' : { 100 : 'q' },
385                     't' : { 100 : 't' }
386                 },
387                 710 : {
388                     'a' : { 110 : 'a' },
389                     'd' : { 110 : 'd' }
390                 },
391                 711 : {
392                     'a' : { 111 : 'a' },
393                     'c' : { 111 : 'c' },
394                     'd' : { 111 : 'd' }
395                 },
396                 730 : {
397                     'a' : { 130 : 'a' },
398                     'd' : { 130 : 'd' }
399                 },
400                 800 : {
401                     'a' : { 100 : 'a' },
402                     'd' : { 100 : 'd' }
403                 },
404                 810 : {
405                     'a' : { 110 : 'a' },
406                     'd' : { 110 : 'd' }
407                 },
408                 811 : {
409                     'a' : { 111 : 'a' },
410                     'd' : { 111 : 'd' }
411                 },
412                 830 : {
413                     'a' : { 130 : 'a' },
414                     'd' : { 130 : 'd' }
415                 },
416                 600 : {
417                     'a' : { 100 : 'a' },
418                     'd' : { 100 : 'd' },
419                     'q' : { 100 : 'q' },
420                     't' : { 100 : 't' },
421                     'v' : { 180 : 'v',
422                         100 : 'v',
423                         181 : 'v',
424                         182 : 'v',
425                         185 : 'v'
426                     },
427                     'x' : { 180 : 'x',
428                         100 : 'x',
429                         181 : 'x',
430                         182 : 'x',
431                         185 : 'x'
432                     },
433                     'y' : { 180 : 'y',
434                         100 : 'y',
435                         181 : 'y',
436                         182 : 'y',
437                         185 : 'y'
438                     },
439                     'z' : { 180 : 'z',
440                         100 : 'z',
441                         181 : 'z',
442                         182 : 'z',
443                         185 : 'z'
444                     }
445                 },
446                 610 : {
447                     'a' : { 110 : 'a' },
448                     'd' : { 110 : 'd' },
449                     't' : { 110 : 't' },
450                     'v' : { 180 : 'v',
451                         110 : 'v',
452                         181 : 'v',
453                         182 : 'v',
454                         185 : 'v'
455                     },
456                     'x' : { 180 : 'x',
457                         110 : 'x',
458                         181 : 'x',
459                         182 : 'x',
460                         185 : 'x'
461                     },
462                     'y' : { 180 : 'y',
463                         110 : 'y',
464                         181 : 'y',
465                         182 : 'y',
466                         185 : 'y'
467                     },
468                     'z' : { 180 : 'z',
469                         110 : 'z',
470                         181 : 'z',
471                         182 : 'z',
472                         185 : 'z'
473                     }
474                 },
475                 611 : {
476                     'a' : { 111 : 'a' },
477                     'd' : { 111 : 'd' },
478                     't' : { 111 : 't' },
479                     'v' : { 180 : 'v',
480                         111 : 'v',
481                         181 : 'v',
482                         182 : 'v',
483                         185 : 'v'
484                     },
485                     'x' : { 180 : 'x',
486                         111 : 'x',
487                         181 : 'x',
488                         182 : 'x',
489                         185 : 'x'
490                     },
491                     'y' : { 180 : 'y',
492                         111 : 'y',
493                         181 : 'y',
494                         182 : 'y',
495                         185 : 'y'
496                     },
497                     'z' : { 180 : 'z',
498                         111 : 'z',
499                         181 : 'z',
500                         182 : 'z',
501                         185 : 'z'
502                     }
503                 },
504                 630 : {
505                     'a' : { 130 : 'a' },
506                     'd' : { 130 : 'd' }
507                 },
508                 648 : {
509                     'a' : { 148 : 'a' },
510                     'v' : { 148 : 'v' },
511                     'x' : { 148 : 'x' },
512                     'y' : { 148 : 'y' },
513                     'z' : { 148 : 'z' }
514                 },
515                 650 : {
516                     'a' : { 150 : 'a' },
517                     'b' : { 150 : 'b' },
518                     'v' : { 180 : 'v',
519                         150 : 'v',
520                         181 : 'v',
521                         182 : 'v',
522                         185 : 'v'
523                     },
524                     'x' : { 180 : 'x',
525                         150 : 'x',
526                         181 : 'x',
527                         182 : 'x',
528                         185 : 'x'
529                     },
530                     'y' : { 180 : 'y',
531                         150 : 'y',
532                         181 : 'y',
533                         182 : 'y',
534                         185 : 'y'
535                     },
536                     'z' : { 180 : 'z',
537                         150 : 'z',
538                         181 : 'z',
539                         182 : 'z',
540                         185 : 'z'
541                     }
542                 },
543                 651 : {
544                     'a' : { 151 : 'a' },
545                     'v' : { 180 : 'v',
546                         151 : 'v',
547                         181 : 'v',
548                         182 : 'v',
549                         185 : 'v'
550                     },
551                     'x' : { 180 : 'x',
552                         151 : 'x',
553                         181 : 'x',
554                         182 : 'x',
555                         185 : 'x'
556                     },
557                     'y' : { 180 : 'y',
558                         151 : 'y',
559                         181 : 'y',
560                         182 : 'y',
561                         185 : 'y'
562                     },
563                     'z' : { 180 : 'z',
564                         151 : 'z',
565                         181 : 'z',
566                         182 : 'z',
567                         185 : 'z'
568                     }
569                 },
570                 655 : {
571                     'a' : { 155 : 'a' },
572                     'v' : { 180 : 'v',
573                         155 : 'v',
574                         181 : 'v',
575                         182 : 'v',
576                         185 : 'v'
577                     },
578                     'x' : { 180 : 'x',
579                         155 : 'x',
580                         181 : 'x',
581                         182 : 'x',
582                         185 : 'x'
583                     },
584                     'y' : { 180 : 'y',
585                         155 : 'y',
586                         181 : 'y',
587                         182 : 'y',
588                         185 : 'y'
589                     },
590                     'z' : { 180 : 'z',
591                         155 : 'z',
592                         181 : 'z',
593                         182 : 'z',
594                         185 : 'z'
595                     }
596                 }
597             }
598         }
599      };
600
601 }