| 1 | package NOLookup::Brreg::Entry; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw(Class::Accessor::Chained); ## provides a new() method | ||
| 6 | use Encode; | ||
| 7 | use POSIX qw(locale_h); | ||
| 8 | |||
| 9 | use Data::Dumper; | ||
| 10 | $Data::Dumper::Indent=1; | ||
| 11 | |||
| 12 | # module methods | ||
| 13 | my @module_methods = ( | ||
| 14 | |||
| 15 | qw / | ||
| 16 | status | ||
| 17 | error | ||
| 18 | warning | ||
| 19 | |||
| 20 | map_json_entries | ||
| 21 | |||
| 22 | next_page | ||
| 23 | cur_page | ||
| 24 | prev_page | ||
| 25 | total_page_count | ||
| 26 | |||
| 27 | result_count | ||
| 28 | total_result_count | ||
| 29 | |||
| 30 | /); | ||
| 31 | |||
| 32 | |||
| 33 | # Brreg entry name | ||
| 34 | my @json_data_methods = ( | ||
| 35 | |||
| 36 | # number and name | ||
| 37 | qw / | ||
| 38 | organisasjonsnummer | ||
| 39 | navn | ||
| 40 | oppdateringsid | ||
| 41 | /, | ||
| 42 | |||
| 43 | # dates | ||
| 44 | qw / | ||
| 45 | registreringsdatoEnhetsregisteret | ||
| 46 | stiftelsesdato | ||
| 47 | sisteInnsendteAarsregnskap | ||
| 48 | slettedato | ||
| 49 | oppstartsdato | ||
| 50 | dato | ||
| 51 | datoEierskifte | ||
| 52 | /, | ||
| 53 | |||
| 54 | qw / | ||
| 55 | overordnetEnhet | ||
| 56 | maalform | ||
| 57 | /, | ||
| 58 | |||
| 59 | # BOOL | ||
| 60 | qw / | ||
| 61 | registrertIStiftelsesregisteret | ||
| 62 | registrertIFrivillighetsregisteret | ||
| 63 | registrertIMvaregisteret | ||
| 64 | registrertIForetaksregisteret | ||
| 65 | underAvvikling | ||
| 66 | konkurs | ||
| 67 | underTvangsavviklingEllerTvangsopplosning | ||
| 68 | /, | ||
| 69 | |||
| 70 | # hashes with their own extra keys | ||
| 71 | qw / | ||
| 72 | forretningsadresse | ||
| 73 | beliggenhetsadresse | ||
| 74 | postadresse | ||
| 75 | institusjonellSektorkode | ||
| 76 | naeringskode1 | ||
| 77 | naeringskode2 | ||
| 78 | naeringskode3 | ||
| 79 | _links | ||
| 80 | page | ||
| 81 | organisasjonsform | ||
| 82 | /, | ||
| 83 | |||
| 84 | # web page, number of employed | ||
| 85 | qw / | ||
| 86 | hjemmeside | ||
| 87 | antallAnsatte | ||
| 88 | /, | ||
| 89 | |||
| 90 | # Arrays | ||
| 91 | qw / | ||
| 92 | frivilligMvaRegistrertBeskrivelser | ||
| 93 | / | ||
| 94 | |||
| 95 | ); | ||
| 96 | |||
| 97 | # The new method and also the accessor methods | ||
| 98 | __PACKAGE__->mk_accessors( | ||
| 99 | @module_methods, | ||
| 100 | @json_data_methods | ||
| 101 | ); | ||
| 102 | |||
| 103 | sub map_json_entries { | ||
| 104 | my ($self, $json) = @_; | ||
| 105 | |||
| 106 | return unless ($json); | ||
| 107 | |||
| 108 | my @aj; | ||
| 109 | my @ao; | ||
| 110 | |||
| 111 | $self->status(""); | ||
| 112 | if ($json->{navn}) { | ||
| 113 | # single entry on the data array | ||
| 114 | push @aj, $json; | ||
| 115 | |||
| 116 | } elsif ($json->{_embedded} && $json->{_embedded}->{enheter}) { | ||
| 117 | # multiple entries | ||
| 118 | push @aj, @{$json->{_embedded}->{enheter}}; | ||
| 119 | |||
| 120 | } elsif ($json->{_embedded} && $json->{_embedded}->{underenheter}) { | ||
| 121 | # multiple entries | ||
| 122 | push @aj, @{$json->{_embedded}->{underenheter}}; | ||
| 123 | |||
| 124 | } elsif ($json->{_embedded} && $json->{_embedded}->{oppdaterteEnheter}) { | ||
| 125 | # multiple updated entries | ||
| 126 | push @aj, @{$json->{_embedded}->{oppdaterteEnheter}}; | ||
| 127 | |||
| 128 | } elsif ($json->{_embedded} && $json->{_embedded}->{oppdaterteUnderenheter}) { | ||
| 129 | # multiple updated entries | ||
| 130 | push @aj, @{$json->{_embedded}->{oppdaterteUnderenheter}}; | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | # count the current posts | ||
| 135 | $self->result_count(scalar @aj); | ||
| 136 | |||
| 137 | #print STDERR "Entry self: ", Dumper $self; | ||
| 138 | |||
| 139 | if ($json->{page}) { | ||
| 140 | # page data | ||
| 141 | #print STDERR "page entry: ", Dumper $json->{page}; | ||
| 142 | |||
| 143 | $self->total_result_count($json->{page}->{totalElements}); | ||
| 144 | $self->cur_page($json->{page}->{number}); | ||
| 145 | $self->total_page_count($json->{page}->{totalPages}); | ||
| 146 | |||
| 147 | $self->prev_page(undef); | ||
| 148 | $self->next_page(undef); | ||
| 149 | |||
| 150 | if ($self->cur_page > 0) { | ||
| 151 | $self->prev_page($self->cur_page - 1); | ||
| 152 | } | ||
| 153 | if ($self->total_page_count > $self->cur_page) { | ||
| 154 | $self->next_page($self->cur_page + 1); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | # Mape found orgs into NOLookup::Brreg::Entry objects | ||
| 159 | foreach my $ej (@aj) { | ||
| 160 | foreach my $k (keys %$ej) { | ||
| 161 | # Check entry array for supported keys | ||
| 162 | # set a warning if some are not expected | ||
| 163 | unless ($self->can($k)) { | ||
| 164 | $self->status($self->status . "JSON data key entry not expected: $k\n"); | ||
| 165 | $self->warning(1); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | my $eo = NOLookup::Brreg::Entry->new($ej); | ||
| 169 | push @ao, $eo; | ||
| 170 | } | ||
| 171 | |||
| 172 | # return a ref to the data array | ||
| 173 | return \@ao; | ||
| 174 | } | ||
| 175 | |||
| 176 | =pod | ||
| 177 | |||
| 178 | =encoding ISO-8859-1 | ||
| 179 | |||
| 180 | =head1 NAME | ||
| 181 | |||
| 182 | NOLookup::Brreg::Entry - Map a Brreg json data structure element | ||
| 183 | to Brreg::Entry data objects. | ||
| 184 | |||
| 185 | =head1 DESCRIPTION | ||
| 186 | |||
| 187 | Map the json data to data objects. | ||
| 188 | |||
| 189 | Return a reference to an array of NOLookup::Brreg::Entry objects. | ||
| 190 | |||
| 191 | =head1 METHODS | ||
| 192 | |||
| 193 | =head2 module methods | ||
| 194 | |||
| 195 | The methods provided by this module. | ||
| 196 | |||
| 197 | =head3 error() | ||
| 198 | |||
| 199 | Set if an error has occured. | ||
| 200 | On errors, the lookup terminates. | ||
| 201 | |||
| 202 | =head3 warning() | ||
| 203 | |||
| 204 | Set if a warning has occured. | ||
| 205 | On warnings, the lookup continues. | ||
| 206 | |||
| 207 | =head3 status() | ||
| 208 | |||
| 209 | Further description of an error/warning situation. | ||
| 210 | |||
| 211 | If the returned JSON structure contains an unsupported data element, | ||
| 212 | the lookup will be performed, but a warning may be returned and | ||
| 213 | and with a status saying: | ||
| 214 | |||
| 215 | 'Warning: JSON data key entry not expected: xxxx' | ||
| 216 | |||
| 217 | where 'xxxx' is the unexpected key. If this happens, the module | ||
| 218 | should be updated with a new method to support the element 'xxx'. | ||
| 219 | |||
| 220 | =head3 map_json_entries() | ||
| 221 | |||
| 222 | Map the JSON data structure from Brreg to NOLookup::Brreg::Entry | ||
| 223 | data objects. | ||
| 224 | |||
| 225 | Returns a ref. to an array of NOLookup::Brreg::Entry data objects. | ||
| 226 | |||
| 227 | =head3 cur_page()/prev_page()/next_page() | ||
| 228 | |||
| 229 | If more pages can be fetched, those methods give the URL to that page | ||
| 230 | |||
| 231 | =head3 total_page_count() | ||
| 232 | |||
| 233 | Count of total nuber of pages matching the search | ||
| 234 | |||
| 235 | =head3 result_count() | ||
| 236 | |||
| 237 | Count of results on this page | ||
| 238 | |||
| 239 | =head3 total_result_count() | ||
| 240 | |||
| 241 | Count of total results for the search | ||
| 242 | |||
| 243 | |||
| 244 | =head2 Accessor methods | ||
| 245 | |||
| 246 | Data elements are available through acessors in the NOLookup::Brreg::Entry object. | ||
| 247 | This is the possible JSON data methods, which are the accessor | ||
| 248 | methods that can be used to find the returned data elements. | ||
| 249 | |||
| 250 | The accessor methods from @json_data_methods | ||
| 251 | |||
| 252 | organisasjonsnummer | ||
| 253 | navn | ||
| 254 | oppdateringsid | ||
| 255 | registreringsdatoEnhetsregisteret | ||
| 256 | stiftelsesdato | ||
| 257 | oppstartsdato | ||
| 258 | sisteInnsendteAarsregnskap | ||
| 259 | * organisasjonsform | ||
| 260 | overordnetEnhet | ||
| 261 | maalform | ||
| 262 | registrertIStiftelsesregisteret | ||
| 263 | registrertIFrivillighetsregisteret | ||
| 264 | registrertIMvaregisteret | ||
| 265 | registrertIForetaksregisteret | ||
| 266 | frivilligMvaRegistrertBeskrivelser | ||
| 267 | |||
| 268 | underAvvikling | ||
| 269 | konkurs | ||
| 270 | underTvangsavviklingEllerTvangsopplosning | ||
| 271 | * forretningsadresse | ||
| 272 | * postadresse | ||
| 273 | * institusjonellSektorkode | ||
| 274 | * naeringskode1 | ||
| 275 | * naeringskode2 | ||
| 276 | * naeringskode3 | ||
| 277 | * _links | ||
| 278 | * page | ||
| 279 | hjemmeside | ||
| 280 | antallAnsatte | ||
| 281 | |||
| 282 | Returned values: | ||
| 283 | |||
| 284 | Most of the accessor methods returns a single value, like 'navn', | ||
| 285 | which returns a scalar with the name of the organization. | ||
| 286 | |||
| 287 | Some of the methods returns a hash, and are marked with an asterix (*). | ||
| 288 | Hash data must be accessed via their respective keys. | ||
| 289 | |||
| 290 | The hashes are described below. | ||
| 291 | |||
| 292 | =head3 organisasjonsform() | ||
| 293 | |||
| 294 | The hash looks like follows: | ||
| 295 | |||
| 296 | "organisasjonsform": { | ||
| 297 | "kode": "AS", | ||
| 298 | "beskrivelse": "Aksjeselskap", | ||
| 299 | "_links": { | ||
| 300 | "self": { | ||
| 301 | "href": "https://data.brreg.no/enhetsregisteret/api/organisasjonsformer/AS" | ||
| 302 | } | ||
| 303 | } | ||
| 304 | }, | ||
| 305 | |||
| 306 | |||
| 307 | =head3 forretningsadresse() / postadresse() | ||
| 308 | |||
| 309 | The hash looks like follows: | ||
| 310 | |||
| 311 | "forretningsadresse": { | ||
| 312 | "land": "Norge", | ||
| 313 | "landkode": "NO", | ||
| 314 | "postnummer": "7030", | ||
| 315 | "poststed": "TRONDHEIM", | ||
| 316 | "adresse": [ | ||
| 317 | "Abels gate 5" | ||
| 318 | ], | ||
| 319 | "kommune": "TRONDHEIM", | ||
| 320 | "kommunenummer": "5001" | ||
| 321 | }, | ||
| 322 | |||
| 323 | =head3 institusjonellSektorkode() | ||
| 324 | |||
| 325 | The hash looks like follows: | ||
| 326 | |||
| 327 | 'institusjonellSektorkode' => { | ||
| 328 | 'beskrivelse' => 'Statlig eide aksjeselskaper mv.', | ||
| 329 | 'kode' => '1120' | ||
| 330 | } | ||
| 331 | |||
| 332 | =head3 naeringskode1/2/3() | ||
| 333 | |||
| 334 | The hash looks like follows: | ||
| 335 | |||
| 336 | 'naeringskode1' => { | ||
| 337 | 'beskrivelse' => "Utvinning av r\x{e5}olje", | ||
| 338 | 'kode' => '06.100' | ||
| 339 | }, | ||
| 340 | |||
| 341 | =head3 frivilligMvaRegistrertBeskrivelser() | ||
| 342 | |||
| 343 | The hash looks like follows: | ||
| 344 | |||
| 345 | 'frivilligMvaRegistrertBeskrivelser' => [ | ||
| 346 | 'Utleier av bygg eller anlegg' | ||
| 347 | ], | ||
| 348 | |||
| 349 | |||
| 350 | =head3 _links() | ||
| 351 | |||
| 352 | The hash looks like follows: | ||
| 353 | |||
| 354 | "_links": { | ||
| 355 | "self": { | ||
| 356 | "href": "https://data.brreg.no/enhetsregisteret/api/enheter/985821585" | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | =head1 SUPPORT | ||
| 362 | |||
| 363 | For now, support questions should be sent to: | ||
| 364 | |||
| 365 | E<lt>(nospam)info(at)norid.noE<gt> | ||
| 366 | |||
| 367 | Please also see the SUPPORT file in the distribution. | ||
| 368 | |||
| 369 | =head1 SEE ALSO | ||
| 370 | |||
| 371 | C<NOLookup::Brreg::DataLookup> | ||
| 372 | |||
| 373 | =head1 AUTHOR | ||
| 374 | |||
| 375 | Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt> | ||
| 376 | |||
| 377 | =head1 COPYRIGHT | ||
| 378 | |||
| 379 | Copyright (c) 2017- Trond Haugen <(nospam)info(at)norid.no>. | ||
| 380 | All rights reserved. | ||
| 381 | |||
| 382 | This program is free software; you can redistribute it and/or modify | ||
| 383 | it under the terms of the GNU General Public License as published by | ||
| 384 | the Free Software Foundation; either version 2 of the License, or | ||
| 385 | (at your option) any later version. | ||
| 386 | |||
| 387 | =head1 LICENSE | ||
| 388 | |||
| 389 | This library is free software. You can redistribute it and/or modify | ||
| 390 | it under the same terms as Perl itself. | ||
| 391 | |||
| 392 | =cut | ||
| 393 | |||
| 394 | 1; |