+=head3 $object->get_from_storage;
+
+=cut
+
+sub get_from_storage {
+ my ( $self, $attrs ) = @_;
+ my $stored_object = $self->_result->get_from_storage($attrs);
+ return unless $stored_object;
+ my $object_class = Koha::Object::_get_object_class( $self->_result->result_class );
+ return $object_class->_new_from_dbic($stored_object);
+}
+
+=head3 $object->messages
+
+ my @messages = @{ $object->messages };
+
+Returns the (probably non-fatal) messages that were recorded on the object.
+
+=cut
+
+sub messages {
+ my ( $self ) = @_;
+
+ $self->{_messages} = []
+ unless defined $self->{_messages};
+
+ return $self->{_messages};
+}
+
+=head3 $object->add_message
+
+ try {
+ <some action that might fail>
+ }
+ catch {
+ if ( <fatal condition> ) {
+ Koha::Exception->throw...
+ }
+
+ # This is a non fatal error, notify the caller
+ $self->add_message({ message => $error, type => 'error' });
+ }
+ return $self;
+
+Adds a message.
+
+=cut
+
+sub add_message {
+ my ( $self, $params ) = @_;
+
+ push @{ $self->{_messages} }, Koha::Object::Message->new($params);
+
+ return $self;
+}
+
+=head3 $object->TO_JSON
+
+Returns an unblessed representation of the object, suitable for JSON output.
+
+=cut
+
+sub TO_JSON {
+
+ my ($self) = @_;
+
+ my $unblessed = $self->unblessed;
+ my $columns_info = Koha::Database->new->schema->resultset( $self->_type )
+ ->result_source->{_columns};
+
+ foreach my $col ( keys %{$columns_info} ) {
+
+ if ( $columns_info->{$col}->{is_boolean} )
+ { # Handle booleans gracefully
+ $unblessed->{$col}
+ = ( $unblessed->{$col} )
+ ? Mojo::JSON->true
+ : Mojo::JSON->false;
+ }
+ elsif ( _numeric_column_type( $columns_info->{$col}->{data_type} )
+ and looks_like_number( $unblessed->{$col} )
+ ) {
+
+ # TODO: Remove once the solution for
+ # https://github.com/perl5-dbi/DBD-mysql/issues/212
+ # is ported to whatever distro we support by that time
+ # or we move to DBD::MariaDB
+ $unblessed->{$col} += 0;
+ }
+ elsif ( _decimal_column_type( $columns_info->{$col}->{data_type} )
+ and looks_like_number( $unblessed->{$col} )
+ ) {
+
+ # TODO: Remove once the solution for
+ # https://github.com/perl5-dbi/DBD-mysql/issues/212
+ # is ported to whatever distro we support by that time
+ # or we move to DBD::MariaDB
+ $unblessed->{$col} += 0.00;
+ }
+ elsif ( _datetime_column_type( $columns_info->{$col}->{data_type} ) ) {
+ eval {
+ return unless $unblessed->{$col};
+ $unblessed->{$col} = output_pref({
+ dateformat => 'rfc3339',
+ dt => dt_from_string($unblessed->{$col}, 'sql'),
+ });
+ };
+ }
+ }
+ return $unblessed;
+}
+
+sub _date_or_datetime_column_type {
+ my ($column_type) = @_;
+
+ my @dt_types = (
+ 'timestamp',
+ 'date',
+ 'datetime'
+ );
+
+ return ( grep { $column_type eq $_ } @dt_types) ? 1 : 0;
+}
+sub _datetime_column_type {
+ my ($column_type) = @_;
+
+ my @dt_types = (
+ 'timestamp',
+ 'datetime'
+ );
+
+ return ( grep { $column_type eq $_ } @dt_types) ? 1 : 0;
+}
+
+sub _numeric_column_type {
+ # TODO: Remove once the solution for
+ # https://github.com/perl5-dbi/DBD-mysql/issues/212
+ # is ported to whatever distro we support by that time
+ # or we move to DBD::MariaDB
+ my ($column_type) = @_;
+
+ my @numeric_types = (
+ 'bigint',
+ 'integer',
+ 'int',
+ 'mediumint',
+ 'smallint',
+ 'tinyint',
+ );
+
+ return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
+}
+
+sub _decimal_column_type {
+ # TODO: Remove once the solution for
+ # https://github.com/perl5-dbi/DBD-mysql/issues/212
+ # is ported to whatever distro we support by that time
+ # or we move to DBD::MariaDB
+ my ($column_type) = @_;
+
+ my @decimal_types = (
+ 'decimal',
+ 'double precision',
+ 'float'
+ );
+
+ return ( grep { $column_type eq $_ } @decimal_types) ? 1 : 0;
+}
+
+=head3 prefetch_whitelist
+
+ my $whitelist = $object->prefetch_whitelist()
+
+Returns a hash of prefetchable subs and the type they return.
+
+=cut
+
+sub prefetch_whitelist {
+ my ( $self ) = @_;
+
+ my $whitelist = {};
+ my $relations = $self->_result->result_source->_relationships;
+
+ foreach my $key (keys %{$relations}) {
+ if($self->can($key)) {
+ my $result_class = $relations->{$key}->{class};
+ my $obj = $result_class->new;
+ try {
+ $whitelist->{$key} = Koha::Object::_get_object_class( $obj->result_class );
+ } catch {
+ $whitelist->{$key} = undef;
+ }
+ }
+ }
+
+ return $whitelist;
+}
+
+=head3 to_api
+
+ my $object_for_api = $object->to_api(
+ {
+ [ embed => {
+ items => {
+ children => {
+ holds => {,
+ children => {
+ ...
+ }
+ }
+ }
+ },
+ library => {
+ ...
+ }
+ },
+ ...
+ ]
+ }
+ );
+
+Returns a representation of the object, suitable for API output.
+
+=cut
+
+sub to_api {
+ my ( $self, $params ) = @_;
+ my $json_object = $self->TO_JSON;
+
+ my $to_api_mapping = $self->to_api_mapping;
+
+ # Rename attributes if there's a mapping
+ if ( $self->can('to_api_mapping') ) {
+ foreach my $column ( keys %{ $self->to_api_mapping } ) {
+ my $mapped_column = $self->to_api_mapping->{$column};
+ if ( exists $json_object->{$column}
+ && defined $mapped_column )
+ {
+ # key != undef
+ $json_object->{$mapped_column} = delete $json_object->{$column};
+ }
+ elsif ( exists $json_object->{$column}
+ && !defined $mapped_column )
+ {
+ # key == undef
+ delete $json_object->{$column};
+ }
+ }
+ }
+
+ my $embeds = $params->{embed};
+
+ if ($embeds) {
+ foreach my $embed ( keys %{$embeds} ) {
+ if ( $embed =~ m/^(?<relation>.*)_count$/
+ and $embeds->{$embed}->{is_count} ) {
+
+ my $relation = $+{relation};
+ $json_object->{$embed} = $self->$relation->count;
+ }
+ else {
+ my $curr = $embed;
+ my $next = $embeds->{$curr}->{children};
+
+ my $children = $self->$curr;
+
+ if ( defined $children and ref($children) eq 'ARRAY' ) {
+ my @list = map {
+ $self->_handle_to_api_child(
+ { child => $_, next => $next, curr => $curr } )
+ } @{$children};
+ $json_object->{$curr} = \@list;
+ }
+ else {
+ $json_object->{$curr} = $self->_handle_to_api_child(
+ { child => $children, next => $next, curr => $curr } );
+ }
+ }
+ }
+ }
+
+
+
+ return $json_object;
+}
+
+=head3 to_api_mapping
+
+ my $mapping = $object->to_api_mapping;
+
+Generic method that returns the attribute name mappings required to
+render the object on the API.
+
+Note: this only returns an empty I<hashref>. Each class should have its
+own mapping returned.
+
+=cut
+
+sub to_api_mapping {
+ return {};
+}
+
+=head3 from_api_mapping
+
+ my $mapping = $object->from_api_mapping;
+
+Generic method that returns the attribute name mappings so the data that
+comes from the API is correctly renamed to match what is required for the DB.
+
+=cut
+
+sub from_api_mapping {
+ my ( $self ) = @_;
+
+ my $to_api_mapping = $self->to_api_mapping;
+
+ unless ( $self->{_from_api_mapping} ) {
+ while (my ($key, $value) = each %{ $to_api_mapping } ) {
+ $self->{_from_api_mapping}->{$value} = $key
+ if defined $value;
+ }
+ }
+
+ return $self->{_from_api_mapping};
+}
+
+=head3 new_from_api
+
+ my $object = Koha::Object->new_from_api;
+ my $object = Koha::Object->new_from_api( $attrs );
+
+Creates a new object, mapping the API attribute names to the ones on the DB schema.
+
+=cut
+
+sub new_from_api {
+ my ( $class, $params ) = @_;
+
+ my $self = $class->new;
+ return $self->set_from_api( $params );
+}
+
+=head3 set_from_api
+
+ my $object = Koha::Object->new(...);
+ $object->set_from_api( $attrs )
+
+Sets the object's attributes mapping API attribute names to the ones on the DB schema.
+
+=cut
+
+sub set_from_api {
+ my ( $self, $from_api_params ) = @_;
+
+ return $self->set( $self->attributes_from_api( $from_api_params ) );
+}
+
+=head3 attributes_from_api
+
+ my $attributes = attributes_from_api( $params );
+
+Returns the passed params, converted from API naming into the model.
+
+=cut
+
+sub attributes_from_api {
+ my ( $self, $from_api_params ) = @_;
+
+ my $from_api_mapping = $self->from_api_mapping;
+
+ my $params;
+ my $columns_info = $self->_result->result_source->columns_info;
+
+ while (my ($key, $value) = each %{ $from_api_params } ) {
+ my $koha_field_name =
+ exists $from_api_mapping->{$key}
+ ? $from_api_mapping->{$key}
+ : $key;
+
+ if ( $columns_info->{$koha_field_name}->{is_boolean} ) {
+ # TODO: Remove when D8 is formally deprecated
+ # Handle booleans gracefully
+ $value = ( $value ) ? 1 : 0;
+ }
+ elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
+ try {
+ $value = dt_from_string($value, 'rfc3339');
+ }
+ catch {
+ Koha::Exceptions::BadParameter->throw( parameter => $key );
+ };
+ }
+
+ $params->{$koha_field_name} = $value;
+ }
+
+ return $params;
+}
+
+=head3 $object->unblessed_all_relateds
+
+my $everything_into_one_hashref = $object->unblessed_all_relateds
+
+The unblessed method only retrieves column' values for the column of the object.
+In a *few* cases we want to retrieve the information of all the prefetched data.
+
+=cut
+
+sub unblessed_all_relateds {
+ my ($self) = @_;
+
+ my %data;
+ my $related_resultsets = $self->_result->{related_resultsets} || {};
+ my $rs = $self->_result;
+ while ( $related_resultsets and %$related_resultsets ) {
+ my @relations = keys %{ $related_resultsets };
+ if ( @relations ) {
+ my $relation = $relations[0];
+ $rs = $rs->related_resultset($relation)->get_cache;
+ $rs = $rs->[0]; # Does it makes sense to have several values here?
+ my $object_class = Koha::Object::_get_object_class( $rs->result_class );
+ my $koha_object = $object_class->_new_from_dbic( $rs );
+ $related_resultsets = $rs->{related_resultsets};
+ %data = ( %data, %{ $koha_object->unblessed } );
+ }
+ }
+ %data = ( %data, %{ $self->unblessed } );
+ return \%data;
+}
+