File Coverage

blib/lib/Elive/Entity/Report.pm
Criterion Covered Total %
statement 15 40 37.5
branch 0 6 0.0
condition 0 11 0.0
subroutine 5 9 55.5
pod 4 4 100.0
total 24 70 34.2


line stmt bran cond sub pod time code
1             package Elive::Entity::Report;
2 1     1   727 use warnings; use strict;
  1     1   2  
  1         40  
  1         5  
  1         2  
  1         37  
3              
4 1     1   6 use Mouse;
  1         2  
  1         8  
5 1     1   358 use Mouse::Util::TypeConstraints;
  1         2  
  1         5  
6              
7             extends 'Elive::Entity';
8              
9 1     1   87 use Elive::Entity::Role;
  1         1  
  1         584  
10              
11             __PACKAGE__->entity_name('Report');
12             __PACKAGE__->collection_name('Reports');
13              
14             has 'reportId' => (is => 'rw', isa => 'Int', required => 1);
15             __PACKAGE__->primary_key('reportId');
16              
17             has 'name' => (is => 'rw', isa => 'Str',
18             documentation => 'report name');
19             __PACKAGE__->_alias(reportName => 'name', freeze => 1);
20              
21             has 'description' => (is => 'rw', isa => 'Str',
22             documentation => 'report description');
23             __PACKAGE__->_alias(reportDescription => 'description', freeze => 1);
24              
25             has 'xml' => (is => 'rw', isa => 'Str',
26             documentation => 'report content');
27             __PACKAGE__->_alias(reportDefinition => 'xml', freeze => 1);
28              
29             has 'role' => (is => 'rw', isa => 'Elive::Entity::Role',
30             documentation => 'default user role',
31             coerce => 1);
32              
33             has 'parentId' => (is => 'rw', isa => 'Int');
34              
35             has 'ownerId' => (is => 'rw', isa => 'Str');
36             __PACKAGE__->_alias(reportOwner => 'ownerId', freeze => 1);
37              
38             =head1 NAME
39              
40             Elive::Entity::Report - Elluminate Report entity instance class
41              
42             =head1 DESCRIPTION
43              
44             This is the entity class for server side reports. These are visible
45             on the Elluminate server under the 'Reports' tab.
46              
47             Please note that the C method (C command) does not return the body
48             of the report. The report object needs to be refetched via the C method.
49              
50             For example, the following code snippet exports all reports for a site:
51              
52             my $reports = Elive::Entity::Report->list;
53             my @report_ids = map {$_->reportId} @$reports;
54              
55             foreach my $report_id (@report_ids) {
56              
57             #
58             # listed objects don't have the report body, refetch them.
59             #
60              
61             my $rpt = Elive::Entity::Report->retrieve( $report_id );
62              
63             my $name = $rpt->name;
64             $name =~ s/[^\w]//g; # sanitise
65             my $export_file = "/tmp/report_${reportId}_${name}.xml";
66              
67             open (my $dump_fh, '>', $export_file)
68             or die "unable to open $export_file: $!";
69             print $dump_fh $rpt->xml;
70             close ($dump_fh);
71              
72             }
73              
74             =cut
75              
76             =head1 METHODS
77              
78             =cut
79              
80             sub BUILDARGS {
81 0     0 1   my $class = shift;
82 0           my $spec = shift;
83 0           my %opt = @_;
84              
85 0 0         die "usage: $class->new(\$hashref)"
86             unless Elive::Util::_reftype($spec) eq 'HASH';
87              
88 0           my %args = %$spec;
89              
90 0   0       $args{ownerId} ||= do {
91              
92 0 0 0       my $connection = $opt{connection} || $class->connection
93             or die "not connected";
94              
95 0           $connection->login->userId;
96             };
97              
98 0           return \%args;
99             }
100              
101             =head2 list
102              
103             my $all_reports = Elive::Entity::Report->list();
104              
105             List reports.
106              
107             Note: This command does not return the body of the report (C property).
108              
109             =cut
110              
111             =head2 retrieve
112              
113             my $report = Elive::Entity::Report->retrieve( $report_id );
114             my $report_xml = $report->xml;
115              
116             Retrieves a report, including the body of the report (C property).
117              
118             =cut
119              
120             =head2 insert
121              
122             The C method is not available for reports.
123              
124             =cut
125              
126             sub insert {
127 0     0 1   my ($class, $_spec, %opt) = @_;
128              
129 0           my $insert_data = $class->BUILDARGS($_spec, %opt);
130              
131 0   0       $opt{command} ||= 'addReport';
132              
133 0           return $class->SUPER::insert( $class->_freeze($insert_data), %opt);
134             }
135              
136             =head2 update
137              
138             The C method is not available for reports.
139              
140             =cut
141              
142             sub update {
143 0     0 1   my ($self, $_spec, %opt) = @_;
144              
145 0           my $update_data;
146 0 0         $update_data = $self->BUILDARGS($_spec, %opt)
147             if $_spec;
148             #
149             # always need to supply these fields to the update command,
150             # whether or not they've actually changed.
151             #
152 0           my %changed;
153 0           @changed{$self->is_changed, 'name','description','xml', 'ownerId'} = undef;
154              
155 0           return $self->SUPER::update($update_data, %opt, changed => [keys %changed]);
156             }
157              
158             =head2 delete
159              
160             my $report = Elive::Entity::Report->retrieve( $report_id );
161             $report->delete if $report;
162              
163             Deletes a report.
164              
165             =cut
166              
167             sub delete {
168             #
169             # response seems to be returned as true/false rather than a record.
170             # hence the need to roll our own
171             #
172 0     0 1   my ($self, %opt) = @_;
173              
174 0           my $som = $self->connection->call('deleteReport'
175 0           => %{$self->_freeze({reportId => $self->reportId})
176             });
177              
178 0           my $results = $self->_get_results($som, $self->connection);
179             #
180             # this command responds with true/false, rather than a report record.
181             #
182 0   0       my $deleted = $results && $results->[0];
183              
184 0           return $self->_deleted($deleted);
185             }
186              
187             1;