Projekt

Allgemein

Profil

Herunterladen (12,7 KB) Statistiken
| Zweig: | Markierung: | Revision:
ea65e003 Moritz Bunkus
package SL::DB::Helper::Mappings;
82515b2d Sven Schöling
05c6840d Moritz Bunkus
use utf8;
82515b2d Sven Schöling
use strict;

bc3a01ae Moritz Bunkus
use SL::Util qw(camelify);

a93b3b64 Moritz Bunkus
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);

5991c23e Sven Schöling
# these will not be managed as Rose::DB models, because they are not normalized,
# significant changes are needed to get them done, or they were done by CRM.
0674bc84 Moritz Bunkus
my @kivitendo_blacklist_permanent = qw(
5991c23e Sven Schöling
leads
f97b0778 Sven Schöling
);
82515b2d Sven Schöling
# these are not managed _yet_, but will hopefully at some point.
# if you are confident that one of these works, remove it here.
0674bc84 Moritz Bunkus
my @kivitendo_blacklist_temp = qw(
82515b2d Sven Schöling
);

2438dd57 Sven Schöling
# tables created by crm module
my @crm_blacklist = qw(
ccfc4635 Moritz Bunkus
blz_data
bundesland
contmasch
contract
crm
crmdefaults
crmemployee
custmsg
docfelder
documents
documenttotc
docvorlage
extra_felder
geodb_changelog
geodb_coordinates
geodb_floatdata
geodb_hierarchies
geodb_intdata
geodb_locations
geodb_textdata
geodb_type_names
grpusr
gruppenname
history
labels
labeltxt
mailvorlage
maschine
maschmat
opport_status
opportunity
postit
repauftrag
telcall
telcallhistory
telnr
tempcsvdata
termdate
termincat
termine
terminmember
timetrack
tt_event
tt_parts
wiedervorlage
wissencategorie
wissencontent
2438dd57 Sven Schöling
);

# tables created by cash register
my @cash_register_blacklist = qw(
ekartikel ekbon ekkunde ektext erptasten
);

