initial release of Asset Report asset allowing reports to be created using asset properties.

This commit is contained in:
khenn 2010-08-10 18:53:22 -05:00
parent cb2bb75d23
commit 22da7c4114
10 changed files with 1405 additions and 0 deletions

View file

@ -0,0 +1,297 @@
package WebGUI::Asset::Wobject::AssetReport;
$VERSION = "1.0.0";
#-------------------------------------------------------------------
# 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 strict;
use Tie::IxHash;
use WebGUI::International;
use WebGUI::Paginator;
use WebGUI::Utility;
use Class::C3;
use base qw/WebGUI::AssetAspect::Installable WebGUI::Asset::Wobject/;
#-------------------------------------------------------------------
=head2 definition ( session, definition )
=cut
sub definition {
my $class = shift;
my $session = shift;
my $definition = shift;
my $i18n = WebGUI::International->new( $session, 'Asset_AssetReport' );
tie my %properties, 'Tie::IxHash', (
settings => {
tab => 'properties',
fieldType => 'AssetReportQuery',
defaultValue => undef,
},
templateId => {
tab => "display",
fieldType => "template",
namespace => "AssetReport",
defaultValue => "sJtcUCfn0CVbKdb4QM61Yw",
label => $i18n->get("templateId label"),
hoverHelp => $i18n->get("templateId description"),
},
paginateAfter => {
tab => 'display',
fieldType => 'integer',
defaultValue => 25,
label => $i18n->get( 'paginateAfter label' ),
hoverHelp => $i18n->get( 'paginateAfter description' ),
},
);
push @{$definition}, {
assetName => $i18n->get('assetName'),
autoGenerateForms => 1,
tableName => 'AssetReport',
className => 'WebGUI::Asset::Wobject::AssetReport',
properties => \%properties,
};
return $class->SUPER::definition( $session, $definition );
} ## end sub definition
#----------------------------------------------------------------------------
=head2 prepareView ( )
Prepare the view. Add stuff to HEAD.
=cut
sub prepareView {
my $self = shift;
$self->SUPER::prepareView(@_);
my $session = $self->session;
# Prepare the template
my $template = WebGUI::Asset::Template->new( $session, $self->get("templateId") );
if (!$template) {
WebGUI::Error::ObjectNotFound::Template->throw(
error => qq{Template not found},
templateId => $self->get("templateId"),
assetId => $self->getId,
);
}
$template->prepare;
$self->{_template} = $template;
return;
} ## end sub prepareView
#----------------------------------------------------------------------------
=head2 getTemplateVars ( )
Get template variables common to all views of the Asset Report.
=cut
sub getTemplateVars {
my $self = shift;
my $session = $self->session;
my $var = $self->get;
#Build the lineage query
my $settings = JSON->new->decode($self->getValue("settings"));
#TO DO - ADD CACHE CONTROL
my $assetId = $settings->{startNode};
my $asset = WebGUI::Asset->newByDynamicClass($session,$assetId);
my $rules = {};
$rules->{'isa'} = $settings->{className};
#Build where condition
my $condition = $settings->{anySelect};
$rules->{'whereClause'} = undef;
my $where = $settings->{where};
foreach my $key (keys %{$where}) {
my $clause = $where->{$key};
my $prop = $clause->{propSelect};
my $op = $clause->{opSelect};
my $value = $clause->{valText};
$rules->{'whereClause'} .= qq{ $condition } if ($key > 1);
$rules->{'whereClause'} .= qq{$prop $op $value};
}
if($rules->{'whereClause'}) {
$rules->{'joinClass'} = $settings->{className};
}
#Build the order by condition
my $order = $settings->{order};
my @order = keys %{$order};
if(scalar(@order)) {
$rules->{'orderByClause'} = undef;
foreach my $key (@order) {
my $orderBy = $order->{$key};
my $orderSelect = $orderBy->{orderSelect};
my $dirSelect = $orderBy->{dirSelect};
$rules->{'orderByClause'} .= q{, } if($key > 1);
$rules->{'orderByClause'} .= qq{$orderSelect $dirSelect};
}
}
if($settings->{'limit'}) {
$rules->{'limit'} = $settings->{'limit'};
}
my $sql = $asset->getLineageSql(["descendants"],$rules);
my $p = WebGUI::Paginator->new($session,$self->getUrl,$self->get("paginateAfter"));
$p->setDataByQuery($sql);
#Build the data for all the assets on the page
$var->{'asset_loop'} = [];
my $data = $p->getPageData;
foreach my $row (@{$data}) {
my $returnAsset = WebGUI::Asset->new($session,$row->{assetId},$row->{className},$row->{revisionDate});
push(@{$var->{'asset_loop'}},$returnAsset->get);
}
#Append template variables
$p->appendTemplateVars($var);
return $var;
}
#----------------------------------------------------------------------------
=head2 view ( )
method called by the www_view method. Returns a processed template
to be displayed within the page style.
=cut
sub view {
my $self = shift;
my $var = $self->getTemplateVars;
return $self->processTemplate( $var, undef, $self->{_template} );
} ## end sub view
#-------------------------------------------------------------------
# Install Methods Below - Do Not Modify
#-------------------------------------------------------------------
sub install {
my $class = shift;
my $session = shift;
$class->next::method( $session );
### Create a folder asset to store the default template
my $importNode = WebGUI::Asset->getImportNode($session);
my $folder = $importNode->addChild({
className => "WebGUI::Asset::Wobject::Folder",
title => "Asset Report",
menuTitle => "Asset Report",
url => "pb_asset_report",
groupIdView =>"3"
},"AssetReportFolder00001");
### Add the template to the folder
$folder->addChild({
className => "WebGUI::Asset::Template",
namespace => "AssetReport",
title => "Asset Report Default Template",
menuTitle => "Asset Report Default Template",
ownerUserId => "3",
groupIdView => "7",
groupIdEdit => "4",
isHidden => 1,
isDefault => 1,
template => qq{
<a name="id<tmpl_var assetId>" id="id<tmpl_var assetId>"></a>
<tmpl_if session.var.adminOn>
<p><tmpl_var controls></p>
</tmpl_if>
<tmpl_if displayTitle>
<h2><tmpl_var title></h2>
</tmpl_if>
<tmpl_if error_loop>
<ul class="errors">
<tmpl_loop error_loop>
<li><b><tmpl_var error.message></b></li>
</tmpl_loop>
</ul>
</tmpl_if>
<tmpl_if description>
<tmpl_var description>
<p />
</tmpl_if>
<table border="1" cellspacing="0" cellpadding="3">
<thead>
<tr>
<th>Title</th>
<th>Creation Date</th>
<th>Created By</th>
</tr>
</thead>
<tbody>
<tmpl_loop asset_loop>
<tr>
<td><a href="<tmpl_var url>"><tmpl_var title></a></td>
<td>^D('%C %D, %y %h:%s %p',<tmpl_var creationDate>);</td>
<td>^User('username',<tmpl_var createdBy>);</td>
</tr>
</tmpl_loop>
</tbody>
</table>
<tmpl_if pagination.pageCount.isMultiple>
<div class="pagination">
<tmpl_var pagination.previousPage> <tmpl_var pagination.pageList.upTo20> <tmpl_var pagination.nextPage>
</div>
</tmpl_if>
},
headBlock =>"",
}, "AssetReport00000000001");
### Commit version tag
my $tag = WebGUI::VersionTag->new($session, WebGUI::VersionTag->getWorking($session)->getId);
if (defined $tag) {
$tag->set({comments=>"Template added/updated by Asset Install Process"});
$tag->requestCommit;
}
}
#-------------------------------------------------------------------
sub uninstall {
my $class = shift;
my $session = shift;
$class->next::method( $session );
my $template = WebGUI::Asset->newByDynamicClass($session,"AssetReport00000000001");
$template->purge if($template);
my $folder = WebGUI::Asset->newByDynamicClass($session,"AssetReportFolder00001");
$folder->purge if($folder);
}
1;

View file

@ -0,0 +1,423 @@
package WebGUI::Form::AssetReportQuery;
use strict;
use base 'WebGUI::Form::Control';
use JSON;
use WebGUI::International;
use WebGUI::Utility;
=head1 NAME
WebGUI::Form::BooleanQuery -- Build a boolean query
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 METHODS
=cut
#-------------------------------------------------------------------
=head2 getDatabaseFieldType ( )
Returns "MEDIUMTEXT".
=cut
sub getDatabaseFieldType {
return "MEDIUMTEXT";
}
#----------------------------------------------------------------------------
=head2 getAnyList
Get the operator list.
=cut
sub getAnyList {
my $self = shift;
my $i18n = $self->i18n;
tie my %options, 'Tie::IxHash', (
'or' => $i18n->get("any option"),
'and' => $i18n->get("all option"),
);
return \%options;
}
#----------------------------------------------------------------------------
=head2 getDirs
Get the direction list.
=cut
sub getDirs {
my $self = shift;
my $i18n = $self->i18n;
tie my %options, 'Tie::IxHash', (
'asc' => $i18n->get("ascending option"),
'desc' => $i18n->get("descending option"),
);
return \%options;
}
#----------------------------------------------------------------------------
=head2 getOps
Get the operator list.
=cut
sub getOps {
my $self = shift;
tie my %options, 'Tie::IxHash', (
'=' => '=',
'<>' => '<>',
'>' => '>',
'>=' => '>=',
'<' => '<',
'<=' => '<=',
'LIKE' => 'LIKE',
'NOT LIKE' => 'NOT LIKE',
'IS NULL' => 'IS NULL',
'IS NOT NULL' => 'IS NOT NULL',
);
return \%options;
}
#----------------------------------------------------------------------------
=head2 getValue ()
Get the value of the form
=cut
sub getValue {
my $self = shift;
my $session = $self->session;
my $form = $session->form;
my $propCount = $form->process("propCount","hidden");
my $orderCount = $form->process("orderCount","hidden");
if($propCount) {
my $where = {};
my $whereCount = 1;
for(my $i = 0; $i < $propCount; $i++ ) {
my $propSelect = $form->process("propSelect_".$i,"selectBox");
if($propSelect ne "") {
my $opSelect = $form->process("opSelect_".$i,"selectBox");
my $valText = $form->process("valText_".$i,"text");
$where->{$whereCount} = {
propSelect => $propSelect,
opSelect => $opSelect,
valText => $valText,
};
$whereCount++;
}
}
my $orderBy = {};
my $orderByCount = 1;
for(my $i = 0; $i < $orderCount; $i++ ) {
my $orderSelect = $form->process("orderSelect_".$i,"selectBox");
if($orderSelect ne "") {
my $dirSelect = $form->process("dirSelect_".$i,"selectBox");
$orderBy->{$orderByCount} = {
"orderSelect" => $orderSelect,
"dirSelect" => $dirSelect,
};
$orderByCount++;
}
}
my $jsonHash = {
isNew => "false",
className => $form->process("className","selectBox"),
startNode => $form->process("startNode","asset"),
anySelect => $form->process("anySelect","selectBox"),
where => $where,
whereCount => $whereCount,
order => $orderBy,
orderCount => $orderByCount,
limit => $form->process("limit","integer"),
};
my $jsonStr = JSON->new->canonical->encode($jsonHash);
#Set the value in the form
$self->set('value',$jsonStr);
}
return $self->get('value') || $self->get('defaultValue');
}
#-------------------------------------------------------------------
=head2 i18n
Returns the i18n object for the form
=cut
sub i18n {
my $self = shift;
my $session = $self->session;
unless ($self->{_i18n}) {
$self->{_i18n}
= WebGUI::International->new($session,'Form_AssetReportQuery');
}
return $self->{_i18n};
}
#----------------------------------------------------------------------------
=head2 toHtml
Render the form control.
=cut
sub toHtml {
my $self = shift;
my $session = $self->session;
my $db = $session->db;
my $style = $session->style;
my $i18n = $self->i18n;
#Build a JSON Array of all the possible classes and their fields
my $json = {};
#Get all of the classes being used in the WebGUI instance
my $classes = $db->buildArrayRef(q{
SELECT
distinct className
FROM
asset
ORDER BY
className
});
#Hard code these for now
my %asset = (
"asset.creationDate" => "creationDate (asset)",
"asset.createdBy" => "createdBy (asset)",
"asset.stateChanged" => "stateChanged (asset)",
"asset.stateChangedBy" => "stateChangedBy (asset)",
"asset.isLockedBy" => "isLockedBy (asset)",
);
#Get the fields from the definition of each class
foreach my $class (@{$classes}) {
my $definitions = $class->definition($session);
tie my %fields, "Tie::IxHash", ();
foreach my $definition (@{$definitions}) {
my $properties = $definition->{properties};
my $tableName = $definition->{tableName};
foreach my $property (keys %{$properties}) {
my $key = $tableName.".".$property;
$fields{$key} = qq{$property ($tableName)};
}
}
%fields = (%asset,%fields);
%fields = WebGUI::Utility::sortHash(%fields);
$json->{$class} = \%fields;
}
#Encode the JSON and add it to the end of the body
my $jsonStr = JSON->new->encode($json);
$style->setRawHeadTags(qq|<script type="text/javascript">var classValues = $jsonStr; </script>|);
my $jsonData = $self->get("value") || q|{ "isNew" : "true" }|;
$style->setRawHeadTags(qq|<script type="text/javascript">var dataValues = $jsonData;</script>|);
$session->style->setScript($session->url->extras("yui-webgui/build/form/assetReportQuery.js"),{ type=>"text/javascript" });
#Decode JSON data for filling in some of the fields
my $jsonDataHash = JSON->new->decode($jsonData);
#Class select list
my $classSelect = WebGUI::Form::selectBox($session,{
name =>"className",
value => "",
options => {},
extras => q{onchange="loadClassName(this.value);"},
});
#Start Node
my $startNode = WebGUI::Form::asset($session,{
name =>"startNode",
value => $jsonDataHash->{startNode},
});
#Any Select
my $anySelect = WebGUI::Form::selectBox($session, {
name => "anySelect",
value => $jsonDataHash->{anySelect},
options => $self->getAnyList,
});
#Property Select
my $propSelect = WebGUI::Form::selectBox($session,{
name => "propSelect",
value => "",
options => { ""=>$i18n->get("choose one option") },
});
#Op Select
my $opSelect = WebGUI::Form::selectBox( $session, {
name => "opSelect",
value => "",
options => $self->getOps,
});
#Value Test
my $valText = WebGUI::Form::text($session,{
name => "valText"
});
#Delete Button
my $deleteButton = WebGUI::Form::button($session,{
value => "-",
extras => q{id="deleteButton_formId"}
});
#Add Button
my $addButton = WebGUI::Form::button($session,{
value => "+",
extras => q{ onclick="addRow(document.getElementById('row_1'),document.getElementById('whereBody'),'propCount_id');" },
});
#Order Select
my $orderSelect = WebGUI::Form::selectBox($session,{
name => "orderSelect",
value => "",
options => { ""=>$i18n->get("choose one option") },
});
#Dir Select
my $dirSelect = WebGUI::Form::selectBox($session, {
name => "dirSelect",
value => "",
options => $self->getDirs,
});
#Delete Button
my $orderDelButton = WebGUI::Form::button($session,{
value => "-",
extras => q{id="orderDelButton_formId"}
});
#Add Button
my $orderAddButton = WebGUI::Form::button($session,{
value => "+",
extras => q{ onclick="addRow(document.getElementById('order_1'),document.getElementById('orderBody'),'orderCount_id');" },
});
#Prop Count
my $propCount = WebGUI::Form::hidden($session, {
name => "propCount",
value => 1,
extras => q{id="propCount_id"},
});
#Order Count
my $orderCount = WebGUI::Form::hidden($session, {
name => "orderCount",
value => 1,
extras => q{id="orderCount_id"},
});
#Limit
my $limit = WebGUI::Form::integer($session,{
name => "limit",
value => $jsonDataHash->{limit},
});
my $classSelectLabel = $i18n->get("class select label");
my $startNodeLabel = $i18n->get("start node label");
my $anySelectLabel = sprintf($i18n->get("any select label"), $anySelect);
my $orderByLabel = $i18n->get("order by label");
my $limitLabel = $i18n->get("limit label");
my $limitSubText = $i18n->get("limit subtext");
#Choose a class
my $output = qq{
$propCount
$orderCount
<table>
<thead>
<tr><th>$classSelectLabel</th></tr>
</thead>
<tbody>
<tr><td>$classSelect</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th>$startNodeLabel</th></tr>
</thead>
<tbody>
<tr><td>$startNode</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th>$anySelectLabel</th><tr>
</thead>
</table>
<table>
<tbody id="whereBody">
<tr>
<td>$propSelect</td>
<td>$opSelect</td>
<td>$valText</td>
<td>$deleteButton</td>
<td>$addButton</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr><th colspan="4">$orderByLabel</th></tr>
</thead>
<tbody id="orderBody">
<tr>
<td>$orderSelect</td>
<td>$dirSelect</td>
<td>$orderDelButton</td>
<td>$orderAddButton</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr><th>$limitLabel</th></tr>
</thead>
<tbody>
<tr>
<td>$limit <span style="font-size:10px">($limitSubText)</span></td>
</tr>
</tbody>
</table>
};
return $output;
}
1;

View file

@ -0,0 +1,37 @@
package WebGUI::i18n::English::Asset_AssetReport;
use strict;
our $I18N = {
'assetName' => {
message => q{Asset Report},
lastUpdated => 0,
},
'templateId label' => {
message => q{Asset Report Template},
lastUpdated => 1226174617,
context => q{Label for asset edit screen},
},
'templateId description' => {
message => q{Select a template to display your asset report.},
lastUpdated => 1226174619,
context => q{Hover help for asset edit screen},
},
'paginateAfter label' => {
message => q{Assets Per Page},
lastUpdated => 1226174617,
context => q{Label for asset edit screen},
},
'paginateAfter description' => {
message => q{Choose the number of assets to display per page.},
lastUpdated => 1226174619,
context => q{Hover help for asset edit screen},
},
};
1;

View file

@ -0,0 +1,73 @@
package WebGUI::i18n::English::Form_AssetReportQuery;
use strict;
our $I18N = {
'any option' => {
message => q|any|,
lastUpdated => 1078852836,
context => q{Select list option in AssetReportQuery Form},
},
'all option' => {
message => q|all|,
lastUpdated => 1078852836,
context => q{Select list option in AssetReportQuery Form},
},
'ascending option' => {
message => q|Ascending|,
lastUpdated => 1078852836,
context => q{Select list option in AssetReportQuery Form},
},
'descending option' => {
message => q|Descending|,
lastUpdated => 1078852836,
context => q{Select list option in AssetReportQuery Form},
},
'choose one option' => {
message => q|Choose One|,
lastUpdated => 1078852836,
context => q{Select list option in AssetReportQuery Form},
},
'class select label' => {
message => q|Search for assets of type|,
lastUpdated => 1078852836,
context => q{Text label in AssetReportQuery Form},
},
'start node label' => {
message => q|That are descendants of the following asset|,
lastUpdated => 1078852836,
context => q{Text label in AssetReportQuery Form},
},
'any select label' => {
message => q|Matching %s of the following constraints|,
lastUpdated => 1078852836,
context => q{Text label in AssetReportQuery Form},
},
'order by label' => {
message => q|Order the results by|,
lastUpdated => 1078852836,
context => q{Text label in AssetReportQuery Form},
},
'limit label' => {
message => q|Limit the number of results returned to|,
lastUpdated => 1078852836,
context => q{Text label in AssetReportQuery Form},
},
'limit subtext' => {
message => q|Enter a zero if you do not wish to limit the number of results|,
lastUpdated => 1078852836,
context => q{Subtext for limit field in AssetReportQuery Form},
},
};
1;