~bobjolliffe/+junk/dhistcl

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
package require TclCurl

set dw_user admin
set dw_passwd district
set hmis_user bobj
set hmis_passwd mZ1l1kaz1

#set dw http://localhost/datawarehouse
set dw http://$dw_user:$dw_passwd@localhost/datawarehouse
set hmis http://$hmis_user:$hmis_passwd@localhost/hmis

proc ignoreData {data} {
}

file delete -force export.zip

# Act 1 : exporting orgunits from hmis
# TODO:  need to trap some errors
set hmis_hnd [curl::init]
$hmis_hnd configure -url $hmis/dhis-web-importexport/exportMetaData.action
$hmis_hnd configure -headervar header
$hmis_hnd configure -postfields "exportFormat=DXF&organisationUnits=true&organisationUnitGroups=true&organisationUnitGroupSets=true&organisationUnitLevels=true"
$hmis_hnd configure -file export.zip 
if [catch {set res [$hmis_hnd perform ]} err] {
    puts stderr [curl::easystrerror $err]
    exit 1
}

$hmis_hnd cleanup
puts stderr "Retrieved orgunits from hmis"

# Got that.  Now on to part 2

# Act 2 : importing orgunits to datawarehouse
# Act 2 Scene 1 - we have to set the import type :-(
set dw_hnd [curl::init]

$dw_hnd configure -cookiefile "" 
$dw_hnd configure -writeproc ignoreData 
$dw_hnd configure -followlocation 1 

$dw_hnd configure -url "$dw/dhis-web-importexport/setImportFormat.action?importFormat=DXF"
set res [$dw_hnd perform ]   
if {$res} {
    puts stderr [curl::easystrerror $res]
    exit 1
}
puts stderr "set importFormat to DXF"

# Act 2 Scene 2 - we can import the metadata :-)
$dw_hnd configure -httppost [list name "type" contents "IMPORT" ] 
$dw_hnd configure -httppost [list name "incomingRecords" contents "NEW_AND_UPDATES"] 
$dw_hnd configure -httppost [list name "upload" file "export.zip" contenttype "application/zip"] 
$dw_hnd configure -httppost [list name "skipCheckMatching" contents "false"]
$dw_hnd configure -httppost [list name "dataValues" contents "false"]
$dw_hnd configure -url $dw/dhis-web-importexport/import.action

set res [$dw_hnd perform ]   
if {$res} {
    puts stderr [curl::easystrerror $res]
    exit 1
}

puts stderr "Export file sent to datawarehouse"

# Act 2 Scene 3 - monitor the status :-)

# should finish ok, but if not, self-destruct after 60s
after 60000 {exit 1}

proc checkStatus {} {
    $::dw_hnd configure -url $::dw/dhis-web-importexport/getImportStatus.action
    $::dw_hnd configure -bodyvar data
    $::dw_hnd perform 
    if {[regexp {statusMessage\>([^\<]*)} $data match msg]} {
	puts stderr $msg
	if [regexp completed|failed $msg] {
	    set ::done 1
	}
    }
    after 500 checkStatus
}

after 500 checkStatus

vwait done
puts stderr "done"