0674bc84 Moritz Bunkus
my @kivitendo_blacklist = (@kivitendo_blacklist_permanent, @kivitendo_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
82515b2d Sven Schöling
# map table names to their models.
# unlike rails we have no singular<->plural magic.
# remeber: tables should be named as the plural of the model name.
0674bc84 Moritz Bunkus
my %kivitendo_package_names = (
9f9f93b3 Moritz Bunkus
# TABLE # MODEL (given in C style)
f97b0778 Sven Schöling
acc_trans => 'acc_transaction',
3ced230d Moritz Bunkus
'auth.clients' => 'auth_client',
'auth.clients_users' => 'auth_client_user',
'auth.clients_groups' => 'auth_client_group',
9f9f93b3 Moritz Bunkus
'auth.group' => 'auth_group',
'auth.group_rights' => 'auth_group_right',
d2c5f647 Moritz Bunkus
'auth.master_rights' => 'auth_master_right',
'auth.schema_info' => 'auth_schema_info',
'auth.session' => 'auth_session',
'auth.session_content' => 'auth_session_content',
9f9f93b3 Moritz Bunkus
'auth.user' => 'auth_user',
'auth.user_config' => 'auth_user_config',
'auth.user_group' => 'auth_user_group',
82515b2d Sven Schöling
ar => 'invoice',
ap => 'purchase_invoice',
013b538f Moritz Bunkus
assembly => 'assembly',
f2b44f13 Geoffrey Richardson
assortment_items => 'assortment_item',
295e585a Moritz Bunkus
background_jobs => 'background_job',
background_job_histories => 'background_job_history',
ap => 'purchase_invoice',
4fd22b56 Sven Schöling
bank_accounts => 'bank_account',
6a12a968 Niclas Zimmermann
bank_transactions => 'bank_transaction',
c77e88d3 Jan Büren
bank_transaction_acc_trans => 'bank_transaction_acc_trans',
4fd22b56 Sven Schöling
buchungsgruppen => 'buchungsgruppe',
013b538f Moritz Bunkus
bin => 'bin',
business => 'business',
chart => 'chart',
4fd22b56 Sven Schöling
contacts => 'contact',
013b538f Moritz Bunkus
customer => 'customer',
c2cf302a Moritz Bunkus
csv_import_profiles => 'csv_import_profile',
csv_import_profile_settings => 'csv_import_profile_setting',
67643d03 Sven Schöling
csv_import_reports => 'csv_import_report',
csv_import_report_rows => 'csv_import_report_row',
a5a42ed1 Sven Schöling
csv_import_report_status => 'csv_import_report_status',
26892ad4 Moritz Bunkus
currencies => 'currency',
e713c314 Moritz Bunkus
custom_data_export_queries => 'CustomDataExportQuery',
custom_data_export_query_parameters => 'CustomDataExportQueryParameter',
36703a86 Bernd Bleßmann
custom_variable_config_partsgroups => 'custom_variable_config_partsgroup',
4fd22b56 Sven Schöling
custom_variable_configs => 'custom_variable_config',
custom_variables => 'custom_variable',
custom_variables_validity => 'custom_variable_validity',
f97b0778 Sven Schöling
datev => 'datev',
defaults => 'default',
82515b2d Sven Schöling
delivery_orders => 'delivery_order',
delivery_order_items => 'delivery_order_item',
013b538f Moritz Bunkus
delivery_order_items_stock => 'delivery_order_items_stock',
70167494 Moritz Bunkus
delivery_terms => 'delivery_term',
f97b0778 Sven Schöling
department => 'department',
4fd22b56 Sven Schöling
drafts => 'draft',
dunning => 'dunning',
dunning_config => 'dunning_config',
24ab7ec0 Moritz Bunkus
email_journal => 'EmailJournal',
email_journal_attachments => 'EmailJournalAttachment',
4fd22b56 Sven Schöling
employee => 'employee',
614c48e0 Moritz Bunkus
employee_project_invoices => 'EmployeeProjectInvoices',
f97b0778 Sven Schöling
exchangerate => 'exchangerate',
2bb452ee Martin Helmling
files => 'file',
f97b0778 Sven Schöling
finanzamt => 'finanzamt',
follow_up_access => 'follow_up_access',
4fd22b56 Sven Schöling
follow_up_links => 'follow_up_link',
follow_ups => 'follow_up',
generic_translations => 'generic_translation',
82515b2d Sven Schöling
gl => 'GLTransaction',
041274ee Bernd Bleßmann
greetings => 'greeting',
4fd22b56 Sven Schöling
history_erp => 'history',
f97b0778 Sven Schöling
inventory => 'inventory',
82515b2d Sven Schöling
invoice => 'invoice_item',
4fd22b56 Sven Schöling
language => 'language',
e30fbb34 Geoffrey Richardson
letter => 'letter',
letter_draft => 'letter_draft',
f97b0778 Sven Schöling
makemodel => 'make_model',
4fd22b56 Sven Schöling
notes => 'note',
82515b2d Sven Schöling
orderitems => 'order_item',
oe => 'order',
parts => 'part',
f97b0778 Sven Schöling
partsgroup => 'parts_group',
65d2537d Martin Helmling
part_classifications => 'PartClassification',
62f21410 Martin Helmling
part_customer_prices => 'PartCustomerPrice',
58190585 Moritz Bunkus
parts_price_history => 'PartsPriceHistory',
4fd22b56 Sven Schöling
payment_terms => 'payment_term',
250fa402 Moritz Bunkus
periodic_invoices => 'periodic_invoice',
periodic_invoices_configs => 'periodic_invoices_config',
7ade1b95 Moritz Bunkus
prices => 'price',
82515b2d Sven Schöling
price_factors => 'price_factor',
9589ecd7 Sven Schöling
price_rules => 'price_rule',
price_rule_items => 'price_rule_item',
4fd22b56 Sven Schöling
pricegroup => 'pricegroup',
013b538f Moritz Bunkus
printers => 'printer',
project => 'project',
03f5aaa3 Sven Schöling
project_participants => 'project_participant',
project_phase_participants => 'project_phase_participant',
project_phases => 'project_phase',
project_roles => 'project_role',
0ffbc528 Moritz Bunkus
project_statuses => 'project_status',
03f5aaa3 Sven Schöling
project_types => 'project_type',
6a12a968 Niclas Zimmermann
reconciliation_links => 'reconciliation_link',
c8a3fef7 Moritz Bunkus
record_links => 'record_link',
record_templates => 'record_template',
record_template_items => 'record_template_item',
d17e1b9d Moritz Bunkus
requirement_spec_acceptance_statuses => 'RequirementSpecAcceptanceStatus',
requirement_spec_complexities => 'RequirementSpecComplexity',
requirement_spec_item_dependencies => 'RequirementSpecDependency',
requirement_spec_items => 'RequirementSpecItem',
687a09d8 Moritz Bunkus
requirement_spec_orders => 'RequirementSpecOrder',
0c319351 Moritz Bunkus
requirement_spec_parts => 'RequirementSpecPart',
9cddaf37 Moritz Bunkus
requirement_spec_pictures => 'RequirementSpecPicture',
d17e1b9d Moritz Bunkus
requirement_spec_predefined_texts => 'RequirementSpecPredefinedText',
requirement_spec_risks => 'RequirementSpecRisk',
requirement_spec_statuses => 'RequirementSpecStatus',
requirement_spec_text_blocks => 'RequirementSpecTextBlock',
requirement_spec_types => 'RequirementSpecType',
requirement_spec_versions => 'RequirementSpecVersion',
requirement_specs => 'RequirementSpec',
4fd22b56 Sven Schöling
sepa_export => 'sepa_export',
sepa_export_items => 'sepa_export_item',
1ac4c07d Moritz Bunkus
sepa_export_message_ids => 'SepaExportMessageId',
85da554f Sven Schöling
schema_info => 'schema_info',
013b538f Moritz Bunkus
shipto => 'shipto',
b2bedb6b Werner Hahn
shops => 'shop',
d3ae6850 Werner Hahn
shop_images => 'shop_image',
b2bedb6b Werner Hahn
shop_orders => 'shop_order',
shop_order_items => 'shop_order_item',
shop_parts => 'shop_part',
f97b0778 Sven Schöling
status => 'status',
dc3382e8 Bernd Bleßmann
stocktakings => 'stocktaking',
4fd22b56 Sven Schöling
tax => 'tax',
f97b0778 Sven Schöling
taxkeys => 'tax_key',
tax_zones => 'tax_zone',
b989d7cf Geoffrey Richardson
taxzone_charts => 'taxzone_chart',
f97b0778 Sven Schöling
todo_user_config => 'todo_user_config',
013b538f Moritz Bunkus
transfer_type => 'transfer_type',
f97b0778 Sven Schöling
translation => 'translation',
09a63fcb Moritz Bunkus
trigger_information => 'trigger_information',
82515b2d Sven Schöling
units => 'unit',
f97b0778 Sven Schöling
units_language => 'units_language',
98625cb1 Geoffrey Richardson
user_preferences => 'user_preference',
5198e9c4 Sven Schöling
vendor => 'vendor',
013b538f Moritz Bunkus
warehouse => 'warehouse',
82515b2d Sven Schöling
);

0674bc84 Moritz Bunkus
my (%kivitendo_tables_to_packages, %kivitendo_tables_to_manager_packages, %kivitendo_packages_to_tables);
a93b3b64 Moritz Bunkus
82515b2d Sven Schöling
sub get_blacklist {
0674bc84 Moritz Bunkus
return KIVITENDO => \@kivitendo_blacklist;
82515b2d Sven Schöling
}

sub get_package_names {
0674bc84 Moritz Bunkus
return KIVITENDO => \%kivitendo_package_names;
82515b2d Sven Schöling
}

48c0b3c9 Sven Schöling
sub get_name_for_table {
return $kivitendo_package_names{ $_[0] };
}

a93b3b64 Moritz Bunkus
sub get_package_for_table {
0674bc84 Moritz Bunkus
%kivitendo_tables_to_packages = map { ($_ => "SL::DB::" . camelify($kivitendo_package_names{$_})) } keys %kivitendo_package_names
unless %kivitendo_tables_to_packages;
a93b3b64 Moritz Bunkus
0674bc84 Moritz Bunkus
return $kivitendo_tables_to_packages{ $_[0] };
a93b3b64 Moritz Bunkus
}

sub get_manager_package_for_table {
0674bc84 Moritz Bunkus
%kivitendo_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($kivitendo_package_names{$_})) } keys %kivitendo_package_names
unless %kivitendo_tables_to_manager_packages;
a93b3b64 Moritz Bunkus
0674bc84 Moritz Bunkus
return $kivitendo_tables_to_manager_packages{ $_[0] };
a93b3b64 Moritz Bunkus
}

