#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::DataImport::Command::Base;
use strict;
use warnings;

use Data::Dumper;
use File::Path;

use lib '/app/tools/support/lib';
use Support::DataImport::DataImport;

use base 'Support::Command::Submit';

sub cmd  { die("overload this"); }
sub area { die("overload this"); }

sub populateImportLists {
    my $self = shift;
    $self->{xml}{Import}{ActiveImportList} = $self->_getImportList( Support::DB::Item::DataImport->GetActive() );
    $self->{xml}{Import}{ClosedImportList} = $self->_getImportList( Support::DB::Item::DataImport->GetClosed() );
}

sub _getImportList {
    my ( $self, $collection ) = @_;

    # Return the data_import data in a format such that we can easily build the HTML lists for use with jstree

    my $list->{DataImport} = [];  # DataImportList / Item / DataImport (each DataImport is in its own "Item" node)

    if ( !$collection ) {
        return;
    }

    while ( $collection->hasNext() ) {
        my $obj = $collection->next();

        next if ( $obj->parent_id != 0 ); # only want top level items

        my $importItem = Support::DataImport::DataImport->new( importID => $obj->import_id, loadSubs => 1 );

        # Check if the user has any client restrictions.  If so then we'll only display data imports
        # for clients that the user has access to.
        #
        if ( scalar @{$self->{client_filter}} > 0 ) {
            my $_clientID = $importItem->ClientID();
            if ( grep( /^$_clientID$/, @{$self->{client_filter}} ) ) {
                push @{ $list->{DataImport} }, $importItem;
            }
        } else {
            push @{ $list->{DataImport} }, $importItem;
        }
    }

    return $list;
}

1;
