dump in more or less correct sort order with seprator pages
[star2016] / star2016-contributions.pl
1 #!/usr/bin/perl
2 use autodie;
3 use warnings;
4 use strict;
5
6 use XML::Simple;
7 use Data::Dump qw(dump);
8 use utf8;
9
10 my $odt_file   = 'contributions.odt';
11
12 my $xs = XML::Simple->new();
13
14 use     OpenOffice::OODoc       2.101;
15 my $doc = odfDocument(file => $odt_file, create => 'text');
16
17 my $stat;
18 my $abstracts;
19
20 $doc->createStyle(
21         "Abstract",
22         family          => 'paragraph',
23         parent          => 'Standard',
24 #       category        => 'auto',
25         properties      => {
26                 -area                   => 'paragraph',
27                 'fo:text-align' => 'justify'
28         }
29 );
30
31
32 my $nr = 0;
33 #foreach my $file ( glob 'contributions/*.xml' ) {
34
35 my $xml;
36 open(my $fh, '<', 'contributions/all.xml');
37 {
38         local $/ = undef;
39         $xml = <$fh>;
40         close($fh);
41 }
42
43 foreach my $xml ( split(/\Q<?xml\E/, $xml) ) {
44
45         next unless $xml;
46
47         $xml = '<?xml' . $xml;
48
49         my $abstract;
50
51 #print $xml;
52
53         $abstract = $xs->XMLin( $xml, ForceArray => qw(PrimaryAuthor) );
54
55         warn "# abstract = ", dump($abstract);
56
57         my $id = $abstract->{'Id'}->[0] || die "no Id in ",dump($abstract);
58         warn "# $id abstract = ", dump($abstract);
59
60         foreach my $k (qw( ContributionType )) {
61                 push @{ $stat->{$k}->{ $abstract->{$k}->[0] } }, $id;
62         }
63
64         if ( $abstract->{'ContributionType'}->[0] eq 'Symposium proposal' ) {
65                 my $t = $abstract->{Title}->[0];
66                 push @{ $stat->{_symposium}->{ $t } }, -$id;
67         }
68
69         if ( my $s = $abstract->{Symposium_title} ) {
70                 $s = $s->[0];
71                 push @{ $stat->{_symposium}->{$s} }, $id;
72         }
73
74         if ( exists $abstract->{PrimaryAuthor}->[0]->{FamilyName} ) {
75                 $stat->{_order_by}->{$id} = $abstract->{PrimaryAuthor}->[0]->{FamilyName}->[0];
76         }
77
78         $abstracts->{ $id } = $abstract;
79
80 }
81
82 #warn "# stat = ",dump($stat);
83
84 sub abstract2doc {
85         my $id = shift;
86
87         my $abstract = $abstracts->{$id} || die "no $id in abstracts ",dump($abstracts);
88
89         my $t = $doc->appendParagraph(text => $abstract->{Title}->[0], style => 'Heading 1');
90         $doc->setPageBreak( $t, position => 'before', style => 'Heading 1');
91
92         foreach my $a ( @{ $abstract->{PrimaryAuthor} } ) {
93                 $doc->appendParagraph(text => join(' ',
94                         map { $a->{$_}->[0] } qw( FirstName FamilyName )
95                 ), style => 'Heading 2');
96                 $doc->appendParagraph(text => join(' ',
97                         map { $a->{$_}->[0] } qw( Affiliation )
98                 ), style => 'Heading 3');
99                 push @{ $stat->{_Affiliation}->{ $a->{Affiliation}->[0] } }, $id;
100         }
101
102 #       $doc->appendParagraph(text => $abstract->{Content}->[0], style => 'Abstract');
103         foreach my $p ( split(/[\n\r]+/, $abstract->{Content}->[0] ) ) {
104                 $doc->appendParagraph(text => $p, style => 'Abstract');
105         }
106
107         $doc->appendParagraph(text => "\nTheme: " . $abstract->{Theme}->[0], style => 'Theme');
108
109         $doc->appendParagraph(text => "\nContributionType: " . $abstract->{ContributionType}->[0], style => 'Quotations');
110
111         $doc->appendParagraph(text => "\nID: " . $abstract->{Id}->[0], style => 'Abstract');
112
113         warn "# added $id to document\n";
114 }
115
116 #foreach my $id ( keys %$abstracts ) {
117 #       abstract2doc($id);
118 #}
119
120 foreach my $symposium ( keys %{ $stat->{'_symposium'} } ) {
121
122         my $t = $doc->appendParagraph(text => $symposium, style => 'Title');
123         $doc->setPageBreak( $t, position => 'before', style => 'Title');
124         $doc->appendParagraph(text => 'Symposium', style => 'Subtitle');
125
126         my @symposium_ids = sort @{ $stat->{'_symposium'}->{$symposium} };
127         warn "## [$symposium] symposium_ids ", dump( @symposium_ids );
128
129         my $proposal_id = shift @symposium_ids;
130         if ( $proposal_id < 0 ) {
131                 $proposal_id = abs( $proposal_id );
132         } else {
133                 warn "ERROR: symposium [$symposium] doesn't have proposal!";
134                 
135         }
136 #       @symposium_ids = sort { $stat->{_order_by}->{$a} cmp $stat->{_order_by}->{$b} } @symposium_ids;
137         @symposium_ids = sort @symposium_ids;
138         unshift @symposium_ids, $proposal_id;
139
140         $stat->{_order_symposium}->{$symposium} = [ @symposium_ids ];
141
142         foreach my $id ( @symposium_ids ) {
143                 abstract2doc($id);
144         }
145
146 }
147
148 foreach my $type ( 'Oral presentation', 'Poster presentation' ) {
149
150         my $t = $doc->appendParagraph(text => $type, style => 'Title');
151         $doc->setPageBreak( $t, position => 'before', style => 'Title');
152
153         my @order = sort { lc $stat->{_order_by}->{$a} cmp lc $stat->{_order_by}->{$b} }
154                 @{ $stat->{'ContributionType'}->{$type} };
155
156         $stat->{ '_order_' . $type } = [ @order ];
157
158         foreach my $id ( @order ) {
159                 abstract2doc($id);
160         }
161 }
162
163
164 warn "# stat = ",dump($stat);
165
166 $doc->save;
167
168
169 open(my $fh, '>', 'affiliation.csv');
170 foreach my $a ( sort keys %{ $stat->{_Affiliation} } ) {
171         print $fh '"',$a,'","',dump( $stat->{_Affiliation}->{$a} ), '"', "\n";
172 }
173
174 __DATA__
175     "Symposium proposal"  => 12,
176         "Symposium abstract"  => 52,
177     "Oral presentation"   => 141,
178     "Poster presentation" => 71,
179