Added a new method getLineageSql() which is called the same was that getLineage is called but returns the SQL that is generated by the getLineage call rather than and arrayRef of assetIds or instantiated assets. This method is now called by getLineage.

This commit is contained in:
Frank Dillon 2008-09-05 17:47:56 +00:00
parent 7dd946a78b
commit 102fb221f9

View file

@ -300,6 +300,10 @@ An array reference containing a list of asset classes to remove from the result
A boolean indicating that we should return objects rather than asset ids.
=head4 returnSQL
A boolean indicating that we should return the sql statement rather than asset ids.
=head4 invertTree
A boolean indicating whether the resulting asset tree should be returned in reverse order.
@ -340,6 +344,136 @@ sub getLineage {
my $relatives = shift;
my $rules = shift;
my $lineage = $self->get("lineage");
my $sql = $self->getLineageSql($relatives,$rules);
my @lineage;
my %relativeCache;
my $sth = $self->session->db->read($sql);
while (my ($id, $class, $parentId, $version) = $sth->array) {
# create whatever type of object was requested
my $asset;
if ($rules->{returnObjects}) {
if ($self->getId eq $id) { # possibly save ourselves a hit to the database
$asset = $self;
} else {
$asset = WebGUI::Asset->new($self->session,$id, $class, $version);
if (!defined $asset) { # won't catch everything, but it will help some if an asset blows up
$self->session->errorHandler->error("AssetLineage::getLineage - failed to instanciate asset with assetId $id, className $class, and revision $version");
next;
}
}
} else {
$asset = $id;
}
# since we have the relatives info now, why not cache it
if ($rules->{returnObjects}) {
my $parent = $relativeCache{$parentId};
$relativeCache{$id} = $asset;
$asset->{_parent} = $parent if exists $relativeCache{$parentId};
$parent->{_firstChild} = $asset unless(exists $parent->{_firstChild});
$parent->{_lastChild} = $asset;
}
push(@lineage,$asset);
}
$sth->finish;
return \@lineage;
}
#-------------------------------------------------------------------
=head2 getLineageLength ( )
Returns the number of Asset members in an Asset's lineage.
=cut
sub getLineageLength {
my $self = shift;
return length($self->get("lineage"))/6;
}
#-------------------------------------------------------------------
=head2 getLineageSql ( relatives,rules )
Returns the sql statment for lineage based on relatives and rules passed in
=head3 relatives
An array reference of relatives to retrieve. Valid parameters are "siblings", "children", "ancestors", "self", "descendants", "pedigree". If you want to retrieve all assets in the tree, use getRoot($session)->getLineage(["self","descendants"],{returnObjects=>1});
=head3 rules
A hash reference comprising modifiers to relative listing. Rules include:
=head4 statesToInclude
An array reference containing a list of states that should be returned. Defaults to 'published'. Options include
'published', 'trash', 'clipboard', 'clipboard-limbo' and 'trash-limbo'.
=head4 statusToInclude
An array reference containing a list of status that should be returned. Defaults to 'approved'. Options include 'approved', 'pending', and 'archived'.
=head4 endingLineageLength
An integer limiting the length of the lineages of the assets to be returned. This can help limit levels of depth in the asset tree.
=head4 assetToPedigree
An asset object reference to draw a pedigree from. A pedigree includes ancestors, siblings, descendants and other information. It's specifically used in flexing navigations.
=head4 ancestorLimit
An integer describing how many levels of ancestry from the start point that should be retrieved.
=head4 excludeClasses
An array reference containing a list of asset classes to remove from the result set. The opposite of the includOnlyClasses rule.
=head4 invertTree
A boolean indicating whether the resulting asset tree should be returned in reverse order.
=head4 includeOnlyClasses
An array reference containing a list of asset classes to include in the result. If this is specified then no other classes except these will be returned. The opposite of the excludeClasses rule.
=head4 isa
A classname where you can look for classes of a similar base class. For example, if you're looking for Donations, Subscriptions, Products and other subclasses of WebGUI::Asset::Sku, then set isa to 'WebGUI::Asset::Sku'.
=head4 includeArchived
A boolean indicating that we should include archived assets in the result set.
=head4 joinClass
A string containing as asset class to join in. There is no real reason to use a joinClass without a whereClause, but it's trivial to use a whereClause if you don't use a joinClass. You will only be able to filter on the asset table, however.
=head4 whereClause
A string containing extra where clause information for the query.
=head4 orderByClause
A string containing an order by clause (without the "order by"). If specified,
will override the "invertTree" option.
=head4 limit
The maximum amount of entries to return
=cut
sub getLineageSql {
my $self = shift;
my $relatives = shift;
my $rules = shift;
my $lineage = $self->get("lineage");
my @whereModifiers;
# let's get those siblings
if (isIn("siblings",@{$relatives})) {
@ -466,53 +600,9 @@ sub getLineage {
if ($rules->{limit}) {
$sql .= " limit ".$rules->{limit};
}
return $sql;
my @lineage;
my %relativeCache;
my $sth = $self->session->db->read($sql);
while (my ($id, $class, $parentId, $version) = $sth->array) {
# create whatever type of object was requested
my $asset;
if ($rules->{returnObjects}) {
if ($self->getId eq $id) { # possibly save ourselves a hit to the database
$asset = $self;
} else {
$asset = WebGUI::Asset->new($self->session,$id, $class, $version);
if (!defined $asset) { # won't catch everything, but it will help some if an asset blows up
$self->session->errorHandler->error("AssetLineage::getLineage - failed to instanciate asset with assetId $id, className $class, and revision $version");
next;
}
}
} else {
$asset = $id;
}
# since we have the relatives info now, why not cache it
if ($rules->{returnObjects}) {
my $parent = $relativeCache{$parentId};
$relativeCache{$id} = $asset;
$asset->{_parent} = $parent if exists $relativeCache{$parentId};
$parent->{_firstChild} = $asset unless(exists $parent->{_firstChild});
$parent->{_lastChild} = $asset;
}
push(@lineage,$asset);
}
$sth->finish;
return \@lineage;
}
#-------------------------------------------------------------------
=head2 getLineageLength ( )
Returns the number of Asset members in an Asset's lineage.
=cut
sub getLineageLength {
my $self = shift;
return length($self->get("lineage"))/6;
}