From b72e3a1cd17c46e514e6d18c3764f8180ccb7083 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 4 Jan 2010 11:33:09 -0800 Subject: [PATCH] getClassById encapculates getting a className from the database, indexed by assetId. Also, work on newById, newByUrl. --- lib/WebGUI/Asset.pm | 85 +++++++++++++++++++++++++++++---------------- t/Asset.t | 18 +++++++++- 2 files changed, 72 insertions(+), 31 deletions(-) diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 4f2811352..e9ff0f051 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -754,6 +754,47 @@ sub getAdminConsole { } +#------------------------------------------------------------------- + +=head2 getClassById ( $session, $assetId ) + +Class method that looks up a className for an object in the database, using it's assetId. + +=head3 $session + +A WebGUI::Session object. + +=head3 $assetId + +The assetId of the object to lookup in the database. + +=cut + +sub getClassById { + my $class = shift; + my $session = shift; + my $assetId = shift; + # Cache the className lookup + my $assetClass = $session->stow->get("assetClass"); + my $className = $assetClass->{$assetId}; + + return $className if $className; + + $className = $session->db->quickScalar( + "select className from asset where assetId=?", + [$assetId] + ); + $assetClass->{ $assetId } = $className; + $session->stow->set("assetClass", $assetClass); + + return $className if $className; + + $session->errorHandler->error("Couldn't find className for asset '$assetId'"); + return undef; + +} + + #------------------------------------------------------------------- =head2 getContainer ( ) @@ -1661,7 +1702,7 @@ A specific revision date for the asset to retrieve. If not specified, the most r =cut sub newById { - my $class = shift; + my $requestedClass = shift; my $session = shift; my $assetId = shift; my $revisionDate = shift; @@ -1675,26 +1716,10 @@ sub newById { return undef unless ( $session && blessed $session eq 'WebGUI::Session' ) && $assetId; - # Cache the className lookup - my $assetClass = $session->stow->get("assetClass"); - my $className = $assetClass->{$assetId}; - - unless ($className) { - $className - = $session->db->quickScalar( - "select className from asset where assetId=?", - [$assetId] - ); - $assetClass->{ $assetId } = $className; - $session->stow->set("assetClass", $assetClass); - } - - unless ( $className ) { - $session->errorHandler->error("Couldn't find className for asset '$assetId'"); - return undef; - } - - return $className->new($session, $assetId, $revisionDate); + my $className = WebGUI::Asset->getClassById($session, $assetId); + my $class = WebGUI::Asset->loadModule($session, $className); + return undef unless $class; + return $class->new($session, $assetId, $revisionDate); } @@ -1753,20 +1778,20 @@ A specific revision to instanciate. By default we instanciate the newest publish =cut sub newByUrl { - my $class = shift; - my $session = shift; - my $url = shift || $session->url->getRequestedUrl; + my $class = shift; + my $session = shift; + my $url = shift || $session->url->getRequestedUrl; my $revisionDate = shift; - $url = lc($url); + $url = lc($url); $url =~ s/\/$//; $url =~ s/^\///; - $url =~ s/\'//; - $url =~ s/\"//; + $url =~ tr/'"//d; if ($url ne "") { - my ($id, $class) = $session->db->quickArray("select asset.assetId, asset.className from assetData join asset using (assetId) where assetData.url = ? limit 1", [ $url ]); + my ($id) = $session->db->quickArray("select assetId from assetData where url = ? limit 1", [ $url ]); if ($id ne "" || $class ne "") { - return WebGUI::Asset->new($session,$id, $class, $revisionDate); - } else { + return WebGUI::Asset->newById($session, $id, $revisionDate); + } + else { $session->errorHandler->warn("The URL $url was requested, but does not exist in your asset tree."); return undef; } diff --git a/t/Asset.t b/t/Asset.t index 4d0234a89..3569e22df 100644 --- a/t/Asset.t +++ b/t/Asset.t @@ -20,12 +20,13 @@ use Test::More; use Test::Deep; use Test::Exception; -plan tests => 22; +plan tests => 26; my $session = WebGUI::Test->session; { + note "session and title"; my $asset = WebGUI::Asset->new({session => $session, }); isa_ok $asset, 'WebGUI::Asset'; @@ -55,6 +56,7 @@ my $session = WebGUI::Test->session; { + note "menuTitle"; my $asset = WebGUI::Asset->new({ session => $session, title => 'asset title', @@ -82,6 +84,7 @@ my $session = WebGUI::Test->session; } { + note "Class dispatch"; my $asset = WebGUI::Asset->new({ session => $session, title => 'testing snippet', @@ -98,3 +101,16 @@ my $session = WebGUI::Test->session; isa_ok $asset, 'WebGUI::Asset::Snippet'; } + +{ + note "getClassById"; + my $class; + $class = WebGUI::Asset->getClassById($session, 'PBasset000000000000001'); + is $class, 'WebGUI::Asset', 'getClassById: retrieve a class'; + $class = WebGUI::Asset->getClassById($session, 'PBasset000000000000001'); + is $class, 'WebGUI::Asset', '... cache check'; + $class = WebGUI::Asset->getClassById($session, 'PBasset000000000000002'); + is $class, 'WebGUI::Asset::Wobject::Folder', '... retrieve another class'; + $class = WebGUI::Asset->getClassById($session, 'noIdHereBoss'); + is $class, undef, '... returns undef if the class cannot be found'; +}