1 |
## ---------------------------------------------------------------------- |
2 |
## $Id: Metadata.pm,v 1.3 2003/03/29 07:13:19 joko Exp $ |
3 |
## ---------------------------------------------------------------------- |
4 |
## $Log: Metadata.pm,v $ |
5 |
## Revision 1.3 2003/03/29 07:13:19 joko |
6 |
## better exception handling if target was not found in xml |
7 |
## |
8 |
## Revision 1.2 2003/02/20 20:04:35 joko |
9 |
## renamed methods |
10 |
## - refactored xml-specific code to Data::Storage::Handler::XML |
11 |
## |
12 |
## Revision 1.1 2003/02/18 15:35:49 joko |
13 |
## + initial commit |
14 |
## |
15 |
## ---------------------------------------------------------------------- |
16 |
|
17 |
|
18 |
package Data::Rap::Metadata; |
19 |
|
20 |
use strict; |
21 |
use warnings; |
22 |
|
23 |
|
24 |
use Data::Dumper; |
25 |
use DesignPattern::Object; |
26 |
use Storable; |
27 |
|
28 |
|
29 |
sub accessMetadata { |
30 |
my $self = shift; |
31 |
|
32 |
# create instance of storage object once |
33 |
if (!$self->{__rap}->{metadbengine}) { |
34 |
$self->{__rap}->{metadbengine} = |
35 |
DesignPattern::Object->fromPackage('Data::Storage::Handler::XML', filename => $self->{__rap}->{filename} ); |
36 |
} |
37 |
|
38 |
return $self->{__rap}->{metadbengine}; |
39 |
|
40 |
} |
41 |
|
42 |
sub getTargetList { |
43 |
my $self = shift; |
44 |
|
45 |
$self->log( "Reading database of targets (dot) from XML.", 'notice' ); |
46 |
|
47 |
# prepare access to metadata (assure instantiated storage handle) |
48 |
my $mdbe = $self->accessMetadata(); |
49 |
|
50 |
$mdbe->sendQuery("*/target"); |
51 |
$mdbe->circumflex('result'); |
52 |
|
53 |
# checks |
54 |
if ($mdbe->isEmpty) { |
55 |
$self->log("XML metadata was empty.", 'critical'); |
56 |
return; |
57 |
} |
58 |
|
59 |
# trace |
60 |
#print Dumper($self->{buffer}); |
61 |
#exit; |
62 |
|
63 |
# behaviour |
64 |
#$self->xml2simplehash(); |
65 |
my $tree = $mdbe->toEasyTree(); |
66 |
#return $result; |
67 |
|
68 |
# trace |
69 |
#print Dumper($tree); |
70 |
#exit; |
71 |
|
72 |
# build result |
73 |
my @targets; |
74 |
foreach my $entry (@{$tree->{content}}) { |
75 |
my $description = $entry->{content}->[0]->{content}->[0]->{content}; |
76 |
push @targets, { name => $entry->{attrib}->{name}, description => $description }; |
77 |
} |
78 |
|
79 |
return \@targets; |
80 |
|
81 |
} |
82 |
|
83 |
|
84 |
sub getTargetDetails { |
85 |
my $self = shift; |
86 |
my $target = shift; |
87 |
my $options = shift; |
88 |
|
89 |
# check cache and return result from there |
90 |
if (my $result = $self->{cache}->{targets}->{$target} and not $options->{force}) { |
91 |
return $result; |
92 |
} |
93 |
|
94 |
# get metadata for single task from storage |
95 |
my $mdbe = $self->accessMetadata(); |
96 |
$mdbe->sendQuery("*/target[\@name=\"$target\"]"); |
97 |
|
98 |
# FIXME: this is wrong behaviour! upper statement should return a proper |
99 |
# result, which itself (already) has the method 'isEmpty' or similar... |
100 |
if ($mdbe->isEmpty()) { |
101 |
$self->log( "Target '$target' not found.", 'notice' ); |
102 |
return; |
103 |
} |
104 |
|
105 |
my $tree = $mdbe->toEasyTree(); |
106 |
|
107 |
# trace |
108 |
#print Dumper($tree); |
109 |
#exit; |
110 |
|
111 |
return $tree; |
112 |
} |
113 |
|
114 |
sub indexTargets { |
115 |
my $self = shift; |
116 |
my $options = shift; |
117 |
|
118 |
my $filename = 'rap.xml.cache'; |
119 |
|
120 |
if ($options->{build}) { |
121 |
$self->log("Building index of Targets", 'notice'); |
122 |
# clear cache |
123 |
delete $self->{cache}->{targets}; |
124 |
my $list = $self->getTargetList(); |
125 |
foreach my $target (@$list) { |
126 |
$self->log("Reading target: $target->{name}", 'info'); |
127 |
my $details = $self->getTargetDetails($target->{name}, { force => 1 }); |
128 |
$self->{cache}->{targets}->{$target->{name}} = $details; |
129 |
} |
130 |
store $self->{cache}->{targets}, $filename; |
131 |
|
132 |
} elsif ($options->{load}) { |
133 |
if (not -e $filename) { |
134 |
return; |
135 |
} |
136 |
if ($self->{cache}->{targets} = retrieve($filename)) { |
137 |
return 1; |
138 |
} |
139 |
|
140 |
} else { |
141 |
$self->log("Please call with options build or load", 'warning'); |
142 |
|
143 |
} |
144 |
} |
145 |
|
146 |
1; |
147 |
__END__ |