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