#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Metadata::Parser::Iterator::Excel;

use Common::UTF8;

#
# Common::Parser::Iterator::Excel
#
# This iterator is used to parse Excel 97/2000 spreedsheets.
# This files assumes that the data is on the first worksheet
# of the workbook.
#

use strict;

use lib '/app/tools/common/lib';
use Common::Parser::Item::Row;

use base 'Common::Parser::Iterator::Excel';

sub hasNext {
    my $self = shift;

    return unless ( defined( $self->{_lastRow} ) && defined( $self->{_currentRow} ) );

    return $self->{_currentRow} < $self->{_lastRow};
}

sub next {
    my $self = shift;

    return unless ( $self->hasNext() );

    my @row = $self->_fetchRow();

    $self->{_currentRow} += 1;

    my $dataClass = $self->_itemClass();

    my $record = $dataClass->new( row => \@row );

    # We are done reading after the first empty row
    return if ( $record->isEmpty() );

    return $record;
}

sub rowCount {
    shift->{_lastRow};
}

# _clean
#
# Clean up a value read from a file, UTF8 encode it.
sub _clean {
    my $self = shift;
    my $val = shift || return;
    return Common::UTF8::FilterControlChars( $self->SUPER::_clean($val) );
}

###
1;    # Play nicely.
###
