Unfinished draft of Unicorn bill converter
[migration-tools.git] / unicorn / unicorn_bills_to_tsv.pl
1 #!/usr/bin/perl -w
2
3 # Converts a Unicorn users.data, bill.data, or charge.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
5
6 my @records;
7 my $serial = -1;
8 my $line = 0;
9 my $section = '';
10 my $field = '';
11 my %unique_fields;
12 my @output_fields = qw( l_form l_user_id l_item_id l_billing_lib l_bill_date l_bill_amt l_bill_reason );
13
14 # Load each record
15 while (<>) {
16     s/\r\n/\n/g;
17 # print STDERR "Loaded this line: " . $_;
18
19         # Is this the start of a new record?
20         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
21                 $line = 0;
22                 $serial++;
23                 $section = ''; # just in case this didn't get reset in the previous record
24                 # print STDERR "Processing record $serial.\n";
25                 next;
26         }
27
28         # Is this a FORM= line?
29         if ( /^FORM=(.*)/ ) {
30                 $records[$serial]{'l_form'} = $1;
31                 next;
32         }
33
34         # If this isn't the start of the new record, it's a new line in the present record.
35         $line++;
36
37         # Is this line the beginning of a block of data (typically an address or a note)?
38         if ( /^\.(.*?)_BEGIN.$/ ) {
39                 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
40                 $section = "$1.";
41                 next;
42         }
43
44         # Is this line the beginning of a block of data (typically an address or a note)?
45         if ( /^\.(.*?)_END.$/ ) {
46                 if ("$1." ne $section) {
47                         print STDERR "Error in record $serial, line $line (input line $.): got an end-of-$1 but I thought I was in a $section block!\n";
48                 }
49                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
50                 $section = '';
51                 next;
52         }
53
54         # Looks like we've got some actual data!  Let's store it.
55         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
56         if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
57
58                 # Build the name of this field (taking note of whether we're in a named section of data)
59                 $field = '';
60                 if ($section ne '') { 
61                         $field .= $section;
62                 }
63                 $field .= $1;
64
65                 # Now we can actually store this line of data!
66 #FIXME: Assign it manually to one of the l_fields in the SQL comment below.
67                 $records[$serial]{$field} = $3;         
68
69                 # print STDERR "Data extracted: \$records[$serial]{'$field'} = '$3'\n";
70
71                 next;
72         }       
73
74         # This is the continuation of the previous line.
75         else {
76                 chomp($_);
77                 $records[$serial]{$field} .= ' ' . $_;
78                 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
79         }
80
81 }
82
83 print STDERR "Loaded " . scalar(@records) . " records.\n";
84
85
86 #CREATE OR REPLACE FUNCTION migration_tools.unicorn_create_money_table (TEXT) RETURNS VOID AS $$
87 #    DECLARE
88 #        migration_schema ALIAS FOR $1;
89 #    BEGIN
90 #        PERFORM migration_tools.exec( $1, 'CREATE TABLE ' || migration_schema || '.money_grocery_unicorn (
91 #            l_form TEXT NOT NULL CHECK ( l_form = ''LDBILL'' ),
92 #            l_user_id TEXT,
93 #            l_item_id TEXT,
94 #            l_billing_lib TEXT,
95 #            l_bill_date TEXT,
96 #            l_bill_amt TEXT,
97 #            l_bill_reason TEXT
98 #        ) INHERITS ( ' || migration_schema || '.money_grocery);' );
99 #    END;
100 #$$ LANGUAGE PLPGSQL STRICT STABLE;
101
102
103 # Print a header line
104 print "SERIAL\t";
105 foreach $i (@output_fields) {
106         print "$i\t";
107 }
108 print "\n";
109
110
111 # Print the results
112 for (my $u = 0; $u < @records; $u++) {
113         foreach $f (@output_fields) {
114                 if (defined $records[$u]{$f}) {
115                         print $records[$u]{$f};
116                 }
117         print "\t";
118         }
119         print "\n";
120 }
121
122 print STDERR "Wrote " . scalar(@records) . " records.\n";
123 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS