| 1 | =head1 NAME - EEDB::EdgeSource | ||
| 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 | my $__riken_gsc_edgesource_global_should_cache = 1; | ||
| 46 | my $__riken_gsc_edgesource_global_id_cache = {}; | ||
| 47 | my $__riken_gsc_edgesource_global_name_cache = {}; | ||
| 48 | |||
| 49 | $VERSION = 0.953; | ||
| 50 | |||
| 51 | package EEDB::EdgeSource; | ||
| 52 | |||
| 53 | use strict; | ||
| 54 | use Time::HiRes qw(time gettimeofday tv_interval); | ||
| 55 | use EEDB::Feature; | ||
| 56 | use EEDB::Peer; | ||
| 57 | |||
| 58 | use MQdb::MappedQuery; | ||
| 59 | our @ISA = qw(MQdb::MappedQuery); | ||
| 60 | |||
| 61 | ################################################# | ||
| 62 | # Class methods | ||
| 63 | ################################################# | ||
| 64 | |||
| 65 | sub class { return "EdgeSource"; } | ||
| 66 | |||
| 67 | sub set_cache_behaviour { | ||
| 68 | my $class = shift; | ||
| 69 | my $mode = shift; | ||
| 70 | |||
| 71 | $__riken_gsc_edgesource_global_should_cache = $mode; | ||
| 72 | |||
| 73 | if(defined($mode) and ($mode eq '0')) { | ||
| 74 | #if turning off caching, then flush the caches | ||
| 75 | $__riken_gsc_edgesource_global_id_cache = {}; | ||
| 76 | $__riken_gsc_edgesource_global_name_cache = {}; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | sub create_from_name { | ||
| 81 | #if $db parameter is supplied then it will check the database first | ||
| 82 | #otherwise it will create one and store it (if $db is provided) | ||
| 83 | my $class = shift; | ||
| 84 | my $esrc_name = shift; | ||
| 85 | my $db = shift; #optional | ||
| 86 | |||
| 87 | if(!$esrc_name) { return undef; } | ||
| 88 | |||
| 89 | my $category = undef; | ||
| 90 | if($esrc_name =~ /(\w+)\:\:(.+)/) { | ||
| 91 | $category = $1; | ||
| 92 | $esrc_name = $2; | ||
| 93 | } | ||
| 94 | my $esrc = EEDB::EdgeSource->fetch_by_name($db, $esrc_name) if($db); | ||
| 95 | unless($esrc){ | ||
| 96 | $esrc = new EEDB::EdgeSource; | ||
| 97 | $esrc->name($esrc_name); | ||
| 98 | $esrc->category($category); | ||
| 99 | $esrc->store($db) if($db); | ||
| 100 | } | ||
| 101 | return $esrc; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | ################################################# | ||
| 106 | # Instance methods | ||
| 107 | ################################################# | ||
| 108 | |||
| 109 | sub init { | ||
| 110 | my $self = shift; | ||
| 111 | $self->SUPER::init; | ||
| 112 | return $self; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub name { | ||
| 116 | my $self = shift; | ||
| 117 | return $self->{'name'} = shift if(@_); | ||
| 118 | $self->{'name'}='' unless(defined($self->{'name'})); | ||
| 119 | return $self->{'name'}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub display_name { | ||
| 123 | my $self = shift; | ||
| 124 | return $self->{'display_name'} = shift if(@_); | ||
| 125 | if(!defined($self->{'display_name'}) and defined($self->{'name'})) { | ||
| 126 | return $self->{'name'}; | ||
| 127 | } else { | ||
| 128 | return $self->{'display_name'}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | sub category { | ||
| 133 | #like 'class', basically a high level controlled vocabulary of types | ||
| 134 | #but not implemented as any constraint in the database. Up to the data | ||
| 135 | #user to control the vocabular prior to data import. Not the DB's job to | ||
| 136 | #babysit the vocabular checks | ||
| 137 | my $self = shift; | ||
| 138 | return $self->{'category'} = shift if(@_); | ||
| 139 | $self->{'category'}='' unless(defined($self->{'category'})); | ||
| 140 | return $self->{'category'}; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub uqname { | ||
| 144 | my $self = shift; | ||
| 145 | return sprintf("%s(%s)", $self->name, $self->id); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub classification { | ||
| 149 | my $self = shift; | ||
| 150 | return $self->{'classification'} = shift if(@_); | ||
| 151 | $self->{'classification'}='' unless(defined($self->{'classification'})); | ||
| 152 | return $self->{'classification'}; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub create_date { | ||
| 156 | my $self = shift; | ||
| 157 | return $self->{'create_date'} = shift if(@_); | ||
| 158 | $self->{'create_date'}='' unless(defined($self->{'create_date'})); | ||
| 159 | return $self->{'create_date'}; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub is_active { | ||
| 163 | my $self = shift; | ||
| 164 | return $self->{'is_active'} = shift if(@_); | ||
| 165 | $self->{'is_active'}='' unless(defined($self->{'is_active'})); | ||
| 166 | return $self->{'is_active'}; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub is_visible { | ||
| 170 | my $self = shift; | ||
| 171 | return $self->{'is_visible'} = shift if(@_); | ||
| 172 | $self->{'is_visible'}='' unless(defined($self->{'is_visible'})); | ||
| 173 | return $self->{'is_visible'}; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub comments { | ||
| 177 | my $self = shift; | ||
| 178 | return $self->{'comments'} = shift if(@_); | ||
| 179 | $self->{'comments'}='' unless(defined($self->{'comments'})); | ||
| 180 | return $self->{'comments'}; | ||
| 181 | } | ||
| 182 | |||
| 183 | ################ | ||
| 184 | |||
| 185 | sub db1_is_external { | ||
| 186 | my $self = shift; | ||
| 187 | if(defined($self->{'_f1_ext_peer'})) { return 1;} else { return 0; } | ||
| 188 | } | ||
| 189 | |||
| 190 | sub db2_is_external { | ||
| 191 | my $self = shift; | ||
| 192 | if(defined($self->{'_f2_ext_peer'})) { return 1;} else { return 0; } | ||
| 193 | } | ||
| 194 | |||
| 195 | sub peer1 { | ||
| 196 | my $self = shift; | ||
| 197 | my $peer = shift; | ||
| 198 | |||
| 199 | if($peer) { | ||
| 200 | unless(defined($peer) && $peer->isa('EEDB::Peer')) { | ||
| 201 | die('peer1 param must be a EEDB::Peer'); | ||
| 202 | } | ||
| 203 | $self->{'_peer1'} = $peer; | ||
| 204 | } | ||
| 205 | |||
| 206 | if(defined($self->{'_f1_ext_peer'})) { | ||
| 207 | if(!defined($self->{'_peer1'})) { | ||
| 208 | $self->{'_peer1'} = EEDB::Peer->fetch_by_name($self->database, $self->{'_f1_ext_peer'}); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | return $self->{'_peer1'}; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub peer2 { | ||
| 215 | my $self = shift; | ||
| 216 | my $peer = shift; | ||
| 217 | |||
| 218 | if($peer) { | ||
| 219 | unless(defined($peer) && $peer->isa('EEDB::Peer')) { | ||
| 220 | die('peer2 param must be a EEDB::Peer'); | ||
| 221 | } | ||
| 222 | $self->{'_peer2'} = $peer; | ||
| 223 | } | ||
| 224 | |||
| 225 | if(defined($self->{'_f2_ext_peer'})) { | ||
| 226 | if(!defined($self->{'_peer2'})) { | ||
| 227 | $self->{'_peer2'} = EEDB::Peer->fetch_by_name($self->database, $self->{'_f2_ext_peer'}); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | return $self->{'_peer2'}; | ||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | sub database1 { | ||
| 235 | my $self = shift; | ||
| 236 | if($self->peer1) { return $self->peer1->peer_database; } | ||
| 237 | else { return $self->database; } | ||
| 238 | } | ||
| 239 | |||
| 240 | sub database2 { | ||
| 241 | my $self = shift; | ||
| 242 | if($self->peer2) { return $self->peer2->peer_database; } | ||
| 243 | else { return $self->database; } | ||
| 244 | } | ||
| 245 | |||
| 246 | ################ | ||
| 247 | |||
| 248 | sub get_edge_count { | ||
| 249 | my $self = shift; | ||
| 250 | |||
| 251 | if(!defined($self->database)) { return 0; } | ||
| 252 | if(!defined($self->{'_edge_count'})) { | ||
| 253 | my $sql = "SELECT count(*) FROM edge WHERE edge_source_id=?"; | ||
| 254 | $self->{'_edge_count'} = $self->fetch_col_value($self->database, $sql, $self->id); | ||
| 255 | } | ||
| 256 | return $self->{'_edge_count'}; | ||
| 257 | } | ||
| 258 | |||
| 259 | ################ | ||
| 260 | |||
| 261 | sub metadataset { | ||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | if(!defined($self->{'metadataset'})) { | ||
| 265 | $self->{'metadataset'} = new EEDB::MetadataSet; | ||
| 266 | |||
| 267 | if($self->database) { | ||
| 268 | my $symbols = EEDB::Symbol->fetch_all_by_edge_source_id($self->database, $self->id); | ||
| 269 | $self->{'metadataset'}->add_metadata(@$symbols); | ||
| 270 | |||
| 271 | my $mdata = EEDB::Metadata->fetch_all_by_edge_source_id($self->database, $self->id); | ||
| 272 | $self->{'metadataset'}->add_metadata(@$mdata); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | return $self->{'metadataset'}; | ||
| 276 | } | ||
| 277 | |||
| 278 | ################ | ||
| 279 | |||
| 280 | sub display_desc { | ||
| 281 | my $self = shift; | ||
| 282 | my $str = sprintf("EdgeSource(%s) %s : %s : %s", | ||
| 283 | $self->id, | ||
| 284 | $self->name, | ||
| 285 | $self->category, | ||
| 286 | $self->classification | ||
| 287 | ); | ||
| 288 | return $str; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub display_contents { | ||
| 292 | my $self = shift; | ||
| 293 | |||
| 294 | my $str = $self->display_desc; | ||
| 295 | $str .= "\n". $self->metadataset->display_contents; | ||
| 296 | return $str; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub xml { | ||
| 300 | my $self = shift; | ||
| 301 | my $str = $self->xml_start; | ||
| 302 | $str .= "\n". $self->metadataset->xml; | ||
| 303 | $str .= $self->xml_end; | ||
| 304 | return $str; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub xml_start { | ||
| 308 | my $self = shift; | ||
| 309 | my $str = sprintf("<edgesource id=\"%s\" name=\"%s\" classification=\"%s\" create_date=\"%s\" count=\"%d\"", | ||
| 310 | $self->id, | ||
| 311 | $self->name, | ||
| 312 | $self->classification, | ||
| 313 | $self->create_date, | ||
| 314 | $self->get_edge_count | ||
| 315 | ); | ||
| 316 | $str .= sprintf(" category=\"%s\"", $self->category) if($self->category); | ||
| 317 | $str .= sprintf(" f1_federated=\"%s\"", $self->peer1->alias) if($self->db1_is_external); | ||
| 318 | $str .= sprintf(" f2_federated=\"%s\"", $self->peer2->alias) if($self->db2_is_external); | ||
| 319 | $str .= " >"; | ||
| 320 | return $str; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub xml_end { | ||
| 324 | my $self = shift; | ||
| 325 | return "</edgesource>\n"; | ||
| 326 | } | ||
| 327 | |||
| 328 | ################################################# | ||
| 329 | # | ||
| 330 | # DBObject override methods | ||
| 331 | # | ||
| 332 | ################################################# | ||
| 333 | |||
| 334 | sub check_exists_db { | ||
| 335 | my $self = shift; | ||
| 336 | my $db = shift; | ||
| 337 | |||
| 338 | unless($db) { return undef; } | ||
| 339 | if(defined($self->primary_id)) { return $self; } | ||
| 340 | |||
| 341 | #check if it is already in the database | ||
| 342 | my $dbID = $db->fetch_col_value("SELECT edge_source_id FROM edge_source where name=?", $self->name); | ||
| 343 | if($dbID) { | ||
| 344 | $self->primary_id($dbID); | ||
| 345 | $self->database($db); | ||
| 346 | return $self; | ||
| 347 | } else { | ||
| 348 | return undef; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | sub store { | ||
| 353 | my $self = shift; | ||
| 354 | my $db = shift; | ||
| 355 | |||
| 356 | unless($db) { return undef; } | ||
| 357 | if($self->check_exists_db($db)) { return $self; } | ||
| 358 | |||
| 359 | my $peer1_name = undef; | ||
| 360 | my $peer2_name = undef; | ||
| 361 | |||
| 362 | if($self->peer1) { $peer1_name = $self->peer1->alias; } | ||
| 363 | if($self->peer2) { $peer2_name = $self->peer2->alias; } | ||
| 364 | |||
| 365 | $db->execute_sql("INSERT ignore INTO edge_source ". | ||
| 366 | "(create_date, is_active, is_visible, name, display_name, category, classification, f1_ext_peer, f2_ext_peer) ". | ||
| 367 | "VALUES(NOW(),?,?,?,?,?,?,?,?)", | ||
| 368 | $self->is_active, | ||
| 369 | $self->is_visible, | ||
| 370 | $self->name, | ||
| 371 | $self->display_name, | ||
| 372 | $self->category, | ||
| 373 | $self->classification, | ||
| 374 | $peer1_name, | ||
| 375 | $peer2_name | ||
| 376 | ); | ||
| 377 | |||
| 378 | $self->check_exists_db($db); #checks the database and sets the primary_id | ||
| 379 | |||
| 380 | #now do the symbols and metadata | ||
| 381 | $self->store_metadata; | ||
| 382 | |||
| 383 | return $self; | ||
| 384 | } | ||
| 385 | |||
| 386 | sub store_metadata { | ||
| 387 | my $self = shift; | ||
| 388 | die("error no database to store metadata\n") unless($self->database); | ||
| 389 | |||
| 390 | my $mdata_list = $self->metadataset->metadata_list; | ||
| 391 | foreach my $mdata (@$mdata_list) { | ||
| 392 | if(!defined($mdata->primary_id)) { | ||
| 393 | $mdata->store($self->database); | ||
| 394 | $mdata->store_link_to_edge_source($self); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | ##### DBObject instance override methods ##### | ||
| 400 | |||
| 401 | sub mapRow { | ||
| 402 | my $self = shift; | ||
| 403 | my $rowHash = shift; | ||
| 404 | my $dbh = shift; | ||
| 405 | |||
| 406 | $self->primary_id($rowHash->{'edge_source_id'}); | ||
| 407 | $self->name($rowHash->{'name'}); | ||
| 408 | $self->display_name($rowHash->{'display_name'}); | ||
| 409 | $self->category($rowHash->{'category'}); | ||
| 410 | $self->classification($rowHash->{'classification'}); | ||
| 411 | $self->create_date($rowHash->{'create_date'}); | ||
| 412 | $self->is_active($rowHash->{'is_active'}); | ||
| 413 | $self->is_visible($rowHash->{'is_visible'}); | ||
| 414 | |||
| 415 | $self->{'_f1_ext_peer'} = $rowHash->{'f1_ext_peer'}; | ||
| 416 | $self->{'_f2_ext_peer'} = $rowHash->{'f2_ext_peer'}; | ||
| 417 | |||
| 418 | |||
| 419 | if($__riken_gsc_edgesource_global_should_cache != 0) { | ||
| 420 | if(defined($__riken_gsc_edgesource_global_id_cache->{$self->database() . $self->id})) { | ||
| 421 | $self = $__riken_gsc_edgesource_global_id_cache->{$self->database() . $self->id}; | ||
| 422 | } else { | ||
| 423 | $__riken_gsc_edgesource_global_id_cache->{$self->database() . $self->id} = $self; | ||
| 424 | $__riken_gsc_edgesource_global_name_cache->{$self->database() . $self->name} = $self; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | return $self; | ||
| 428 | } | ||
| 429 | |||
| 430 | |||
| 431 | ##### public class methods for fetching by utilizing DBObject framework methods ##### | ||
| 432 | |||
| 433 | sub fetch_all { | ||
| 434 | my $class = shift; | ||
| 435 | my $db = shift; | ||
| 436 | my $sql = "SELECT * FROM edge_source"; | ||
| 437 | return $class->fetch_multiple($db, $sql); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub fetch_by_id { | ||
| 441 | my $class = shift; | ||
| 442 | my $db = shift; | ||
| 443 | my $id = shift; | ||
| 444 | |||
| 445 | if($__riken_gsc_edgesource_global_should_cache != 0) { | ||
| 446 | my $obj = $__riken_gsc_edgesource_global_id_cache->{$db . $id}; | ||
| 447 | if(defined($obj)) { return $obj; } | ||
| 448 | } | ||
| 449 | |||
| 450 | my $sql = "SELECT * FROM edge_source WHERE edge_source_id=?"; | ||
| 451 | return $class->fetch_single($db, $sql, $id); | ||
| 452 | } | ||
| 453 | |||
| 454 | sub fetch_by_name { | ||
| 455 | my $class = shift; | ||
| 456 | my $db = shift; | ||
| 457 | my $name = shift; | ||
| 458 | |||
| 459 | if($__riken_gsc_edgesource_global_should_cache != 0) { | ||
| 460 | my $obj = $__riken_gsc_edgesource_global_name_cache->{$db . $name}; | ||
| 461 | if(defined($obj)) { return $obj; } | ||
| 462 | } | ||
| 463 | |||
| 464 | my $sql = "SELECT * FROM edge_source WHERE name=?"; | ||
| 465 | return $class->fetch_single($db, $sql, $name); | ||
| 466 | } | ||
| 467 | |||
| 468 | sub fetch_all_by_category { | ||
| 469 | my $class = shift; | ||
| 470 | my $db = shift; | ||
| 471 | my $category = shift; | ||
| 472 | |||
| 473 | my $sql = "SELECT * FROM edge_source WHERE category=?"; | ||
| 474 | return $class->fetch_multiple($db, $sql, $category); | ||
| 475 | } | ||
| 476 | |||
| 477 | 1; | ||
| 478 |