| 1 | package oEdtk::EDMS; | ||
| 2 | |||
| 3 | # Electronic Document Management (GED in French). | ||
| 4 | # Merged at S54 from the fork's ECM-oriented rewrite (36 index columns, the | ||
| 5 | # Alfresco header, _log / my_system) plus the mainline EDMS_package and the | ||
| 6 | # edidx helpers (which the fork exported but never defined). | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use Carp qw(carp croak); | ||
| 10 | |||
| 11 | use Exporter; | ||
| 12 | our $VERSION = 2.1126; | ||
| 13 | our @ISA = qw(Exporter); | ||
| 14 | our @EXPORT_OK = qw( | ||
| 15 | EDMS_edidx_build | ||
| 16 | EDMS_edidx_write | ||
| 17 | EDMS_idldoc_seqpg | ||
| 18 | EDMS_idx_create_csv | ||
| 19 | EDMS_import | ||
| 20 | EDMS_package | ||
| 21 | EDMS_prepare | ||
| 22 | EDMS_process | ||
| 23 | EDMS_process_zip | ||
| 24 | ); | ||
| 25 | |||
| 26 | use Archive::Zip qw(:ERROR_CODES); | ||
| 27 | use Cwd; | ||
| 28 | use File::Basename; | ||
| 29 | use File::Copy; | ||
| 30 | use IPC::Run3 qw(run3); | ||
| 31 | use Net::FTP; | ||
| 32 | use oEdtk::Main; | ||
| 33 | use oEdtk::Config qw(config_read); | ||
| 34 | use POSIX qw(strftime); | ||
| 35 | use Text::CSV; | ||
| 36 | use Time::HiRes qw(gettimeofday); | ||
| 37 | use XML::Writer; | ||
| 38 | |||
| 39 | # use PDF; # add to the prerequisites | ||
| 40 | |||
| 41 | # The per-page .idx1 read by EDMS_idx_process (and posted by | ||
| 42 | # oEUser::DisseminationPlan) carries these 36 columns, in this exact order: the | ||
| 43 | # ECM layout of the fork. oEUser::DisseminationPlan's INDEX_* constants pin the | ||
| 44 | # same positions (ID_LOT=1, PAGE=2, DOC=3, NOM_PRENOM=7, DATE_EDITION=9, | ||
| 45 | # CONTRAT_INDIV=13, TYPE_DOC=31). | ||
| 46 | my @INDEX_ECM_COLS = ( | ||
| 47 | 'ED_REFIDDOC', # 1 id in the document repository | ||
| 48 | 'ED_IDLDOC', # 2 unique document id within the page-layout batch | ||
| 49 | 'ED_IDSEQPG', # 3 page sequence number within the page-layout batch | ||
| 50 | 'ED_SEQDOC', # 4 document sequence number within the batch | ||
| 51 | 'ED_CPDEST', # 5 recipient postal code | ||
| 52 | 'ED_VILLDEST', # 6 recipient city | ||
| 53 | 'ED_IDDEST', # 7 recipient id in the management system | ||
| 54 | 'ED_NOMDEST', # 8 recipient name | ||
| 55 | 'ED_IDEMET', # 9 sender id | ||
| 56 | 'ED_DTEDTION', # 10 edition date printed on the document | ||
| 57 | 'ED_TYPPROD', # 11 production type associated with the batch | ||
| 58 | 'ED_PORTADR', # 12 flag: document carries an address | ||
| 59 | 'ED_ADRLN1', # 13 address line 1 | ||
| 60 | 'ED_CLEGED1', # 14 archiving system key | ||
| 61 | 'ED_ADRLN2', # 15 address line 2 | ||
| 62 | 'ED_CLEGED2', # 16 archiving system key | ||
| 63 | 'ED_ADRLN3', # 17 address line 3 | ||
| 64 | 'ED_CLEGED3', # 18 archiving system key | ||
| 65 | 'ED_ADRLN4', # 19 address line 4 | ||
| 66 | 'ED_CLEGED4', # 20 archiving system key | ||
| 67 | 'ED_ADRLN5', # 21 address line 5 | ||
| 68 | 'ED_CORP', # 22 issuing entity of the page | ||
| 69 | 'ED_DOCLIB', # 23 merge library associated with the page | ||
| 70 | 'ED_REFIMP', # 24 pre-printed, printed or insert reference | ||
| 71 | 'ED_ADRLN6', # 25 address line 6 | ||
| 72 | 'ED_SOURCE', # 26 index source or ed_corp entity | ||
| 73 | 'ED_OWNER', # 27 document owner | ||
| 74 | 'ED_HOST', # 28 hostname this entry came from | ||
| 75 | 'ED_IDIDX', # 29 index id | ||
| 76 | 'ED_CATDOC', # 30 document category | ||
| 77 | 'ED_CODRUPT', # 31 burst forcing code | ||
| 78 | 'ED_TYPDOC', # 32 document type (ECM) | ||
| 79 | 'ED_IDCOLL', # 33 collection id (ECM) | ||
| 80 | 'ED_IDMNT', # 34 archive id from the extension (ECM) | ||
| 81 | 'ED_CLEGED5', # 35 extra archiving key (ECM) | ||
| 82 | 'ED_IDPMSIR', # 36 legal-entity / SIRET id (ECM) | ||
| 83 | ); | ||
| 84 | |||
| 85 | # Timestamp of the previous _log call, so it can print the elapsed time. | ||
| 86 | my $LOG_SEC0 = 0; | ||
| 87 | my $LOG_USEC0 = 0; | ||
| 88 | |||
| 89 | # Log one message on STDERR, with a timestamp, the caller location and (from the | ||
| 90 | # second call on) the elapsed time. Kept file-scoped rather than 'state' so the | ||
| 91 | # module stays free of feature pragmas. | ||
| 92 | sub _log { | ||
| 93 | my ($str) = @_; | ||
| 94 | $str //= q{}; | ||
| 95 | $str =~ s{^\s*}{ }sx; | ||
| 96 | my ( $package, $filename, $line ) = caller; | ||
| 97 | my ( $seconds, $microseconds ) = gettimeofday; | ||
| 98 | |||
| 99 | my $sec1 = $seconds - $LOG_SEC0; | ||
| 100 | my $usec1 = $microseconds - $LOG_USEC0; | ||
| 101 | if ( $usec1 < 0 ) { | ||
| 102 | $sec1--; | ||
| 103 | $usec1 += 1_000_000; | ||
| 104 | } | ||
| 105 | if ($LOG_SEC0) { | ||
| 106 | print STDERR sprintf( '-- elapsed: %d.%06d sec', $sec1, $usec1 ), "\n"; | ||
| 107 | } | ||
| 108 | ( $LOG_SEC0, $LOG_USEC0 ) = ( $seconds, $microseconds ); | ||
| 109 | |||
| 110 | my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($seconds); | ||
| 111 | my $timestamp = sprintf '%04d-%02d-%02d %02d:%02d:%02d.%06d', 1900 + $year, 1 + $mon, | ||
| 112 | $mday, $hour, $min, $sec, $microseconds; | ||
| 113 | print STDERR sprintf( "\n%s %s %s +%d%s", $timestamp, $package, $filename, $line, $str ), "\n"; | ||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Run a command, capture its streams and return its exit value. | ||
| 118 | sub my_system { | ||
| 119 | my ($cmd) = @_; | ||
| 120 | _log("Running command:\n$cmd"); | ||
| 121 | my ( $out, $err ); | ||
| 122 | run3 $cmd, undef, \$out, \$err; | ||
| 123 | my $ret = $?; | ||
| 124 | my ( $exit_value, $signal ) = ( $ret >> 8, $ret & 127 ); | ||
| 125 | _log("...done. exit_value=$exit_value signal=$signal"); | ||
| 126 | _log("...stdout:\n[[$out]]"); | ||
| 127 | _log("...stderr:\n[[$err]]"); | ||
| 128 | return $exit_value; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Utility function to construct filenames. | ||
| 132 | sub EDMS_idldoc_seqpg { | ||
| 133 | my ( $idldoc, $page ) = @_; | ||
| 134 | |||
| 135 | return sprintf( "${idldoc}_%07d", $page ); | ||
| 136 | } | ||
| 137 | |||
| 138 | # Package a DOC along with its index in a zip archive for later processing. | ||
| 139 | sub EDMS_prepare { | ||
| 140 | my ( $app, $idldoc, $doc_path, $idx_path ) = @_; | ||
| 141 | |||
| 142 | if ( !-e $doc_path ) { | ||
| 143 | carp "$doc_path don't exist"; | ||
| 144 | exit 0; | ||
| 145 | } | ||
| 146 | if ( !-e $idx_path ) { | ||
| 147 | carp "$idx_path don't exist"; | ||
| 148 | exit 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | my $cfg = config_read('EDOCMNGR'); | ||
| 152 | my $zip = Archive::Zip->new(); | ||
| 153 | $zip->addFile( $doc_path, "$app.$idldoc.pdf" ); | ||
| 154 | $zip->addFile( $idx_path, basename($idx_path) ); | ||
| 155 | |||
| 156 | my $zipfile = "$cfg->{'EDTK_DIR_EDOCMNGR'}/$app.$idldoc.out.zip"; | ||
| 157 | die "ERROR: Could not create zip achive \"$zipfile\"\n" | ||
| 158 | unless $zip->writeToFileNamed($zipfile) == AZ_OK; | ||
| 159 | print "$zipfile\n"; | ||
| 160 | |||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | # Package some documents along with one index in a zip archive. | ||
| 165 | sub EDMS_package { | ||
| 166 | my $app = shift; | ||
| 167 | my $idldoc = shift; | ||
| 168 | my @elements = @_; | ||
| 169 | |||
| 170 | my $cfg = config_read('EDOCMNGR'); | ||
| 171 | my $zip = Archive::Zip->new(); | ||
| 172 | |||
| 173 | foreach (@elements) { | ||
| 174 | $zip->addFile( $_, basename($_) ); | ||
| 175 | } | ||
| 176 | |||
| 177 | my $zipfile = "$cfg->{'EDTK_DIR_EDOCMNGR'}/$app.$idldoc.out.zip"; | ||
| 178 | die "ERROR: Could not create zip achive \"$zipfile\"\n" | ||
| 179 | unless $zip->writeToFileNamed($zipfile) == AZ_OK; | ||
| 180 | print "$zipfile\n"; | ||
| 181 | |||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub EDMS_process_zip { | ||
| 186 | my ( $zipfile, $outdir ) = @_; | ||
| 187 | |||
| 188 | my $zipname = basename($zipfile); | ||
| 189 | if ( $zipname !~ /^([^.]+)\.(.+)\.out\.zip$/ ) { | ||
| 190 | die "ERROR: Unexpected zip filename: $zipname\n"; | ||
| 191 | } | ||
| 192 | my ( $app, $idldoc ) = ( $1, $2 ); | ||
| 193 | |||
| 194 | my $zip = Archive::Zip->new(); | ||
| 195 | if ( $zip->read($zipfile) != AZ_OK ) { | ||
| 196 | die "ERROR: Could not read zip archive \"$zipfile\"\n"; | ||
| 197 | } | ||
| 198 | |||
| 199 | my @files = $zip->members(); | ||
| 200 | my ($idx_member) = $zip->membersMatching('\.idx1$'); | ||
| 201 | my ($doc_member) = $zip->membersMatching('\.pdf$'); | ||
| 202 | if ( !defined($doc_member) ) { | ||
| 203 | ($doc_member) = $zip->membersMatching('\.xls$'); | ||
| 204 | } | ||
| 205 | if ( !defined($doc_member) ) { | ||
| 206 | ($doc_member) = $zip->membersMatching('\.doc$'); | ||
| 207 | } | ||
| 208 | if ( !defined($doc_member) || !defined($idx_member) ) { | ||
| 209 | die "ERROR: Could not find document(s) or index file in archive\n"; | ||
| 210 | } | ||
| 211 | my $doc_name = $doc_member->fileName(); | ||
| 212 | my $idx_name = $idx_member->fileName(); | ||
| 213 | my $doc_path = $doc_name; | ||
| 214 | my $idx_path = $idx_name; | ||
| 215 | if ( defined($outdir) ) { | ||
| 216 | $doc_path = "$outdir/$doc_path"; | ||
| 217 | $idx_path = "$outdir/$idx_path"; | ||
| 218 | } | ||
| 219 | if ( $zip->extractMember( $doc_member, $doc_path ) != AZ_OK ) { | ||
| 220 | die "ERROR: Could not extract \"$doc_name\" from archive\n"; | ||
| 221 | } | ||
| 222 | if ( $zip->extractMember( $idx_member, $idx_path ) != AZ_OK ) { | ||
| 223 | die "ERROR: Could not extract \"$idx_name\" from archive\n"; | ||
| 224 | } | ||
| 225 | |||
| 226 | return EDMS_process( $app, $idldoc, $doc_name, $idx_name, $outdir ); | ||
| 227 | } | ||
| 228 | |||
| 229 | # Process document(s) with its index in a way suitable for the edms software. | ||
| 230 | sub EDMS_process { | ||
| 231 | my ( $app, $idldoc, $doc, $index, $outdir ) = @_; | ||
| 232 | |||
| 233 | _log("EDMS_process: app=$app idldoc=$idldoc doc=$doc index=$index outdir=$outdir"); | ||
| 234 | |||
| 235 | # Replace - and . with _ because Docubase cannot import names with dots. | ||
| 236 | $idldoc =~ s/[-\.]/_/g; | ||
| 237 | $app =~ s/[-\.]/_/g; | ||
| 238 | |||
| 239 | my $cfg = config_read('EDOCMNGR'); | ||
| 240 | my $format = $cfg->{'EDMS_IDX_FORMAT'}; | ||
| 241 | my @edmscols = split( /,/, $cfg->{'EDMS_INDEX_COLS'} ); | ||
| 242 | |||
| 243 | my $oldcwd; | ||
| 244 | if ( defined($outdir) ) { | ||
| 245 | $oldcwd = getcwd(); | ||
| 246 | chdir($outdir) | ||
| 247 | or die "ERROR: Cannot change current directory to \"$outdir\": $!\n"; | ||
| 248 | } | ||
| 249 | my @outfiles = (); | ||
| 250 | |||
| 251 | if ( $doc =~ /pdf$/i ) { | ||
| 252 | my $rv = | ||
| 253 | my_system( $cfg->{'EDMS_BIN_PDFTK'} . " $doc burst output ${app}_${idldoc}_%07d.pdf " ); | ||
| 254 | if ( $rv != 0 ) { | ||
| 255 | die "ERROR: Could not burst PDF file $doc!\n"; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | else { | ||
| 259 | my $cible = _docubase_file_name($doc); | ||
| 260 | move( $doc, $cible ) or die "ERROR: failed to move $cible ($doc)\n"; | ||
| 261 | push( @outfiles, $cible ); | ||
| 262 | } | ||
| 263 | |||
| 264 | if ( $format eq 'DOCUBASE' ) { | ||
| 265 | @outfiles = EDMS_idx_create_csv( $cfg, $index, $app, $idldoc, \@edmscols ); | ||
| 266 | } | ||
| 267 | elsif ( $format eq 'SCOPMASTER' ) { | ||
| 268 | @outfiles = EDMS_idx_create_xml( $cfg, $index, $app, $idldoc, \@edmscols ); | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | die "ERROR: Unexpected index format: $format\n"; | ||
| 272 | } | ||
| 273 | |||
| 274 | if ( $cfg->{'EDTK_TYPE_ENV'} ne 'Test' ) { | ||
| 275 | unlink($doc) if ( $doc =~ /pdf$/i ); | ||
| 276 | unlink('doc_data.txt'); # pdftk creates this one. | ||
| 277 | } | ||
| 278 | |||
| 279 | if ( defined($outdir) ) { | ||
| 280 | |||
| 281 | # Restore the original working directory. | ||
| 282 | chdir($oldcwd); | ||
| 283 | } | ||
| 284 | |||
| 285 | return @outfiles; | ||
| 286 | } | ||
| 287 | |||
| 288 | # Transfer the PDF files and the index to the EDMS application. On success it | ||
| 289 | # returns a true value: both callers guard with "EDMS_import(...) or die" | ||
| 290 | # (oEdtk::Run), so a false success return would make that guard fatal. | ||
| 291 | sub EDMS_import { | ||
| 292 | my ( $index, @docs ) = @_; | ||
| 293 | |||
| 294 | my $cfg = config_read('EDOCMNGR'); | ||
| 295 | my $ftp = Net::FTP->new( $cfg->{'EDMS_FTP_HOST'}, Port => $cfg->{'EDMS_FTP_PORT'} ) | ||
| 296 | or die "ERROR: Cannot connect to $cfg->{'EDMS_FTP_HOST'}: $@\n"; | ||
| 297 | $ftp->login( $cfg->{'EDMS_FTP_USER'}, $cfg->{'EDMS_FTP_PASS'} ) | ||
| 298 | or die "ERROR: Cannot login: " . $ftp->message() . "\n"; | ||
| 299 | $ftp->binary() | ||
| 300 | or die "ERROR: Cannot set binary mode: " . $ftp->message() . "\n"; | ||
| 301 | $ftp->cwd( $cfg->{'EDMS_FTP_DIR_DOCS'} ) | ||
| 302 | or die "ERROR: Cannot change working directory: " . $ftp->message() . "\n"; | ||
| 303 | |||
| 304 | # The index must be transferred last, otherwise the PDF files that have not | ||
| 305 | # been transferred yet will not be processed. | ||
| 306 | foreach my $doc (@docs) { | ||
| 307 | $ftp->put($doc) | ||
| 308 | or die "ERROR: Cannot upload DOC file: " . $ftp->message() . "\n"; | ||
| 309 | } | ||
| 310 | $ftp->cwd() | ||
| 311 | or die "ERROR: Cannot change working directory: " . $ftp->message() . "\n"; | ||
| 312 | $ftp->cwd( $cfg->{'EDMS_FTP_DIR_IDX'} ) | ||
| 313 | or die "ERROR: Cannot change working directory: " . $ftp->message() . "\n"; | ||
| 314 | $ftp->put($index) | ||
| 315 | or die "ERROR: Cannot upload index file: " . $ftp->message() . "\n"; | ||
| 316 | $ftp->quit(); | ||
| 317 | |||
| 318 | return 1; | ||
| 319 | } | ||
| 320 | |||
| 321 | # Read the initial index file, and call the given function for each new | ||
| 322 | # document. Also concatenate PDF files if needed (multi-page documents). | ||
| 323 | sub EDMS_idx_process($$$$&) { ## no critic (ProhibitSubroutinePrototypes) | ||
| 324 | my ( $app, $idx, $idldoc, $keys, $sub ) = @_; | ||
| 325 | |||
| 326 | _log("EDMS_idx_process: app=$app idx=$idx idldoc=$idldoc"); | ||
| 327 | |||
| 328 | open( my $fh, '<', $idx ) or die "ERROR: Cannot open \"$idx\": $!\n"; | ||
| 329 | my $csv = Text::CSV->new( { binary => 1, sep_char => ';' } ); | ||
| 330 | $csv->column_names(@INDEX_ECM_COLS); | ||
| 331 | my $lastdoc = 0; | ||
| 332 | my $firstpg = 0; | ||
| 333 | my $numpgs = 1; | ||
| 334 | my %docvals = (); | ||
| 335 | my $vals; | ||
| 336 | |||
| 337 | while ( $vals = $csv->getline_hr($fh) ) { | ||
| 338 | if ( $vals->{'ED_SEQDOC'} != $lastdoc ) { | ||
| 339 | if ( $lastdoc != 0 ) { | ||
| 340 | EDMS_merge_docs( $app, $idldoc, $firstpg, $numpgs ); | ||
| 341 | $sub->( \%docvals, $firstpg, $numpgs ); | ||
| 342 | undef(%docvals); | ||
| 343 | } | ||
| 344 | $lastdoc = $vals->{'ED_SEQDOC'}; | ||
| 345 | |||
| 346 | # Remember the values we are interested in for the edms. | ||
| 347 | foreach (@$keys) { | ||
| 348 | $docvals{$_} = $vals->{$_}; | ||
| 349 | } | ||
| 350 | $docvals{'ED_DOCLIB'} = $vals->{'ED_DOCLIB'}; | ||
| 351 | $firstpg = $vals->{'ED_IDSEQPG'}; | ||
| 352 | $numpgs = 1; | ||
| 353 | |||
| 354 | } | ||
| 355 | else { | ||
| 356 | |||
| 357 | # Remember the values we are interested in for the edms. | ||
| 358 | foreach (@$keys) { | ||
| 359 | $docvals{$_} = $vals->{$_} if $vals->{$_}; | ||
| 360 | } | ||
| 361 | $docvals{'ED_DOCLIB'} = $vals->{'ED_DOCLIB'}; | ||
| 362 | $numpgs++; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | close($fh); | ||
| 366 | |||
| 367 | # Handle the last document. | ||
| 368 | if ( $lastdoc != 0 ) { | ||
| 369 | EDMS_merge_docs( $app, $idldoc, $firstpg, $numpgs ); | ||
| 370 | $sub->( \%docvals, $firstpg, $numpgs ); | ||
| 371 | } | ||
| 372 | return 0; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub _docubase_file_name { | ||
| 376 | my $filename = shift; | ||
| 377 | |||
| 378 | $filename =~ s/(^.*)(\.\w{2,4}$)/$1/; | ||
| 379 | my $ext = $2 || ""; | ||
| 380 | $filename =~ s/[-\.]/_/g; | ||
| 381 | $filename .= $ext; | ||
| 382 | |||
| 383 | return $filename; | ||
| 384 | } | ||
| 385 | |||
| 386 | # Prepend the Alfresco header line to the index CSV, when the site activates it | ||
| 387 | # (EDMS_ACTIVATE_HEADER_ALFRESCO = YES). Default behaviour: do not activate. | ||
| 388 | sub preprend_alfresco_header_if_needed { | ||
| 389 | my ( $cfg, $fh, $csv ) = @_; | ||
| 390 | |||
| 391 | if ( exists $cfg->{'EDMS_ACTIVATE_HEADER_ALFRESCO'} | ||
| 392 | && $cfg->{'EDMS_ACTIVATE_HEADER_ALFRESCO'} eq 'YES' ) | ||
| 393 | { | ||
| 394 | my $_csv = Text::CSV->new( { sep_char => ';' } ); | ||
| 395 | if ( $_csv->parse( $cfg->{'EDMS_IDX_HEADER'} ) ) { | ||
| 396 | $csv->print( $fh, [ $_csv->fields() ] ); | ||
| 397 | } | ||
| 398 | else { | ||
| 399 | croak join( "\n", | ||
| 400 | 'Cannot parse config value EDMS_IDX_HEADER as a CSV record:', | ||
| 401 | $_csv->error_diag(), '', 'Config value EDMS_IDX_HEADER is:', | ||
| 402 | $_csv->error_input(), '' ); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | return 0; | ||
| 406 | } | ||
| 407 | |||
| 408 | # Create an EDMS index file in CSV format (for the EDMS application). | ||
| 409 | sub EDMS_idx_create_csv { | ||
| 410 | my ( $cfg, $idx, $app, $idldoc, $keys ) = @_; | ||
| 411 | |||
| 412 | my $csv = Text::CSV->new( { binary => 1, sep_char => ';', eol => "\n", quote_space => 0 } ); | ||
| 413 | my $edmsidx = "${app}_$idldoc.idx"; | ||
| 414 | open( my $fh, '>', $edmsidx ) or die "ERROR: Cannot create \"$edmsidx\": $!\n"; | ||
| 415 | |||
| 416 | preprend_alfresco_header_if_needed( $cfg, $fh, $csv ); | ||
| 417 | |||
| 418 | # Always return the index file as the first file in the list, see | ||
| 419 | # EDMS_import() for why this is important. | ||
| 420 | my @outfiles = ($edmsidx); | ||
| 421 | EDMS_idx_process( | ||
| 422 | $app, $idx, $idldoc, $keys, | ||
| 423 | sub { | ||
| 424 | my ( $vals, $firstpg, $numpgs ) = @_; | ||
| 425 | my $iddoc = $vals->{'ED_IDLDOC'}; | ||
| 426 | delete( $vals->{'ED_IDLDOC'} ); | ||
| 427 | if ( $vals->{'ED_DOCLIB'} =~ /pdf$/i ) { | ||
| 428 | $vals->{'EDMS_IDLDOC_SEQPG'} = EDMS_idldoc_seqpg( $iddoc, $firstpg ); | ||
| 429 | $vals->{'EDMS_FILENAME'} = | ||
| 430 | "${app}_" . EDMS_idldoc_seqpg( $idldoc, $firstpg ) . ".pdf"; | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | $vals->{'EDMS_FILENAME'} = _docubase_file_name( $vals->{'ED_DOCLIB'} ); | ||
| 434 | } | ||
| 435 | |||
| 436 | # Dates need to be in a specific format. | ||
| 437 | my $datefmt = $cfg->{'EDMS_DATE_FORMAT'}; | ||
| 438 | if ( $vals->{'ED_DTEDTION'} !~ /^(\d{4})(\d{2})(\d{2})$/ ) { | ||
| 439 | die "ERROR: Unexpected date format for ED_DTEDTION: $vals->{'ED_DTEDTION'}\n"; | ||
| 440 | } | ||
| 441 | my ( $year, $month, $day ) = ( $1, $2, $3 ); | ||
| 442 | $vals->{'EDMS_PROCESS_DT'} = | ||
| 443 | strftime( $datefmt, 0, 0, 0, $day, $month - 1, $year - 1900 ); | ||
| 444 | |||
| 445 | # Owner id for group access in edms. | ||
| 446 | if ( $vals->{'ED_IDEMET'} =~ /^\D{1}\d{3}/ ) { | ||
| 447 | $vals->{'ED_OWNER'} = $vals->{'ED_IDEMET'}; | ||
| 448 | } | ||
| 449 | else { | ||
| 450 | $vals->{'ED_OWNER'} = $vals->{'ED_SOURCE'}; | ||
| 451 | } | ||
| 452 | |||
| 453 | # Remove every ';' from the values (the fork's ECM constraint). | ||
| 454 | foreach my $key (@$keys) { | ||
| 455 | $vals->{$key} =~ s/;//g; | ||
| 456 | } | ||
| 457 | |||
| 458 | my @edmsvals = map { $vals->{$_} } @$keys; | ||
| 459 | $csv->print( $fh, \@edmsvals ); | ||
| 460 | |||
| 461 | push( @outfiles, $vals->{'EDMS_FILENAME'} ); | ||
| 462 | } | ||
| 463 | ); | ||
| 464 | close($fh); | ||
| 465 | return @outfiles; | ||
| 466 | } | ||
| 467 | |||
| 468 | # Create edms indexes in XML format (one per PDF file). | ||
| 469 | sub EDMS_idx_create_xml { | ||
| 470 | my ( $cfg, $idx, $app, $idldoc, $keys ) = @_; | ||
| 471 | |||
| 472 | my @outfiles = (); | ||
| 473 | EDMS_idx_process( | ||
| 474 | $app, $idx, $idldoc, $keys, | ||
| 475 | sub { | ||
| 476 | my ( $vals, $firstpg, $numpgs ) = @_; | ||
| 477 | |||
| 478 | my $docid = EDMS_idldoc_seqpg( $vals->{'ED_IDLDOC'}, $firstpg ); | ||
| 479 | delete( $vals->{'ED_IDLDOC'} ); | ||
| 480 | my $xmlfile = "$docid.edms.xml"; | ||
| 481 | $vals->{'ED_DOCLIB'} =~ /\.(\w{2,4})$/; | ||
| 482 | my $ext = $1; | ||
| 483 | |||
| 484 | open( my $fh, '>', $xmlfile ) or die "ERROR: Cannot create \"$xmlfile\": $!\n"; | ||
| 485 | my $xml = XML::Writer->new( OUTPUT => $fh, ENCODING => 'utf-8' ); | ||
| 486 | $xml->xmlDecl('utf-8'); | ||
| 487 | $xml->startTag('idxext'); | ||
| 488 | |||
| 489 | foreach my $pagenum ( 1 .. $numpgs ) { | ||
| 490 | $xml->startTag( 'page', num => $pagenum ); | ||
| 491 | if ( $pagenum == 1 ) { | ||
| 492 | while ( my ( $key, $val ) = each(%$vals) ) { | ||
| 493 | $xml->emptyTag( 'index', key => $key, value => $val ); | ||
| 494 | } | ||
| 495 | } | ||
| 496 | $xml->endTag('page'); | ||
| 497 | } | ||
| 498 | $xml->endTag('idxext'); | ||
| 499 | $xml->end(); | ||
| 500 | close($fh); | ||
| 501 | |||
| 502 | push( @outfiles, $xmlfile ); | ||
| 503 | push( @outfiles, "${app}_$docid.$ext" ); | ||
| 504 | } | ||
| 505 | ); | ||
| 506 | return @outfiles; | ||
| 507 | } | ||
| 508 | |||
| 509 | # Concatenate PDF documents if needed. | ||
| 510 | sub EDMS_merge_docs { | ||
| 511 | my ( $app, $idldoc, $firstpg, $numpgs ) = @_; | ||
| 512 | |||
| 513 | _log("EDMS_merge_docs: app=$app idldoc=$idldoc firstpg=$firstpg numpgs=$numpgs"); | ||
| 514 | my $cfg = config_read('EDOCMNGR'); | ||
| 515 | |||
| 516 | # A single-page document has nothing to concatenate. | ||
| 517 | return unless $numpgs > 1; | ||
| 518 | |||
| 519 | my $lastpg = $firstpg + $numpgs - 1; | ||
| 520 | my @pages = | ||
| 521 | map { "${app}_" . EDMS_idldoc_seqpg( $idldoc, $_ ) . ".pdf" } ( $firstpg .. $lastpg ); | ||
| 522 | my $output = "$pages[0].tmp"; | ||
| 523 | |||
| 524 | if ( defined $cfg->{'EDMS_BIN_GS'} && $cfg->{'EDMS_BIN_GS'} ne "" ) { | ||
| 525 | |||
| 526 | # PDFs created with pdftk are too heavy, so change the operating mode. | ||
| 527 | my $gs = | ||
| 528 | my_system( $cfg->{'EDMS_BIN_GS'} | ||
| 529 | . " -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$output @pages " ); | ||
| 530 | if ( $gs != 0 ) { | ||
| 531 | die "ERROR: Could not concatenate pages $firstpg to $lastpg!\n"; | ||
| 532 | } | ||
| 533 | } | ||
| 534 | else { | ||
| 535 | my $rv = | ||
| 536 | my_system( | ||
| 537 | $cfg->{'EDMS_BIN_PDFTK'} . " " . join( ' ', @pages ) . " cat output $output" ); | ||
| 538 | if ( $rv != 0 ) { | ||
| 539 | die "ERROR: Could not concatenate pages $firstpg to $lastpg!\n"; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | # Remove the old files and rename the concatenated PDF after the first page. | ||
| 544 | foreach (@pages) { | ||
| 545 | unlink($_); | ||
| 546 | } | ||
| 547 | move( $output, $pages[0] ); | ||
| 548 | return; | ||
| 549 | } | ||
| 550 | |||
| 551 | # Build the ECM index keys of one document from the raw request parameters. The | ||
| 552 | # fork exported this sub but never defined it; the mainline body is kept here. | ||
| 553 | sub EDMS_edidx_build(\%) { ## no critic (ProhibitSubroutinePrototypes) | ||
| 554 | my ($refOpt) = @_; | ||
| 555 | my $cfg = config_read('EDOCMNGR'); | ||
| 556 | |||
| 557 | # required index keys : ED_REFIDDOC, ED_CORP, ED_SOURCE, ED_IDDEST, ED_NOMDEST, ED_IDEMET, ED_OWNER, ED_CORP | ||
| 558 | # optional keys : ED_DTEDTION, ED_CLEGED1, ED_VILLDEST, ED_CLEGED2, ED_CLEGED3, ED_CLEGED4 | ||
| 559 | # computed/(re)computed keys : ED_DTEDTION, EDMS_IDLDOC_SEQPG, EDMS_FILENAME | ||
| 560 | |||
| 561 | # REQUIRED KEYS | ||
| 562 | if ( !defined $$refOpt{'ED_REFIDDOC'} or $$refOpt{'ED_REFIDDOC'} eq "" ) { | ||
| 563 | die "ERROR: ED_REFIDDOC required.\n"; | ||
| 564 | } | ||
| 565 | if ( !defined $$refOpt{'ED_SOURCE'} or $$refOpt{'ED_SOURCE'} eq "" ) { | ||
| 566 | die "ERROR: ED_SOURCE required.\n"; | ||
| 567 | } | ||
| 568 | if ( !defined $$refOpt{'ED_IDDEST'} or $$refOpt{'ED_IDDEST'} eq "" ) { | ||
| 569 | die "ERROR: ED_IDDEST required.\n"; | ||
| 570 | } | ||
| 571 | if ( !defined $$refOpt{'ED_NOMDEST'} or $$refOpt{'ED_NOMDEST'} eq "" ) { | ||
| 572 | die "ERROR: ED_NOMDEST required.\n"; | ||
| 573 | } | ||
| 574 | if ( !defined $$refOpt{'ED_IDEMET'} or $$refOpt{'ED_IDEMET'} eq "" ) { | ||
| 575 | die "ERROR: ED_IDEMET required.\n"; | ||
| 576 | } | ||
| 577 | if ( !defined $$refOpt{'ED_OWNER'} or $$refOpt{'ED_OWNER'} eq "" ) { | ||
| 578 | die "ERROR: ED_OWNER required.\n"; | ||
| 579 | } | ||
| 580 | if ( !defined $$refOpt{'ED_CORP'} or $$refOpt{'ED_CORP'} eq "" ) { | ||
| 581 | die "ERROR: ED_CORP required.\n"; | ||
| 582 | } | ||
| 583 | |||
| 584 | # COMPUTED KEYS | ||
| 585 | my $FILE_EXT = $$refOpt{'ED_FILENAME'}; | ||
| 586 | $FILE_EXT =~ s/^(.*\.)(\w+)$/$2/; | ||
| 587 | $$refOpt{'ED_FORMFLUX'} = uc($FILE_EXT); | ||
| 588 | |||
| 589 | my ( $sec, $min, $hour, $day, $month, $year ); | ||
| 590 | if ( !defined $$refOpt{'ED_DTEDTION'} || $$refOpt{'ED_DTEDTION'} !~ /^(\d{4})(\d{2})(\d{2})$/ ) | ||
| 591 | { | ||
| 592 | ( $sec, $min, $hour, $day, $month, $year ) = localtime(); | ||
| 593 | $month++; | ||
| 594 | $year += 1900; | ||
| 595 | } | ||
| 596 | else { | ||
| 597 | ( $year, $month, $day ) = ( $1, $2, $3 ); | ||
| 598 | } | ||
| 599 | |||
| 600 | # DATES NEED TO BE IN A SPECIFIC FORMAT. | ||
| 601 | my $datefmt = $cfg->{'EDMS_DATE_FORMAT'}; | ||
| 602 | $$refOpt{'ED_DTEDTION'} = strftime( $datefmt, 0, 0, 0, $day, $month - 1, $year - 1900 ); | ||
| 603 | $$refOpt{'ED_IDLDOC'} = oEdtk::Main::oe_ID_LDOC(); | ||
| 604 | $$refOpt{'ED_IDSEQPG'} = 1; | ||
| 605 | $$refOpt{'ED_SEQDOC'} = 1; | ||
| 606 | $$refOpt{'EDMS_IDLDOC_SEQPG'} = | ||
| 607 | EDMS_idldoc_seqpg( $$refOpt{'ED_IDLDOC'}, $$refOpt{'ED_IDSEQPG'} ); | ||
| 608 | $$refOpt{'EDMS_FILENAME'} = $$refOpt{'ED_REFIDDOC'} . "_" . $$refOpt{'EDMS_IDLDOC_SEQPG'}; | ||
| 609 | $$refOpt{'EDMS_FILENAME'} =~ s/[-\.\s]/_/g; | ||
| 610 | $$refOpt{'EDMS_FILENAME'} = $$refOpt{'EDMS_FILENAME'} . "." . $FILE_EXT; | ||
| 611 | $$refOpt{'ED_DOCLIB'} = $$refOpt{'EDMS_FILENAME'}; | ||
| 612 | |||
| 613 | # OPTIONAL KEYS | ||
| 614 | $$refOpt{'ED_VILLDEST'} |= ""; | ||
| 615 | $$refOpt{'ED_CLEGED1'} |= ""; | ||
| 616 | $$refOpt{'ED_CLEGED2'} |= ""; | ||
| 617 | $$refOpt{'ED_CLEGED3'} |= ""; | ||
| 618 | $$refOpt{'ED_CLEGED4'} |= ""; | ||
| 619 | return; | ||
| 620 | } | ||
| 621 | |||
| 622 | # Append one index line, in the EDMS_INDEX_COLS order, to the EDMS index file. | ||
| 623 | sub EDMS_edidx_write(\%) { ## no critic (ProhibitSubroutinePrototypes) | ||
| 624 | my ($refOpt) = shift; | ||
| 625 | my $cfg = config_read('EDOCMNGR'); | ||
| 626 | my @edms_cols = split( /,/, $cfg->{'EDMS_INDEX_COLS'} ); | ||
| 627 | my $index = $$refOpt{'ED_REFIDDOC'} . "_" . $$refOpt{'ED_IDLDOC'} . ".idx"; | ||
| 628 | |||
| 629 | open( my $fh, '>>', $index ) or die "ERROR: can't open $index : $!"; | ||
| 630 | my $csv = Text::CSV->new( { binary => 1, sep_char => ';', eol => "\n", quote_space => 0 } ); | ||
| 631 | |||
| 632 | my @fields; | ||
| 633 | foreach my $key (@edms_cols) { | ||
| 634 | push( @fields, $$refOpt{$key} ); | ||
| 635 | } | ||
| 636 | |||
| 637 | $csv->print( $fh, \@fields ); | ||
| 638 | close($fh); | ||
| 639 | return; | ||
| 640 | } | ||
| 641 | |||
| 642 | 1; | ||
| 643 | |||
| 644 | =head1 NAME | ||
| 645 | |||
| 646 | oEdtk::EDMS - electronic document management output: index files and archive transfer | ||
| 647 | |||
| 648 | =head1 SYNOPSIS | ||
| 649 | |||
| 650 | use oEdtk::EDMS qw(EDMS_process EDMS_import); | ||
| 651 | |||
| 652 | my @files = EDMS_process($app, $idldoc, $doc, $index, $outdir); | ||
| 653 | EDMS_import($index, @files) or die "transfer failed\n"; | ||
| 654 | |||
| 655 | =head1 DESCRIPTION | ||
| 656 | |||
| 657 | Builds the index files that feed an electronic document management system and | ||
| 658 | transfers them to it. A document and its per-page index are packed into a zip | ||
| 659 | archive, then unpacked and processed: PDF pages are burst or concatenated with | ||
| 660 | the configured helpers, and the index is rewritten in the format the site | ||
| 661 | expects (C<DOCUBASE> CSV or C<SCOPMASTER> XML). C<EDMS_import> uploads the | ||
| 662 | result over FTP, the index last so that the documents are already in place. | ||
| 663 | |||
| 664 | =head1 FUNCTIONS | ||
| 665 | |||
| 666 | =head2 EDMS_edidx_build | ||
| 667 | |||
| 668 | EDMS_edidx_build(\%options); | ||
| 669 | |||
| 670 | Fill the required ECM index keys of one document from the raw request | ||
| 671 | parameters. Dies when a required key is missing, and computes the derived keys | ||
| 672 | (the edition date, the document id, the page sequence and the file name). | ||
| 673 | |||
| 674 | =head2 EDMS_edidx_write | ||
| 675 | |||
| 676 | EDMS_edidx_write(\%options); | ||
| 677 | |||
| 678 | Append one index line, in the C<EDMS_INDEX_COLS> order, to the per-document | ||
| 679 | index file. | ||
| 680 | |||
| 681 | =head2 EDMS_idldoc_seqpg | ||
| 682 | |||
| 683 | my $id = EDMS_idldoc_seqpg($idldoc, $page); | ||
| 684 | |||
| 685 | Build the C<idldoc_NNNNNNN> identifier that names one page of a document. | ||
| 686 | |||
| 687 | =head2 EDMS_idx_create_csv | ||
| 688 | |||
| 689 | my @files = EDMS_idx_create_csv($cfg, $idx, $app, $idldoc, $keys); | ||
| 690 | |||
| 691 | Build the C<DOCUBASE> index in CSV form, one line per document, and return the | ||
| 692 | index file first followed by each document file name. | ||
| 693 | |||
| 694 | =head2 EDMS_import | ||
| 695 | |||
| 696 | EDMS_import($index, @docs) or die; | ||
| 697 | |||
| 698 | Upload the documents and then the index to the EDMS application over FTP. | ||
| 699 | Returns true on success, so callers can guard with C<or die>. | ||
| 700 | |||
| 701 | =head2 EDMS_package | ||
| 702 | |||
| 703 | EDMS_package($app, $idldoc, @elements); | ||
| 704 | |||
| 705 | Pack some documents together with one index into the standard out zip archive | ||
| 706 | and print the path of the archive. Returns true on success. | ||
| 707 | |||
| 708 | =head2 EDMS_prepare | ||
| 709 | |||
| 710 | EDMS_prepare($app, $idldoc, $doc_path, $idx_path); | ||
| 711 | |||
| 712 | Pack a document and its index into the standard out zip archive, or stop when | ||
| 713 | either file is missing. Returns true on success. | ||
| 714 | |||
| 715 | =head2 EDMS_process | ||
| 716 | |||
| 717 | my @files = EDMS_process($app, $idldoc, $doc, $index, $outdir); | ||
| 718 | |||
| 719 | Burst or move the document, build its index in the configured format, and | ||
| 720 | return the produced files. The optional C<$outdir> is used as a working | ||
| 721 | directory and restored afterwards. | ||
| 722 | |||
| 723 | =head2 EDMS_process_zip | ||
| 724 | |||
| 725 | my @files = EDMS_process_zip($zipfile, $outdir); | ||
| 726 | |||
| 727 | Read a standard out zip archive, extract its document and index, and return | ||
| 728 | L</EDMS_process> of the pair. | ||
| 729 | |||
| 730 | The following subs are internal helpers and are not part of the public API: | ||
| 731 | C<_log>, C<my_system>, C<EDMS_idx_process>, C<EDMS_merge_docs>, | ||
| 732 | C<EDMS_idx_create_xml>, C<_docubase_file_name> and | ||
| 733 | C<preprend_alfresco_header_if_needed>. | ||
| 734 | |||
| 735 | =head1 AUTHOR | ||
| 736 | |||
| 737 | The oEdtk team. | ||
| 738 | |||
| 739 | =cut |