]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/fieldmapper.pl
added another class to the web_fieldmapper
[Evergreen.git] / Open-ILS / src / extras / fieldmapper.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use Data::Dumper; 
4 use OpenILS::Utils::Fieldmapper;  
5
6 my $map = $Fieldmapper::fieldmap;
7
8 # if a true value is provided, we generate the web (light) version of the fieldmapper
9 my $web = $ARGV[0];
10 # List of classes needed by the opac
11 my @web_hints = ("ex", "mvr", "au", "aou",
12                 "aout", "asv", "asva", "asvr", "asvq", 
13                 "circ", "acp", "acn", "ccs");
14
15 print <<JS;
16
17 //  ----------------------------------------------------------------
18 // Autogenerated by fieldmapper.pl
19 // Requires JSON.js
20 //  ----------------------------------------------------------------
21
22 function Fieldmapper() {}
23
24 Fieldmapper.prototype.clone = function() {
25         var obj = new this.constructor();
26
27         for( var i in this.array ) {
28                 var thing = this.array[i];
29                 if(thing == null) continue;
30
31                 if( thing._isfieldmapper ) {
32                         obj.array[i] = thing.clone();
33                 } else {
34
35                         if(instanceOf(thing, Array)) {
36                                 obj.array[i] = new Array();
37
38                                 for( var j in thing ) {
39
40                                         if( thing[j]._isfieldmapper )
41                                                 obj.array[i][j] = thing[j].clone();
42                                         else
43                                                 obj.array[i][j] = thing[j];
44                                 }
45                         } else {
46                                 obj.array[i] = thing;
47                         }
48                 }
49         }
50         return obj;
51 }
52
53
54
55 function FieldmapperException(message) {
56         this.message = message;
57 }
58
59 FieldmapperException.toString = function() {
60         return "FieldmapperException: " + this.message + "\\n";
61
62 }
63
64
65 JS
66
67 for my $object (keys %$map) {
68
69         if($web) {
70                 my $hint = $map->{$object}->{hint};
71                 next unless (grep { $_ eq $hint } @web_hints );
72                 #next unless( $hint eq "mvr" or $hint eq "aou" or $hint eq "aout" );
73         }
74
75 my $short_name = $map->{$object}->{hint};
76
77 print <<JS;
78
79 //  ----------------------------------------------------------------
80 // Class: $short_name
81 //  ----------------------------------------------------------------
82
83 JS
84
85 print   <<JS;
86
87 $short_name.prototype                                   = new Fieldmapper();
88 $short_name.prototype.constructor       = $short_name;
89 $short_name.baseClass                                   = Fieldmapper.constructor;
90
91 function $short_name(array) {
92
93         this.classname = "$short_name";
94         this._isfieldmapper = true;
95
96         if(array) { 
97                 if( array.constructor == Array) 
98                         this.array = array;  
99
100                 else
101                         throw new FieldmapperException(
102                                 "Attempt to build fieldmapper object with non-array");
103
104         } else { this.array = []; }
105
106 }
107
108 $short_name._isfieldmapper = true;
109
110
111 JS
112
113 for my $field (keys %{$map->{$object}->{fields}}) {
114
115 my $position = $map->{$object}->{fields}->{$field}->{position};
116
117 print <<JS;
118 $short_name.prototype.$field = function(new_value) {
119         if(arguments.length == 1) { this.array[$position] = new_value; }
120         return this.array[$position];
121 }
122 JS
123
124 }
125 }
126