package MpmasProducerOrganizationSet;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use SimpleObjectRegistry;
use MpmasInputBlock;
use SimpleObject;
use IndexedTable;

our @ISA = qw(MpmasInputfileSet);
use MpmasProducerOrganization;

sub class_of_files{
	return "MpmasProducerOrganization";
 }
sub required {
	my $self = shift;
	my $dbh = $self->get_dbh();
	if (defined $self->config_entry("PRODUCER_ORGANIZATIONS") ) {
		my $prodtab = $self->config_entry("PRODUCER_ORGANIZATIONS")->transform($dbh, 1);
		return 0 if ($prodtab->is_empty());
		$self->set_global_table("PRODUCER_ORGANIZATIONS", $prodtab);
		$self->set_config_entry("PRODUCER_ORGS", scalar( $prodtab->query([], 0)));
		return 1;
	}
	else {
		return 0;
	}
}
sub determine_files {
	my $self = shift;
	my $prodtab = $self->get_global("PRODUCER_ORGANIZATIONS");
	my $i = 0;
	for my $po (sort {$a <=> $b } $prodtab->query([],0) ) {
		$self->add_file($po, $i++);
	}
}

sub prepare_shared_files {
	my ($self) = @_;
	my $potab = $self->get_global("PRODUCER_ORGANIZATIONS");
	my $dbh = $self->get_dbh();
	my $poservicetab = $self->config_entry("PRODUCER_ORGANIZATION_SERVICES")->transform($dbh, 2);
	$self->set_global_table("PRODUCER_ORGANIZATION_SERVICES", $poservicetab);	
}

sub prepare_special_files {
	my $self = shift;
	for my $f ($self->files) {
		$self->delete_file($f) unless $f->prepare_special();
	}
}

1;
