| 1 | # ===================================================================== | ||
| 2 | # Copyright (c) 2002,2003 IBM Corporation | ||
| 3 | # All rights reserved. This program and the accompanying materials | ||
| 4 | # are made available under the terms of the Common Public License v1.0 | ||
| 5 | # which accompanies this distribution, and is available at | ||
| 6 | # http://www.opensource.org/licenses/cpl.php | ||
| 7 | # | ||
| 8 | # ===================================================================== | ||
| 9 | package LS::Resource; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use vars qw( @ISA ); | ||
| 15 | |||
| 16 | use URI; | ||
| 17 | |||
| 18 | use LS; | ||
| 19 | use LS::ID; | ||
| 20 | |||
| 21 | use LS::Authority::WSDL::Constants; | ||
| 22 | |||
| 23 | use base 'LS::Base'; | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | # | ||
| 28 | # new( $lsid, $authority, $wsdl ) - | ||
| 29 | # | ||
| 30 | sub new { | ||
| 31 | my ($class, $lsid, $authority, $wsdl) = @_; | ||
| 32 | |||
| 33 | $lsid = LS::ID->new($lsid) unless(UNIVERSAL::isa($lsid, 'LS::ID')); | ||
| 34 | |||
| 35 | unless ($lsid) { | ||
| 36 | |||
| 37 | LS::Resource->recordError( 'Invalid LSID' ); | ||
| 38 | LS::Resource->addStackTrace(); | ||
| 39 | |||
| 40 | return undef; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (!$authority) { | ||
| 44 | |||
| 45 | # Moved here, no need to have it global | ||
| 46 | require LS::Locator; | ||
| 47 | |||
| 48 | my $locator = LS::Locator->new(); | ||
| 49 | |||
| 50 | unless($locator) { | ||
| 51 | |||
| 52 | LS::Resource->recordError( 'Unable to create LS::Locator object' ); | ||
| 53 | LS::Resource->appendError( (LS::Locator->errorString() || 'No error message from LS::Locator') ); | ||
| 54 | |||
| 55 | LS::Resource->addStackTrace( LS::Locator->getStackTrace() ); | ||
| 56 | |||
| 57 | return undef; | ||
| 58 | } | ||
| 59 | $authority = $locator->resolveAuthority($lsid); | ||
| 60 | |||
| 61 | unless ($authority) { | ||
| 62 | |||
| 63 | LS::Resource->recordError( 'Authority not found' ); | ||
| 64 | LS::Resource->appendError( ($locator->errorString() || 'No error message from LS::Locator') ); | ||
| 65 | |||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if(!$wsdl) { | ||
| 71 | |||
| 72 | my $wsdl = $authority->getAvailableServices($lsid); | ||
| 73 | |||
| 74 | unless ($wsdl) { | ||
| 75 | |||
| 76 | LS::Resource->recordError( 'The authority failed to get available services for the resource.'); | ||
| 77 | LS::Resource->appendError( ($authority->errorString() || 'No error message from LS::Authority') ); | ||
| 78 | |||
| 79 | return undef; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | require LS::Cache::Manager; | ||
| 84 | |||
| 85 | my $self = bless { | ||
| 86 | _lsid=> $lsid, | ||
| 87 | _authority=> $authority, | ||
| 88 | _wsdl=> $wsdl, | ||
| 89 | |||
| 90 | _default_service=> undef, # Name of the "default" service | ||
| 91 | |||
| 92 | _services=> [ ], # Arrayref of service names | ||
| 93 | _metadata_locations=> {}, | ||
| 94 | _data_locations=> {}, | ||
| 95 | |||
| 96 | _rdf_model=> undef, | ||
| 97 | _cache=> LS::Cache::Manager->new(), | ||
| 98 | }, $class; | ||
| 99 | |||
| 100 | $self->_parse_wsdl($wsdl) || return; | ||
| 101 | |||
| 102 | $self->recordError( undef ); | ||
| 103 | |||
| 104 | return $self; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub authority { | ||
| 109 | return $_[0]->{'_authority'}; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub lsid { | ||
| 113 | return $_[0]->{'_lsid'}; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub wsdl { | ||
| 117 | return $_[0]->{'_wsdl'}; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub services { | ||
| 121 | return $_[0]->{'_services'}; | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | # Deprecated | ||
| 129 | sub get_data_locations { | ||
| 130 | |||
| 131 | my $self = shift; | ||
| 132 | return $self->getDefaultDataLocations(); | ||
| 133 | } | ||
| 134 | |||
| 135 | # Deprecated | ||
| 136 | sub get_metadata_locations { | ||
| 137 | |||
| 138 | my $self = shift; | ||
| 139 | return $self->getDefaultMetadataLocations(); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | # | ||
| 144 | # getDefaultDataLocations( ) - | ||
| 145 | # | ||
| 146 | sub getDefaultDataLocations { | ||
| 147 | |||
| 148 | my $self = shift; | ||
| 149 | return $self->{'_data_locations'}->{ $self->{'_default_service'} }; | ||
| 150 | } | ||
| 151 | |||
| 152 | # | ||
| 153 | # getDefaultMetadataLocations( ) - | ||
| 154 | # | ||
| 155 | sub getDefaultMetadataLocations { | ||
| 156 | |||
| 157 | my $self = shift; | ||
| 158 | return $self->{'_metadata_locations'}->{ $self->{'_default_service'} }; | ||
| 159 | } | ||
| 160 | |||
| 161 | # | ||
| 162 | # getDataLocations( $serviceName ) - | ||
| 163 | # | ||
| 164 | sub getDataLocations { | ||
| 165 | |||
| 166 | my ($self, $serviceName) = @_; | ||
| 167 | $serviceName ? return $self->{'_data_locations'}->{ $serviceName } : return $self->{'_data_locations'}; | ||
| 168 | } | ||
| 169 | |||
| 170 | # | ||
| 171 | # getMetadataLocations( $serviceName ) - | ||
| 172 | # | ||
| 173 | sub getMetadataLocations { | ||
| 174 | |||
| 175 | my ($self, $serviceName) = @_; | ||
| 176 | $serviceName ? return $self->{'_metadata_locations'}->{ $serviceName } : return $self->{'_metadata_locations'}; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | # | ||
| 181 | # Access to SINGLE locations | ||
| 182 | # | ||
| 183 | # getMetadataLocation( %options ) - | ||
| 184 | # | ||
| 185 | sub getMetadataLocation { | ||
| 186 | |||
| 187 | my $self = shift; | ||
| 188 | my %options = @_; | ||
| 189 | |||
| 190 | my $serviceName = $options{'serviceName'}; | ||
| 191 | |||
| 192 | my $metadataLocation; | ||
| 193 | |||
| 194 | # | ||
| 195 | # Look in the default service for a metadata location, preferring | ||
| 196 | # one with the properties specified. | ||
| 197 | # | ||
| 198 | if($serviceName) { | ||
| 199 | |||
| 200 | $options{'locations'} = $self->getMetadataLocations()->{ $serviceName }; | ||
| 201 | $metadataLocation = $self->findLocation( %options ); | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | |||
| 205 | foreach $serviceName (keys( %{ $self->getMetadataLocations() } ) ) { | ||
| 206 | |||
| 207 | $options{'locations'} = $self->getMetadataLocations()->{ $serviceName }; | ||
| 208 | |||
| 209 | $metadataLocation = $self->findLocation( %options ); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | return $metadataLocation; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | # | ||
| 218 | # get_metadata_location( %options ) - Synonym for getMetadataLocation. | ||
| 219 | # | ||
| 220 | sub get_metadata_location { | ||
| 221 | |||
| 222 | my $self = shift; | ||
| 223 | return $self->getMetadataLocation( @_ ); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | # | ||
| 228 | # getDataLocation( %options ) - | ||
| 229 | # | ||
| 230 | sub getDataLocation { | ||
| 231 | |||
| 232 | my $self = shift; | ||
| 233 | my (%options) = @_; | ||
| 234 | |||
| 235 | my $serviceName = $options{'serviceName'}; | ||
| 236 | |||
| 237 | my $dataLocation; | ||
| 238 | |||
| 239 | if($serviceName) { | ||
| 240 | |||
| 241 | $options{'locations'} = $self->getDataLocations()->{ $serviceName }; | ||
| 242 | $dataLocation = $self->findLocation( %options ); | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | |||
| 246 | foreach $serviceName (keys( %{ $self->getDataLocations() } ) ) { | ||
| 247 | |||
| 248 | $options{'locations'} = $self->getDataLocations()->{ $serviceName }; | ||
| 249 | |||
| 250 | $dataLocation = $self->findLocation( %options ); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | return $dataLocation; | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | # | ||
| 259 | # get_data_location( %options ) - Synonym for getDataLocation( ). | ||
| 260 | # | ||
| 261 | sub get_data_location { | ||
| 262 | |||
| 263 | my $self = shift; | ||
| 264 | return $self->getDataLocation( @_ ); | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | # | ||
| 269 | # findLocation( %options ) - | ||
| 270 | # | ||
| 271 | sub findLocation { | ||
| 272 | |||
| 273 | my $self = shift; | ||
| 274 | |||
| 275 | my %options = @_; | ||
| 276 | |||
| 277 | my $protocol = $options{'protocol'} || 'http'; | ||
| 278 | my $method = $options{'method'}; | ||
| 279 | |||
| 280 | my $locations = $options{'locations'}; | ||
| 281 | |||
| 282 | unless($locations) { | ||
| 283 | |||
| 284 | $self->recordError('Missing parameter \'locations\''); | ||
| 285 | $self->addStackTrace(); | ||
| 286 | |||
| 287 | return undef; | ||
| 288 | } | ||
| 289 | |||
| 290 | my $foundLocation; | ||
| 291 | foreach my $location (@{ $locations } ) { | ||
| 292 | |||
| 293 | $foundLocation = $location; | ||
| 294 | next unless $location->protocol() eq $protocol; | ||
| 295 | |||
| 296 | if ($protocol eq ${LS::Authority::WSDL::Constants::Protocols::HTTP} && $method) { | ||
| 297 | |||
| 298 | return $location if $location->method() eq $method; | ||
| 299 | } | ||
| 300 | else { | ||
| 301 | return $location; | ||
| 302 | } | ||
| 303 | |||
| 304 | } # end foreach for locations | ||
| 305 | |||
| 306 | return $foundLocation; | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | # | ||
| 311 | # getMetadata( %options ) - | ||
| 312 | # | ||
| 313 | sub getMetadata { | ||
| 314 | |||
| 315 | my ($self, %options) = @_; | ||
| 316 | |||
| 317 | my $format = ($options{'format'} || 'application/rdf+xml'); | ||
| 318 | |||
| 319 | my $location = $options{'location'}; | ||
| 320 | |||
| 321 | $location = $self->getMetadataLocation( %options ) | ||
| 322 | unless($location); | ||
| 323 | |||
| 324 | unless ($location) { | ||
| 325 | |||
| 326 | $self->recordError( 'No metadata locations found' ); | ||
| 327 | $self->addStackTrace(); | ||
| 328 | |||
| 329 | return undef; | ||
| 330 | } | ||
| 331 | |||
| 332 | my $url = URI->new($location->url()); | ||
| 333 | |||
| 334 | my $metadata_rsp = $self->{'_cache'}->lookupMetadata(lsid=> $self->lsid(), | ||
| 335 | portName=> $location->name(), | ||
| 336 | serviceName=> $location->parentName()); | ||
| 337 | |||
| 338 | unless($metadata_rsp) { | ||
| 339 | |||
| 340 | my $base_url = $location->url(); | ||
| 341 | my $protocol = uc $location->protocol(); | ||
| 342 | |||
| 343 | |||
| 344 | my $class = 'LS::Client::' . $protocol; | ||
| 345 | eval("require $class"); | ||
| 346 | |||
| 347 | my $client = $class->new(url=> $base_url); | ||
| 348 | |||
| 349 | unless($client) { | ||
| 350 | |||
| 351 | $self->recordError( "Unable to initialize client object of type $class." ); | ||
| 352 | $self->addStackTrace(); | ||
| 353 | |||
| 354 | return undef; | ||
| 355 | } | ||
| 356 | |||
| 357 | $metadata_rsp = $client->getMetadata(url=> $base_url, | ||
| 358 | lsid=> $self->lsid(), | ||
| 359 | acceptedFormats=> $format); | ||
| 360 | |||
| 361 | unless(UNIVERSAL::isa($metadata_rsp, 'LS::Service::Response')) { | ||
| 362 | |||
| 363 | $self->recordError( "Client object: $client invoked getMetadata, " . | ||
| 364 | "did not return LS::Service::Response.\n"); | ||
| 365 | $self->appendError( ($client->errorString() || 'No client error string') ); | ||
| 366 | |||
| 367 | $self->addStackTrace(); | ||
| 368 | |||
| 369 | return undef; | ||
| 370 | } | ||
| 371 | |||
| 372 | $metadata_rsp = $self->{'_cache'}->cacheMetadata(lsid=> $self->lsid(), | ||
| 373 | response=> $metadata_rsp, | ||
| 374 | portName=> $location->name(), | ||
| 375 | serviceName=> $location->parentName()); | ||
| 376 | } | ||
| 377 | |||
| 378 | return $metadata_rsp; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | # | ||
| 383 | # getMetadataSubset( %options ) - | ||
| 384 | # | ||
| 385 | sub getMetadataSubset { | ||
| 386 | |||
| 387 | my ($self, $destination, $protocol, $method) = @_; | ||
| 388 | |||
| 389 | return undef; | ||
| 390 | } | ||
| 391 | |||
| 392 | |||
| 393 | # | ||
| 394 | # getData( %options ) - | ||
| 395 | # | ||
| 396 | sub getData { | ||
| 397 | |||
| 398 | my ($self, %options) = @_; | ||
| 399 | |||
| 400 | my $location = $options{'location'}; | ||
| 401 | |||
| 402 | $location = $self->getDataLocation( %options ) | ||
| 403 | unless($location); | ||
| 404 | |||
| 405 | unless ($location) { | ||
| 406 | |||
| 407 | $self->recordError( 'No data locations found' ); | ||
| 408 | $self->addStackTrace(); | ||
| 409 | |||
| 410 | return undef; | ||
| 411 | } | ||
| 412 | |||
| 413 | my $data_rsp = $self->{'_cache'}->lookupData(lsid=> $self->lsid()); | ||
| 414 | |||
| 415 | unless ($data_rsp) { | ||
| 416 | |||
| 417 | my $base_url = $location->url(); | ||
| 418 | my $protocol = uc $location->protocol(); | ||
| 419 | |||
| 420 | my $class = 'LS::Client::' . $protocol; | ||
| 421 | eval("require $class"); | ||
| 422 | |||
| 423 | my $client = $class->new(url=> $base_url); | ||
| 424 | |||
| 425 | unless($client) { | ||
| 426 | |||
| 427 | $self->recordError( "Unable to initialize client object of type $class." ); | ||
| 428 | $self->addStackTrace(); | ||
| 429 | |||
| 430 | return undef; | ||
| 431 | } | ||
| 432 | |||
| 433 | $data_rsp = $client->getData(url=> $base_url, | ||
| 434 | lsid=> $self->lsid()); | ||
| 435 | |||
| 436 | unless(UNIVERSAL::isa($data_rsp, 'LS::Service::Response')) { | ||
| 437 | |||
| 438 | $self->recordError( "Client object: $client invoked getData, " . | ||
| 439 | "did not return LS::Service::Response." ); | ||
| 440 | |||
| 441 | $self->appendError( ($client->errorString() || 'No client error string') ); | ||
| 442 | $self->addStackTrace(); | ||
| 443 | |||
| 444 | return undef; | ||
| 445 | } | ||
| 446 | |||
| 447 | $data_rsp = $self->{'_cache'}->cacheData(lsid=> $self->lsid(), | ||
| 448 | response=> $data_rsp); | ||
| 449 | } | ||
| 450 | |||
| 451 | return $data_rsp; | ||
| 452 | } | ||
| 453 | |||
| 454 | |||
| 455 | # | ||
| 456 | # getDataByRange( %options ) - | ||
| 457 | # | ||
| 458 | sub getDataByRange { | ||
| 459 | |||
| 460 | my ($self, %options) = @_; | ||
| 461 | |||
| 462 | my $location = $options{'location'}; | ||
| 463 | |||
| 464 | my $start = $options{'start'}; | ||
| 465 | my $length = $options{'length'}; | ||
| 466 | |||
| 467 | $location = $self->getDataLocation( %options ) | ||
| 468 | unless($location); | ||
| 469 | |||
| 470 | unless ($location) { | ||
| 471 | |||
| 472 | $self->recordError( 'No data locations found' ); | ||
| 473 | $self->addStackTrace(); | ||
| 474 | |||
| 475 | return undef; | ||
| 476 | } | ||
| 477 | |||
| 478 | my $data_rsp = $self->{'_cache'}->lookupData(lsid=> $self->lsid(), | ||
| 479 | start=> $start, | ||
| 480 | length=> $length); | ||
| 481 | |||
| 482 | unless ($data_rsp) { | ||
| 483 | |||
| 484 | my $base_url = $location->url(); | ||
| 485 | my $protocol = uc $location->protocol(); | ||
| 486 | |||
| 487 | my $class = 'LS::Client::' . $protocol; | ||
| 488 | eval("require $class;"); | ||
| 489 | |||
| 490 | my $client = $class->new(url=> $base_url); | ||
| 491 | |||
| 492 | unless($client) { | ||
| 493 | |||
| 494 | $self->recordError( "Unable to initialize client object of type $class." ); | ||
| 495 | $self->addStackTrace(); | ||
| 496 | |||
| 497 | return undef; | ||
| 498 | } | ||
| 499 | |||
| 500 | $data_rsp = $client->getDataByRange(url=> $base_url, | ||
| 501 | lsid=> $self->lsid(), | ||
| 502 | start=> $start, | ||
| 503 | length=> $length); | ||
| 504 | |||
| 505 | unless(UNIVERSAL::isa($data_rsp, 'LS::Service::Response')) { | ||
| 506 | |||
| 507 | $self->recordError( "Client object: $client invoked getDataByRange, " . | ||
| 508 | "did not return LS::Service::Response"); | ||
| 509 | $self->appendErorr( ($client->errorString() || 'No client error string') ); | ||
| 510 | |||
| 511 | $self->addStackTrace(); | ||
| 512 | |||
| 513 | return undef; | ||
| 514 | } | ||
| 515 | |||
| 516 | $data_rsp = $self->{'_cache'}->cacheData(lsid=> $self->lsid(), | ||
| 517 | response=> $data_rsp, | ||
| 518 | start=> $start, | ||
| 519 | length=> $length); | ||
| 520 | |||
| 521 | } | ||
| 522 | |||
| 523 | return $data_rsp; | ||
| 524 | } | ||
| 525 | |||
| 526 | # | ||
| 527 | # _parse_wsdl ($xml ) - | ||
| 528 | # | ||
| 529 | sub _parse_wsdl { | ||
| 530 | |||
| 531 | my ($self, $xml) = @_; | ||
| 532 | |||
| 533 | require LS::Authority::WSDL::Simple; | ||
| 534 | |||
| 535 | my $wsdl = LS::Authority::WSDL::Simple->from_xml($xml); | ||
| 536 | |||
| 537 | unless(UNIVERSAL::isa($wsdl, 'LS::Authority::WSDL::Simple')) { | ||
| 538 | |||
| 539 | return undef; | ||
| 540 | } | ||
| 541 | |||
| 542 | $self->{'_wsdl'} = $wsdl; | ||
| 543 | $self->{'_default_service'} = $wsdl->defaultServiceName(); | ||
| 544 | |||
| 545 | # Build the service name array, the metadata and data location structures | ||
| 546 | foreach my $svc (values(%{ $wsdl->services() }) ) { | ||
| 547 | |||
| 548 | push @{ $self->{'_services'} }, $svc->name(); | ||
| 549 | |||
| 550 | $self->{'_metadata_locations'}->{ $svc->name() } = $wsdl->getMetadataLocations($svc->name()); | ||
| 551 | $self->{'_data_locations'}->{ $svc->name() } = $wsdl->getDataLocations($svc->name()); | ||
| 552 | } | ||
| 553 | |||
| 554 | return $self; | ||
| 555 | } | ||
| 556 | |||
| 557 | |||
| 558 | sub _escape { | ||
| 559 | my $string = $_[0]; | ||
| 560 | |||
| 561 | $string =~ s/&/&/g; | ||
| 562 | $string =~ s/</</g; | ||
| 563 | $string =~ s/>/>/g; | ||
| 564 | |||
| 565 | return $string; | ||
| 566 | } | ||
| 567 | |||
| 568 | |||
| 569 | |||
| 570 | |||
| 571 | # | ||
| 572 | # | ||
| 573 | # WARNING: The following my be deprecated in future releases | ||
| 574 | # | ||
| 575 | # | ||
| 576 | |||
| 577 | sub data_locations { | ||
| 578 | my $self = shift; | ||
| 579 | |||
| 580 | return $self->get_data_locations; | ||
| 581 | } | ||
| 582 | |||
| 583 | sub data_location { | ||
| 584 | my $self = shift; | ||
| 585 | |||
| 586 | return $self->get_data_location(@_); | ||
| 587 | } | ||
| 588 | |||
| 589 | sub get_metadata { | ||
| 590 | |||
| 591 | my $self = shift; | ||
| 592 | |||
| 593 | return $self->getMetadata(@_); | ||
| 594 | } | ||
| 595 | |||
| 596 | sub get_data { | ||
| 597 | |||
| 598 | my $self = shift; | ||
| 599 | |||
| 600 | return $self->getData(@_); | ||
| 601 | } | ||
| 602 | |||
| 603 | sub _get_rdf { | ||
| 604 | my $self = shift; | ||
| 605 | |||
| 606 | my $rdf_string = $self->get_metadata() || return; | ||
| 607 | |||
| 608 | require RDF::Core::Model; | ||
| 609 | require RDF::Core::Model::Parser; | ||
| 610 | require RDF::Core::Storage::Memory; | ||
| 611 | require RDF::Core::Evaluator; | ||
| 612 | require RDF::Core::NodeFactory; | ||
| 613 | require RDF::Core::Query; | ||
| 614 | |||
| 615 | my $rdf_model = RDF::Core::Model->new( | ||
| 616 | Storage => RDF::Core::Storage::Memory->new | ||
| 617 | ); | ||
| 618 | |||
| 619 | my $parser = RDF::Core::Model::Parser->new( | ||
| 620 | Model => $rdf_model, | ||
| 621 | Source => $rdf_string, | ||
| 622 | SourceType => 'string', | ||
| 623 | BaseURI => 'http://www.foo.com', | ||
| 624 | ); | ||
| 625 | |||
| 626 | eval { | ||
| 627 | $parser->parse(); | ||
| 628 | }; | ||
| 629 | |||
| 630 | if ($@) { | ||
| 631 | my $err_string = $@; | ||
| 632 | $err_string =~ s/^\s+|\s+$//g; | ||
| 633 | |||
| 634 | $self->recordError( $err_string ); | ||
| 635 | return undef; | ||
| 636 | } | ||
| 637 | |||
| 638 | my $evaluator = RDF::Core::Evaluator->new( | ||
| 639 | Model => $rdf_model, | ||
| 640 | Factory => RDF::Core::NodeFactory->new, | ||
| 641 | Namespaces => { | ||
| 642 | rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', | ||
| 643 | i3cp => 'urn:lsid:i3c.org:predicates:', | ||
| 644 | dc => 'http://purl.org/dc/elements/1.1/', | ||
| 645 | } | ||
| 646 | ); | ||
| 647 | |||
| 648 | my $query = RDF::Core::Query->new( | ||
| 649 | Evaluator => $evaluator | ||
| 650 | ); | ||
| 651 | |||
| 652 | $self->{_rdf_model} = $rdf_model; | ||
| 653 | $self->{_rdf_query} = $query; | ||
| 654 | |||
| 655 | $self->recordError( undef ); | ||
| 656 | } | ||
| 657 | |||
| 658 | |||
| 659 | sub _rdf_query { | ||
| 660 | my $self = shift; | ||
| 661 | my ($query_string) = @_; | ||
| 662 | |||
| 663 | unless ($self->{_rdf_query}) { | ||
| 664 | $self->_get_rdf; | ||
| 665 | |||
| 666 | return unless $self->{_rdf_query}; | ||
| 667 | } | ||
| 668 | |||
| 669 | |||
| 670 | my $rs = $self->{_rdf_query}->query($query_string); | ||
| 671 | |||
| 672 | return $rs; | ||
| 673 | } | ||
| 674 | |||
| 675 | |||
| 676 | sub get_types { | ||
| 677 | my $self = shift; | ||
| 678 | |||
| 679 | my $rs = $self->_rdf_query('Select ?type from [' . $self->lsid->canonical() . ']->rdf:type{?type}') || return; | ||
| 680 | |||
| 681 | my @types; | ||
| 682 | |||
| 683 | foreach my $row (@$rs) { | ||
| 684 | my $resource = $row->[0] || next; | ||
| 685 | |||
| 686 | push(@types, $resource->getURI()); | ||
| 687 | } | ||
| 688 | |||
| 689 | $self->recordError( undef ); | ||
| 690 | |||
| 691 | return \@types; | ||
| 692 | } | ||
| 693 | |||
| 694 | |||
| 695 | sub get_format { | ||
| 696 | my $self = shift; | ||
| 697 | |||
| 698 | my $rs = $self->_rdf_query('Select ?format from [' . $self->lsid->canonical() . ']->dc:format{?format}') || return; | ||
| 699 | |||
| 700 | my $row = $rs->[0] || return; | ||
| 701 | my $resource = $row->[0] || return; | ||
| 702 | |||
| 703 | $self->recordError( undef ); | ||
| 704 | |||
| 705 | return $resource->getURI(); | ||
| 706 | } | ||
| 707 | |||
| 708 | |||
| 709 | sub get_instances { | ||
| 710 | my $self = shift; | ||
| 711 | |||
| 712 | my $rs = $self->_rdf_query('Select ?instance, ?format from [' . $self->lsid->canonical() . ']->i3cp:storedas{?instance}->dc:format{?format}') || return; | ||
| 713 | |||
| 714 | my @lsids; | ||
| 715 | |||
| 716 | foreach my $row (@$rs) { | ||
| 717 | my $resource = $row->[0] || next; | ||
| 718 | my $format = $row->[1]; | ||
| 719 | |||
| 720 | push(@lsids, [$resource->getURI(), $format->getURI() ]); | ||
| 721 | } | ||
| 722 | |||
| 723 | $self->recordError( undef ); | ||
| 724 | |||
| 725 | return \@lsids; | ||
| 726 | } | ||
| 727 | |||
| 728 | |||
| 729 | sub get_abstract { | ||
| 730 | my $self = shift; | ||
| 731 | |||
| 732 | my $rs = $self->_rdf_query('Select ?abstract from ?abstract->i3cp:storedas=>[' . $self->lsid->canonical() . ']') || return; | ||
| 733 | |||
| 734 | my $row = $rs->[0] || return; | ||
| 735 | my $resource = $row->[0] || return; | ||
| 736 | |||
| 737 | $self->recordError( undef ); | ||
| 738 | |||
| 739 | return $resource->getURI(); | ||
| 740 | } | ||
| 741 | |||
| 742 | |||
| 743 | sub get_instances_in_format { | ||
| 744 | my $self = shift; | ||
| 745 | my ($format) = @_; | ||
| 746 | |||
| 747 | $format = LS::ID->new($format) unless ref $format; | ||
| 748 | |||
| 749 | if (!$format) { | ||
| 750 | |||
| 751 | $self->recordError( 'Invalid format' ); | ||
| 752 | return; | ||
| 753 | } | ||
| 754 | |||
| 755 | my $rs = $self->_rdf_query('Select ?instance from [' . $self->lsid->canonical() . "]->i3cp:storedas{?instance}->dc:format=>[" . $format->canonical() . "]") || return; | ||
| 756 | |||
| 757 | my @lsids; | ||
| 758 | |||
| 759 | foreach my $row (@$rs) { | ||
| 760 | my $resource = $row->[0] || next; | ||
| 761 | |||
| 762 | push(@lsids, $resource->getURI()); | ||
| 763 | } | ||
| 764 | |||
| 765 | $self->recordError( undef ); | ||
| 766 | |||
| 767 | return \@lsids; | ||
| 768 | } | ||
| 769 | |||
| 770 | # | ||
| 771 | # | ||
| 772 | # End deprecation warning | ||
| 773 | # | ||
| 774 | # | ||
| 775 | |||
| 776 | |||
| 777 | 1; | ||
| 778 | |||
| 779 | __END__ |