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