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