| 1 | package SPOPS::Export; | ||||||||||
| 2 | |||||||||||
| 3 | # $Id: Export.pm,v 3.4 2004/06/02 00:48:21 lachoy Exp $ | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use base qw( Class::Accessor Class::Factory ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use SPOPS::Exception qw( spops_error ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | $SPOPS::Export::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/); | ||||||||||
| 10 | |||||||||||
| 11 | 5 | ▶ | use constant AKEY => '_attrib'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | my @FIELDS = qw( object_class where value include_id skip_fields DEBUG ); | ||||||||||
| 14 | SPOPS::Export->mk_accessors( @FIELDS ); | ||||||||||
| 15 | |||||||||||
| 16 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=14.2
| |||||||||||
| 17 | 5 | my ( $pkg, $type, $params ) = @_; | |||||||||
| 18 | 5 | my $class = eval { $pkg->get_factory_class( $type ) }; | |||||||||
| 19 | 5 | ▶ | spops_error $@ if ( $@ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | my $self = bless( {}, $class );; | |||||||||
| 21 | 0 | foreach my $field ( $self->get_fields ) { | |||||||||
| 22 | 0 | $self->$field( $params->{ $field } ); | |||||||||
| 23 | } | ||||||||||
| 24 | 0 | return $self->initialize( $params ); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | sub initialize { return $_[0] } | ||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
| |||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub get_fields { return @FIELDS } | ||||||||
|
Subroutine
get_fields: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | # Class::Accessor stuff | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub get { return $_[0]->{ AKEY() }{ $_[1] } } | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||
| 36 | 0 | ▶ | sub set { return $_[0]->{ AKEY() }{ $_[1] } = $_[2] } | ||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | # Main event | ||||||||||
| 40 | |||||||||||
| 41 | sub run { | ||||||||||
| 42 | my ( $self ) = @_; | ||||||||||
| 43 | my $object_class = $self->object_class; | ||||||||||
| 44 | unless ( $object_class ) { | ||||||||||
| 45 | spops_error "Cannot export objects without an object class! ", | ||||||||||
| 46 | "Please set using\n", | ||||||||||
| 47 | "\$exporter->object_class( \$object_class )"; | ||||||||||
| 48 | } | ||||||||||
| 49 | my @export_fields = $self->_find_export_fields; | ||||||||||
| 50 | my @output = (); | ||||||||||
| 51 | push @output, $self->create_header( \@export_fields ); | ||||||||||
| 52 | |||||||||||
| 53 | my $iter = $object_class->fetch_iterator({ where => $self->where, | ||||||||||
| 54 | value => $self->value, | ||||||||||
| 55 | DEBUG => $self->DEBUG }); | ||||||||||
| 56 | while ( my $o = $iter->get_next ) { | ||||||||||
| 57 | push @output, $self->create_record( $o, \@export_fields ); | ||||||||||
| 58 | } | ||||||||||
| 59 | push @output, $self->create_footer; | ||||||||||
| 60 | return join( "", @output ); | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # Subclasses should override these actions as necessary -- note that | ||||||||||
| 64 | # if you remove the 'return' you'll get a nasty surprise, since the | ||||||||||
| 65 | # exporter object is returned :-) | ||||||||||
| 66 | |||||||||||
| 67 | sub create_header { return } | ||||||||||
| 68 | sub create_record { return } | ||||||||||
| 69 | sub create_footer { return } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | # Private | ||||||||||
| 73 | |||||||||||
| 74 | sub _find_export_fields { | ||||||||||
| 75 | my ( $self ) = @_; | ||||||||||
| 76 | my %skip_fields = (); | ||||||||||
| 77 | my $object_class = $self->object_class; | ||||||||||
| 78 | if ( $self->skip_fields ) { | ||||||||||
| 79 | map { $skip_fields{ $_ }++ } @{ $self->skip_fields }; | ||||||||||
| 80 | } | ||||||||||
| 81 | unless ( $self->include_id ) { | ||||||||||
| 82 | $skip_fields{ $object_class->CONFIG->{id_field} }++; | ||||||||||
| 83 | } | ||||||||||
| 84 | return grep { ! $skip_fields{ $_ } } @{ $object_class->field_list }; | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | ######################################## | ||||||||||
| 89 | # INITIALIZE | ||||||||||
| 90 | |||||||||||
| 91 | __PACKAGE__->register_factory_type( object => 'SPOPS::Export::Object' ); | ||||||||||
| 92 | __PACKAGE__->register_factory_type( xml => 'SPOPS::Export::XML' ); | ||||||||||
| 93 | __PACKAGE__->register_factory_type( perl => 'SPOPS::Export::Perl' ); | ||||||||||
| 94 | __PACKAGE__->register_factory_type( sql => 'SPOPS::Export::SQL' ); | ||||||||||
| 95 | __PACKAGE__->register_factory_type( dbdata => 'SPOPS::Export::DBI::Data' ); | ||||||||||
| 96 | |||||||||||
| 97 | 1; | ||||||||||
| 98 | |||||||||||
| 99 | __END__ | ||||||||||