| 1 |
# Copyright (C) 2006 Software Freedom Law Center, Inc. |
|---|
| 2 |
# Author: Bradley M. Kuhn <bkuhn@softwarefreedom.org> |
|---|
| 3 |
# |
|---|
| 4 |
# This software gives you freedom; it is licensed to you under version |
|---|
| 5 |
# 3 of the GNU Affero General Public License. |
|---|
| 6 |
# |
|---|
| 7 |
# This software is distributed WITHOUT ANY WARRANTY, without even the |
|---|
| 8 |
# implied warranties of MERCHANTABILITY and FITNESS FOR A PARTICULAR |
|---|
| 9 |
# PURPOSE. See the GNU Affero General Public License for further |
|---|
| 10 |
# details. |
|---|
| 11 |
# |
|---|
| 12 |
# You should have received a copy of the GNU Affero General Public |
|---|
| 13 |
# License, version 3 along with this software. If not, see |
|---|
| 14 |
# <http://www.gnu.org/licenses/>. |
|---|
| 15 |
package SFLC::TimeTracker::Output::DeleteEntry; |
|---|
| 16 |
|
|---|
| 17 |
use strict; |
|---|
| 18 |
use warnings; |
|---|
| 19 |
|
|---|
| 20 |
use base qw(SFLC::TimeTracker::Output::Entry); |
|---|
| 21 |
|
|---|
| 22 |
use Lingua::EN::Inflect qw ( PL); |
|---|
| 23 |
use Date::Manip; |
|---|
| 24 |
use Carp; |
|---|
| 25 |
|
|---|
| 26 |
sub getDatabase { |
|---|
| 27 |
return $SFLC::TimeTracker::Input::DATABASE; |
|---|
| 28 |
} |
|---|
| 29 |
############################################################################### |
|---|
| 30 |
sub new { |
|---|
| 31 |
my($class) = shift; |
|---|
| 32 |
my %input = @_; |
|---|
| 33 |
|
|---|
| 34 |
$input{status} = 'DELETED' unless defined $input{status}; |
|---|
| 35 |
$input{type} = 'abandoned' unless defined $input{type}; |
|---|
| 36 |
if (defined $input{entries}) { |
|---|
| 37 |
$input{object} = $input{entries}; |
|---|
| 38 |
} elsif (defined $input{entry}) { |
|---|
| 39 |
$input{entries} = [ $input{entry} ]; |
|---|
| 40 |
} else { |
|---|
| 41 |
$input{object} = [] unless defined $input{object}; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
my %save; |
|---|
| 45 |
foreach my $field (qw/userHandle date/) { |
|---|
| 46 |
croak "DeleteEntry instance needs $field" if not defined $input{$field}; |
|---|
| 47 |
$save{$field} = $input{$field}; |
|---|
| 48 |
delete $input{$field}; |
|---|
| 49 |
} |
|---|
| 50 |
my $self = SFLC::TimeTracker::Output::Entry::new($class, %input); |
|---|
| 51 |
|
|---|
| 52 |
foreach my $field (keys %save) { |
|---|
| 53 |
$self->set($field => $save{$field}); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
return $self; |
|---|
| 57 |
} |
|---|
| 58 |
############################################################################### |
|---|
| 59 |
sub get { |
|---|
| 60 |
my($self, $field) = @_; |
|---|
| 61 |
if ($field =~ /^(?:entries)$/) { |
|---|
| 62 |
# Be smart here. First, see if the entry is in the database. If it is, |
|---|
| 63 |
# We want the freshest version of it we can. It could have been deleted |
|---|
| 64 |
# subsequent to the creation of this objec, however, so we have to |
|---|
| 65 |
# sometimes use the cached version. |
|---|
| 66 |
my $obj = $self->SUPER::get('object'); |
|---|
| 67 |
my @returnEntries; |
|---|
| 68 |
foreach my $cachedEntry (@{$obj}) { |
|---|
| 69 |
my $dbEntry = |
|---|
| 70 |
$self->getDatabase()->getEntryById($cachedEntry->{userHandle}, |
|---|
| 71 |
$cachedEntry->{id}); |
|---|
| 72 |
push(@returnEntries, , (defined $dbEntry and ref $dbEntry) ? $dbEntry |
|---|
| 73 |
: $cachedEntry); |
|---|
| 74 |
} |
|---|
| 75 |
return \@returnEntries; |
|---|
| 76 |
} elsif ($field =~ /^(?:userHandle|date)$/) { |
|---|
| 77 |
return $self->{$field}; |
|---|
| 78 |
} elsif ($field =~ /^(?:entry)$/) { |
|---|
| 79 |
confess "entry is not valid for this class, use entries"; |
|---|
| 80 |
} else { |
|---|
| 81 |
return $self->SUPER::get($field); |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
############################################################################### |
|---|
| 85 |
sub set { |
|---|
| 86 |
my($self, $field, $value) = @_; |
|---|
| 87 |
if ($field =~ /^(?:entries)$/i) { |
|---|
| 88 |
return $self->SUPER::set('object', $value); |
|---|
| 89 |
} elsif ($field =~ /^(?:date|userHandle)$/i) { |
|---|
| 90 |
$self->{$field} = $value; |
|---|
| 91 |
} elsif ($field =~ /^(?:entry)$/) { |
|---|
| 92 |
confess "entry is not valid for this class, use entry"; |
|---|
| 93 |
} else { |
|---|
| 94 |
return $self->SUPER::set($field, $value); |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
############################################################################### |
|---|
| 98 |
sub action ($$) { |
|---|
| 99 |
my($self, $actionData) = @_; |
|---|
| 100 |
|
|---|
| 101 |
print "in DeletedEntry action with edge $actionData->{edge}\n"; |
|---|
| 102 |
if ($actionData->{edge} eq "abort") { |
|---|
| 103 |
# Do nothing but set the status, if we have aborted the deletion |
|---|
| 104 |
$self->set('status', 'ABORTED'); |
|---|
| 105 |
} elsif ($actionData->{edge} eq "category") { |
|---|
| 106 |
my $date = $self->get('date'); |
|---|
| 107 |
die "caught a delete edge without category" |
|---|
| 108 |
unless defined $actionData->{category}; |
|---|
| 109 |
|
|---|
| 110 |
die "caught a delete edge without a date" unless defined $date; |
|---|
| 111 |
my(@entries); |
|---|
| 112 |
|
|---|
| 113 |
if ($date eq "last") { |
|---|
| 114 |
my $entry = |
|---|
| 115 |
$self->getDatabase()->getLastEntryWithCategory($self->get('userHandle'), |
|---|
| 116 |
$actionData->{category}); |
|---|
| 117 |
push(@entries, $entry) if defined $entry; |
|---|
| 118 |
} else { |
|---|
| 119 |
@entries = $self->getDatabase()-> |
|---|
| 120 |
getEntriesOnDateWithCategory($self->get('userHandle'), |
|---|
| 121 |
$date, |
|---|
| 122 |
$actionData->{category}); |
|---|
| 123 |
} |
|---|
| 124 |
if (@entries <= 0) { |
|---|
| 125 |
print "NO ENTRIES FOUND\n"; |
|---|
| 126 |
$self->set('status', 'NOT FOUND'); |
|---|
| 127 |
return $actionData; |
|---|
| 128 |
} |
|---|
| 129 |
$self->set(entries => \@entries); |
|---|
| 130 |
foreach my $entry (@entries) { $entry->remove(); } |
|---|
| 131 |
} |
|---|
| 132 |
# Always allow data to flow downward through chains of entry nodes. |
|---|
| 133 |
return $actionData; |
|---|
| 134 |
} |
|---|
| 135 |
############################################################################### |
|---|
| 136 |
sub prettyPrintIRC ($$) { |
|---|
| 137 |
my($self, $now) = @_; |
|---|
| 138 |
|
|---|
| 139 |
return "Aborted. No entry deleted." if ($self->status eq "ABORTED"); |
|---|
| 140 |
return "Aborted. No entry found to delete." |
|---|
| 141 |
if ($self->status eq "NOT FOUND"); |
|---|
| 142 |
|
|---|
| 143 |
my $today = UnixDate($now, '%b %E'); |
|---|
| 144 |
|
|---|
| 145 |
my $retStr = "Did " . $self->status . "."; |
|---|
| 146 |
|
|---|
| 147 |
my $entries = $self->get('entries'); |
|---|
| 148 |
foreach my $entry (@{$entries}) { |
|---|
| 149 |
my($startTime, $endTime, $amountTime, $dateOccurred, $category) = |
|---|
| 150 |
$self->_getTimesAndCat($entry); |
|---|
| 151 |
|
|---|
| 152 |
$retStr .= " Deleted entry in $category" if defined $category; |
|---|
| 153 |
$retStr .= ", which lasted for $amountTime" if defined $amountTime; |
|---|
| 154 |
$retStr .= " (on $dateOccurred)" if $dateOccurred ne $today; |
|---|
| 155 |
$retStr .= " which had started at $startTime" if defined $startTime and |
|---|
| 156 |
not defined $amountTime; |
|---|
| 157 |
$retStr .= "."; |
|---|
| 158 |
} |
|---|
| 159 |
return $retStr; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
1; |
|---|
| 163 |
__END__ |
|---|
| 164 |
|
|---|
| 165 |
# |
|---|
| 166 |
# Local variables: |
|---|
| 167 |
# compile-command: "perl -I ../../../../Modules -c DeleteEntry.pm" |
|---|
| 168 |
# End: |
|---|