Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[kivitendo-erp.git] / scripts / rose_auto_create_model.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
7   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
8 }
9
10 use CGI qw( -no_xhtml);
11 use Config::Std;
12 use Data::Dumper;
13 use English qw( -no_match_vars );
14 use List::MoreUtils qw(any);
15
16 use SL::Auth;
17 use SL::DBUtils;
18 use SL::DB;
19 use SL::Form;
20 use SL::Locale;
21 use SL::LXDebug;
22 use SL::LxOfficeConf;
23 use SL::DB::Helper::ALL;
24 use SL::DB::Helper::Mappings;
25
26 our $form;
27 our $cgi;
28 our $auth;
29 our %lx_office_conf;
30
31 our $script =  __FILE__;
32 $script     =~ s:.*/::;
33
34 $OUTPUT_AUTOFLUSH       = 1;
35 $Data::Dumper::Sortkeys = 1;
36
37 our $meta_path = "SL/DB/MetaSetup";
38
39 sub setup {
40   if (@ARGV < 2) {
41     print "Usage: $PROGRAM_NAME login table1[=package1] [table2[=package2] ...]\n";
42     print "   or  $PROGRAM_NAME login [--all|-a] [--sugar|-s]\n";
43     exit 1;
44   }
45
46   SL::LxOfficeConf->read;
47
48   my $login     = shift @ARGV;
49
50   $::lxdebug    = LXDebug->new();
51
52   # locale messages
53   $::locale       = Locale->new("de");
54   $::form         = new Form;
55   $::cgi          = new CGI('');
56   $::auth         = SL::Auth->new();
57
58   $::user         = User->new($login);
59
60   %::myconfig     = $auth->read_user($login);
61   $form->{script} = 'rose_meta_data.pl';
62   $form->{login}  = $login;
63
64   map { $form->{$_} = $::myconfig{$_} } qw(stylesheet charset);
65
66   mkdir $meta_path unless -d $meta_path;
67 }
68
69 sub process_table {
70   my @spec       =  split(/=/, shift, 2);
71   my $table      =  $spec[0];
72   my $schema     = '';
73   ($schema, $table) = split(m/\./, $table) if $table =~ m/\./;
74   my $package    =  ucfirst($spec[1] || $spec[0]);
75   $package       =~ s/_+(.)/uc($1)/ge;
76   my $meta_file  =  "${meta_path}/${package}.pm";
77   my $file       =  "SL/DB/${package}.pm";
78
79   $schema        = <<CODE if $schema;
80     __PACKAGE__->meta->schema('$schema');
81 CODE
82
83   my $definition =  eval <<CODE;
84     package SL::DB::AUTO::$package;
85     use SL::DB::Object;
86     use base qw(SL::DB::Object);
87
88     __PACKAGE__->meta->table('$table');
89 $schema
90     __PACKAGE__->meta->auto_initialize;
91
92     __PACKAGE__->meta->perl_class_definition(indent => 2); # , braces => 'bsd'
93 CODE
94
95   if ($EVAL_ERROR) {
96     print STDERR "Error in execution for table '$table': $EVAL_ERROR";
97     return;
98   }
99
100   $definition =~ s/::AUTO::/::/g;
101
102   my $file_exists = -f $meta_file;
103
104   open(OUT, ">$meta_file") || die;
105   print OUT <<CODE;
106 # This file has been auto-generated. Do not modify it; it will be overwritten
107 # by $::script automatically.
108 $definition;
109 CODE
110   close OUT;
111
112   print "File '$meta_file' " . ($file_exists ? 'updated' : 'created') . " for table '$table'\n";
113
114   if (! -f $file) {
115     open(OUT, ">$file") || die;
116     print OUT <<CODE;
117 # This file has been auto-generated only because it didn't exist.
118 # Feel free to modify it at will; it will not be overwritten automatically.
119
120 package SL::DB::${package};
121
122 use strict;
123
124 use SL::DB::MetaSetup::${package};
125
126 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
127 __PACKAGE__->meta->make_manager_class;
128
129 1;
130 CODE
131     close OUT;
132
133     print "File '$file' created as well.\n";
134   }
135 }
136
137 setup();
138
139 my %blacklist     = SL::DB::Helper::Mappings->get_blacklist;
140 my %package_names = SL::DB::Helper::Mappings->get_package_names;
141
142 my @tables = ();
143 if (($ARGV[0] eq '--all') || ($ARGV[0] eq '-a') || ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s')) {
144   my ($type, $prefix) = ($ARGV[0] eq '--sugar') || ($ARGV[0] eq '-s') ? ('SUGAR', 'sugar_') : ('LXOFFICE', '');
145   my $db              = SL::DB::create(undef, $type);
146   @tables             = map  { $package_names{$type}->{$_} ? "${_}=" . $package_names{$type}->{$_} : $prefix ? "${_}=${prefix}${_}" : $_ }
147                         grep { my $table = $_; !any { $_ eq $table } @{ $blacklist{$type} } }
148                         $db->list_tables;
149
150 } else {
151   @tables = @ARGV;
152 }
153
154 foreach my $table (@tables) {
155   # add default model name unless model name is given or no defaults exists
156   $table .= '=' . $package_names{LXOFFICE}->{lc $table} if $table !~ /=/ && $package_names{LXOFFICE}->{lc $table};
157
158   process_table($table);
159 }