/usr/share/perl5/Magpie/Resource/DBIC.pm is in libmagpie-perl 1.140280-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | package Magpie::Resource::DBIC;
{
$Magpie::Resource::DBIC::VERSION = '1.140280';
}
# ABSTRACT: Resource implementation for DBIx::Class ResultSources.
use Moose;
extends 'Magpie::Resource';
with 'Magpie::Plugin::DBI';
use Class::Load;
use Magpie::Constants;
use Try::Tiny;
has data_source => (
is => 'ro',
isa => 'KiokuDB',
lazy_build => 1,
);
has wrapper_class => (
isa => "Str",
is => "ro",
required => 1,
default => 'MagpieGenericWrapper',
);
has dsn => (
isa => "Str",
is => "ro",
predicate => "has_dsn",
);
has extra_args => (
isa => "HashRef|ArrayRef",
is => "ro",
predicate => "has_extra_args",
);
has typemap => (
isa => "KiokuDB::TypeMap",
is => "ro",
predicate => "has_typemap",
);
has _kioku_scope => (
is => 'rw',
isa => 'KiokuDB::LiveObjects::Scope',
);
has username => (
is => 'ro',
isa => 'Maybe[Str]',
predicate => 'has_username',
);
has password => (
is => 'ro',
isa => 'Maybe[Str]',
predicate => 'has_password',
);
sub _connect_args {
my $self = shift;
my @args = ( $self->dsn || die "dsn is required" );
if ( $self->has_username ) {
push @args, user => $self->username;
}
if ( $self->has_password ) {
push @args, password => $self->password;
}
if ( $self->has_typemap ) {
push @args, typemap => $self->typemap;
}
if ( $self->has_extra_args ) {
my $extra = $self->extra_args;
if ( ref($extra) eq 'ARRAY' ) {
push @args, @$extra;
}
else {
push @args, %$extra;
}
}
\@args;
}
sub _build_data_source {
my $self = shift;
my $k = undef;
try {
$k = $self->resolve_asset( service => 'kioku_dir' );
}
catch {
try {
$k = KiokuDB->connect( @{ $self->_connect_args } );
}
catch {
my $error = "Could not connect to Kioku data source: $_\n";
warn $error;
$self->set_error( { status_code => 500, reason => $error } );
};
};
return undef if $self->has_error;
$self->_kioku_scope( $k->new_scope );
return $k;
}
sub GET {
my $self = shift;
$self->parent_handler->resource($self);
my $req = $self->request;
my $path = $req->path_info;
my $id = $self->get_entity_id;
if ($path =~ /\/$/ && !$id ) {
$self->state('prompt');
return OK;
}
my $data = undef;
try {
($data) = $self->data_source->lookup($id);
}
catch {
my $error = "Could not GET data from Kioku data source: $_\n";
$self->set_error( { status_code => 500, reason => $error } );
};
return OK if $self->has_error;
unless ($data) {
$self->set_error({ status_code => 404, reason => 'Resource not found.'});
return OK;
}
#warn "got data " . Dumper($data);
$self->data($data);
return OK;
}
sub POST {
my $self = shift;
$self->parent_handler->resource($self);
my $req = $self->request;
my $to_store = undef;
my $wrapper_class = $self->wrapper_class;
# XXX should check for a content body first.
my %args = ();
if ( $self->has_data ) {
%args = %{ $self->data };
$self->clear_data;
}
else {
for ( $req->param ) {
$args{$_} = $req->param($_);
}
}
# permit POST to update if there's an entity ID.
# XXX: Should this go in an optional Role?
if (my $existing_id = $self->get_entity_id) {
my $existing = undef;
try {
($existing) = $self->data_source->lookup($existing_id);
}
catch {
my $error = "Could not fetch data from Kioku data source for POST editing if entity with ID $existing_id: $_\n";
$self->set_error( { status_code => 500, reason => $error } );
};
return OK if $self->has_error;
if ($existing) {
foreach my $key (keys(%args)) {
$existing->$key( $args{$key} );
}
try {
$self->data_source->store($existing);
}
catch {
my $error = "Error updating data entity with ID $existing_id: $_\n";
$self->set_error( { status_code => 500, reason => $error } );
};
return OK if $self->has_error;
# finally, if it all went OK, say so.
$self->state('updated');
$self->response->status(204);
return OK;
}
}
# if we make it here there is no existing record, so make a new one.
try {
Class::Load::load_class($wrapper_class);
$to_store = $wrapper_class->new(%args);
}
catch {
my $error
= "Could not create instance of wrapper class '$wrapper_class': $_\n";
warn $error;
$self->set_error( { status_code => 500, reason => $error } );
};
return DECLINED if $self->has_error;
my $id = undef;
try {
$id = $self->data_source->store($to_store);
}
catch {
my $error = "Could not store POST data in Kioku data source: $_\n";
warn $error;
$self->set_error( { status_code => 500, reason => $error } );
};
return DECLINED if $self->has_error;
# XXX: all of this needs to go in an abstract object downstream serializer
# can figure stuff out
my $path = $req->path_info;
$path =~ s|^/||;
$path =~ s|/$||;
$self->state('created');
$self->response->status(201);
$self->response->header( 'Location' => $req->base . $path . "/$id" );
return OK;
}
sub DELETE {
my $self = shift;
$self->parent_handler->resource( $self );
my $req = $self->request;
my $path = $req->path_info;
if ( $path =~ /\/$/ ) {
$self->state('prompt');
return OK;
}
my @steps = split '/', $path;
my $id = $req->param('id') || pop @steps;
# should we do a separate lookup to make sure the data is there?
try {
$self->data_source->delete( $id );
}
catch {
my $error = "Could not delete data from Kioku data source: $_\n";
$self->set_error({ status_code => 500, reason => $error });
};
return OK if $self->has_error;
$self->state('deleted');
$self->response->status(204);
return OK;
}
package MagpieGenericWrapper;
{
$MagpieGenericWrapper::VERSION = '1.140280';
}
sub new {
my $proto = shift;
my %args = @_;
return bless \%args, $proto;
}
1;
=pod
=head1 NAME
Magpie::Resource::DBIC - Resource implementation for DBIx::Class ResultSources.
=head1 VERSION
version 1.140280
# SEEALSO: Magpie, Magpie::Resource
=head1 AUTHORS
=over 4
=item *
Kip Hampton <kip.hampton@tamarou.com>
=item *
Chris Prather <chris.prather@tamarou.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Tamarou, LLC.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
__END__
|