X-Git-Url: http://wagnertech.de/git?p=kivitendo-erp.git;a=blobdiff_plain;f=SL%2FClipboard%2FBase.pm;fp=SL%2FClipboard%2FBase.pm;h=fc0a16a1ff67574a7f8219d2741059695036e684;hp=0000000000000000000000000000000000000000;hb=bd25baf67d259c1b805951dfd92a310766c13661;hpb=51eca07c805710334a326a2eac6df7ba5bdc8aec diff --git a/SL/Clipboard/Base.pm b/SL/Clipboard/Base.pm new file mode 100644 index 000000000..fc0a16a1f --- /dev/null +++ b/SL/Clipboard/Base.pm @@ -0,0 +1,236 @@ +package SL::Clipboard::Base; + +use strict; + +use parent qw(Rose::Object); + +use Rose::Object::MakeMethods::Generic ( + 'scalar --get_set_init' => [ qw(content timestamp) ], +); + +use Rose::DB::Object::Helpers (); + +sub init_timestamp { die "'timestamp' property not set"; } +sub init_content { die "'content' property not set"; } + +sub type { + my ($self_or_class) = @_; + return (split m/::/, ref($self_or_class) ? ref($self_or_class) : $self_or_class)[-1]; +} + +sub reload_object { + my ($self, $object) = @_; + + return ref($object)->new(map { $_ => $object->$_ } $object->meta->primary_key)->load; +} + +sub as_tree { + my ($self, $object, %params) = @_; + + my $tree = Rose::DB::Object::Helpers::as_tree($object, %params); + $self->_fix_tree($tree, $object); + return $tree; +} + +sub to_object { + my ($self) = @_; + my $object = Rose::DB::Object::Helpers::new_from_tree("SL::DB::" . $self->type, $self->content); + + # Reset primary key columns and itime/mtime if the class supports it. + foreach ($object->meta->primary_key, 'itime', 'mtime') { + $object->$_(undef) if $object->can($_); + } + + # Let sub classes fix the objects further. + $self->_fix_object($object); + return $object; +} + +sub dump { + my ($self, $object) = @_; + return $self->as_tree($self->reload_object($object), max_depth => 1); +} + +sub describe { + die "'describe' method not overwritten by derived class"; +} + +sub _fix_object { + my ($self, $object) = @_; + # To be overwritten by child classes. +} + +sub _fix_tree { + my ($self, $tree, $object) = @_; + + # Delete primary key columns and itime/mtime if the class supports it. + foreach ($object->meta->primary_key, 'itime', 'mtime') { + delete $tree->{$_} if $object->can($_); + } +} + +sub _binary_column_names { + my ($self, $class) = @_; + return map { $_->name } + grep { ref($_) =~ m/Pg::Bytea$/i } + @{ $class->meta->columns }; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::Clipboard::Base - Base class for clipboard specialization classes + +=head1 SYNOPSIS + +See the synopsis of L. + +=head1 OVERVIEW + +This is a base class providing a lot of utility and +defaults. Sub-classes must overwrite at least the function +L but can overwrite others as well. + +Writing a specialized sub-class for a database type involves +overwriting one or more functions. These are: + +=over 4 + +=item * C + +Must be overwritten. Returns a human-readable description of the +content. Should only be one line. + +=item * C + +Optional. Overwrite if sub-class needs to dump more/less than the +implementation in this class dumps. + +=item * C<_fix_object> + +Optional. Overwrite if re-created Rose::DB::Object instances must be +cleaned further before they're returned to the caller. + +=item * C<_fix_tree> + +Optional. Overwrite if the tree created during a copy operation of a +Rose::DB::Object instance must be cleaned further before it's stored. + +=back + +You don't have to or should not overwrite the other functions: + +=over 4 + +=item * C + +=item * C + +=item * C + +=item * C + +=back + +Don't forget to C the specialized module here in Base! + +=head1 FUNCTIONS + +=over 4 + +=item C + +A convenience function calling L +with C<$object> and C<%params> as parameters. Returns a hash/array +reference tree of the function. + +Don't overwrite this function in sub-classes. Overwrite L +instead. + +=item C + +Returns a human-readable description of the content. This should only +be a single line without any markup. + +Sub-classes must overwrite this function. + +=item C + +Dumps the object as a hash/array tree and returns it by calling +L. The default implementation +reloads the object first by calling L. It also only +dumps the object itself, not any of the relationships, by calling +C with the parameter C 1>. + +Overwrite this in a sub-class if you need to dump more or differently +(see L for an example). + +=item C + +Reloads C<$object> from the database and returns a new instance. Can +be useful for sanitizing the object given to L before +converting into a tree. It is used by the default implementation of +L. + +=item C + +Converts the dumped representation back to a Rose::DB::Object +instance. Several columns of the newly created object are cleared by +C itself: the primary key columns (if any) and the columns +C and C (if the object has such columns). + +This function should not be overwritten by sub-classes. Instead, +functions can overwrite C<_fix_object> which can be used for sanitizing +the newly created object before handing it back to the caller. + +=item C + +Returns the actual clipped type (e.g. C). This is +derived from the actual class name of C<$self>. + +=item C<_binary_column_names $class> + +Returns an array of column names that have a binary type. Useful for +sub-classes which need to encode binary content in Base64 during +C. + +=item C<_fix_object $object> + +This function is called by L before the object is passed +back to the caller. It does not do anything in the default +implementation, but sub-classes are free to overwrite it if they need +to sanitize the object. See L for +an example. + +Its return value is ignored. + +=item C<_fix_tree $tree, $object> + +This function is called by L after dumping and before the +object is stored during a copy operation. In the default +implementation all primary key columns and the columns C and +C (if the object has such columns) are removed from the tree. +Sub-classes are free to overwrite it if they need to sanitize the +tree. See L for an example. + +C<$object> is just passed in for reference and should not be modified. + +Its return value is ignored. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut