comparison lib/Benzin/Bugzilla/XmlWriter.pm @ 11:4eb9fdf4efa9

refactoring, non-working bookings
author cin
date Mon, 07 Sep 2015 19:18:21 +0300
parents 14a966369278
children 52b34ea50eff
comparison
equal deleted inserted replaced
10:14a966369278 11:4eb9fdf4efa9
1 package Benzin::Bugzilla::XmlWriter; 1 package Benzin::Bugzilla::XmlWriter;
2 2
3 our %Transform = ( 3 our %Transform = (
4 'Benzin::Bugzilla::Bug' => 'WriteBug', 4 'Benzin::Bugzilla::Bug' => 'WriteBug',
5 'Benzin::Bugzilla::BugComment' => 'WriteBugComment', 5 'Benzin::Bugzilla::BugComment' => 'WriteBugComment',
6 'HASH' => 'WriteHash', 6 'HASH' => 'WriteHash',
7 -plain => 'WriteValue', 7 'DateTime' => 'WriteTJ3DateTime',
8 -default => 'WriteValue' 8 -plain => 'WriteValue',
9 -default => 'WriteValue'
9 ); 10 );
10 11
11 use IMPL::Const qw(:prop); 12 use IMPL::Const qw(:prop);
12 use IMPL::declare { 13 use IMPL::declare {
13 require => { 14 require => {
14 XMLWriter => 'XML::Writer', 15 XMLWriter => 'XML::Writer',
15 Bug => 'Benzin::Bugzilla::Bug', 16 Bug => 'Benzin::Bugzilla::Bug',
16 BugComment => 'Benzin::Bugzilla::BugComment' 17 BugComment => 'Benzin::Bugzilla::BugComment',
17 }, 18 },
18 base => [ 19 base => [
19 'IMPL::Transform' => sub { %Transform } 20 'IMPL::Transform' => sub { %Transform }
20 ], 21 ],
21 props => [ 22 props => [
22 _writer => PROP_RW 23 _writer => PROP_RW,
24 timereports => PROP_RW,
25 timeresolution => PROP_RW
23 ] 26 ]
24 }; 27 };
25 28
26 use fields qw(_writer); 29 use fields qw(_writer);
27 30
28 sub CTOR { 31 sub CTOR {
29 my SELF $this = shift; 32 my SELF $this = shift;
30 33
31 $this->_writer(XMLWriter->new(@_)); 34 $this->_writer( XMLWriter->new(@_) );
32 } 35 }
33 36
34 sub WriteBugList { 37 sub WriteBugList {
35 my SELF $this = shift; 38 my SELF $this = shift;
36 my $bugs = shift || []; 39 my $bugs = shift || [];
37 my $writer = $this->_writer; 40 my $writer = $this->_writer;
38 41
39
40 $writer->xmlDecl("UTF-8"); 42 $writer->xmlDecl("UTF-8");
41 $writer->startTag("bugzilla"); 43 $writer->startTag("bugzilla");
42 44
43
44 foreach my $bug (@$bugs) { 45 foreach my $bug (@$bugs) {
45 $writer->startTag("bug"); 46 $writer->startTag("bug");
46 $this->WriteBug($bug); 47 $this->WriteBug($bug);
47 $writer->endTag(); 48 $writer->endTag();
48 } 49 }
49 50
50 $writer->endTag(); 51 $writer->endTag();
51 52
52 } 53 }
53 54
54 sub WriteBug { 55 sub WriteBug {
55 my SELF $this = shift; 56 my SELF $this = shift;
56 my $value = shift; 57 my $value = shift;
57 my $writer = $this->_writer; 58 my $writer = $this->_writer;
58 59
59 foreach my $field ( @{ Bug->BUG_FIELDS } ) { 60 foreach my $field ( @{ Bug->BUG_FIELDS } ) {
60 next unless $value->{$field}; 61 next unless $value->{$field};
61 $this->WriteElement( $field, $value->{$field} ); 62 $this->WriteElement( $field, $value->{$field} );
62 } 63 }
64 $this->WriteElement( 'time',
65 $value->GetTimeReports( $this->timeresolution ) )
66 if $this->timereports;
63 } 67 }
64 68
65 sub WriteBugComment { 69 sub WriteBugComment {
66 my SELF $this = shift; 70 my SELF $this = shift;
67 my $value = shift; 71 my $value = shift;
68 my $writer = $this->_writer; 72 my $writer = $this->_writer;
69 73
70 foreach my $field ( @{ BugComment->COMMENT_FIELDS } ) { 74 foreach my $field ( @{ BugComment->COMMENT_FIELDS } ) {
71 next unless $value->{$field}; 75 next unless $value->{$field};
72 $this->WriteElement( $field, $value->{$field} ); 76 $this->WriteElement( $field, $value->{$field} );
74 } 78 }
75 79
76 sub WriteHash { 80 sub WriteHash {
77 my SELF $this = shift; 81 my SELF $this = shift;
78 my $value = shift; 82 my $value = shift;
79 83
80 while(my ($k,$v) = each %$value) { 84 while ( my ( $k, $v ) = each %$value ) {
81 $this->WriteElement($k,$v); 85 $this->WriteElement( $k, $v );
82 } 86 }
83 } 87 }
84 88
85 sub WriteElement { 89 sub WriteElement {
86 my SELF $this = shift; 90 my SELF $this = shift;
93 ? @{$data} 97 ? @{$data}
94 : $data; 98 : $data;
95 99
96 foreach my $v (@values) { 100 foreach my $v (@values) {
97 $writer->startTag($name); 101 $writer->startTag($name);
98 $this->Transform( $v ); 102 $this->Transform($v);
99 $writer->endTag; 103 $writer->endTag;
100 } 104 }
101 } 105 }
102 106
103 sub WriteValue { 107 sub WriteValue {
104 my SELF $this = shift; 108 my SELF $this = shift;
105 my $value = shift; 109 my $value = shift;
106 110
107 $this->_writer->characters($value) if defined $value; 111 $this->_writer->characters($value) if defined $value;
108 } 112 }
109 113
114 sub WriteTJ3DateTime {
115 my SELF $this = shift;
116 my $value = shift;
117
118 $this->_writer->characters($value->strftime('%F-%T')) if defined $value;
119 }
120
110 1; 121 1;