Apache-Sling
view release on metacpan or search on metacpan
lib/Apache/Sling/Group.pm view on Meta::CPAN
#{{{sub new
sub new {
my ( $class, $authn, $verbose, $log ) = @_;
if ( !defined $authn ) { croak 'no authn provided!'; }
my $response;
$verbose = ( defined $verbose ? $verbose : 0 );
my $group = {
BaseURL => ${$authn}->{'BaseURL'},
Authn => $authn,
Message => q{},
Response => \$response,
Verbose => $verbose,
Log => $log
};
bless $group, $class;
return $group;
}
#}}}
#{{{sub set_results
sub set_results {
my ( $group, $message, $response ) = @_;
$group->{'Message'} = $message;
$group->{'Response'} = $response;
return 1;
}
#}}}
#{{{sub add
sub add {
my ( $group, $act_on_group, $properties ) = @_;
my $res = Apache::Sling::Request::request(
\$group,
Apache::Sling::GroupUtil::add_setup(
$group->{'BaseURL'}, $act_on_group, $properties
)
);
my $success = Apache::Sling::GroupUtil::add_eval($res);
my $message = "Group: \"$act_on_group\" ";
$message .= ( $success ? 'added!' : 'was not added!' );
$group->set_results( "$message", $res );
return $success;
}
#}}}
#{{{sub add_from_file
sub add_from_file {
my ( $group, $file, $fork_id, $number_of_forks ) = @_;
$fork_id = defined $fork_id ? $fork_id : 0;
$number_of_forks = defined $number_of_forks ? $number_of_forks : 1;
my $csv = Text::CSV->new();
my $count = 0;
my $number_of_columns = 0;
my @column_headings;
if ( !defined $file ) {
croak 'File to upload from not defined';
}
if ( open my ($input), '<', $file ) {
while (<$input>) {
if ( $count++ == 0 ) {
# Parse file column headings first to determine field names:
if ( $csv->parse($_) ) {
@column_headings = $csv->fields();
# First field must be group:
if ( $column_headings[0] !~ /^[Gg][Rr][Oo][Uu][Pp]$/msx ) {
croak 'First CSV column must be the group ID, '
. 'column heading must be "group". '
. 'Found: "'
. $column_headings[0] . "\".\n";
}
$number_of_columns = @column_headings;
}
else {
croak 'CSV broken, failed to parse line: '
. $csv->error_input;
}
}
elsif ( $fork_id == ( $count++ % $number_of_forks ) ) {
my @properties;
if ( $csv->parse($_) ) {
my @columns = $csv->fields();
my $columns_size = @columns;
# Check row has same number of columns as there were column headings:
if ( $columns_size != $number_of_columns ) {
croak
"Found \"$columns_size\" columns. There should have been \"$number_of_columns\".\n"
. "Row contents was: $_";
}
my $id = $columns[0];
for ( my $i = 1 ; $i < $number_of_columns ; $i++ ) {
my $heading = $column_headings[$i];
my $data = $columns[$i];
my $value = "$heading=$data";
push @properties, $value;
}
$group->add( $id, \@properties );
Apache::Sling::Print::print_result($group);
}
else {
croak 'CSV broken, failed to parse line: '
. $csv->error_input;
}
}
}
close $input or croak q{Problem closing input!};
}
else {
croak "Problem opening file: '$file'";
}
return 1;
}
#}}}
( run in 0.859 second using v1.01-cache-2.11-cpan-b16cb0d3907 )