EEDB/Metadata.pm untested

filter: Statementstmt 0.0% 0 / 214 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 368 CC 37 SCAR 35.1
CC
37
Cov
0%
extract_keywords
156.0
fetch_all_by_feature
20.0
equals
12.0
CRAP 33.6
1 =head1 NAME - EEDB::Metadata
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 =head1 CONTACT
8  
9 Jessica Severin <severin@gsc.riken.jp>
10  
11 =head1 LICENSE
12  
13 * Software License Agreement (BSD License)
14 * EdgeExpressDB [eeDB] system
15 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
16 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * Neither the name of Jessica Severin RIKEN OSC nor the
25 * names of its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 =head1 APPENDIX
40  
41 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
42  
43 =cut
44  
45 $VERSION = 0.953;
46  
47 package EEDB::Metadata;
48  
49 use strict;
50 use Time::HiRes qw(time gettimeofday tv_interval);
51  
52 use MQdb::MappedQuery;
53 our @ISA = qw(MQdb::MappedQuery);
54  
55 #################################################
56 # Class methods
57 #################################################
58  
59 sub class { return "Metadata"; }
60  
61 #################################################
62 # Instance methods
63 #################################################
64  
65 sub init {
66 my $self = shift;
67 my @args = @_;
68 $self->SUPER::init(@args);
69  
70 if(@args and (scalar(@args)==2)) {
71 $self->type($args[0]);
72 $self->data($args[1]);
73 }
74 return $self;
75 }
76  
77 sub equals {
78 my $self = shift;
79 my $other = shift;
80 return undef unless(defined($other));
81 if(($self->type eq $other->type) and ($self->data eq $other->data)) { return 1; }
82 return undef;
83 }
84  
85  
86 ##########################
87 #
88 # getter/setter methods of data which is stored in database
89 #
90 ##########################
91  
92  
93 sub type {
94 my $self = shift;
95 return $self->{'type'} = shift if(@_);
96 $self->{'type'}='' unless(defined($self->{'type'}));
97 return $self->{'type'};
98 }
99  
100 sub data {
101 my $self = shift;
102 return $self->{'data'} = shift if(@_);
103 return $self->{'data'};
104 }
105  
106 sub data_size {
107 my $self = shift;
108 if(defined($self->{'data'})) { $self->{'data_size'} = length($self->{'data'}); }
109 $self->{'data_size'}='' unless(defined($self->{'data_size'}));
110 return $self->{'data_size'};
111 }
112  
113  
114 sub display_desc {
115 my $self = shift;
116 return sprintf("Metadata(%s) %s (%d bytes)",
117 $self->id,
118 $self->type,
119 $self->data_size
120 );
121 }
122  
123 sub display_contents {
124 my $self = shift;
125 return sprintf("Metadata(%s) %s:\"%s\"",
126 $self->id,
127 $self->type,
128 $self->data);
129 }
130  
131 sub xml {
132 my $self = shift;
133 my $data = $self->data;
134 $data =~ s/\&/&amp;/g;
135 $data =~ s/\"/&quot;/g; #\"
136 $data =~ s/\</&lt;/g;
137 $data =~ s/\>/&gt;/g;
138  
139 my $str = sprintf("<mdata type=\"%s\">%s</mdata>\n",
140 $self->type,
141 $data);
142 return $str;
143 }
144  
145 #################################################
146 #
147 # keyword analysis
148 #
149 #################################################
150  
151 sub extract_keywords {
152 my $self = shift;
153  
154 my $valid_keywords = new EEDB::MetadataSet;
155
156 #split keywords on , ( ) [ ] : ; or whitespace
157 #my $desc = lc($self->data);
158 my $desc = $self->data;
159 my @keywords = split /[\,\(\)\[\]\:\;\s]/, $desc;
160 foreach my $keyword (@keywords) {
161 next unless($keyword);
162 next unless(length($keyword) > 1);
163
164 #remove an ending . (period)
165 if($keyword =~ /\.$/) { chop($keyword); }
166
167 #capitalization test
168 my $tkey = $keyword;
169 if(!($tkey =~ /\d/) and ($tkey =~ s/^([A-Z])/lc($1)/e) and ($tkey eq lc($keyword))) {
170 #printf("'%s' is capitalized\n", $keyword);
171 $keyword = lc($keyword);
172 }
173  
174 next if($keyword eq 'the');
175 next if($keyword eq 'and');
176 next if($keyword eq 'or');
177 next if($keyword eq 'with');
178  
179 #printf(" '%s'\n", $keyword);
180 $valid_keywords->add_tag_symbol("keyword", $keyword);
181
182 #sub-keyword tests
183 my @sub_keywords = split /[\-\/]/, $keyword;
184 if(scalar(@sub_keywords)>1) {
185 foreach my $keyw (@sub_keywords) {
186 next unless($keyw);
187 next unless(length($keyw) > 2);
188 $valid_keywords->add_tag_symbol("keyword", $keyw);
189 }
190 }
191 }
192 $valid_keywords->remove_duplicates;
193 return $valid_keywords;
194 }
195  
196  
197 #################################################
198 #
199 # DBObject override methods
200 #
201 #################################################
202  
203 sub store {
204 my $self = shift;
205 my $db = shift;
206
207 unless($db) { return undef; }
208 #if($self->check_exists_db($db)) { return $self; }
209
210 my $dbc = $db->get_connection;
211 my $sql = "INSERT INTO metadata (data_type, data) VALUES(?,?)";
212 my $sth = $dbc->prepare($sql);
213 $sth->execute($self->type, $self->data);
214 my $dbID = $dbc->last_insert_id(undef, undef, qw(metadata metadata_id));
215 $sth->finish;
216 if($dbID) {
217 $self->primary_id($dbID);
218 $self->database($db);
219 return $self;
220 } else {
221 return undef;
222 }
223 }
224  
225 sub update {
226 #use with extreme caution!!!! I should not even include it
227 my $self = shift;
228 my $sql = "UPDATE metadata set data_type=?, data=? where metadata_id=?";
229 $self->database->execute_sql($sql, $self->type, $self->data, $self->id);
230 }
231  
232 sub check_exists_db {
233 my $self = shift;
234 my $db = shift;
235
236 return undef unless($db);
237 my $dbc = $db->get_connection;
238  
239 #first do check if it is already in the database
240 my $sth = $dbc->prepare("SELECT metadata_id FROM metadata where data_type=? and data=?");
241 $sth->execute($self->type, $self->data);
242 my ($dbID) = $sth->fetchrow_array();
243 $sth->finish;
244 if($dbID) {
245 $self->primary_id($dbID);
246 $self->database($db);
247 return $self;
248 } else {
249 return undef;
250 }
251 }
252  
253  
254 sub store_link_to_feature {
255 my $self = shift;
256 my $feature = shift;
257
258 unless($feature->database) {
259 printf("ERROR:: %s has no database to link Metadata\n", $feature->simple_display_desc);
260 die();
261 }
262 $feature->database->execute_sql(
263 "INSERT ignore INTO feature_2_metadata (feature_id, metadata_id) VALUES(?,?)",
264 $feature->id, $self->id);
265 }
266  
267 sub unlink_from_feature {
268 my $self = shift;
269 my $feature = shift;
270
271 unless($feature->database) {
272 printf("ERROR:: %s has no database to link Metadata\n", $feature->simple_display_desc);
273 die();
274 }
275 $feature->database->execute_sql(
276 "DELETE from feature_2_metadata where feature_id=? and metadata_id=?",
277 $feature->id, $self->id);
278 }
279  
280  
281 sub store_link_to_feature_link {
282 my $self = shift;
283 my $edge = shift;
284
285 unless($edge->database) {
286 printf("ERROR:: %s has no database to link Metadata\n", $edge->display_desc);
287 die();
288 }
289 $edge->database->execute_sql(
290 "INSERT ignore INTO edge_2_metadata (edge_id, metadata_id) VALUES(?,?)",
291 $edge->id, $self->id);
292 }
293  
294  
295 sub store_link_to_experiment {
296 my $self = shift;
297 my $experiment = shift;
298
299 unless($experiment->database) {
300 printf("ERROR:: %s has no database to link Metadata\n", $experiment->display_desc);
301 die();
302 }
303 $experiment->database->execute_sql(
304 "INSERT ignore INTO experiment_2_metadata (experiment_id, metadata_id) VALUES(?,?)",
305 $experiment->id, $self->id);
306 }
307  
308  
309 sub store_link_to_feature_source {
310 my $self = shift;
311 my $fsrc = shift;
312
313 unless($fsrc->database) {
314 printf("ERROR:: %s has no database to link Metadata\n", $fsrc->display_desc);
315 die();
316 }
317 $fsrc->database->execute_sql(
318 "INSERT ignore INTO feature_source_2_metadata (feature_source_id, metadata_id) VALUES(?,?)",
319 $fsrc->id, $self->id);
320 }
321  
322  
323 sub store_link_to_edge_source {
324 my $self = shift;
325 my $edge_source = shift;
326
327 unless($edge_source->database) {
328 printf("ERROR:: %s has no database to link Metadata\n", $edge_source->display_desc);
329 die();
330 }
331 $edge_source->database->execute_sql(
332 "INSERT ignore INTO edge_source_2_metadata (edge_source_id, metadata_id) VALUES(?,?)",
333 $edge_source->id, $self->id);
334 }
335  
336  
337 ##### DBObject instance override methods #####
338  
339 sub mapRow {
340 my $self = shift;
341 my $rowHash = shift;
342 my $dbc = shift;
343  
344 $self->primary_id($rowHash->{'metadata_id'});
345 $self->type($rowHash->{'data_type'});
346 $self->data($rowHash->{'data'});
347
348 return $self;
349 }
350  
351  
352 ##### public class methods for fetching by utilizing DBObject framework methods #####
353  
354 sub fetch_by_id {
355 my $class = shift;
356 my $db = shift;
357 my $id = shift;
358  
359 my $sql = "SELECT * FROM metadata WHERE metadata_id=?";
360 return $class->fetch_single($db, $sql, $id);
361 }
362  
363 sub stream_all {
364 my $class = shift;
365 my $db = shift;
366  
367 my $sql = "SELECT * FROM metadata";
368 return $class->stream_multiple($db, $sql);
369 }
370  
371 sub fetch_all_by_data {
372 my $class = shift;
373 my $db = shift;
374 my $data = shift;
375 my $data_type = shift; #optional
376 my $response_limit = shift; #optional
377
378 my $sql = sprintf("SELECT * FROM metadata WHERE data = ?");
379 if(defined($data_type)) {
380 $sql .= sprintf(" AND data_type='%s'", $data_type);
381 }
382 if($response_limit) {
383 $sql .= sprintf(" LIMIT %d", $response_limit);
384 }
385 return $class->fetch_multiple($db, $sql, $data);
386 }
387  
388 sub fetch_all_by_type {
389 my $class = shift;
390 my $db = shift;
391 my $data_type = shift;
392 my $response_limit = shift; #optional
393
394 my $sql = sprintf("SELECT * FROM metadata WHERE data_type='%s'", $data_type);
395 if($response_limit) {
396 $sql .= sprintf(" ORDER BY metadata_id LIMIT %d", $response_limit);
397 }
398 return $class->fetch_multiple($db, $sql);
399 }
400  
401 ##
402  
403 sub fetch_all_by_feature {
404 my $class = shift;
405 my $feature = shift;
406 my %options = @_; #like types=>['authors','title']
407  
408 my @types = @{$options{'types'}} if($options{'types'});
409  
410 my $sql = "SELECT m.* FROM metadata m join feature_2_metadata using(metadata_id) WHERE feature_id=?";
411 $sql .= " AND data_type in (?)" if(@types);
412 if(@types) {
413 return $class->fetch_multiple($feature->database, $sql, $feature->id, join(',', @types));
414 } else {
415 return $class->fetch_multiple($feature->database, $sql, $feature->id);
416 }
417 }
418  
419 sub fetch_all_by_feature_id {
420 my $class = shift;
421 my $db = shift;
422 my $id = shift;
423 my $type = shift;
424  
425 my $sql = "SELECT m.* FROM metadata m join feature_2_metadata using(metadata_id) WHERE feature_id=?";
426 $sql .= sprintf(" AND data_type=\"%s\"", $type) if($type);
427 return $class->fetch_multiple($db, $sql, $id);
428 }
429  
430 sub fetch_all_by_edge_id {
431 my $class = shift;
432 my $db = shift;
433 my $id = shift;
434  
435 my $sql = "SELECT m.* FROM metadata m join edge_2_metadata using(metadata_id) WHERE edge_id=?";
436 return $class->fetch_multiple($db, $sql, $id);
437 }
438  
439 sub fetch_all_by_experiment_id {
440 my $class = shift;
441 my $db = shift;
442 my $id = shift;
443  
444 my $sql = "SELECT m.* FROM metadata m join experiment_2_metadata using(metadata_id) WHERE experiment_id=?";
445 return $class->fetch_multiple($db, $sql, $id);
446 }
447  
448 sub fetch_all_by_feature_source_id {
449 my $class = shift;
450 my $db = shift;
451 my $id = shift;
452  
453 my $sql = "SELECT m.* FROM metadata m join feature_source_2_metadata using(metadata_id) where feature_source_id = ?";
454 return $class->fetch_multiple($db, $sql, $id);
455 }
456  
457 sub fetch_all_by_edge_source_id {
458 my $class = shift;
459 my $db = shift;
460 my $id = shift;
461  
462 my $sql = "SELECT m.* FROM metadata m join edge_source_2_metadata using(metadata_id) where edge_source_id = ?";
463 return $class->fetch_multiple($db, $sql, $id);
464 }
465  
466 1;
467