Skip to content

Commit

Permalink
fix(vsup_ifis): merge relations from IS and STAG
Browse files Browse the repository at this point in the history
- Temporary append new STAG relations to IFIS.
- Do not delete relations missing from IS.
- Remove all chars from VZTAH_ID before comparison.
  • Loading branch information
zlamalp committed Oct 5, 2023
1 parent a921d88 commit 490b1e0
Showing 1 changed file with 112 additions and 16 deletions.
128 changes: 112 additions & 16 deletions send/vsup_ifis
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use Data::Dumper;
use Time::Piece;
use ScriptLock;
use VsupIfis;

use utf8;
use open qw/ :std :encoding(utf8) /;
binmode STDOUT, ":utf8";

sub handleContact;
Expand Down Expand Up @@ -120,6 +121,10 @@ my $is_inserted = 0;
my $is_foundAndUpdated = 0;
my $is_foundAndSkipped = 0;
my $is_deleted = 0;
# FIXME - temporary stag in own variable
my $stag_inserted = 0;
my $stag_foundAndUpdated = 0;
my $stag_foundAndSkipped = 0;

# Allow storing change info (AKT)
my $insertAkt = $dbh->prepare(qq{INSERT INTO $tableNameAkt (OSB_ID, KDYZAP, OBJ, PRIO) VALUES (?,sysdate,?,?)});
Expand Down Expand Up @@ -642,23 +647,107 @@ foreach my $key (sort keys %$is_data) {
}

# Delete unwanted relations of STU type
# FIXME - temporarily do not remove studies missing from IS, we must include studies from STAG
#my $deleteVztah_is = $dbh->prepare(qq{DELETE FROM $tableNameVzt where VZTAH_CISLO=? and TYP_VZTAHU='STU'});
#my $existingVztah_is = $dbh->prepare(qq{select VZTAH_CISLO, OSB_ID from $tableNameVzt where OSB_ID is not null and VZTAH_CISLO is not null and TYP_VZTAHU='STU'});
#$existingVztah_is->execute();
#while(my $row = $existingVztah_is->fetchrow_hashref()) {
# my $VZTAH_CISLO = $row->{VZTAH_CISLO};
# my $UCO = $row->{OSB_ID};
# unless (exists $is_data->{$VZTAH_CISLO}) {
# # STU vztah is not in IS source, remove from IFIS.
# if ($DEBUG == 1) { print "DELETE VZTAH RECORD CISLO_VZTAHU: $VZTAH_CISLO /STU \n"; }
# $deleteVztah_is->execute($VZTAH_CISLO);
# $is_deleted++;
# handleAkt($UCO, 'VZTAHY');
# }
#}
#$existingVztah_is->finish();

