MOBY
view release on metacpan or search on metacpan
lib/MOBY/RDF/Ontologies/Cache/ServiceTypeCache.pm view on Meta::CPAN
This method is not thread safe.
Throw an exception if any of the following occurs:
* A SOAP error as a result of calling the registry
* Problems writing to the cache directory
=cut
sub create_service_type_cache {
my ($self) = @_;
# 2 steps:
# -> create a LIST file
my $xml = $self->_create_list_file;
# 2-> foreach datatype store RDF for the authority
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($xml);
my %authorities_completed = ();
my $nodes = $doc->documentElement()->getChildrenByTagName('serviceType');
for ( 1 .. $nodes->size() ) {
my $name = $nodes->get_node($_)->getAttribute('name');
next if $authorities_completed{$name};
$authorities_completed{$name} = 1;
$xml = MOBY::RDF::Ontologies::ServiceTypes->new(
endpoint => $self->{utils}->_endpoint );
$xml = $xml->createByName( { term => $name });
my $file = File::Spec->catfile(
$self->{utils}->cachedir,
$self->{utils}->_clean( $self->{utils}->_endpoint ),
$self->{utils}->SERVICETYPES_CACHE,
$name
);
open( FILE, ">$file" )
or die("Can't open file '$file' for writing: $!");
print FILE $xml;
close FILE;
}
}
#-----------------------------------------------------------------
# update_service_type_cache
#-----------------------------------------------------------------
=head2 update_service_type_cache
Update the service type cache. This will update any items that are 'old',
by relying on the LSID for the service type. This method is not thread safe.
This method returns the number of changed resources.
To update the cache with a thread safe method, call C<get_rdf>.
Throw an exception if any of the following occur:
* There is a SOAP error calling the registry
* There were read/write errors on the cache directory or its contents
=cut
sub update_service_type_cache {
my ($self) = @_;
my $wasOld = 0;
my %old_services = ();
my %new_services = ();
my %changed_services = ();
if (
!(
-e File::Spec->catfile(
$self->{utils}->cachedir,
$self->{utils}->_clean( $self->{utils}->_endpoint ),
$self->{utils}->SERVICETYPES_CACHE
)
)
)
{
$self->create_service_type_cache;
return -1;
}
if (
!(
-e File::Spec->catfile(
$self->{utils}->cachedir,
$self->{utils}->_clean( $self->{utils}->_endpoint ),
$self->{utils}->SERVICETYPES_CACHE,
$self->{utils}->LIST_FILE
)
)
)
{
warn( "service type LIST_FILE doesn't exist, so I created the cache from scratch!"
);
$self->create_service_type_cache;
return -1;
}
# steps:
# read in the LIST file and extract lsids for all datatypes
my $file = File::Spec->catfile(
$self->{utils}->cachedir,
$self->{utils}->_clean( $self->{utils}->_endpoint ),
$self->{utils}->SERVICETYPES_CACHE,
$self->{utils}->LIST_FILE
);
my $parser = XML::LibXML->new();
my $doc;
eval {
$doc = $parser->parse_file($file);
};
warn "There was something wrong with '$file' and we couldn't parse it.\nWill attempt to create from scratch.\n" if $@;
$doc = $parser->parse_string($self->_create_list_file) if $@;
my $nodes = $doc->documentElement()->getChildrenByTagName('serviceType');
for ( 1 .. $nodes->size() ) {
my $name = $nodes->get_node($_)->getAttribute('name');
my $lsid = $nodes->get_node($_)->getAttribute('lsid');
$old_services{$name}{$lsid} = 1;
}
( run in 2.188 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )