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