From 02dc71055b68a9437117871863d0e82a1eff99e7 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 2 Mar 2009 20:20:43 +0000 Subject: [PATCH] Story code. Added validParent, storage location code. --- docs/upgrades/upgrade_7.6.14-7.7.0.pl | 1 + lib/WebGUI/Asset/Story.pm | 143 +++++++++++++++++++++++--- t/Asset/Story.t | 78 ++++++++++++++ 3 files changed, 209 insertions(+), 13 deletions(-) create mode 100644 t/Asset/Story.t diff --git a/docs/upgrades/upgrade_7.6.14-7.7.0.pl b/docs/upgrades/upgrade_7.6.14-7.7.0.pl index b9aabe998..11c3a06e0 100644 --- a/docs/upgrades/upgrade_7.6.14-7.7.0.pl +++ b/docs/upgrades/upgrade_7.6.14-7.7.0.pl @@ -60,6 +60,7 @@ CREATE TABLE Story ( location CHAR(255), highlights TEXT, story MEDIUMTEXT, + storageId CHAR(255), photo LONGTEXT, PRIMARY KEY ( assetId, revisionDate ) ) diff --git a/lib/WebGUI/Asset/Story.pm b/lib/WebGUI/Asset/Story.pm index 490143a85..0ba85ef33 100644 --- a/lib/WebGUI/Asset/Story.pm +++ b/lib/WebGUI/Asset/Story.pm @@ -40,8 +40,6 @@ These methods are available from this class: =cut - - #------------------------------------------------------------------- =head2 addChild ( ) @@ -58,16 +56,26 @@ sub addChild { =head2 addRevision -Make sure that Stories are always hidden from navigation. +Make sure that Stories are always hidden from navigation. Copy storage locations so that +purging individual revisions works correctly. =cut sub addRevision { my $self = shift; my $newSelf = $self->next::method(@_); - $newSelf->update({ + + my $newProperties = { isHidden => 1, - }); + }; + + if ($newSelf->get("storageId") && $newSelf->get("storageId") eq $self->get('storageId')) { + my $newStorage = $self->getStorageClass->get($self->session,$self->get("storageId"))->copy; + $newProperties->{storageId} = $newStorage->getId; + } + + $newSelf->update($newProperties); + return $newSelf; } @@ -134,7 +142,12 @@ sub definition { photo => { fieldType => 'text', defaultValue => '', - } + }, + storageId => { + fieldType => 'hidden', + defaultValue => '', + noFormPost => 1, + }, ); push(@{$definition}, { assetName => $i18n->get('assetName'), @@ -150,18 +163,37 @@ sub definition { #------------------------------------------------------------------- -=head2 indexContent ( ) +=head2 exportAssetData ( ) -Making private. See WebGUI::Asset::indexContent() for additonal details. +See WebGUI::AssetPackage::exportAssetData() for details. +Add the storage location to the export data. =cut -sub indexContent { - my $self = shift; - my $indexer = $self->SUPER::indexContent; - $indexer->setIsPublic(0); +sub exportAssetData { + my $self = shift; + my $data = $self->SUPER::exportAssetData; + push(@{$data->{storage}}, $self->get("storageId")) if ($self->get("storageId") ne ""); + return $data; } +#------------------------------------------------------------------- + +=head2 getStorageLocation + +Returns the storage location for this Story. If it does not exist, +then it creates it via setStorageLocation. Subsequent lookups return +an internally cached Storage object to save time. + +=cut + +sub getStorageLocation { + my $self = shift; + unless (exists $self->{_storageLocation}) { + $self->setStorageLocation; + } + return $self->{_storageLocation}; +} #------------------------------------------------------------------- @@ -200,22 +232,107 @@ sub processPropertiesFromFormPost { =head2 purge ( ) -Cleaning up storage objects. +Cleaning up storage objects in all revisions. =cut sub purge { my $self = shift; + my $sth = $self->session->db->read("select storageId from Story where assetId=".$self->session->db->quote($self->getId)); + while (my ($storageId) = $sth->array) { + WebGUI::Storage->get($self->session,$storageId)->delete; + } + $sth->finish; return $self->SUPER::purge; } #------------------------------------------------------------------- + +=head2 purgeRevision + +Remove the storage location for this revision of the Asset. + +=cut + +sub purgeRevision { + my $self = shift; + $self->getStorageLocation->delete; + return $self->SUPER::purgeRevision; +} + +#------------------------------------------------------------------- + +=head2 setSize ( fileSize ) + +Set the size of this asset by including all the files in its storage +location. C is an integer of additional bytes to include in +the asset size. + +=cut + +sub setSize { + my $self = shift; + my $fileSize = shift || 0; + my $storage = $self->getStorageLocation; + if (defined $storage) { + foreach my $file (@{$storage->getFiles}) { + $fileSize += $storage->getFileSize($file); + } + } + return $self->SUPER::setSize($fileSize); +} + +#------------------------------------------------------------------- + +=head2 setStorageLocation ( [ $storage] ) + +=head3 $storage + +A storage location to use for this Story. + +=cut + +sub setStorageLocation { + my $self = shift; + my $storage = shift; + if (defined $storage) { + $self->{_storageLocation} = $storage; + } + elsif ($self->get("storageId") eq "") { + $self->{_storageLocation} = WebGUI::Storage->create($self->session); + $self->update({storageId=>$self->{_storageLocation}->getId}); + } + else { + $self->{_storageLocation} = WebGUI::Storage->get($self->session,$self->get("storageId")); + } +} + +#------------------------------------------------------------------- + +=head2 validParent + +Make sure that the current session asset is a StoryArchive for pasting and adding checks. + +This is a class method. + +=cut + +sub validParent { + my $class = shift; + my $session = shift; + return $session->asset && $session->asset->isa('WebGUI::Asset::Wobject::StoryArchive'); +} + +#------------------------------------------------------------------- + =head2 view ( ) method called by the container www_view method. =cut +##Keyword cloud generated by WebGUI::Keyword + sub view { my $self = shift; my $var = $self->get; # $var is a hash reference. diff --git a/t/Asset/Story.t b/t/Asset/Story.t new file mode 100644 index 000000000..7a6f956f1 --- /dev/null +++ b/t/Asset/Story.t @@ -0,0 +1,78 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 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"; + +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Storage; + +use Test::More; # increment this value for each test you create +use Test::Deep; + +my $tests = 1; +plan tests => 4 + + $tests + ; + +#TODO: This script tests certain aspects of WebGUI::Storage and it should not + +my $session = WebGUI::Test->session; + +my $class = 'WebGUI::Asset::Story'; +my $loaded = use_ok($class); +my $story; + +my $defaultNode = WebGUI::Asset->getDefault($session); +my $archive = WebGUI::Asset->newByPropertyHashRef($session, { + className => 'WebGUI::Asset::Wobject::StoryArchive', + title => 'Test Archive', + #1234567890123456789012 + assetId => 'TestStoryArchiveAsset1', +}); + +SKIP: { + +skip "Unable to load module $class", $tests unless $loaded; + +############################################################ +# +# validParent +# +############################################################ + +ok(! WebGUI::Asset::Story->validParent($session), 'validParent: no session asset'); +$session->asset($defaultNode); +ok(! WebGUI::Asset::Story->validParent($session), 'validParent: wrong type of asset'); +$session->asset($archive); +ok( WebGUI::Asset::Story->validParent($session), 'validParent: StoryArchive is valid'); + +############################################################ +# +# make a new one +# +############################################################ + +$story = $defaultNode->addChild({ + className => 'WebGUI::Asset::Story', + title => 'Story 1', +}); + +isa_ok($story, 'WebGUI::Asset::Story', 'Created a Story asset'); + +} + +END { + WebGUI::VersionTag->getWorking($session)->rollback; + $story->purge if $story; + $archive->purge if $archive; +}