]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/fieldmapper.pl
made it a little smaller..
[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 = qw/ex mvr au aou aout asv asva asvr asvq 
12                 circ acp acpl acn ccs perm_ex ahn ahr aua ac 
13                 actscecm crcd crmf crrf mus mbts aoc aus/;
14
15 print <<JS;
16
17 //  ----------------------------------------------------------------
18 // Autogenerated by fieldmapper.pl
19 // Requires JSON.js
20 //  ----------------------------------------------------------------
21
22 function Fieldmapper() {}
23
24 var errorstr = "Attempt to build fieldmapper object with non-array";
25
26 Fieldmapper.prototype.clone = function() {
27         var obj = new this.constructor();
28
29         for( var i in this.a ) {
30                 var thing = this.a[i];
31                 if(thing == null) continue;
32
33                 if( thing._isfieldmapper ) {
34                         obj.a[i] = thing.clone();
35                 } else {
36
37                         if(instanceOf(thing, Array)) {
38                                 obj.a[i] = new Array();
39
40                                 for( var j in thing ) {
41
42                                         if( thing[j]._isfieldmapper )
43                                                 obj.a[i][j] = thing[j].clone();
44                                         else
45                                                 obj.a[i][j] = thing[j];
46                                 }
47                         } else {
48                                 obj.a[i] = thing;
49                         }
50                 }
51         }
52         return obj;
53 }
54 Fieldmapper.prototype.isnew = function(n) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; }
55 Fieldmapper.prototype.ischanged = function(n) { if(arguments.length == 1) this.a[1] =n; return this.a[1]; }
56 Fieldmapper.prototype.isdeleted = function(a) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; }
57 function FMEX(message) { this.message = message; }
58 FMEX.toString = function() { return "FieldmapperException: " + this.message + "\\n"; }
59
60 var _c = {};
61
62 JS
63
64 for my $object (keys %$map) {
65
66         if($web) {
67                 my $hint = $map->{$object}->{hint};
68                 next unless (grep { $_ eq $hint } @web_hints );
69         }
70
71         my $short_name = $map->{$object}->{hint};
72
73         my @fields;
74         for my $field (keys %{$map->{$object}->{fields}}) {
75                 my $position = $map->{$object}->{fields}->{$field}->{position};
76                 $fields[$position] = $field;
77         }
78
79         print "_c[\"$short_name\"] = [";
80         for my $f (@fields) { 
81                 if( $f ne "isnew" and $f ne "ischanged" and $f ne "isdeleted" ) {
82                         print "\"$f\","; 
83                 }
84         }
85         print "];\n"
86
87
88
89 }
90
91 print <<JS;
92
93         var string = "";
94         for( var cl in _c ) {
95                 string += cl + ".prototype = new Fieldmapper(); " + 
96                                                         cl + ".prototype.constructor = " + cl + ";" +
97                                                         cl + ".baseClass = Fieldmapper.constructor;" +
98                                                         "function " + cl + "(a) { " +
99                                                                 "this.classname = \\\"" + cl + "\\\";" +
100                                                                 "this._isfieldmapper = true;" +
101                                                                 "if(a) { if(a.constructor == Array) this.a = a; else throw new FMEX(errorstr);} else this.a = []}"; 
102
103                 string += cl + "._isfieldmapper=true;";
104
105                 for( var pos in _c[cl] ) {
106                         var p = parseInt(pos) + 3;
107                         var field = _c[cl][pos];
108                         string += cl + ".prototype." + field + 
109                                 "=function(n){if(arguments.length == 1)this.a[" + 
110                                 p + "]=n;return this.a[" + p + "];};";
111                 }
112
113         }
114
115         eval(string);
116
117 JS
118