sub get_table_for_package {
0674bc84 Moritz Bunkus
get_package_for_table('dummy') if !%kivitendo_tables_to_packages;
%kivitendo_packages_to_tables = reverse %kivitendo_tables_to_packages unless %kivitendo_packages_to_tables;
a93b3b64 Moritz Bunkus
my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
0674bc84 Moritz Bunkus
return $kivitendo_packages_to_tables{ $package };
a93b3b64 Moritz Bunkus
}

3cc8ad87 Sven Schöling
sub db {
my $string = $_[0];
0674bc84 Moritz Bunkus
my $lookup = $kivitendo_package_names{$_[0]} ||
plurify($kivitendo_package_names{singlify($_[0])});
3cc8ad87 Sven Schöling
for my $thing ($string, $lookup) {

# best guess? its already the name. like part. camelize it first
my $class = "SL::DB::" . camelify($thing);
return $class if defined *{ $class. '::' };

# next, someone wants a manager and pluralized.
my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
return $manager if defined *{ $manager . '::' };
}

die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
}

sub plurify {
my ($str) = @_;
$str . 's';
}

sub singlify {
my ($str) = @_;
local $/ = 's';
chomp $str;
$str;
}

82515b2d Sven Schöling
1;

__END__

a93b3b64 Moritz Bunkus
=encoding utf8

