From 236928ec3ff1bbd7b9d4594c25e1cbdde57a5bf0 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Tue, 14 Jul 2015 15:01:04 +0000 Subject: [PATCH] Bug 14539 - Introduction to castToObject(), aka. make a Koha::Object from various input types Finds a Borrower-object from various different types of inputs. Currently Koha is like an archeological dig site, we have different layers of dealing with various business objects. We started with DBI and numerous ways of passing an HASH around. There is no telling if it will be a List of column => values, or a reference to HASH, or a HASH or just any of the business object's (eg. Borrower's) unique identifiers (userid, cardnumber, borrowernumber). Then DBIx came to the rescue and now we are need to learn DBI and DBIx and SQL to do DB operatons in Koha. Migration to DBIx is on the way. Finally we have Koha::Object and subclasses, which include and use the DBIx, but those are not directly compatible, since Koha::Object is not a subclass of DBIx::Class making life occasionally miserable. Now we need to know 3 methods of DB accession. I am really frustrated with all of those different layers of history, and making things work nicely across all different programming patterns, I have had great success in using a casting system, where we take any value and try to make a Koha::Object-subclass out of it. So we try to cast a Scalar or a reference of Koha::Object-implementation or DBIx::ResultSet or HASH, to the desired Koha::Object-implementation. This is a nice validation/entry function in any subroutine dealing with business objects, making sure that we always have the "correct" implementation of the same business object. --- Koha/Borrowers.pm | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Koha/Borrowers.pm b/Koha/Borrowers.pm index 9fb01f0..78a8067 100644 --- a/Koha/Borrowers.pm +++ b/Koha/Borrowers.pm @@ -18,6 +18,7 @@ package Koha::Borrowers; # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use Modern::Perl; +use Scalar::Util qw(blessed); use Carp; @@ -49,6 +50,85 @@ sub object_class { return 'Koha::Borrower'; } +=head castToBorrower + + my $borrower = Koha::Borrowers::castToBorrower('cardnumber'); + my $borrower = Koha::Borrowers::castToBorrower($Koha::Borrower); + my $borrower = Koha::Borrowers::castToBorrower('userid'); + my $borrower = Koha::Borrowers::castToBorrower('borrowernumber'); + my $borrower = Koha::Borrowers::castToBorrower({borrowernumber => 123, + }); + my $borrower = Koha::Borrowers::castToBorrower({firstname => 'Olli-Antti', + surname => 'Kivi', + address => 'Koskikatu 25', + }); + +Because there are gazillion million ways in Koha to invoke a Borrower, this is a +omnibus for easily creating a Borrower-object from all the arcane invocations present +in many parts of Koha. +Just throw the crazy and unpredictable return values from myriad subroutines returning +some kind of an borrowerish value to this casting function to get a brand new Koha::Borrower. +@PARAM1 Scalar, or HASHRef. +@RETURNS Koha::Borrower, possibly already in DB or a completely new one if nothing was + inferred from the DB. +@THROWS Koha::Exception::BadParameter, if no idea what to do with the input. +=cut + +sub castToBorrower { + my ($input) = @_; + + unless ($input) { + Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> No parameter given!"); + } + if (blessed($input) && $input->isa('Koha::Borrower')) { + return $input; + } + if (blessed($input) && $input->isa('Koha::Schema::Result::Borrower')) { + return Koha::Borrower->_new_from_dbic($input); + } + + my ($borrowernumber, $cardnumber, $userid, $borrower); + #Extract unique keys and try to get the borrower from them. + if (ref($input) eq 'HASH') { + $borrowernumber = $input->{borrowernumber}; + $cardnumber = $input->{cardnumber}; + $userid = $input->{userid}; + } + elsif (not(ref($input))) { #We have a scalar + $borrowernumber = $input; + $cardnumber = $input; + $userid = $input; + } + if ($borrowernumber || $cardnumber || $userid) { + $borrower = Koha::Borrowers->search({'-or' => [{borrowernumber => $borrowernumber}, + {cardnumber => $cardnumber}, + {userid => $userid}, + ] + })->next(); + unless ($borrower) { + Koha::Exception::UnknownObject->throw(error => "Koha::Borrower::castToBorrower->new():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'."); + } + } + +# ##If we have a borrower now, overwrite it with given hash keys +# #if not, try to make one if we have a hash as input. +# if (ref($input) eq 'HASH') { +# $borrower = Koha::Borrower->new() unless $borrower; +# my $columns = $borrower->_columns(); +# my $setHash = {}; +# foreach my $columnName (@$columns) { +# $setHash->{$columnName} = $input->{$columnName}; +# } +# Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> Given HASH contains no Borrower column parameters, cannot build a Borrower out of them.") +# unless scalar($setHash); +# $borrower->set($setHash); +# } +# +# unless ($borrower) { +# Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'."); +# } +} + =head1 AUTHOR Kyle M Hall -- 1.9.1