my $deleteVztah_is = $dbh->prepare(qq{DELETE FROM $tableNameVzt where VZTAH_CISLO=? and TYP_VZTAHU='STU'});
my $existingVztah_is = $dbh->prepare(qq{select VZTAH_CISLO, OSB_ID from $tableNameVzt where OSB_ID is not null and VZTAH_CISLO is not null and TYP_VZTAHU='STU'});
$existingVztah_is->execute();
while(my $row = $existingVztah_is->fetchrow_hashref()) {
my $VZTAH_CISLO = $row->{VZTAH_CISLO};
my $UCO = $row->{OSB_ID};
unless (exists $is_data->{$VZTAH_CISLO}) {
# STU vztah is not in IS source, remove from IFIS.
if ($DEBUG == 1) { print "DELETE VZTAH RECORD CISLO_VZTAHU: $VZTAH_CISLO /STU \n"; }
$deleteVztah_is->execute($VZTAH_CISLO);
$is_deleted++;
handleAkt($UCO, 'VZTAHY');
#
# PROCESS RELATIONS FROM STAG
#

# FIXME - for now it provides only studies for new students
my $stag_data = load_stag();

foreach my $key (sort keys %$stag_data) {

my $OSB_ID = $stag_data->{$key}->{'OSB_ID'};
my $NS = $stag_data->{$key}->{'NS'};
my $TYP_VZTAHU = $stag_data->{$key}->{'TYP_VZTAHU'};
my $DRUH_VZTAHU = $stag_data->{$key}->{'DRUH_VZTAHU'};
my $STU_FORMA = $stag_data->{$key}->{'STU_FORMA'};
my $STU_STAV = $stag_data->{$key}->{'STU_STAV'};
my $STU_PROGR = $stag_data->{$key}->{'STU_PROGR'};
my $STU_GRADUATE = $stag_data->{$key}->{'STU_GRADUATE'};
my $VZTAH_CISLO = $key;
my $VZTAH_OD = $stag_data->{$key}->{'VZTAH_OD'};
my $VZTAH_DO = $stag_data->{$key}->{'VZTAH_DO'};
my $KARTA_IDENT = $stag_data->{$key}->{'KARTA_IDENT'};
my $EXT_ID = $key; # same as VZTAH_CISLO

#If osb_id or typ_vztahu are undef, skip this record
if ((!defined($OSB_ID)) || (!defined($TYP_VZTAHU))) {
next;
}
}

$existingVztah_is->finish();
# If relation belongs to person which is missing in data from perun, skip it.
unless (exists $dataByKey->{$OSB_ID}) {
next;
}

#Check if record already exists in output database (IFIS)
my $personExists = $dbh->prepare(qq{select 1 from $tableNameVzt where OSB_ID=? AND TYP_VZTAHU=? AND VZTAH_CISLO=?});
$personExists->execute($OSB_ID, $TYP_VZTAHU, $VZTAH_CISLO);

if($personExists->fetch) {
if($DEBUG == 1) { print "FIND: UCO -> $OSB_ID, VZTAH_ID -> $VZTAH_CISLO\n"; }

my $select = "SELECT 1 from $tableNameVzt where OSB_ID=? and VZTAH_CISLO=? and EXT_ID=? and TYP_VZTAHU=?";
my @params = ($OSB_ID, $VZTAH_CISLO, $EXT_ID, $TYP_VZTAHU);

$select = appendParam(select => $select, params => \@params, paramName => 'NS', paramValue => $NS);
$select = appendParam(select => $select, params => \@params, paramName => 'DRUH_VZTAHU', paramValue => $DRUH_VZTAHU);
$select = appendParam(select => $select, params => \@params, paramName => 'STU_FORMA', paramValue => $STU_FORMA);
$select = appendParam(select => $select, params => \@params, paramName => 'STU_STAV', paramValue => $STU_STAV);
$select = appendParam(select => $select, params => \@params, paramName => 'STU_PROGR', paramValue => $STU_PROGR);
$select = appendParam(select => $select, params => \@params, paramName => 'STU_GRADUATE', paramValue => $STU_GRADUATE);
$select = appendParam(select => $select, params => \@params, paramName => 'OD', paramValue => $VZTAH_OD);
$select = appendParam(select => $select, params => \@params, paramName => 'DO_', paramValue => $VZTAH_DO);
$select = appendParam(select => $select, params => \@params, paramName => 'KARTA_IDENT', paramValue => $KARTA_IDENT);

# We need to know if these two records are without changes, if yes, skip them
my $recordAreEquals = $dbh->prepare($select);
$recordAreEquals->execute(@params);

if(!$recordAreEquals->fetch) {

my $updatePerson = $dbh->prepare(qq{UPDATE $tableNameVzt SET NS=? , DRUH_VZTAHU=? , STU_FORMA=? , STU_STAV=? , STU_PROGR=? , STU_GRADUATE=? , OD=? , DO_=? , KARTA_IDENT=? , EXT_ID=? WHERE OSB_ID=? AND VZTAH_CISLO=? AND TYP_VZTAHU=?});
$updatePerson->execute($NS, $DRUH_VZTAHU, $STU_FORMA, $STU_STAV, $STU_PROGR, $STU_GRADUATE, $VZTAH_OD, $VZTAH_DO, $KARTA_IDENT, $EXT_ID, $OSB_ID, $VZTAH_CISLO, $TYP_VZTAHU);
handleAkt($OSB_ID, 'VZTAHY');
if($DEBUG == 1) { print "UPDATING EXISTING RECORD: UCO -> $OSB_ID, VZTAH_ID -> $VZTAH_CISLO\n"; }
$stag_foundAndUpdated++;

} else {

if($DEBUG == 1) { print "SKIP RECORD: UCO -> $OSB_ID, VZTAH_ID -> $VZTAH_CISLO\n"; }
$stag_foundAndSkipped++;

}

} else {

if($DEBUG == 1) { print "INSERT NEW RECORD: UCO -> $OSB_ID, VZTAH_ID -> $VZTAH_CISLO\n"; }
$stag_inserted++;
#We will insert new record to output database (IFIS)
my $insertPerson = $dbh->prepare(qq{INSERT INTO $tableNameVzt (OSB_ID, NS, TYP_VZTAHU, DRUH_VZTAHU, STU_FORMA, STU_STAV, STU_PROGR, STU_GRADUATE, VZTAH_CISLO, OD, DO_, KARTA_IDENT, EXT_ID) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)});
$insertPerson->execute($OSB_ID, $NS, $TYP_VZTAHU, $DRUH_VZTAHU, $STU_FORMA, $STU_STAV, $STU_PROGR, $STU_GRADUATE, $VZTAH_CISLO, $VZTAH_OD, $VZTAH_DO, $KARTA_IDENT, $EXT_ID);
handleAkt($OSB_ID, 'VZTAHY');

}

}

#
# SET WHICH RELATIONS ARE 'HLAVNI'
Expand Down Expand Up @@ -787,8 +876,11 @@ for my $UCO (sort keys %$dataByKey) {
}
} elsif ($existingVztah->{$key}->{"TYP_VZTAHU"} eq "STU") {
if (exists $priorityMap->{3}) {
# VZTAH_ID from STAG contain chars, we must compare only numbers!
my $compareIdPriority = $priorityMap->{3}->{"VZTAH_CISLO"} =~ s/[^0-9]//gr;
my $compareIdExisting = $existingVztah->{$key}->{"VZTAH_CISLO"} =~ s/[^0-9]//gr;
# multiple STU VZTAH
unless ($priorityMap->{3}->{"VZTAH_CISLO"} gt $existingVztah->{$key}->{"VZTAH_CISLO"}) {
unless ($compareIdPriority gt $compareIdExisting) {
# next VZTAH has ID > current VZTAH
$priorityMap->{3}->{"VZTAH_CISLO"} = $existingVztah->{$key}->{"VZTAH_CISLO"};
}
Expand Down Expand Up @@ -889,6 +981,10 @@ print "Vztah (stu) inserted: \t$is_inserted\n";
print "Vztah (stu) skipped: \t$is_foundAndSkipped\n";
print "Vztah (stu) updated: \t$is_foundAndUpdated\n";
print "Vztah (stu) deleted: \t$is_deleted\n";
print "---------------------------------------\n";
print "Vztah (stu/stag) inserted: \t$stag_inserted\n";
print "Vztah (stu/stag) skipped: \t$stag_foundAndSkipped\n";
print "Vztah (stu/stag) updated: \t$stag_foundAndUpdated\n";
print "=======================================\n";

#
Expand Down

0 comments on commit 490b1e0

Please sign in to comment.