File: Import.pm

package info (click to toggle)
eekboek 2.04-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,232 kB
  • sloc: perl: 19,733; sql: 370; ansic: 124; makefile: 35; lisp: 26
file content (207 lines) | stat: -rw-r--r-- 5,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#! perl --			-*- coding: utf-8 -*-

use utf8;

# Import.pm -- Import EekBoek administratie
# Author          : Johan Vromans
# Created On      : Tue Feb  7 11:56:50 2006
# Last Modified By: Johan Vromans
# Last Modified On: Thu Jan 14 20:51:53 2021
# Update Count    : 143
# Status          : Unknown, Use with caution!

package main;

our $cfg;
our $dbh;

package EB::Import;

use strict;
use warnings;

use EB;
use EB::Format;			# needs to be setup before we can use Schema
use EB::Tools::Attachments;

my $ident;

sub do_import {
    my ($self, $cmdobj, $opts) = @_;

    require EB::Tools::Schema;

    my $dir = $opts->{dir};
    if ( defined $dir ) {

	my $fail;

	$fail++, warn("?".__x("Directory {dir} bestaat niet",
			      dir => $dir)."\n") unless -d $dir;
	$fail++, warn("?".__x("Geen toegang tot directory {dir}",
			      dir => $dir)."\n") unless -r _ || -x _;

	-r "$dir/schema.dat"
	  or $fail++, warn("?".__x("Bestand \"{file}\" ontbreekt ({err})",
				   file => "schema.dat", err => $!)."\n");

	# Do not open these with :encoding(utf-8) -- we'll do it ourselves.
	open(my $relaties, "<", "$dir/relaties.eb")
	  or $fail++, warn("?".__x("Bestand \"{file}\" ontbreekt ({err})",
				   file => "relaties.eb", err => $!)."\n");
	open(my $opening, "<", "$dir/opening.eb")
	  or $fail++, warn("?".__x("Bestand \"{file}\" ontbreekt ({err})",
				   file => "opening.eb", err => $!)."\n");
	open(my $mutaties, "<", "$dir/mutaties.eb")
	  or $fail++, warn("?".__x("Bestand \"{file}\" ontbreekt ({err})",
				   file => "mutaties.eb", err => $!)."\n");

	if ( $fail ) {
	    die("?"._T("DE IMPORT IS NIET UITGEVOERD")."\n");
	}

	# To temporary suspend journaling.
	my $jnl_state = $cfg->val(qw(preferences journal), undef);

	# Delete daybook-associated shell functions.
	$cmdobj->_forget_cmds;

	# Create DB.
	$dbh->cleardb if $opts->{clean};

	# Schema.
	EB::Tools::Schema->create("$dir/schema.dat");
	$dbh->setup;

	# Add daybook-associated shell functions.
	$cmdobj->_plug_cmds;

	# Relaties, Opening, Mutaties.
	# Remember: These are executed in LIFO.
	$cmdobj->attach_lines(["journal --quiet $jnl_state"]) if $jnl_state;
	$cmdobj->attach_file($mutaties);
	$cmdobj->attach_file($opening);
	$cmdobj->attach_file($relaties);
	$cmdobj->attach_lines(["journal --quiet 0"]) if $jnl_state;

	my $att = EB::Tools::Attachments->new;
	my @atts = sort glob("$dir/" . ("[0-9]" x 8) . "_*");
	my $max_id = -1;
	foreach my $file ( @atts ) {
	    my ($id, $name) = substr($file, length($dir)+1) =~ m;^(\d+)_(.+);;
	    $att->{id} = 0+$id;
	    $max_id = $id if $id > $max_id;
	    $att->{name} = $name;
	    $att->store_from_file($file);
	}
	$dbh->set_sequence( "attachments_id_seq", $max_id+1 ) if $max_id > 0;
	return;
    }

    my $inp = $opts->{file};
    if ( defined $inp ) {

	eval { require Archive::Zip }
	  or die("?"._T("Module Archive::Zip, nodig voor import van file, is niet beschikbaar")."\n");

	open(my $zipf, "<", $inp)
	  or die("?".__x("Bestand \"{file}\" is niet beschikbaar ({err})",
			 file => $inp, err => $!)."\n");
	binmode($zipf);

	my $zip = Archive::Zip->new;
	my $status = $zip->read($zipf);
	die("?".__x("Fout {code} tijdens het lezen van {file}",
		    code => $status, file => $inp)."\n") if $status;

	my $c = $zip->zipfileComment;
	if ( $c ) {
	    warn("$inp: $c\n");
	}

	my $fail;

	my $d_schema   = $zip->contents("schema.dat");
	unless ( $d_schema ) {
	    warn("?".__x("Het schema ontbreekt in bestand {file}",
			 file => $inp)."\n");
	    $fail++;
	}

	my $d_relaties = $zip->contents("relaties.eb");
	unless ( $d_relaties ) {
	    warn("?".__x("De relatiegegevens ontbreken in bestand {file}",
			 file => $inp)."\n");
	    $fail++;
	}

	my $d_opening  = $zip->contents("opening.eb");
	unless ( $d_opening ) {
	    warn("?".__x("De openingsgegevens ontbreken in bestand {file}",
			 file => $inp)."\n");
	    $fail++;
	}

	my $d_mutaties = $zip->contents("mutaties.eb");
	unless ( $d_mutaties ) {
	    warn("?".__x("De mutatiegegevens ontbreken in bestand {file}",
			 file => $inp)."\n");
	    $fail++;
	}

	if ( $fail ) {
	    close($zipf);
	    die("?"._T("DE IMPORT IS NIET UITGEVOERD")."\n");
	}

	foreach ( $d_mutaties, $d_relaties, $d_opening, $d_schema ) {
	    # Do not recode, the input loop will do that for us.
	    $_ = [ map { "$_\n" } split(/[\n\r]+/, $_) ];
	}

	# To temporary suspend journaling.
	my $jnl_state = $cfg->val(qw(preferences journal), undef);

	# Delete daybook-associated shell functions.
	$cmdobj->_forget_cmds;

	# Create DB.
	$dbh->cleardb if $opts->{clean};

	# Schema.
	my @s = @$d_schema;	# copy for 2nd pass
	EB::Tools::Schema->_create1(sub { shift(@$d_schema) });
	EB::Tools::Schema->_create2(sub { shift(@s) });
	$dbh->setup;

	# Add daybook-associated shell functions.
	$cmdobj->_plug_cmds;

	# Relaties, Opening, Mutaties. In reverse order.
	$cmdobj->attach_lines(["journal --quiet $jnl_state"]) if $jnl_state;
	$cmdobj->attach_lines($d_mutaties);
	$cmdobj->attach_lines($d_opening );
	$cmdobj->attach_lines($d_relaties);
	$cmdobj->attach_lines(["journal --quiet 0"]) if $jnl_state;

	my @att = $zip->membersMatching( '^\d+_.+' );
	my $att = EB::Tools::Attachments->new;
	my $max_id = -1;
	foreach my $mem ( @att ) {
	    my ($id, $name) = $mem->fileName =~ m;^(\d+)_(.+);;
	    $att->{id} = 0+$id;
	    $max_id = $id if $id > $max_id;
	    $att->{name} = $name;
	    my $d = $mem->contents;
	    $att->{content} = \$d;
	    $att->store;
	}
	close($zipf);
	$dbh->set_sequence( "attachments_id_seq", $max_id+1 ) if $max_id > 0;
	return;
    }

    die("?ASSERT ERROR: missing --dir / --file in Import\n");
}

1;