root/gs3-extensions/html-to-expeditee/trunk/src/perllib/cgiactions/ExpediteeExportAction.pm @ 26696

Revision 26696, 5.1 KB (checked in by davidb, 7 years ago)

Now obtains the correct frame number when exporting a document to an Expeditee frame.

Line 
1###########################################################################
2#
3# ExpediteeExportAction.pm --
4# Based on HTMLToExpediteeAction.pm - Used for exporting INDIVIDUAL
5# Greenstone documents to Expeditee. If you want to export an entire
6# collection, use HTMLToExpediteeAction.pm.
7#
8# A component of the Greenstone digital library software
9# from the New Zealand Digital Library Project at the
10# University of Waikato, New Zealand.
11#
12# Copyright (C) 2009 New Zealand Digital Library Project
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27#
28###########################################################################
29
30package ExpediteeExportAction;
31
32use strict;
33
34use cgiactions::baseaction;
35
36use ExpediteeFrameIO;
37use JSON;
38
39use Devel::Peek;
40
41BEGIN {
42#    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
43}
44
45@ExpediteeExportAction::ISA = ('baseaction');
46
47# c=collect and site=xxxx implicitly handled by code
48# 'c' is handled by base action, site is tested for only if greenstone3.
49
50my $action_table=
51{
52        "generate-frame"     => { 'compulsory-args' => [ "fn", "json" ],
53                  'optional-args'   => ['assoc'] },
54};
55
56sub new
57{
58    my $class = shift (@_);
59    my ($gsdl_cgi,$iis6_mode) = @_;
60
61    my $self = new baseaction($action_table,$gsdl_cgi,$iis6_mode);
62
63    return bless $self, $class;
64}
65
66sub generate_frame
67{
68    my $self = shift @_;
69   
70    my $username  = $self->{'username'};
71    my $collect   = $self->{'collect'};
72    my $gsdl_cgi  = $self->{'gsdl_cgi'};
73    my $gsdlhome  = $self->{'gsdlhome'};
74
75    my $assocfilepath = $self->{'assoc'};
76   
77    my $site = $self->{'site'};
78    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
79
80    my $fn = $self->{'fn'};
81   
82    my $json_str_unicode = $self->{'json'};
83   
84     if (!Encode::is_utf8( $json_str_unicode )) {
85         $json_str_unicode = Encode::decode_utf8( $json_str_unicode );
86     }
87     
88    # decode_json expects string to convert to be raw utf8
89    my $json_str_utf8 = Encode::encode("utf8",$json_str_unicode);
90    my $exp_frame_tree = decode_json $json_str_utf8;   
91   
92    my $output_dir = &util::filename_cat($collect_dir,$collect,"export");
93
94    if (!-d $output_dir) {
95    &util::mk_dir($output_dir);
96    }
97
98    my $expeditee_frame_io = new ExpediteeFrameIO($output_dir,"kgas1A");
99     $expeditee_frame_io->buildFrame($exp_frame_tree);
100   
101    my $last = 0;   #check if new frame is replacing/will be the last frame in the frameset.
102     
103     if($fn == -1){
104        #get last frame number and add 1.
105        #BUT what if a matching frame already exists for this document but we just haven't
106        #created a frameID metadata for that document yet - sol'n: at this stage, just use
107        #original code of reading file names
108       
109        my $export_dir = &util::filename_cat($collect_dir,$collect,"export");
110        my $frameInf = "$export_dir\\frame.inf";
111        my $getFn = "";
112        my @files;
113       
114        opendir DIR, $output_dir or die "cannot open output directory $output_dir: $!";
115        @files = readdir DIR;
116        closedir DIR;
117       
118        my $found = 0;
119        my $frameNumString;
120        for my $file(@files){
121            if($file =~ m/\d/){
122               
123                my $fullFile = "$export_dir\\$file";
124                open FILE, $fullFile or die $!;
125                my @lines = <FILE>;
126                my $numLines = scalar (@lines);
127                my $counter = 1;
128               
129                for my $line(@lines){
130                   
131                    if($line eq "T \@assocfilepath $assocfilepath\n"){
132                        $frameNumString = $fullFile;
133                       
134                        #extract frame number out of file name
135                        my @getFrameNumArray = split('\\\\',$frameNumString);
136                        @getFrameNumArray = reverse(@getFrameNumArray);
137                       
138                        $fn = $getFrameNumArray[0];
139                       
140                        $found = 1;
141                       
142                        if($counter == $numLines){
143                            $last = 1;
144                        }
145                    }
146                   
147                    $counter = $counter + 1;
148                }
149               
150                close(FILE);
151            }
152        }
153       
154        unless($found){
155            #try to get frame number from frame.inf.
156            if(-e $frameInf){
157               
158                open FRAME_INF, $frameInf or die $!;
159                my @lines = <FRAME_INF>;
160               
161                $fn = $lines[0];
162               
163                #get number from string
164                $fn =~ s/[^0-9]//g;
165                $fn = $fn + 1;
166               
167                $last = 1;
168            }else{
169                printf "$frameInf not found\n";
170                $fn = 1;
171                $last = 1;
172            }
173        }
174
175     }
176     
177     
178     
179     if($expeditee_frame_io->saveFrame("$fn.exp")){
180       
181        printf "*** $fn";
182       
183        if($last){
184            #update last frame number
185            $expeditee_frame_io->saveLastFrameNumber($fn,$collect);
186        }
187       
188        $gsdl_cgi->generate_message("Document Exported to Expeditee $fn");
189     }else{
190        $gsdl_cgi->generate_error("Failed to save frame number $fn");
191     }
192     
193     
194}
195
1961;
Note: See TracBrowser for help on using the browser.