82515b2d Sven Schöling
=head1 NAME

ea65e003 Moritz Bunkus
SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
82515b2d Sven Schöling
=head1 SYNOPSIS

ea65e003 Moritz Bunkus
use SL::DB::Helper::Mappings qw(@blacklist %table2model);
82515b2d Sven Schöling
=head1 DESCRIPTION

This modul stores table <-> model mappings used by the
L<scripts/rose_auto_create_model.pl> script. If you add a new table that has
custom mappings, add it here.

a93b3b64 Moritz Bunkus
=head1 FUNCTIONS
3cc8ad87 Sven Schöling
a93b3b64 Moritz Bunkus
=over 4

=item C<db $name>

A special function provided here is C<db>. Without it you'd have to write:
3cc8ad87 Sven Schöling
my $part = SL::DB::Part->new(id => 1234);
my @all_parts = SL::DB::Manager::Part->get_all;

with them it becomes:

my $part = db('part')->new(id => 123);
my @all_parts = db('parts')->get_all;

You don't have to care about add that SL::DB:: incantation anymore. Also, a
simple s at the end will get you the associated Manager class.

db is written to try to make sense of what you give it, but if all fails, it
will die with an error.

a93b3b64 Moritz Bunkus
=item C<get_package_for_table $table_name>

Returns the package name for a table name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_package_for_table('oe')
a93b3b64 Moritz Bunkus
# SL::DB::Order

=item C<get_manager_package_for_table $table_name>

Returns the manager package name for a table name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
a93b3b64 Moritz Bunkus
# SL::DB::Manager::Order

=item C<get_table_for_package $package_name>

Returns the table name for a package name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
a93b3b64 Moritz Bunkus
# oe
0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_table_for_package('Order')
a93b3b64 Moritz Bunkus
# oe

=back

82515b2d Sven Schöling
=head1 BUGS

nothing yet

=head1 SEE ALSO

L<scripts/rose_auto_create_model.pl>

=head1 AUTHOR

a93b3b64 Moritz Bunkus
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
82515b2d Sven Schöling
=cut