blib/lib/MOBY/lsid/authority/MobyMetadataResolver.pm untested

filter: Statementstmt 0.0% 0 / 324 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 50 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 524 CC 44 SCAR 31.5
CC
44
Cov
0%
getMetadata
72.0
getData
56.0
getMetadata
42.0
CRAP 23.2
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------
3 # MOBY::lsid::authority::MobyMetadataResolver
4 # Author: Edward Kawas <edward.kawas@gmail.com>,
5 # For copyright and disclaimer see below.
6 #
7 # $Id: MobyMetadataResolver.pm,v 1.4 2008/11/17 15:27:02 kawas Exp $
8 #-----------------------------------------------------------------
9  
10 package MobyNamespaceType;
11  
12 use strict;
13 use warnings;
14  
15 use LS::ID;
16 use LS::Service::Response;
17 use LS::Service::Fault;
18  
19 use MOBY::RDF::Ontologies::Namespaces;
20  
21 use vars qw /$VERSION/;
22 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
23  
24 use base 'LS::Service::Namespace';
25  
26 =head1 NAME
27  
28 MobyNamespaceType - LSID Metadata Handler
29  
30 =head1 SYNOPSIS
31  
32 use MOBY::lsid::authority::MobyMetadataResolver;
33  
34 # create a LS::Service::DataService and pass it our handler
35 my $metadata = LS::Service::DataService->new();
36 $metadata->addNamespace( MobyNamespaceType->new() );
37  
38 =head1 DESCRIPTION
39  
40 This module implements the subroutines needed to implement
41 an LSID authority service that handles this namespace.
42  
43 =cut
44  
45 =head1 AUTHORS
46  
47 Edward Kawas (edward.kawas [at] gmail [dot] com)
48  
49 =cut
50  
51 #
52 # new - no parameters
53 #
54 sub new {
55  
56 my ( $self, %options ) = @_;
57
58 my $CONF = MOBY::Config->new;
59
60 $options{'name'} = $CONF->{mobynamespace}->{lsid_namespace} || 'namespacetype';
61  
62 $self = $self->SUPER::new(%options);
63
64
65 $self->{mobyconf} = $CONF->{mobynamespace};
66 $self->{moby_data_handler} = $CONF-> getDataAdaptor( source => "mobynamespace" )->dbh;
67
68 return $self;
69 }
70  
71 #-----------------------------------------------------------------
72 # getMetadata
73 #-----------------------------------------------------------------
74  
75 =head2 getMetadata
76  
77 This subroutine is the handler that actually performs
78 the action when getMetadata is called on an LSID under this namespace
79  
80 This routine has 2 parameters:
81 lsid - the LSID
82 format - output format <optional>
83  
84 Example: getMetadata(LS::ID->new('urn:lsid:authority:namespace:object'));
85  
86 A LS::Service::Response is returned if getMetadata is successful.
87  
88 =cut
89  
90  
91 sub getMetadata {
92 my ( $self, $lsid, $format ) = @_;
93 $lsid = $lsid->canonical();
94  
95 return LS::Service::Fault->fault('Unknown LSID')
96 unless (
97 $self->lsidExists(
98 $lsid->namespace, $lsid->object, $lsid->revision
99 )
100 );
101  
102 my $latest =
103 $self->isLatest( $lsid->namespace, $lsid->object,
104 $lsid->revision );
105 do {
106 my $data = MOBY::RDF::Ontologies::Namespaces->new;
107 $format = 'application/xml' if ( !$format );
108 return LS::Service::Response->new(
109 response => $data->createByName( { term => $lsid->object } ),
110 format => $format
111 );
112 } unless $latest;
113  
114 return LS::Service::Fault->serverFault( 'Unable to load metadata', 600 )
115 if ( $latest eq "" );
116  
117 my $object = $lsid->object();
118 my $uri = MOBY::RDF::Ontologies::Namespaces->new();
119 $uri = $uri->{uri} || "http://biomoby.org/RESOURCES/MOBY-S/Namespaces#$object";
120 my $data = <<END;
121 <?xml version="1.0" encoding="UTF-8"?>
122 <rdf:RDF
123 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
124 xmlns:lsid="http://lsid.omg.org/predicates#"
125 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
126 <rdf:Description rdf:about="$uri">
127 <rdfs:comment>The Namespace described by the LSID: $lsid has since been modified. Please update your lsid.</rdfs:comment>
128 <lsid:latest>$latest</lsid:latest>
129 </rdf:Description>
130 </rdf:RDF>
131 END
132  
133 $format = 'application/xml' if ( !$format );
134 return LS::Service::Response->new(
135 response => $data,
136 format => $format
137 );
138  
139 }
140  
141 #-----------------------------------------------------------------
142 # lsidExists
143 #-----------------------------------------------------------------
144  
145 =head2 lsidExists
146  
147 This subroutine checks to see whether the thing that the LSID points to
148 exists at all.
149  
150 This routine has 3 parameters:
151 namespace - the LSID namespace
152 id - the LSID object
153 revision - the LSID revision
154  
155 Example: lsidExists('someNamespace','someObject','someRevision');
156  
157 If the thing pointed at by the lsid exists, then 1 is returned. Otherwise undef is returned.
158  
159 =cut
160  
161 sub lsidExists {
162 my ( $self, $namespace, $id, $revision ) = @_;
163 return 1 if ( $id =~ /^Namespace$/ );
164  
165 my $db = $self->{moby_data_handler};
166 my $query = <<END;
167 SELECT namespace_lsid
168 FROM namespace
169 WHERE namespace_type = ?
170 ORDER BY namespace_lsid asc
171 END
172 my $sth = $db->prepare($query);
173 $sth->execute( ($id) );
174  
175 # returns an array of hash references
176 while ( my $ref = $sth->fetchrow_arrayref ) {
177  
178 #if we are here, it means the namespace exists!
179 return 1;
180 }
181  
182 # doesnt exist
183 return undef;
184 }
185  
186 #-----------------------------------------------------------------
187 # isLatest
188 #-----------------------------------------------------------------
189  
190 =head2 isLatest
191  
192 This subroutine checks to see whether the LSID is the latest, based on the revision.
193  
194 This routine has 3 parameters:
195 namespace - the LSID namespace
196 id - the LSID object
197 revision - the LSID revision
198  
199 Example: isLatest('someNamespace','someObject','someRevision');
200  
201 If the lsid is the latest, then undef is returned.
202 If the lsid doesnt exist, then an empty string is returned.
203 And if the lsid isnt the latest, then the latest lsid is returned.
204  
205 =cut
206  
207 sub isLatest {
208 my ( $self, $namespace, $id, $revision ) = @_;
209 $revision = "__invalid__" unless $revision;
210 return undef if ( $id =~ /^Namespace$/ );
211  
212 my $db = $self->{moby_data_handler};
213 my $query = <<END;
214 SELECT namespace_lsid
215 FROM namespace
216 WHERE namespace_type = ?
217 ORDER BY namespace_lsid asc
218 END
219 my $sth = $db->prepare($query);
220 $sth->execute( ($id) );
221  
222 # returns an array of hash references
223 while ( my $ref = $sth->fetchrow_arrayref ) {
224  
225 #if we are here, it means the namespace exists!
226 my $lsid = LS::ID->new( $$ref[0] );
227 return undef if $lsid->revision() and $lsid->revision() eq $revision;
228 return $$ref[0];
229 }
230  
231 # doesnt exist
232 return "";
233 }
234  
235 package MobyServiceType;
236 use strict;
237 use warnings;
238 use LS::ID;
239 use LS::Service::Response;
240 use LS::Service::Fault;
241  
242 use MOBY::RDF::Ontologies::ServiceTypes;
243  
244 use base 'LS::Service::Namespace';
245  
246 =head1 NAME
247  
248 MobyServiceType - LSID Metadata Handler
249  
250 =head1 SYNOPSIS
251  
252 use MOBY::lsid::authority::MobyMetadataResolver;
253  
254 # create a LS::Service::DataService and pass it our handler
255 my $metadata = LS::Service::DataService->new();
256 $metadata->addNamespace( MobyServiceType->new() );
257  
258 =head1 DESCRIPTION
259  
260 This module implements the subroutines needed to implement
261 an LSID authority service that handles this namespace.
262  
263 =cut
264  
265 =head1 AUTHORS
266  
267 Edward Kawas (edward.kawas [at] gmail [dot] com)
268  
269 =cut
270  
271 #
272 # new - no parameters
273 #
274  
275 sub new {
276  
277 my ( $self, %options ) = @_;
278  
279 my $CONF = MOBY::Config->new;
280
281 $options{'name'} = $CONF->{mobyservice}->{lsid_namespace} || 'servicetype';
282  
283 $self = $self->SUPER::new(%options);
284
285 $self->{mobyconf} = $CONF->{mobyservice};
286 $self->{moby_data_handler} = $CONF-> getDataAdaptor( source => "mobyservice" )->dbh;
287
288 return $self;
289 }
290  
291 #-----------------------------------------------------------------
292 # getMetadata
293 #-----------------------------------------------------------------
294  
295 =head2 getMetadata
296  
297 This subroutine is the handler that actually performs
298 the action when getMetadata is called on an LSID under this namespace
299  
300 This routine has 2 parameters:
301 lsid - the LSID
302 format - output format <optional>
303  
304 Example: getMetadata(LS::ID->new('urn:lsid:authority:namespace:object'));
305  
306 A LS::Service::Response is returned if getMetadata is successful.
307  
308 =cut
309  
310 sub getMetadata {
311  
312 my ( $self, $lsid, $format ) = @_;
313  
314 $lsid = $lsid->canonical();
315  
316 return LS::Service::Fault->fault('Unknown LSID')
317 unless (
318 $self->lsidExists(
319 $lsid->namespace, $lsid->object, $lsid->revision
320 )
321 );
322  
323 my $latest =
324 $self->isLatest( $lsid->namespace, $lsid->object, $lsid->revision );
325 do {
326 my $data = MOBY::RDF::Ontologies::ServiceTypes->new;
327 $format = 'application/xml' if ( !$format );
328 return LS::Service::Response->new(
329 response => $data->createByName( { term => $lsid->object } ),
330 format => $format
331 );
332 } unless $latest;
333  
334 return LS::Service::Fault->serverFault( 'Unable to load metadata', 600 )
335 if ( $latest eq "" );
336  
337 my $object = $lsid->object();
338 my $uri = MOBY::RDF::Ontologies::ServiceTypes->new();
339 $uri = $uri->{uri} || "http://biomoby.org/RESOURCES/MOBY-S/Services#$object";
340 my $data = <<END;
341 <?xml version="1.0" encoding="UTF-8"?>
342 <rdf:RDF
343 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
344 xmlns:lsid="http://lsid.omg.org/predicates#"
345 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
346 <rdf:Description rdf:about="$uri">
347 <rdfs:comment>The ServiceType described by the LSID: $lsid has since been modified. Please update your lsid.</rdfs:comment>
348 <lsid:latest>$latest</lsid:latest>
349 </rdf:Description>
350 </rdf:RDF>
351 END
352  
353 $format = 'application/xml' if ( !$format );
354 return LS::Service::Response->new(
355 response => $data,
356 format => $format
357 );
358 }
359  
360 #-----------------------------------------------------------------
361 # lsidExists
362 #-----------------------------------------------------------------
363  
364 =head2 lsidExists
365  
366 This subroutine checks to see whether the thing that the LSID points to
367 exists at all.
368  
369 This routine has 3 parameters:
370 namespace - the LSID namespace
371 id - the LSID object
372 revision - the LSID revision
373  
374 Example: lsidExists('someNamespace','someObject','someRevision');
375  
376 If the thing pointed at by the lsid exists, then 1 is returned. Otherwise undef is returned.
377  
378 =cut
379  
380 sub lsidExists {
381 my ( $self, $namespace, $id, $revision ) = @_;
382 my $db = $self->{moby_data_handler};
383 my $query = <<END;
384 SELECT service_lsid
385 FROM service
386 WHERE service_type = ?
387 order by service_lsid
388 END
389 my $sth = $db->prepare($query);
390 $sth->execute( ($id) );
391  
392 # returns an array of hash references
393 while ( my $ref = $sth->fetchrow_arrayref ) {
394  
395 #if we are here, it means the namespace exists!
396 return 1;
397 }
398  
399 # doesnt exist
400 return undef;
401 }
402  
403 #-----------------------------------------------------------------
404 # isLatest
405 #-----------------------------------------------------------------
406  
407 =head2 isLatest
408  
409 This subroutine checks to see whether the LSID is the latest, based on the revision.
410  
411 This routine has 3 parameters:
412 namespace - the LSID namespace
413 id - the LSID object
414 revision - the LSID revision
415  
416 Example: isLatest('someNamespace','someObject','someRevision');
417  
418 If the lsid is the latest, then undef is returned.
419 If the lsid doesnt exist, then an empty string is returned.
420 And if the lsid isnt the latest, then the latest lsid is returned.
421  
422 =cut
423  
424  
425 sub isLatest {
426 my ( $self, $namespace, $id, $revision ) = @_;
427 $revision = "__invalid__" unless $revision;
428 my $db = $self->{moby_data_handler};
429 my $query = <<END;
430 SELECT service_lsid
431 FROM service
432 WHERE service_type = ?
433 order by service_lsid
434 END
435 my $sth = $db->prepare($query);
436 $sth->execute( ($id) );
437  
438 # returns an array of hash references
439 while ( my $ref = $sth->fetchrow_arrayref ) {
440  
441 #if we are here, it means the namespace exists!
442 my $lsid = LS::ID->new( $$ref[0] );
443 return undef if $lsid->revision() and $lsid->revision() eq $revision;
444 return $$ref[0];
445 }
446  
447 # doesnt exist
448 return "";
449 }
450  
451 package MobyObjectClass;
452  
453 use strict;
454 use warnings;
455  
456 use LS::ID;
457 use LS::Service::Response;
458 use LS::Service::Fault;
459  
460 use MOBY::RDF::Ontologies::Objects;
461  
462 use base 'LS::Service::Namespace';
463  
464 =head1 NAME
465  
466 MobyObjectClass - LSID Metadata Handler
467  
468 =head1 SYNOPSIS
469  
470 use MOBY::lsid::authority::MobyMetadataResolver;
471  
472 # create a LS::Service::DataService and pass it our handler
473 my $metadata = LS::Service::DataService->new();
474 $metadata->addNamespace( MobyObjectClass->new() );
475  
476 =head1 DESCRIPTION
477  
478 This module implements the subroutines needed to implement
479 an LSID authority service that handles this namespace.
480  
481 =cut
482  
483 =head1 AUTHORS
484  
485 Edward Kawas (edward.kawas [at] gmail [dot] com)
486  
487 =cut
488  
489 #
490 # new - no parameters
491 #
492 sub new {
493  
494 my ( $self, %options ) = @_;
495  
496 my $CONF = MOBY::Config->new;
497
498 $options{'name'} = $CONF->{mobyobject}->{lsid_namespace} || 'objectclass';
499  
500 $self = $self->SUPER::new(%options);
501
502
503 $self->{mobyconf} = $CONF->{mobyobject};
504 $self->{moby_data_handler} = $CONF-> getDataAdaptor( source => "mobyobject" )->dbh;
505
506 return $self;
507 }
508  
509 #-----------------------------------------------------------------
510 # getMetadata
511 #-----------------------------------------------------------------
512  
513 =head2 getMetadata
514  
515 This subroutine is the handler that actually performs
516 the action when getMetadata is called on an LSID under this namespace
517  
518 This routine has 2 parameters:
519 lsid - the LSID
520 format - output format <optional>
521  
522 Example: getMetadata(LS::ID->new('urn:lsid:authority:namespace:object'));
523  
524 A LS::Service::Response is returned if getMetadata is successful.
525  
526 =cut
527  
528 sub getMetadata {
529  
530 my ( $self, $lsid, $format ) = @_;
531  
532 $lsid = $lsid->canonical();
533  
534 return LS::Service::Fault->fault('Unknown LSID')
535 unless (
536 $self->lsidExists(
537 $lsid->namespace, $lsid->object, $lsid->revision
538 )
539 );
540  
541 my $latest =
542 $self->isLatest( $lsid->namespace, $lsid->object, $lsid->revision );
543 do {
544 my $data = MOBY::RDF::Ontologies::Objects->new;
545 $format = 'application/xml' if ( !$format );
546 return LS::Service::Response->new(
547 response => $data->createByName( { term => $lsid->object } ),
548 format => $format
549 );
550 } unless $latest;
551  
552 return LS::Service::Fault->serverFault( 'Unable to load metadata', 600 )
553 if ( $latest eq "" );
554  
555 my $object = $lsid->object();
556 my $uri = MOBY::RDF::Ontologies::Objects->new();
557 $uri = $uri->{uri} || "http://biomoby.org/RESOURCES/MOBY-S/Objects#$object";
558 my $data = <<END;
559 <?xml version="1.0" encoding="UTF-8"?>
560 <rdf:RDF
561 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
562 xmlns:lsid="http://lsid.omg.org/predicates#"
563 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
564 <rdf:Description rdf:about="$uri">
565 <rdfs:comment>The Datatype described by the LSID: $lsid has since been modified. Please update your lsid.</rdfs:comment>
566 <lsid:latest>$latest</lsid:latest>
567 </rdf:Description>
568 </rdf:RDF>
569 END
570  
571 $format = 'application/xml' if ( !$format );
572 return LS::Service::Response->new(
573 response => $data,
574 format => $format
575 );
576 }
577  
578 #-----------------------------------------------------------------
579 # lsidExists
580 #-----------------------------------------------------------------
581  
582 =head2 lsidExists
583  
584 This subroutine checks to see whether the thing that the LSID points to
585 exists at all.
586  
587 This routine has 3 parameters:
588 namespace - the LSID namespace
589 id - the LSID object
590 revision - the LSID revision
591  
592 Example: lsidExists('someNamespace','someObject','someRevision');
593  
594 If the thing pointed at by the lsid exists, then 1 is returned. Otherwise undef is returned.
595  
596 =cut
597  
598 sub lsidExists {
599 my ( $self, $namespace, $id, $revision ) = @_;
600 my $db = $self->{moby_data_handler};
601 my $query = <<END;
602 SELECT object_lsid
603 FROM object
604 WHERE object_type = ?
605 ORDER BY object_lsid
606 END
607 my $sth = $db->prepare($query);
608 $sth->execute( ($id) );
609  
610 # returns an array of hash references
611 while ( my $ref = $sth->fetchrow_arrayref ) {
612  
613 #if we are here, it means the namespace exists!
614 return 1;
615 }
616  
617 # doesnt exist
618 return undef;
619 }
620 #-----------------------------------------------------------------
621 # isLatest
622 #-----------------------------------------------------------------
623  
624 =head2 isLatest
625  
626 This subroutine checks to see whether the LSID is the latest, based on the revision.
627  
628 This routine has 3 parameters:
629 namespace - the LSID namespace
630 id - the LSID object
631 revision - the LSID revision
632  
633 Example: isLatest('someNamespace','someObject','someRevision');
634  
635 If the lsid is the latest, then undef is returned.
636 If the lsid doesnt exist, then an empty string is returned.
637 And if the lsid isnt the latest, then the latest lsid is returned.
638  
639 =cut
640  
641 sub isLatest {
642 my ( $self, $namespace, $id, $revision ) = @_;
643 $revision = "__invalid__" unless $revision;
644 my $db = $self->{moby_data_handler};
645 my $query = <<END;
646 SELECT object_lsid
647 FROM object
648 WHERE object_type = ?
649 ORDER BY object_lsid
650 END
651 my $sth = $db->prepare($query);
652 $sth->execute( ($id) );
653  
654 # returns an array of hash references
655 while ( my $ref = $sth->fetchrow_arrayref ) {
656  
657 #if we are here, it means the namespace exists!
658 my $lsid = LS::ID->new( $$ref[0] );
659 return undef if $lsid->revision() and $lsid->revision() eq $revision;
660 return $$ref[0];
661 }
662  
663 # doesnt exist
664 return "";
665 }
666  
667 package MobyServiceInstance;
668  
669 use strict;
670 use warnings;
671  
672 use LS::ID;
673 use LS::Service::Response;
674 use LS::Service::Fault;
675 use MOBY::Client::Service;
676 use MOBY::Client::Central;
677 use MOBY::Config;
678 use MOBY::RDF::Ontologies::Services;
679  
680 use base 'LS::Service::Namespace';
681  
682 =head1 NAME
683  
684 MobyServiceInstance - LSID Metadata Handler
685  
686 =head1 SYNOPSIS
687  
688 use MOBY::lsid::authority::MobyMetadataResolver;
689  
690 # create a LS::Service::DataService and pass it our handler
691 my $metadata = LS::Service::DataService->new();
692 $metadata->addNamespace( MobyServiceInstance->new() );
693  
694 =head1 DESCRIPTION
695  
696 This module implements the subroutines needed to implement
697 an LSID authority service that handles this namespace.
698  
699 =cut
700  
701 =head1 AUTHORS
702  
703 Edward Kawas (edward.kawas [at] gmail [dot] com)
704  
705 =cut
706  
707 #
708 # new - no parameters
709 #
710 sub new {
711  
712 my ( $self, %options ) = @_;
713
714 my $CONF = MOBY::Config->new;
715
716 $options{'name'} = $CONF->{mobycentral}->{lsid_namespace} || 'serviceinstance';
717  
718 $self = $self->SUPER::new(%options);
719
720
721 $self->{mobyconf} = $CONF->{mobycentral};
722 $self->{moby_data_handler} = $CONF-> getDataAdaptor( source => "mobycentral" )->dbh;
723
724 return $self;
725 }
726  
727 #-----------------------------------------------------------------
728 # getData
729 #-----------------------------------------------------------------
730  
731 =head2 getData
732  
733 This subroutine is the handler that actually performs
734 the action when getData is called on an LSID under this namespace
735  
736 This routine has 2 parameters:
737 lsid - the LSID
738 format - output format <optional>
739  
740 Example: getData(LS::ID->new('urn:lsid:authority:namespace:object'));
741  
742 A LS::Service::Response is returned if getData is successful.
743  
744 =cut
745  
746 sub getData {
747  
748 my ( $self, $lsid, $format ) = @_;
749 $lsid = $lsid->canonical();
750 my $length = length( $lsid->object() );
751  
752 # some error conditions
753 return LS::Service::Fault->fault('malformed LSID') unless $length > 0;
754 return LS::Service::Fault->fault('malformed LSID')
755 unless index( $lsid->object(), ',' ) > 0;
756  
757 my $servicename =
758 substr( $lsid->object(), index( $lsid->object(), ',' ) + 1, $length );
759 my $authURI = substr( $lsid->object(), 0, index( $lsid->object(), ',' ) );
760  
761 return LS::Service::Fault->fault('Unknown LSID')
762 unless (
763 $self->lsidExists(
764 $lsid->namespace, $lsid->object, $lsid->revision
765 )
766 );
767  
768 my $latest =
769 $self->isLatest( $lsid->namespace, $lsid->object,
770 $lsid->revision );
771 do {
772 my $data = MOBY::RDF::Ontologies::Services->new;
773 $format = 'application/xml' if ( !$format );
774 my $wsdl = $self->_getServiceWSDL($authURI, $servicename);
775 print STDERR $wsdl;
776 return LS::Service::Response->new(
777 response => $wsdl,
778 format => $format
779 );
780 } unless $latest;
781  
782 return LS::Service::Fault->serverFault( 'Unable to load Data', 600 )
783 if ( $latest eq "" );
784  
785 $format = 'text/plain';
786 return LS::Service::Response->new(
787 response => "",
788 format => $format
789 );
790  
791  
792 }
793 #-----------------------------------------------------------------
794 # _getServiceWSDL
795 #-----------------------------------------------------------------
796  
797 =head2 _getServiceWSDL
798  
799 This subroutine obtains the wsdl for moby services given the name/auth
800 combination. It uses the registry that is set in the enviroment.
801 TODO - might have to change this behaviour, if we think of a good
802 reason!
803  
804 =cut
805  
806 sub _getServiceWSDL {
807 my ( $self, $authority, $servicename ) = @_;
808 my $moby = MOBY::Client::Central->new();
809 my ( $services, $RegObject ) = $moby->findService(
810 authURI => $authority,
811 serviceName => $servicename
812 );
813 unless ($services && @{$services}[0] ) {
814 return "";
815 };
816 # should only be one ...
817 foreach my $ServiceInstance ( @{ $services } ) {
818 return $moby->retrieveService($ServiceInstance);
819 }
820 return ""
821 }
822  
823 #-----------------------------------------------------------------
824 # getMetadata
825 #-----------------------------------------------------------------
826  
827 =head2 getMetadata
828  
829 This subroutine is the handler that actually performs
830 the action when getMetadata is called on an LSID under this namespace
831  
832 This routine has 2 parameters:
833 lsid - the LSID
834 format - output format <optional>
835  
836 Example: getMetadata(LS::ID->new('urn:lsid:authority:namespace:object'));
837  
838 A LS::Service::Response is returned if getMetadata is successful.
839  
840 =cut
841  
842 sub getMetadata {
843  
844 my ( $self, $lsid, $format ) = @_;
845 $lsid = $lsid->canonical();
846  
847 my $length = length( $lsid->object() );
848  
849 # some error conditions
850 return LS::Service::Fault->fault('malformed LSID') unless $length > 0;
851 return LS::Service::Fault->fault('malformed LSID')
852 unless index( $lsid->object(), ',' ) > 0;
853  
854 my $servicename =
855 substr( $lsid->object(), index( $lsid->object(), ',' ) + 1, $length );
856 my $authURI = substr( $lsid->object(), 0, index( $lsid->object(), ',' ) );
857  
858 return LS::Service::Fault->fault('Unknown LSID')
859 unless (
860 $self->lsidExists(
861 $lsid->namespace, $lsid->object, $lsid->revision
862 )
863 );
864  
865 my $latest =
866 $self->isLatest( $lsid->namespace, $lsid->object,
867 $lsid->revision );
868 do {
869 my $data = MOBY::RDF::Ontologies::Services->new;
870 $format = 'application/xml' if ( !$format );
871 return LS::Service::Response->new(
872 response => $data->findService(
873 {
874 serviceName => $servicename,
875 authURI => $authURI
876 }
877 ),
878 format => $format
879 );
880 } unless $latest;
881  
882 return LS::Service::Fault->serverFault( 'Unable to load metadata', 600 )
883 if ( $latest eq "" );
884  
885 my $object = $lsid->object();
886 my $data = <<END;
887 <?xml version="1.0" encoding="UTF-8"?>
888 <rdf:RDF
889 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
890 xmlns:lsid="http://lsid.omg.org/predicates#"
891 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
892 <rdf:Description rdf:about="$lsid">
893 <rdfs:comment>The service instance described by the LSID: $lsid has since been modified. Please update your lsid.</rdfs:comment>
894 <lsid:latest>$latest</lsid:latest>
895 </rdf:Description>
896 </rdf:RDF>
897 END
898  
899 $format = 'application/xml' if ( !$format );
900 return LS::Service::Response->new(
901 response => $data,
902 format => $format
903 );
904 }
905  
906 #-----------------------------------------------------------------
907 # lsidExists
908 #-----------------------------------------------------------------
909  
910 =head2 lsidExists
911  
912 This subroutine checks to see whether the thing that the LSID points to
913 exists at all.
914  
915 This routine has 3 parameters:
916 namespace - the LSID namespace
917 id - the LSID object
918 revision - the LSID revision
919  
920 Example: lsidExists('someNamespace','someObject','someRevision');
921  
922 If the thing pointed at by the lsid exists, then 1 is returned. Otherwise undef is returned.
923  
924 =cut
925  
926 sub lsidExists {
927 my ( $self, $namespace, $id, $revision ) = @_;
928  
929 my $length = length($id);
930  
931 # some error conditions
932 return "" unless $length > 0;
933 return "" unless index( $id, ',' ) > 0;
934  
935 my $servicename = substr( $id, index( $id, ',' ) + 1, $length );
936 my $authURI = substr( $id, 0, index( $id, ',' ) );
937 my $db = $self->{moby_data_handler};
938 my $query = <<END;
939 SELECT si.lsid
940 FROM service_instance as si, authority as a
941 WHERE si.servicename = ? AND si.authority_id = a.authority_id AND a.authority_uri = ?
942 END
943 my $sth = $db->prepare($query);
944 $sth->execute( ( $servicename, $authURI ) );
945  
946 # returns an array of hash references
947 while ( my $ref = $sth->fetchrow_arrayref ) {
948  
949 #if we are here, it means the namespace exists!
950 return 1;
951 }
952  
953 # doesnt exist
954 return undef;
955  
956 }
957  
958 #-----------------------------------------------------------------
959 # isLatest
960 #-----------------------------------------------------------------
961  
962 =head2 isLatest
963  
964 This subroutine checks to see whether the LSID is the latest, based on the revision.
965  
966 This routine has 3 parameters:
967 namespace - the LSID namespace
968 id - the LSID object
969 revision - the LSID revision
970  
971 Example: isLatest('someNamespace','someObject','someRevision');
972  
973 If the lsid is the latest, then undef is returned.
974 If the lsid doesnt exist, then an empty string is returned.
975 And if the lsid isnt the latest, then the latest lsid is returned.
976  
977 =cut
978  
979 sub isLatest {
980 my ( $self, $namespace, $id, $revision ) = @_;
981 $revision = "__invalid__" unless $revision;
982  
983 my $length = length($id);
984  
985 # some error conditions
986 return "" unless $length > 0;
987 return "" unless index( $id, ',' ) > 0;
988  
989 my $servicename = substr( $id, index( $id, ',' ) + 1, $length );
990 my $authURI = substr( $id, 0, index( $id, ',' ) );
991  
992 my $db = $self->{moby_data_handler};
993 my $query = <<END;
994 SELECT si.lsid
995 FROM service_instance as si, authority as a
996 WHERE si.servicename = ? AND si.authority_id = a.authority_id AND a.authority_uri = ?
997 END
998 my $sth = $db->prepare($query);
999 $sth->execute( ( $servicename, $authURI ) );
1000  
1001 # returns an array of hash references
1002 while ( my $ref = $sth->fetchrow_arrayref ) {
1003  
1004 #if we are here, it means the namespace exists!
1005 my $lsid = LS::ID->new( $$ref[0] );
1006 return undef if $lsid->revision() and $lsid->revision() eq $revision;
1007 return $$ref[0];
1008 }
1009  
1010 # doesnt exist
1011 return "";
1012 }
1013  
1014 1;