From f684e728c8de14af085e38ebd138724cbc2eeee5 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Fri, 5 Oct 2007 00:27:14 +0000 Subject: [PATCH] fix: Shortcuts now follow their linked asset when trashing, purging, and restoring from trash --- lib/WebGUI/Asset.pm | 10 ++++ lib/WebGUI/Asset/Shortcut.pm | 39 +++++++++++++++ lib/WebGUI/AssetTrash.pm | 57 ++++++++++++++------- t/Asset/Shortcut/000-create-delete.t | 74 ++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 18 deletions(-) create mode 100644 t/Asset/Shortcut/000-create-delete.t diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 52ae49154..ff19ac9c2 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1976,6 +1976,16 @@ sub publish { $cache->deleteChunk(["asset",$id]); } $self->{_properties}{state} = "published"; + + # Also publish any shortcuts to this asset that are in the trash + my $shortcuts + = WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, { + returnObjects => 1, + statesToInclude => ['trash','trash-limbo'], + }); + for my $shortcut ( @$shortcuts ) { + $shortcut->publish; + } } diff --git a/lib/WebGUI/Asset/Shortcut.pm b/lib/WebGUI/Asset/Shortcut.pm index 4513698b1..06132567b 100644 --- a/lib/WebGUI/Asset/Shortcut.pm +++ b/lib/WebGUI/Asset/Shortcut.pm @@ -11,6 +11,7 @@ package WebGUI::Asset::Shortcut; #------------------------------------------------------------------- use strict; +use Carp; use Tie::IxHash; use WebGUI::Asset; use WebGUI::International; @@ -906,5 +907,43 @@ sub www_view { return $output; } +#---------------------------------------------------------------------------- + +=head1 STATIC METHODS + +These methods are called using CLASS->method + +#---------------------------------------------------------------------------- + +=head2 getShortcutsForAssetId ( session, assetId [, properties] ) + +Get an arrayref of assetIds of all the shortcuts for the passed-in assetId. + +"properties" is a hash reference of properties to give to getLineage. +Probably the only useful key will be "returnObjects". + +=cut + +sub getShortcutsForAssetId { + my $class = shift; + my $session = shift; + my $assetId = shift; + my $properties = shift || {}; + + croak "First argument to getShortcutsForAssetId must be WebGUI::Session" + unless $session && $session->isa("WebGUI::Session"); + croak "Second argument to getShortcutsForAssetId must be assetId" + unless $assetId; + croak "Third argument to getShortcutsForAssetId must be hash reference" + if $properties && !ref $properties eq "HASH"; + + my $db = $session->db; + + $properties->{ joinClass } = 'WebGUI::Asset::Shortcut'; + $properties->{ whereClause } = 'Shortcut.shortcutToAssetId = ' . $db->quote($assetId); + + return WebGUI::Asset->getRoot($session)->getLineage(['descendants'], $properties); +} + 1; diff --git a/lib/WebGUI/AssetTrash.pm b/lib/WebGUI/AssetTrash.pm index e13ce1280..457d7968a 100644 --- a/lib/WebGUI/AssetTrash.pm +++ b/lib/WebGUI/AssetTrash.pm @@ -134,6 +134,16 @@ sub purge { } } + # Delete shortcuts to this asset + # Also publish any shortcuts to this asset that are in the trash + my $shortcuts + = WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, { + returnObjects => 1, + }); + for my $shortcut ( @$shortcuts ) { + $shortcut->purge; + } + # gotta delete stuff we've exported unless ($options->{skipExported}) { $self->_invokeWorkflowOnExportedFiles($self->session->setting->get('purgeWorkflow'), 1); @@ -178,29 +188,40 @@ sub purge { =head2 trash ( ) -Removes asset from lineage, places it in trash state. The "gap" in the lineage is changed in state to trash-limbo. +Removes asset from lineage, places it in trash state. The "gap" in the +lineage is changed in state to trash-limbo. =cut sub trash { - my $self = shift; - return undef if ($self->getId eq $self->session->setting->get("defaultPage") || $self->getId eq $self->session->setting->get("notFoundPage")); - foreach my $asset ($self, @{$self->getLineage(['descendants'], {returnObjects => 1})}) { - $asset->_invokeWorkflowOnExportedFiles($self->session->setting->get('trashWorkflow'), 1); - } + my $self = shift; + return undef if ($self->getId eq $self->session->setting->get("defaultPage") || $self->getId eq $self->session->setting->get("notFoundPage")); + for my $asset ($self, @{$self->getLineage(['descendants'], {returnObjects => 1})}) { + $asset->_invokeWorkflowOnExportedFiles($self->session->setting->get('trashWorkflow'), 1); + } - my $db = $self->session->db; - $db->beginTransaction; - my $sth = $db->read("select assetId from asset where lineage like ?",[$self->get("lineage").'%']); - while (my ($id) = $sth->array) { - $db->write("delete from assetIndex where assetId=?",[$id]); - } - $db->write("update asset set state='trash-limbo' where lineage like ?",[$self->get("lineage").'%']); - $db->write("update asset set state='trash', stateChangedBy=?, stateChanged=? where assetId=?",[$self->session->user->userId, $self->session->datetime->time(), $self->getId]); - $db->commit; - $self->{_properties}{state} = "trash"; - $self->updateHistory("trashed"); - $self->purgeCache; + # Trash any shortcuts to this asset + my $shortcuts + = WebGUI::Asset::Shortcut->getShortcutsForAssetId($self->session, $self->getId, { returnObjects => 1}); + for my $shortcut ( @$shortcuts ) { + $shortcut->trash; + } + + # Raw database work is more efficient than $asset->update + my $db = $self->session->db; + $db->beginTransaction; + my $sth = $db->read("select assetId from asset where lineage like ?",[$self->get("lineage").'%']); + while (my ($id) = $sth->array) { + $db->write("delete from assetIndex where assetId=?",[$id]); + } + $db->write("update asset set state='trash-limbo' where lineage like ?",[$self->get("lineage").'%']); + $db->write("update asset set state='trash', stateChangedBy=?, stateChanged=? where assetId=?",[$self->session->user->userId, $self->session->datetime->time(), $self->getId]); + $db->commit; + + # Update ourselves since we didn't use update() + $self->{_properties}{state} = "trash"; + $self->updateHistory("trashed"); + $self->purgeCache; } require WebGUI::Workflow::Activity::DeleteExportedFiles; diff --git a/t/Asset/Shortcut/000-create-delete.t b/t/Asset/Shortcut/000-create-delete.t new file mode 100644 index 000000000..01051d23e --- /dev/null +++ b/t/Asset/Shortcut/000-create-delete.t @@ -0,0 +1,74 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2007 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; + +## The goal of this test is to test the creation and deletion of shortcut assets + +use WebGUI::Test; +use WebGUI::Session; +use Test::More; +use WebGUI::Asset::Snippet; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +my $node = WebGUI::Asset->getImportNode($session); +my $versionTag = WebGUI::VersionTag->getWorking($session); +$versionTag->set({name=>"Shortcut Test"}); + +# Make a snippet to shortcut +my $snippet + = $node->addChild({ + className => "WebGUI::Asset::Snippet", + }); + +#---------------------------------------------------------------------------- +# Cleanup +END { + $versionTag->rollback(); +} + +#---------------------------------------------------------------------------- +# Tests +plan tests => 2; + +#---------------------------------------------------------------------------- +# Test module compiles okay +# plan tests => 0 +BEGIN { use_ok("WebGUI::Asset::Shortcut"); } + +#---------------------------------------------------------------------------- +# Test creating a shortcut to snippet +# plan tests => 2 +my $shortcut + = $node->addChild({ + className => "WebGUI::Asset::Shortcut", + shortcutToAssetId => $snippet->getId, + }); + +isa_ok( + $shortcut, "WebGUI::Asset::Shortcut", +); + +isa_ok( + $shortcut, "WebGUI::Asset", +); + +#---------------------------------------------------------------------------- +# Test deleting a shortcut +# plan tests => +TODO: { + local $TODO = "Test deleting a shortcut."; +} + +