| 1 | package WWW::Garden::Design::Import; | ||
| 2 | |||
| 3 | use Moo::Role; | ||
| 4 | |||
| 5 | use 5.30.0; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::Dumper::Concise; # For Dumper(). | ||
| 10 | |||
| 11 | use File::Slurper qw/read_text/; | ||
| 12 | use File::Spec; | ||
| 13 | |||
| 14 | use FindBin; | ||
| 15 | |||
| 16 | use Text::CSV::Encoded; | ||
| 17 | |||
| 18 | use Types::Standard qw/Any HashRef/; | ||
| 19 | |||
| 20 | has crossref_locations => | ||
| 21 | ( | ||
| 22 | default => sub{ return {} }, | ||
| 23 | is => 'rw', | ||
| 24 | isa => HashRef, | ||
| 25 | required => 0, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has db => | ||
| 29 | ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => Any, | ||
| 32 | required => 0, | ||
| 33 | ); | ||
| 34 | |||
| 35 | our $VERSION = '0.97'; | ||
| 36 | |||
| 37 | # ----------------------------------------------- | ||
| 38 | |||
| 39 | sub populate_all_tables | ||
| 40 | { | ||
| 41 | my($self) = @_; | ||
| 42 | |||
| 43 | $self -> db -> logger -> debug('Populating all tables'); | ||
| 44 | |||
| 45 | my($path) = File::Spec -> catfile($FindBin::Bin, '..', 'data', 'flowers.csv'); | ||
| 46 | my($csv) = Text::CSV::Encoded -> new | ||
| 47 | ({ | ||
| 48 | allow_whitespace => 1, | ||
| 49 | encoding_in => 'utf-8', | ||
| 50 | }); | ||
| 51 | |||
| 52 | my(%attribute_type_keys); | ||
| 53 | my(%flower_keys, %feature_keys); | ||
| 54 | my(%garden_keys); | ||
| 55 | my(%property_keys); | ||
| 56 | |||
| 57 | $self -> populate_attribute_types_table($path, $csv, \%attribute_type_keys); | ||
| 58 | $self -> populate_constants_table($path, $csv); | ||
| 59 | $self -> populate_flowers_table($path, $csv, \%flower_keys); | ||
| 60 | $self -> populate_properties_table($path, $csv, \%property_keys); | ||
| 61 | $self -> populate_gardens_table($path, $csv, \%garden_keys, \%property_keys); | ||
| 62 | $self -> populate_flower_locations_table($path, $csv, \%flower_keys, \%property_keys, \%garden_keys); | ||
| 63 | $self -> populate_features_table($path, $csv, \%feature_keys); | ||
| 64 | $self -> populate_feature_locations_table($path, $csv, \%property_keys, \%garden_keys, \%feature_keys); | ||
| 65 | $self -> populate_attributes_table($path, $csv, \%attribute_type_keys, \%flower_keys); | ||
| 66 | $self -> populate_notes_table($path, $csv, \%flower_keys); | ||
| 67 | $self -> populate_images_table($path, $csv, \%flower_keys); | ||
| 68 | $self -> populate_urls_table($path, $csv, \%flower_keys); | ||
| 69 | |||
| 70 | # Return 0 for OK and 1 for error. | ||
| 71 | |||
| 72 | return 0; | ||
| 73 | |||
| 74 | } # End of populate_all_tables. | ||
| 75 | |||
| 76 | # ----------------------------------------------- | ||
| 77 | |||
| 78 | sub populate_attributes_table | ||
| 79 | { | ||
| 80 | my($self, $path, $csv, $attribute_type_keys, $flower_keys) = @_; | ||
| 81 | my($table_name) = 'attributes'; | ||
| 82 | $path =~ s/flowers/$table_name/; | ||
| 83 | |||
| 84 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 85 | |||
| 86 | $csv -> column_names($csv -> getline($io) ); | ||
| 87 | |||
| 88 | my($count) = 0; | ||
| 89 | |||
| 90 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 91 | { | ||
| 92 | $count++; | ||
| 93 | |||
| 94 | # Column names are tested in alphabetical order. | ||
| 95 | |||
| 96 | for my $column (qw/attribute_name common_name range/) | ||
| 97 | { | ||
| 98 | if (! defined $$item{$column}) | ||
| 99 | { | ||
| 100 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | if (! defined $$attribute_type_keys{$$item{attribute_name} }) | ||
| 105 | { | ||
| 106 | $self -> db -> logger -> error("$table_name. Row: $count. Attribute name '$$item{attribute_name}' undefined"); | ||
| 107 | } | ||
| 108 | |||
| 109 | if (! defined $$flower_keys{$$item{common_name} }) | ||
| 110 | { | ||
| 111 | $self -> db -> logger -> error("$table_name. Row: $count. Common name '$$item{common_name}' undefined"); | ||
| 112 | } | ||
| 113 | |||
| 114 | $self -> db -> insert_hashref | ||
| 115 | ( | ||
| 116 | $table_name, | ||
| 117 | { | ||
| 118 | attribute_type_id => $$attribute_type_keys{$$item{attribute_name} }, | ||
| 119 | flower_id => $$flower_keys{$$item{common_name} }, | ||
| 120 | range => $$item{range}, | ||
| 121 | } | ||
| 122 | ); | ||
| 123 | } | ||
| 124 | |||
| 125 | close $io; | ||
| 126 | |||
| 127 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 128 | |||
| 129 | } # End of populate_attributes_table. | ||
| 130 | |||
| 131 | # ----------------------------------------------- | ||
| 132 | |||
| 133 | sub populate_attribute_types_table | ||
| 134 | { | ||
| 135 | my($self, $path, $csv, $attribute_type_keys) = @_; | ||
| 136 | my($table_name) = 'attribute_types'; | ||
| 137 | $path =~ s/flowers/$table_name/; | ||
| 138 | |||
| 139 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 140 | |||
| 141 | $csv -> column_names($csv -> getline($io) ); | ||
| 142 | |||
| 143 | my($count) = 0; | ||
| 144 | |||
| 145 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 146 | { | ||
| 147 | $count++; | ||
| 148 | |||
| 149 | # Column names are tested in alphabetical order. | ||
| 150 | |||
| 151 | for my $column (qw/name range sequence/) | ||
| 152 | { | ||
| 153 | if (! defined $$item{$column}) | ||
| 154 | { | ||
| 155 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | $$attribute_type_keys{$$item{name} } = $self -> db -> insert_hashref | ||
| 160 | ( | ||
| 161 | $table_name, | ||
| 162 | { | ||
| 163 | name => $$item{name}, | ||
| 164 | range => $$item{range}, | ||
| 165 | sequence => $$item{sequence}, | ||
| 166 | } | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | |||
| 170 | close $io; | ||
| 171 | |||
| 172 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 173 | |||
| 174 | } # End of populate_attribute_types_table. | ||
| 175 | |||
| 176 | # ----------------------------------------------- | ||
| 177 | |||
| 178 | sub populate_constants_table | ||
| 179 | { | ||
| 180 | my($self, $path, $csv) = @_; | ||
| 181 | my($table_name) = 'constants'; | ||
| 182 | $path =~ s/flowers/$table_name/; | ||
| 183 | |||
| 184 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 185 | |||
| 186 | $csv -> column_names($csv -> getline($io) ); | ||
| 187 | |||
| 188 | my($count) = 0; | ||
| 189 | |||
| 190 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 191 | { | ||
| 192 | $count++; | ||
| 193 | |||
| 194 | # Column names are tested in alphabetical order. | ||
| 195 | |||
| 196 | for my $column (qw/name value/) | ||
| 197 | { | ||
| 198 | if (! defined $$item{$column}) | ||
| 199 | { | ||
| 200 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | $self -> db -> insert_hashref | ||
| 205 | ( | ||
| 206 | $table_name, | ||
| 207 | { | ||
| 208 | name => $$item{name}, | ||
| 209 | value => $$item{value}, | ||
| 210 | } | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | |||
| 214 | close $io; | ||
| 215 | |||
| 216 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 217 | |||
| 218 | } # End of populate_constants_table. | ||
| 219 | |||
| 220 | # ----------------------------------------------- | ||
| 221 | |||
| 222 | sub populate_feature_locations_table | ||
| 223 | { | ||
| 224 | my($self, $path, $csv, $property_keys, $garden_keys, $feature_keys) = @_; | ||
| 225 | my($table_name) = 'feature_locations'; | ||
| 226 | $path =~ s/flowers/$table_name/; | ||
| 227 | |||
| 228 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 229 | |||
| 230 | $csv -> column_names($csv -> getline($io) ); | ||
| 231 | |||
| 232 | my($crossref_locations) = $self -> crossref_locations; | ||
| 233 | my($count) = 0; | ||
| 234 | my($max_x) = 0; | ||
| 235 | my($max_y) = 0; | ||
| 236 | |||
| 237 | my($feature_name); | ||
| 238 | my($garden_name); | ||
| 239 | my($property_name); | ||
| 240 | my($tenant, $tenant_name, $type); | ||
| 241 | my(@xy, $x, $xy_pair); | ||
| 242 | my($y); | ||
| 243 | |||
| 244 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 245 | { | ||
| 246 | $count++; | ||
| 247 | |||
| 248 | # Column names are tested in alphabetical order. | ||
| 249 | |||
| 250 | for my $column (qw/property_name garden_name name xy/) | ||
| 251 | { | ||
| 252 | if (! defined $$item{$column}) | ||
| 253 | { | ||
| 254 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | next if (length($$item{xy}) == 0); | ||
| 259 | |||
| 260 | $feature_name = $$item{name}; | ||
| 261 | $garden_name = $$item{garden_name}; | ||
| 262 | $property_name = $$item{property_name}; | ||
| 263 | @xy = split(/\s/, $$item{xy}); | ||
| 264 | |||
| 265 | for my $i (0 .. $#xy) | ||
| 266 | { | ||
| 267 | $xy_pair = $xy[$i]; | ||
| 268 | ($x, $y) = split(/,/, $xy_pair); | ||
| 269 | $max_x = $x if ($x > $max_x); | ||
| 270 | $max_y = $y if ($y > $max_y); | ||
| 271 | |||
| 272 | if (defined $$crossref_locations{$xy_pair}) | ||
| 273 | { | ||
| 274 | $tenant = $$crossref_locations{$xy_pair}; | ||
| 275 | |||
| 276 | if ( ($property_name eq $$tenant{property_name}) && ($garden_name eq $$tenant{garden_name}) ) | ||
| 277 | { | ||
| 278 | $type = $$tenant{type}; | ||
| 279 | $tenant_name = ($type eq 'Flower') ? $$tenant{common_name} : $feature_name; | ||
| 280 | |||
| 281 | $self -> db -> logger -> error("$table_name. Row: $count. Property name: $property_name. " | ||
| 282 | . "Garden name: $garden_name. Feature '$feature_name'. " | ||
| 283 | . "Feature location '$xy_pair' is already in use by '$tenant_name'"); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | $$crossref_locations{$xy_pair} = | ||
| 288 | { | ||
| 289 | feature_name => $feature_name, | ||
| 290 | garden_name => $garden_name, | ||
| 291 | property_name => $property_name, | ||
| 292 | type => 'Feature', # 'Flower' is checked above. | ||
| 293 | }; | ||
| 294 | |||
| 295 | $self -> db -> insert_hashref | ||
| 296 | ( | ||
| 297 | $table_name, | ||
| 298 | { | ||
| 299 | feature_id => $$feature_keys{$feature_name}, | ||
| 300 | garden_id => $$garden_keys{$garden_name}, | ||
| 301 | property_id => $$property_keys{$property_name}, | ||
| 302 | x => $x, | ||
| 303 | y => $y, | ||
| 304 | } | ||
| 305 | ); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | close $io; | ||
| 310 | |||
| 311 | $self -> db -> logger -> info("Max (x, y) = ($max_x, $max_y)"); | ||
| 312 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 313 | |||
| 314 | } # End of populate_feature_locations_table. | ||
| 315 | |||
| 316 | # ----------------------------------------------- | ||
| 317 | |||
| 318 | sub populate_features_table | ||
| 319 | { | ||
| 320 | my($self, $path, $csv, $feature_keys) = @_; | ||
| 321 | my($table_name) = 'features'; | ||
| 322 | $path =~ s/flowers/$table_name/; | ||
| 323 | |||
| 324 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 325 | |||
| 326 | $csv -> column_names($csv -> getline($io) ); | ||
| 327 | |||
| 328 | my($count) = 0; | ||
| 329 | |||
| 330 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 331 | { | ||
| 332 | $count++; | ||
| 333 | |||
| 334 | # Column names are tested in alphabetical order. | ||
| 335 | |||
| 336 | for my $column (qw/hex_color name publish/) | ||
| 337 | { | ||
| 338 | if (! defined $$item{$column}) | ||
| 339 | { | ||
| 340 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | $$feature_keys{$$item{name} } = $self -> db -> insert_hashref | ||
| 345 | ( | ||
| 346 | $table_name, | ||
| 347 | { | ||
| 348 | hex_color => $$item{hex_color}, | ||
| 349 | name => $$item{name}, | ||
| 350 | publish => $$item{publish}, | ||
| 351 | } | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | |||
| 355 | close $io; | ||
| 356 | |||
| 357 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 358 | |||
| 359 | } # End of populate_features_table. | ||
| 360 | |||
| 361 | # ----------------------------------------------- | ||
| 362 | |||
| 363 | sub populate_flower_locations_table | ||
| 364 | { | ||
| 365 | my($self, $path, $csv, $flower_keys, $property_keys, $garden_keys) = @_; | ||
| 366 | my($table_name) = 'flower_locations'; | ||
| 367 | $path =~ s/flowers/$table_name/; | ||
| 368 | |||
| 369 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 370 | |||
| 371 | $csv -> column_names($csv -> getline($io) ); | ||
| 372 | |||
| 373 | my($count) = 0; | ||
| 374 | my($crossref_locations) = {}; | ||
| 375 | my($max_x) = 0; | ||
| 376 | my($max_y) = 0; | ||
| 377 | |||
| 378 | my($common_name); | ||
| 379 | my($garden_name); | ||
| 380 | my($property_name); | ||
| 381 | my($tenant); | ||
| 382 | my(@xy, $x, $xy_pair); | ||
| 383 | my($y); | ||
| 384 | |||
| 385 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 386 | { | ||
| 387 | $count++; | ||
| 388 | |||
| 389 | # Column names are tested in alphabetical order. | ||
| 390 | |||
| 391 | for my $column (qw/common_name property_name garden_name xy/) | ||
| 392 | { | ||
| 393 | if (! defined $$item{$column}) | ||
| 394 | { | ||
| 395 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | next if ( (! defined($$item{xy}) ) || (length($$item{xy}) == 0 )); | ||
| 400 | |||
| 401 | $common_name = $$item{common_name}; | ||
| 402 | $garden_name = $$item{garden_name}; | ||
| 403 | $property_name = $$item{property_name}; | ||
| 404 | |||
| 405 | if (! defined $$flower_keys{$$item{common_name} }) | ||
| 406 | { | ||
| 407 | $self -> db -> logger -> error("$table_name. Row: $count. Common name '$common_name' undefined"); | ||
| 408 | |||
| 409 | next; | ||
| 410 | } | ||
| 411 | |||
| 412 | if (! defined $$garden_keys{$garden_name}) | ||
| 413 | { | ||
| 414 | $self -> db -> logger -> error("$table_name. Row: $count. Garden '$garden_name' undefined"); | ||
| 415 | |||
| 416 | next; | ||
| 417 | } | ||
| 418 | |||
| 419 | if (! defined $$property_keys{$property_name}) | ||
| 420 | { | ||
| 421 | $self -> db -> logger -> error("$table_name. Row: $count. Property '$property_name' undefined"); | ||
| 422 | |||
| 423 | next; | ||
| 424 | } | ||
| 425 | |||
| 426 | @xy = split(/\s/, $$item{xy}); | ||
| 427 | |||
| 428 | for my $i (0 .. $#xy) | ||
| 429 | { | ||
| 430 | $xy_pair = $xy[$i]; | ||
| 431 | ($x, $y) = split(/,/, $xy_pair); | ||
| 432 | $max_x = $x if ($x > $max_x); | ||
| 433 | $max_y = $y if ($y > $max_y); | ||
| 434 | |||
| 435 | if (defined $$crossref_locations{$xy_pair}) | ||
| 436 | { | ||
| 437 | $tenant = $$crossref_locations{$xy_pair}; | ||
| 438 | |||
| 439 | # No need to check 'type' because we called this method before calling the code | ||
| 440 | # which reads in the feature locations. | ||
| 441 | |||
| 442 | if ( ($property_name eq $$tenant{property_name}) && ($garden_name eq $$tenant{garden_name}) ) | ||
| 443 | { | ||
| 444 | $self -> db -> logger -> error("$table_name. Row: $count. Property name: $property_name. " | ||
| 445 | . "Garden name: $garden_name. Common name '$common_name'. " | ||
| 446 | . "Flower location '$xy_pair' is already in use by '$$tenant{common_name}'"); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | $$crossref_locations{$xy_pair} = | ||
| 451 | { | ||
| 452 | common_name => $common_name, | ||
| 453 | garden_name => $garden_name, | ||
| 454 | property_name => $property_name, | ||
| 455 | type => 'Flower', # Or 'Feature'. These are checked later. | ||
| 456 | }; | ||
| 457 | |||
| 458 | $self -> db -> insert_hashref | ||
| 459 | ( | ||
| 460 | $table_name, | ||
| 461 | { | ||
| 462 | flower_id => $$flower_keys{$common_name}, | ||
| 463 | garden_id => $$garden_keys{$garden_name}, | ||
| 464 | property_id => $$property_keys{$property_name}, | ||
| 465 | x => $x, | ||
| 466 | y => $y, | ||
| 467 | } | ||
| 468 | ); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | close $io; | ||
| 473 | |||
| 474 | $self -> crossref_locations($crossref_locations); | ||
| 475 | |||
| 476 | $self -> db -> logger -> info("Max (x, y) = ($max_x, $max_y)"); | ||
| 477 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 478 | |||
| 479 | } # End of populate_flower_locations_table. | ||
| 480 | |||
| 481 | # ----------------------------------------------- | ||
| 482 | |||
| 483 | sub populate_flowers_table | ||
| 484 | { | ||
| 485 | my($self, $path, $csv, $flower_keys) = @_; | ||
| 486 | my($table_name) = 'flowers'; | ||
| 487 | |||
| 488 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 489 | |||
| 490 | $csv -> column_names($csv -> getline($io) ); | ||
| 491 | |||
| 492 | my($count) = 0; | ||
| 493 | my($lines) = $csv -> getline_hr_all($io); | ||
| 494 | |||
| 495 | # Stockpile some info we'll need. In particular we need %scientific_name | ||
| 496 | # so we can generate the pig_latin column in the flowers table. | ||
| 497 | |||
| 498 | my($common_name, %common_name); | ||
| 499 | my($max_height, $min_height, $max_width, $min_width); | ||
| 500 | my($scientific_name, %scientific_name); | ||
| 501 | |||
| 502 | for my $item (@$lines) | ||
| 503 | { | ||
| 504 | $count++; | ||
| 505 | |||
| 506 | # Column names are tested in alphabetical order. | ||
| 507 | |||
| 508 | for my $column (qw/aliases common_name height kind max_height max_width min_height min_width pig_latin planted publish scientific_name thumbnail width/) | ||
| 509 | { | ||
| 510 | if (! defined $$item{$column}) | ||
| 511 | { | ||
| 512 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 513 | } | ||
| 514 | elsif ( ($column !~ /height|width/) && (length $$item{$column} == 0) ) | ||
| 515 | { | ||
| 516 | $$item{$column} = '-'; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | $common_name = $$item{common_name}; | ||
| 521 | $scientific_name = $$item{scientific_name}; | ||
| 522 | |||
| 523 | if ($common_name{$common_name}) | ||
| 524 | { | ||
| 525 | $self -> db -> logger -> info("$table_name. Row: $count. Duplicate common_name: $common_name"); | ||
| 526 | } | ||
| 527 | |||
| 528 | if ($scientific_name{$scientific_name}) | ||
| 529 | { | ||
| 530 | $self -> db -> logger -> warn("$table_name. Row: $count. Duplicate scientific_name: $scientific_name"); | ||
| 531 | } | ||
| 532 | |||
| 533 | $common_name{$common_name} = 0 if (! $common_name{$common_name}); | ||
| 534 | $common_name{$common_name} += 1; | ||
| 535 | $scientific_name{$scientific_name} = 0 if (! $scientific_name{$scientific_name}); | ||
| 536 | $scientific_name{$scientific_name} += 1; | ||
| 537 | } | ||
| 538 | |||
| 539 | my($pig_latin); | ||
| 540 | |||
| 541 | for my $item (@$lines) | ||
| 542 | { | ||
| 543 | $common_name = $$item{common_name}; | ||
| 544 | $scientific_name = $$item{scientific_name}; | ||
| 545 | $pig_latin = $self -> db -> scientific_name2pig_latin($lines, $scientific_name, $common_name); | ||
| 546 | ($max_height, $min_height) = $self -> validate_size($table_name, $count, lc $self -> db -> trim($$item{height}), lc $self -> db -> trim($$item{height}) ); | ||
| 547 | ($max_width, $min_width) = $self -> validate_size($table_name, $count, lc $self -> db -> trim($$item{width}), lc $self -> db -> trim($$item{width}) ); | ||
| 548 | |||
| 549 | # qw/aliases common_name height kind max_height max_width min_height min_width pig_latin planted publish scientific_name thumbnail width/; | ||
| 550 | |||
| 551 | $$flower_keys{$common_name} = $self -> db -> insert_hashref | ||
| 552 | ( | ||
| 553 | $table_name, | ||
| 554 | { | ||
| 555 | aliases => $$item{aliases}, | ||
| 556 | common_name => $common_name, | ||
| 557 | height => $$item{height}, | ||
| 558 | kind => $$item{kind}, | ||
| 559 | max_height => $max_height, | ||
| 560 | min_height => $min_height, | ||
| 561 | max_width => $max_width, | ||
| 562 | min_width => $min_width, | ||
| 563 | pig_latin => $pig_latin, | ||
| 564 | planted => $$item{planted}, | ||
| 565 | publish => $$item{publish}, | ||
| 566 | scientific_name => $scientific_name, | ||
| 567 | thumbnail => $$item{thumbnail}, | ||
| 568 | width => $$item{width}, | ||
| 569 | } | ||
| 570 | ); | ||
| 571 | } | ||
| 572 | |||
| 573 | close $io; | ||
| 574 | |||
| 575 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 576 | |||
| 577 | } # End of populate_flowers_table. | ||
| 578 | |||
| 579 | # ----------------------------------------------- | ||
| 580 | |||
| 581 | sub populate_gardens_table | ||
| 582 | { | ||
| 583 | my($self, $path, $csv, $garden_keys, $property_keys) = @_; | ||
| 584 | my($table_name) = 'gardens'; | ||
| 585 | $path =~ s/flowers/$table_name/; | ||
| 586 | |||
| 587 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 588 | |||
| 589 | $csv -> column_names($csv -> getline($io) ); | ||
| 590 | |||
| 591 | my($count) = 0; | ||
| 592 | my($max_x) = 0; | ||
| 593 | my($max_y) = 0; | ||
| 594 | |||
| 595 | my(@xy, $x); | ||
| 596 | my($y); | ||
| 597 | |||
| 598 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 599 | { | ||
| 600 | $count++; | ||
| 601 | |||
| 602 | # Column names are tested in alphabetical order. | ||
| 603 | |||
| 604 | for my $column (qw/garden_name description property_name publish/) | ||
| 605 | { | ||
| 606 | if (! defined $$item{$column}) | ||
| 607 | { | ||
| 608 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | $$garden_keys{$$item{garden_name} } = $self -> db -> insert_hashref | ||
| 613 | ( | ||
| 614 | $table_name, | ||
| 615 | { | ||
| 616 | description => $$item{description}, | ||
| 617 | name => $$item{garden_name}, | ||
| 618 | property_id => $$property_keys{$$item{property_name} }, | ||
| 619 | publish => $$item{publish}, | ||
| 620 | } | ||
| 621 | ); | ||
| 622 | } | ||
| 623 | |||
| 624 | close $io; | ||
| 625 | |||
| 626 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 627 | |||
| 628 | } # End of populate_gardens_table. | ||
| 629 | |||
| 630 | # ----------------------------------------------- | ||
| 631 | |||
| 632 | sub populate_images_table | ||
| 633 | { | ||
| 634 | my($self, $path, $csv, $flower_keys) = @_; | ||
| 635 | my($table_name) = 'images'; | ||
| 636 | $path =~ s/flowers/$table_name/; | ||
| 637 | |||
| 638 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 639 | |||
| 640 | $csv -> column_names($csv -> getline($io) ); | ||
| 641 | |||
| 642 | my($count) = 0; | ||
| 643 | |||
| 644 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 645 | { | ||
| 646 | $count++; | ||
| 647 | |||
| 648 | # Column names are tested in alphabetical order. | ||
| 649 | |||
| 650 | for my $column (qw/common_name description file_name/) | ||
| 651 | { | ||
| 652 | if (! defined $$item{$column}) | ||
| 653 | { | ||
| 654 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | if (! defined $$flower_keys{$$item{common_name} }) | ||
| 659 | { | ||
| 660 | $self -> db -> logger -> error("$table_name. Row: $count. Common name '$$item{common_name}' undefined"); | ||
| 661 | |||
| 662 | next; | ||
| 663 | } | ||
| 664 | |||
| 665 | $self -> db -> insert_hashref | ||
| 666 | ( | ||
| 667 | $table_name, | ||
| 668 | { | ||
| 669 | flower_id => $$flower_keys{$$item{common_name} }, | ||
| 670 | description => $$item{description}, | ||
| 671 | file_name => $$item{file_name}, | ||
| 672 | } | ||
| 673 | ); | ||
| 674 | } | ||
| 675 | |||
| 676 | close $io; | ||
| 677 | |||
| 678 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 679 | |||
| 680 | } # End of populate_images_table. | ||
| 681 | |||
| 682 | # ----------------------------------------------- | ||
| 683 | |||
| 684 | sub populate_notes_table | ||
| 685 | { | ||
| 686 | my($self, $path, $csv, $flower_keys) = @_; | ||
| 687 | my($table_name) = 'notes'; | ||
| 688 | $path =~ s/flowers/$table_name/; | ||
| 689 | |||
| 690 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 691 | |||
| 692 | $csv -> column_names($csv -> getline($io) ); | ||
| 693 | |||
| 694 | my($count) = 0; | ||
| 695 | |||
| 696 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 697 | { | ||
| 698 | $count++; | ||
| 699 | |||
| 700 | # Column names are tested in alphabetical order. | ||
| 701 | |||
| 702 | for my $column (qw/common_name note/) | ||
| 703 | { | ||
| 704 | if (! defined $$item{$column}) | ||
| 705 | { | ||
| 706 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | if (! defined $$flower_keys{$$item{common_name} }) | ||
| 711 | { | ||
| 712 | $self -> db -> logger -> error("$table_name. Row: $count. Common name '$$item{common_name}' undefined"); | ||
| 713 | |||
| 714 | next; | ||
| 715 | } | ||
| 716 | |||
| 717 | $self -> db -> insert_hashref | ||
| 718 | ( | ||
| 719 | $table_name, | ||
| 720 | { | ||
| 721 | flower_id => $$flower_keys{$$item{common_name} }, | ||
| 722 | note => $$item{note}, | ||
| 723 | } | ||
| 724 | ); | ||
| 725 | } | ||
| 726 | |||
| 727 | close $io; | ||
| 728 | |||
| 729 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 730 | |||
| 731 | } # End of populate_notes_table. | ||
| 732 | |||
| 733 | # ----------------------------------------------- | ||
| 734 | |||
| 735 | sub populate_properties_table | ||
| 736 | { | ||
| 737 | my($self, $path, $csv, $property_keys) = @_; | ||
| 738 | my($table_name) = 'properties'; | ||
| 739 | $path =~ s/flowers/$table_name/; | ||
| 740 | |||
| 741 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 742 | |||
| 743 | $csv -> column_names($csv -> getline($io) ); | ||
| 744 | |||
| 745 | my($count) = 0; | ||
| 746 | my($max_x) = 0; | ||
| 747 | my($max_y) = 0; | ||
| 748 | |||
| 749 | my(@xy, $x); | ||
| 750 | my($y); | ||
| 751 | |||
| 752 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 753 | { | ||
| 754 | $count++; | ||
| 755 | |||
| 756 | # Column names are tested in alphabetical order. | ||
| 757 | |||
| 758 | for my $column (qw/description name/) | ||
| 759 | { | ||
| 760 | if (! defined $$item{$column}) | ||
| 761 | { | ||
| 762 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | $$property_keys{$$item{name} } = $self -> db -> insert_hashref | ||
| 767 | ( | ||
| 768 | $table_name, | ||
| 769 | { | ||
| 770 | description => $$item{description}, | ||
| 771 | name => $$item{name}, | ||
| 772 | publish => $$item{publish}, | ||
| 773 | } | ||
| 774 | ); | ||
| 775 | } | ||
| 776 | |||
| 777 | close $io; | ||
| 778 | |||
| 779 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 780 | |||
| 781 | } # End of populate_properties_table. | ||
| 782 | |||
| 783 | # ----------------------------------------------- | ||
| 784 | |||
| 785 | sub populate_urls_table | ||
| 786 | { | ||
| 787 | my($self, $path, $csv, $flower_keys) = @_; | ||
| 788 | my($table_name) = 'urls'; | ||
| 789 | $path =~ s/flowers/$table_name/; | ||
| 790 | |||
| 791 | open(my $io, '<', $path) || die "Can't open($path): $!\n"; | ||
| 792 | |||
| 793 | $csv -> column_names($csv -> getline($io) ); | ||
| 794 | |||
| 795 | my($count) = 0; | ||
| 796 | |||
| 797 | for my $item (@{$csv -> getline_hr_all($io) }) | ||
| 798 | { | ||
| 799 | $count++; | ||
| 800 | |||
| 801 | # Column names are tested in alphabetical order. | ||
| 802 | |||
| 803 | for my $column (qw/common_name url/) | ||
| 804 | { | ||
| 805 | if (! defined $$item{$column}) | ||
| 806 | { | ||
| 807 | $self -> db -> logger -> error("$table_name. Row: $count. Column $column undefined"); | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | if (! defined $$flower_keys{$$item{common_name} }) | ||
| 812 | { | ||
| 813 | $self -> db -> logger -> error("$table_name. Row: $count. Common name '$$item{common_name}' undefined"); | ||
| 814 | |||
| 815 | next; | ||
| 816 | } | ||
| 817 | |||
| 818 | $self -> db -> insert_hashref | ||
| 819 | ( | ||
| 820 | $table_name, | ||
| 821 | { | ||
| 822 | flower_id => $$flower_keys{$$item{common_name} }, | ||
| 823 | url => $$item{url}, | ||
| 824 | } | ||
| 825 | ); | ||
| 826 | } | ||
| 827 | |||
| 828 | close $io; | ||
| 829 | |||
| 830 | $self -> db -> logger -> info("Read $count records into '$table_name'"); | ||
| 831 | |||
| 832 | } # End of populate_urls_table. | ||
| 833 | |||
| 834 | # ----------------------------------------------- | ||
| 835 | |||
| 836 | sub validate_size | ||
| 837 | { | ||
| 838 | my($self, $table_name, $count, $value) = @_; | ||
| 839 | my($max_value) = ''; | ||
| 840 | my($min_value) = ''; | ||
| 841 | |||
| 842 | if ($value ne '') | ||
| 843 | { | ||
| 844 | my($unit) = ''; | ||
| 845 | |||
| 846 | if ($value =~ /^([0-9]{0,3}(?:[.][0-9]{0,2})?)\s*-\s*([0-9]{0,3}(?:[.][0-9]{0,2})?)\s*(cm|m)$/) | ||
| 847 | { | ||
| 848 | $max_value = $2; | ||
| 849 | $min_value = $1; | ||
| 850 | $unit = $3; | ||
| 851 | } | ||
| 852 | elsif ($value =~ /^([0-9]{0,3}(?:[.][0-9]{0,2})?)\s*(cm|m)$/) | ||
| 853 | { | ||
| 854 | $max_value = $1; | ||
| 855 | $min_value = $1; | ||
| 856 | $unit = $2; | ||
| 857 | } | ||
| 858 | else | ||
| 859 | { | ||
| 860 | $self -> db -> logger -> info("$table_name. Row: $count. Cannot interpret <$value> as height or width"); | ||
| 861 | } | ||
| 862 | |||
| 863 | if ($unit eq 'm') | ||
| 864 | { | ||
| 865 | # Format as whole cd, i. e. with no decimal places. | ||
| 866 | |||
| 867 | $max_value = sprintf('%.0f', $max_value * 100); | ||
| 868 | $min_value = sprintf('%.0f', $min_value * 100); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | return ($max_value, $min_value); | ||
| 873 | |||
| 874 | } # End of validate_size. | ||
| 875 | |||
| 876 | # ----------------------------------------------- | ||
| 877 | |||
| 878 | 1; | ||
| 879 | |||
| 880 | =pod | ||
| 881 | |||
| 882 | =head1 NAME | ||
| 883 | |||
| 884 | WWW::Garden::Design::Import - Manage the 'flowers' database | ||
| 885 | |||
| 886 | =head1 Machine-Readable Change Log | ||
| 887 | |||
| 888 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 889 | |||
| 890 | =head1 Version Numbers | ||
| 891 | |||
| 892 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 893 | |||
| 894 | =head1 Repository | ||
| 895 | |||
| 896 | L<https://github.com/ronsavage/WWW-Garden-Design> | ||
| 897 | |||
| 898 | =head1 Support | ||
| 899 | |||
| 900 | Email the author, or log a bug on RT: | ||
| 901 | |||
| 902 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>. | ||
| 903 | |||
| 904 | =head1 Author | ||
| 905 | |||
| 906 | L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014. | ||
| 907 | |||
| 908 | My homepage: L<https://savage.net.au/>. | ||
| 909 | |||
| 910 | =head1 Copyright | ||
| 911 | |||
| 912 | Australian copyright (c) 2018, Ron Savage. | ||
| 913 | |||
| 914 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 915 | you can redistribute them and/or modify them under the terms of | ||
| 916 | The Perl License, a copy of which is available at: | ||
| 917 | http://dev.perl.org/licenses/ | ||
| 918 | |||
| 919 | =cut |