blib/lib/MOBY/service_instance.pm untested

filter: Statementstmt 0.0% 0 / 241 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 82 MC/DCmcdc 0.0% 0 / 50 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 489 CC 30 SCAR 43.1
CC
30
Cov
0%
new
272.0
AUTOLOAD
30.0
service_name
12.0
CRAP 74.3
1 package MOBY::service_instance;
2 use SOAP::Lite;
3 use strict;
4 use Carp;
5 use vars qw($AUTOLOAD @ISA);
6 use MOBY::central_db_connection;
7 use MOBY::OntologyServer;
8 use MOBY::authority;
9 use MOBY::Config;
10  
11 use vars qw /$VERSION/;
12 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
13  
14 #@ISA = qw(MOBY::central_db_connection); # can't do this yet...
15  
16 =head1 NAME
17  
18 MOBY::service_instance - a lightweight connection to the
19 service_instance table in the database
20  
21 =head1 SYNOPSIS
22  
23 use MOBY::service_instance;
24 my $Instance = MOBY::service_instance->new(
25 authority => $AUTHORITY,
26 servicename => 'marksFabulousService',
27 service_type => $SERVICE_TYPE,
28 category => 'moby',
29 url => "http://www.illuminae.com/mobyservice.pl",
30 contact_email => "markw@illuminae.com",
31 authoritative => 1,
32 inputs => \@inputs,
33 output => \@outputs,
34 description => 'retrieves random sequences from a database');
35  
36 print $Instance->service_instance_id;
37 print $Instance->authority->authority_common_name;
38  
39  
40 =cut
41  
42 =head1 DESCRIPTION
43  
44 representation of the service_instance table. Can write to the database
45  
46 =head1 AUTHORS
47  
48 Mark Wilkinson (mwilkinson@mrl.ubc.ca)
49  
50  
51 =cut
52  
53 {
54  
55 # Encapsulated:
56 # DATA
57 #___________________________________________________________
58 #ATTRIBUTES
59 my %_attr_data = # DEFAULT ACCESSIBILITY
60 (
61 service_instance_id => [ undef, 'read/write' ],
62 category => [ undef, 'read/write' ],
63 servicename => [ undef, 'read/write' ],
64 _authority => [ undef, 'read/write' ], # the authority object
65 service_type => [ undef, 'read/write' ],
66 service_type_uri => [ undef, 'read/write' ],
67 authority => [ undef, 'read/write' ],
68 authority_uri => [ undef, 'read/write' ],
69 signatureURL => [ undef, 'read/write' ],
70 url => [ undef, 'read/write' ],
71 inputs => [ undef, 'read/write' ],
72 outputs => [ undef, 'read/write' ],
73 secondaries => [ undef, 'read/write' ],
74 contact_email => [ undef, 'read/write' ],
75 authoritative => [ 0, 'read/write' ],
76 description => [ undef, 'read/write' ],
77 registry => [ 'MOBY_Central', 'read/write' ],
78 lsid => [ undef, 'read/write' ],
79 test => [ 0, 'read/write' ]
80 , # toggles create or test_existence behaviour
81 );
82  
83 #_____________________________________________________________
84 # METHODS, to operate on encapsulated class data
85 # Is a specified object attribute accessible in a given mode
86 sub _accessible {
87 my ( $self, $attr, $mode ) = @_;
88 $_attr_data{$attr}[1] =~ /$mode/;
89 }
90  
91 # Classwide default value for a specified object attribute
92 sub _default_for {
93 my ( $self, $attr ) = @_;
94 $_attr_data{$attr}[0];
95 }
96  
97 # List of names of all specified object attributes
98 sub _standard_keys {
99 keys %_attr_data;
100 }
101  
102 sub service_name
103 { # give them a break if they chose service_name or servicename as the parameter
104 my ( $self, $val ) = @_;
105 if ( defined $val ) {
106 if ( defined $self->{servicename} ) {
107 return
108 undef # you are not allowed to change it once it has been set!
109 } else {
110 $self->{servicename} = $val;
111 }
112 }
113 return $self->{servicename};
114 }
115  
116 sub category {
117 my ( $self, $val ) = @_;
118 if ( ( defined $val ) && $self->category ) { return undef }
119 ( defined $val ) && ( $self->{category} = $val );
120 return $self->{category};
121 }
122  
123 sub service_type {
124 my ( $self, $val ) = @_;
125 if ( defined $val && $self->service_type ) { return undef }
126 ( defined $val ) && ( $self->{service_type} = $val );
127 return $self->{service_type};
128 }
129  
130 sub url {
131 my ( $self, $val ) = @_;
132 if ( defined $val && $self->url ) { return undef }
133 ( defined $val ) && ( $self->{url} = $val );
134 return $self->{url};
135 }
136  
137 sub signatureURL {
138 my ( $self, $val ) = @_;
139 if ( defined $val && $self->signatureURL ) { return undef }
140 ( defined $val ) && ( $self->{signatureURL} = $val );
141 return $self->{signatureURL};
142 }
143  
144 sub contact_email {
145 my ( $self, $val ) = @_;
146 if ( defined $val && $self->contact_email ) { return undef }
147 ( defined $val ) && ( $self->{contact_email} = $val );
148 return $self->{contact_email};
149 }
150  
151 sub description {
152 my ( $self, $val ) = @_;
153 if ( defined $val && $self->description ) { return undef }
154 ( defined $val ) && ( $self->{description} = $val );
155 return $self->{description};
156 }
157  
158 sub dbh {
159 $CONFIG ||= MOBY::Config->new; # exported by Config.pm
160 my $adaptor =
161 $CONFIG->getDataAdaptor( datasource => 'mobycentral' )->dbh;
162 }
163  
164 sub adaptor {
165 $CONFIG ||= MOBY::Config->new; # exported by Config.pm
166 my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
167 }
168 }
169  
170 sub new {
171 my ( $caller, %args ) = @_;
172 my $caller_is_obj = ref($caller);
173 return $caller if $caller_is_obj;
174 my $class = $caller_is_obj || $caller;
175 my $proxy;
176 my ($self) = bless {}, $class;
177 foreach my $attrname ( $self->_standard_keys ) {
178 if ( exists $args{$attrname} ) {
179 $self->{$attrname} = $args{$attrname};
180 } elsif ($caller_is_obj) {
181 $self->{$attrname} = $caller->{$attrname};
182 } else {
183 $self->{$attrname} = $self->_default_for($attrname);
184 }
185 }
186 return undef unless $self->authority_uri;
187 return undef unless $self->servicename;
188 if( $self->lsid){
189 my $l = $self->lsid; # but is LSID valid format?
190 return undef unless $l =~ m'^[uU][rR][nN]:[lL][sS][iI][dD]:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*:[A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\!\*\']*:\d\d\d\d-\d\d\-\d\dT\d\d\-\d\d\-\d\d(Z|[\+|-]\d\d\d\d){1,1}$';
191 }
192 if ( $self->test ) { return $self->service_instance_exists } # returns boolean
193  
194 $self->authority( $self->_get_authority() ); # as MOBY::authority object
195  
196 if ( $self->service_type ) {
197 my $OE = MOBY::OntologyServer->new( ontology => 'service' );
198 my ( $success, $message, $servicetypeURI ) =
199 $OE->serviceExists( term => $self->service_type );
200 unless (
201 $success || ( ( $self->service_type =~ /urn:lsid/i ) && !( $self->service_type =~ /urn:lsid:biomoby.org/i ) )
202 )
203 {
204 return undef;
205 }
206 ( $self->service_type =~ /urn:lsid/ )?
207 $self->service_type_uri( $self->service_type )
208 : $self->service_type_uri($servicetypeURI);
209 }
210 my $existing_services = $self->adaptor->query_service_instance(servicename => $self->servicename,
211 authority_uri => $self->authority_uri);
212 my $existing_service = shift(@$existing_services);
213 if ($existing_service->{servicename}) { # if service exists, then instantiate it from the database retrieval we just did
214 $self->servicename( $existing_service->{'servicename'} );
215 $self->authoritative( $existing_service->{'authoritative'} );
216 $self->service_instance_id( $existing_service->{'service_instance_id'} );
217 $self->category( $existing_service->{'category'} );
218 $self->service_type( $existing_service->{'service_type_uri'} );
219 $self->url( $existing_service->{'url'} );
220 $self->contact_email( $existing_service->{'contact_email'} );
221 $self->description( $existing_service->{'description'} );
222 $self->authority( $existing_service->{'authURI'} );
223 $self->signatureURL( $existing_service->{'signatureURL'} );
224 $self->lsid( $existing_service->{'lsid'} );
225 $self->{__exists__} = 1; # this service already existed
226 } elsif (!($existing_service->{servicename}) # if it doesn't exist
227 && (defined $self->category) # and you have given me things I need to create it
228 && ( defined $self->service_type )
229 && ( defined $self->url )
230 && ( defined $self->contact_email )
231 && ( defined $self->description )
232 ) { # then create it de novo if we have enough information
233 # create a timestamp for the LSID
234 my ($sec,$min,$hour,$mday,$month,$year, $wday,$yday,$dst) =gmtime(time);
235 my $date = sprintf ("%04d-%02d-%02dT%02d-%02d-%02dZ",$year+1900,$month+1,$mday,$hour,$min,$sec);
236  
237 #create LSID for service and register it in the DB
238 my $_config ||= MOBY::Config->new;
239 unless ($self->lsid){
240 # create an LSID if one wasnt passed in
241 my $LSID_Auth = $_config->{mobycentral}->{lsid_authority};
242 my $LSID_NS = $_config->{mobycentral}->{lsid_namespace};
243 $LSID_Auth ||="biomoby.org";
244 $LSID_NS ||="serviceinstance";
245
246 # TODO - # MOBY Central should validate the format of authority uri and servicename when it starts up, sice we are using them to construct LSID's
247 my $service_lsid = "urn:lsid:$LSID_Auth:$LSID_NS:"
248 . $self->authority_uri . ","
249 . $self->servicename.":"."$date"; # LSID with timestamp
250 $self->lsid($service_lsid);
251 }
252 my $id = $self->adaptor->insert_service_instance(
253 category => $self->category,
254 servicename => $self->servicename,
255 service_type_uri => $self->service_type_uri,
256 authority_uri => $self->authority_uri,
257 url => $self->url,
258 contact_email => $self->contact_email,
259 authoritative => $self->authoritative,
260 description => $self->description,
261 signatureURL => $self->signatureURL,
262 lsid => $self->lsid
263 );
264 return undef unless $id;
265 $self->service_instance_id($id);
266 $self->{__exists__} = 1; # this service now exists
267 } else { # if it doesn't exist, and you havne't given me anyting I need to create it, then bail out
268 return undef;
269 }
270 return $self;
271 }
272  
273 sub DELETE_THYSELF {
274 my ($self) = @_;
275 my $dbh = $self->dbh;
276 unless ( $self->{__exists__} ) {
277 return undef;
278 }
279 $CONFIG ||= MOBY::Config->new;
280 my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
281  
282 #********FIX this should really be delete_input and delete_output
283 # the routines below know too much about the database (e.g. that
284 # the delete_simple_input routines are broken into two parts - by LSID and
285 # by collecion ID... BAD BAD BAD
286 $adaptor->delete_simple_input(service_instance_lsid => $self->lsid);
287 $adaptor->delete_simple_output(service_instance_lsid => $self->lsid);
288
289 my $result = $adaptor->query_collection_input(service_instance_lsid => $self->lsid);
290
291 foreach my $row (@$result) {
292 my $id = $row->{collection_input_id};
293 $adaptor->delete_simple_input(collection_input_id => $id);
294 }
295 $result = $adaptor->query_collection_output(service_instance_lsid => $self->lsid);
296
297 foreach my $row (@$result) {
298 my $id = $row->{collection_output_id};
299
300 $adaptor->delete_simple_output(collection_output_id => $id);
301 }
302 $adaptor->delete_collection_input(service_instance_lsid => $self->lsid);
303 $adaptor->delete_collection_output(service_instance_lsid => $self->lsid);
304 $adaptor->delete_secondary_input(service_instance_lsid => $self->lsid);
305 $adaptor->delete_service_instance(service_instance_lsid => $self->lsid);
306
307 return 1;
308 }
309  
310 sub authority_id {
311 my ($self) = @_;
312 return $self->authority->authority_id;
313 }
314  
315 sub service_instance_exists {
316 my ($self) = @_;
317 $CONFIG ||= MOBY::Config->new;
318 my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
319 my $dbh = $self->dbh;
320 my $authority;
321  
322 my $result = $adaptor->query_service_existence(authority_uri => $self->authority_uri, servicename => $self->servicename);
323 return $result
324 }
325  
326 sub _get_authority
327 { # there's somethign fishy here... the authority.pm object already knows about authority_id and authorty_uri, doens't it?
328 my ($self) = @_;
329 my $dbh = $self->dbh;
330 my $authority;
331 $CONFIG ||= MOBY::Config->new;
332 my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
333 my $result = $adaptor->query_authority(authority_uri => $self->authority_uri);
334 #*********FIX we should nver need to know the authority ID in this level of code!
335 if ( @$result[0]) {
336 my $row = shift(@$result);
337 #my $id = $row->{authority_id};
338 my $name = $row->{authority_common_name};
339 my $uri = $row->{authority_uri};
340 my $email = $row->{contact_email};
341  
342 $authority = MOBY::authority->new(
343 dbh => $self->dbh,
344 # authority_id => $id,
345 authority_uri => $uri,
346 contact_email => $email,
347 );
348 } else {
349 $authority = MOBY::authority->new(
350 dbh => $self->dbh,
351 authority_uri => $self->authority_uri,
352 contact_email => $self->contact_email,
353 );
354 }
355 return $authority;
356 }
357  
358 sub add_simple_input {
359 my ( $self, %a ) = @_;
360  
361 # validate here... one day...
362 my $simple = MOBY::simple_input->new(
363 object_type_uri => $a{'object_type_uri'},
364 namespace_type_uris => $a{'namespace_type_uris'},
365 article_name => $a{'article_name'},
366 service_instance_id => $self->service_instance_id,
367 service_instance_lsid => $self->lsid,
368 collection_input_id => $a{'collection_input_id'}
369 );
370 push @{ $self->{inputs} }, $simple;
371 return $simple->simple_input_id;
372 }
373  
374 sub add_simple_output {
375 my ( $self, %a ) = @_;
376  
377 # validate here... one day...
378 my $simple = MOBY::simple_output->new(
379 object_type_uri => $a{'object_type_uri'},
380 namespace_type_uris => $a{'namespace_type_uris'},
381 article_name => $a{'article_name'},
382 service_instance_id => $self->service_instance_id,
383 service_instance_lsid => $self->lsid,
384 collection_output_id => $a{'collection_output_id'}
385 );
386 push @{ $self->{outputs} }, $simple;
387 return $simple->simple_output_id;
388 }
389  
390 sub add_collection_input {
391 my ( $self, %a ) = @_;
392  
393 # validate here... one day...
394 my $coll = MOBY::collection_input->new(
395 article_name => $a{'article_name'},
396 service_instance_lsid => $self->lsid,
397 service_instance_id => $self->service_instance_id, );
398 push @{ $self->{inputs} }, $coll;
399 return $coll->collection_input_id;
400 }
401  
402 sub add_collection_output {
403 my ( $self, %a ) = @_;
404  
405 # validate here... one day...
406 my $coll = MOBY::collection_output->new(
407 article_name => $a{'article_name'},
408 service_instance_lsid => $self->lsid,
409 service_instance_id => $self->service_instance_id, );
410 push @{ $self->{outputs} }, $coll;
411 return $coll->collection_output_id;
412 }
413  
414 sub add_secondary_input {
415 my ( $self, %a ) = @_;
416  
417 # validate here... one day...
418 my $sec = MOBY::secondary_input->new(
419 default_value => $a{'default_value'},
420 maximum_value => $a{'maximum_value'},
421 minimum_value => $a{'minimum_value'},
422 enum_value => $a{'enum_value'},
423 datatype => $a{'datatype'},
424 article_name => $a{'article_name'},
425 description => $a{'description'},
426 service_instance_id => $self->service_instance_id,
427 service_instance_lsid => $self->lsid,
428 );
429 push @{ $self->{inputs} }, $sec;
430 return $sec->secondary_input_id;
431 }
432  
433 sub AUTOLOAD {
434 no strict "refs";
435 my ( $self, $newval ) = @_;
436 $AUTOLOAD =~ /.*::(\w+)/;
437 my $attr = $1;
438 if ( $self->_accessible( $attr, 'write' ) ) {
439 *{$AUTOLOAD} = sub {
440 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
441 return $_[0]->{$attr};
442 }; ### end of created subroutine
443 ### this is called first time only
444 if ( defined $newval ) {
445 $self->{$attr} = $newval;
446 }
447 return $self->{$attr};
448 } elsif ( $self->_accessible( $attr, 'read' ) ) {
449 *{$AUTOLOAD} = sub {
450 return $_[0]->{$attr};
451 }; ### end of created subroutine
452 return $self->{$attr};
453 }
454  
455 # Must have been a mistake then...
456 croak "No such method: $AUTOLOAD";
457 }
458 sub DESTROY { }
459 1;