konova/konova/management/commands/kspMigrater/intervention_migrater.py
mpeltriaux 06af30fc49 #132 WIP Minor change
* changed comment field string if conservation/registration office or handler not identifiable
2022-05-12 12:07:24 +02:00

306 lines
13 KiB
Python

import datetime
from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.utils import timezone
from codelist.models import KonovaCode
from codelist.settings import CODELIST_LAW_ID, CODELIST_PROCESS_TYPE_ID, CODELIST_HANDLER_ID, \
CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID
from compensation.models import Payment
from intervention.models import Intervention, InterventionDocument, Legal, Handler, Responsibility, Revocation
from konova.management.commands.kspMigrater.base_migrater import BaseMigrater
from konova.settings import ETS_GROUP
class InterventionMigrater(BaseMigrater):
def _migrate_intervention_responsibility(self, intervention, eiv):
fallback = "Kein Eintrag"
intervention.responsible = intervention.responsible or Responsibility.objects.create()
intervention.responsible.handler = intervention.responsible.handler or Handler.objects.create()
eiv_reg_off = eiv[7]
eiv_cons_off = eiv[9]
eiv_handler_type = eiv[11]
eiv_handler_detail = eiv[12]
eiv_reg_file_num = eiv[8]
eiv_cons_file_num = eiv[10]
if eiv_reg_file_num is None or len(eiv_reg_file_num) == 0:
eiv_reg_file_num = fallback
if eiv_cons_file_num is None or len(eiv_cons_file_num) == 0:
eiv_cons_file_num = fallback
if eiv_reg_off is not None and eiv_reg_off != 0:
try:
reg_office_code = KonovaCode.objects.get(
atom_id=eiv_reg_off,
is_leaf=True,
code_lists__in=[CODELIST_REGISTRATION_OFFICE_ID],
)
intervention.responsible.registration_office = reg_office_code
self._migrate_responsible_code_to_team(intervention, reg_office_code, "ZB")
except ObjectDoesNotExist:
intervention.responsible.registration_office = None
intervention.comment = f"{intervention.comment or ''}\nUneindeutige Zulassungsbehörde: {eiv_reg_off}"
intervention.responsible.registration_file_number = eiv_reg_file_num
if eiv_cons_off is not None and eiv_cons_off != 0:
try:
cons_office_code = KonovaCode.objects.get(
atom_id=eiv_cons_off,
is_leaf=True,
code_lists__in=[CODELIST_CONSERVATION_OFFICE_ID],
)
intervention.responsible.conservation_office = cons_office_code
self._migrate_responsible_code_to_team(intervention, cons_office_code, "ETS")
except ObjectDoesNotExist:
intervention.responsible.conservation_office = None
intervention.comment = f"{intervention.comment or ''}\nUneindeutige Eintragungsstelle: '{eiv_cons_off}"
intervention.responsible.conservation_file_number = eiv_cons_file_num
if eiv_handler_type is not None and eiv_handler_type != 0:
try:
handler_type_code = KonovaCode.objects.get(
atom_id=eiv_handler_type,
is_leaf=True,
code_lists__in=[CODELIST_HANDLER_ID]
)
if not handler_type_code.is_leaf:
handler_type_code = cons_office_code.long_name
raise handler_type_code
intervention.responsible.handler.type = handler_type_code
except ObjectDoesNotExist:
intervention.responsible.handler.type = None
intervention.comment = f"{intervention.comment or ''}\nNicht migrierbarer Eingriffsverursacher_TYP: {eiv_handler_type}"
intervention.responsible.handler.detail = eiv_handler_detail
intervention.responsible.handler.save()
intervention.responsible.save()
return intervention
def _migrate_intervention_legal(self, intervention, eiv):
intervention.legal = intervention.legal or Legal.objects.create()
eiv_process_type = eiv[5]
eiv_law = eiv[6]
eiv_date_registration = eiv[14]
eiv_date_binding = eiv[15]
if eiv_process_type is None or eiv_process_type == 0:
# Fallback "Genehmigung"
eiv_process_type = 623645439
process_type_code = KonovaCode.objects.get(
atom_id=eiv_process_type,
is_leaf=True,
code_lists__in=[CODELIST_PROCESS_TYPE_ID],
)
intervention.legal.process_type = process_type_code
if eiv_law is not None and eiv_law != 0:
law_code = KonovaCode.objects.get(
atom_id=eiv_law,
is_leaf=True,
code_lists__in=[CODELIST_LAW_ID],
)
intervention.legal.laws.add(law_code)
fallback_date = datetime.date.fromisoformat("1970-01-01")
if eiv_date_registration is None:
eiv_date_registration = fallback_date
intervention.comment += "\nKein Eintrag für Zulassungsdatum. Platzhalter 01.01.1970 gesetzt"
if eiv_date_binding is None:
eiv_date_binding = fallback_date
intervention.comment += "\nKein Eintrag für Bestandskraftdatum. Platzhalter 01.01.1970 gesetzt"
intervention.legal.binding_date = eiv_date_binding
intervention.legal.registration_date = eiv_date_registration
intervention.legal.save()
return intervention
def _migrate_intervention_payment(self, intervention, eiv):
payment_date = eiv[16]
payment_amount = eiv[17]
if payment_amount is not None and payment_amount != 0:
payment_exists = intervention.payments.filter(
amount=payment_amount
).exists()
if payment_exists:
return intervention
payment = Payment(
amount=payment_amount,
)
if payment_date is None:
payment.comment = "Datenmigration: Kein Zahlungsdatum hinterlegt! Schnellstmöglich nachtragen oder diesen Kommentar mit einer Begründung ersetzen, falls kein Datum existiert."
else:
payment.due_on = payment_date
payment.save()
intervention.payments.add(payment)
return intervention
def migrate(self):
self.connect_db()
cursor = self.db_connection.cursor()
cursor.execute(
'select '
'om."KENNUNG", '
'linf."OBJBEZ", '
'ST_AsEWKT(ST_Multi(ST_CollectionExtract(ST_MakeValid(ST_Transform(geomf.the_geom,4326)), 3))) as geomf, '
'ST_AsEWKT(ST_Multi(ST_CollectionExtract(ST_MakeValid(ST_Transform(geoml.the_geom,4326)), 2))) as geoml, '
'ST_AsEWKT(ST_Multi(ST_CollectionExtract(ST_MakeValid(ST_Transform(geomp.the_geom,4326)), 1))) as geomp, '
'eiv.verfahrenstyp, '
'vr.verfahrensrecht, '
'adr.behoerde as zb, '
'linf."AZ" as zb_az, '
'adr.adr_pruef as ets, '
'auf."AZ" as ets_az, '
'adr.adressrolle as eingriffsverursacher_typ, '
'adr."Bemerkung" as eingriffsverursacher_bemerkung, '
'linf."Bemerkung" as eiv_comment, '
'zt.erlass as Zulassungsdatum, '
'zt.rechtskraft as Bestandskraftdatum, '
'zt.baubeginn as ersatzzahlungstermin, '
'eiv.ersatzzahlung '
'from "OBJ_MASTER" om '
'left join "LINFOS" linf on om."GISPADID"=linf."GISPADID" '
'left join eiv on om."GISPADID"=eiv.gispadid '
'left join verfahrensrecht vr on om."GISPADID"=vr.gispadid '
'left join geometry_f geomf on om."GISPADID"=geomf.gispadid '
'left join geometry_l geoml on om."GISPADID"=geoml.gispadid '
'left join geometry_p geomp on om."GISPADID"=geomp.gispadid '
'left join adressrolle adr on om."GISPADID"=adr."GISPADID" '
'left join "Aufwertung" auf on om."GISPADID"=auf."GISPADID" '
'left join zulassungstermin zt on eiv.zulassung=zt.id '
'where '
'om."OKL"=7730085 and '
'om.archiv=false and '
'om.nicht_vollstaendig=0 '
)
all_eivs = cursor.fetchall()
len_all_eivs = len(all_eivs)
num_processed = 0
print(f"Migrate EIVs to interventions...")
print(f"--Found {len_all_eivs} entries. Process now...")
for eiv in all_eivs:
if num_processed % 500 == 0:
print(f"----{num_processed}/{len_all_eivs} processed")
with transaction.atomic():
eiv_comment = eiv[13] or ""
intervention = Intervention.objects.get_or_create(
identifier=eiv[0]
)[0]
intervention.title = eiv[1]
intervention.comment = eiv_comment
intervention = self._migrate_geometry(intervention, eiv)
intervention = self._migrate_intervention_legal(intervention, eiv)
intervention = self._migrate_intervention_responsibility(intervention, eiv)
intervention = self._migrate_intervention_payment(intervention, eiv)
intervention = self._migrate_documents(intervention, InterventionDocument, eiv)
intervention = self._migrate_log(intervention, eiv)
intervention = self._migrate_revocation(intervention, eiv)
intervention = self._migrate_recorded(intervention, eiv)
intervention.save()
num_processed += 1
cursor.close()
def _migrate_revocation(self, intervention, eiv):
identifier = f"'{eiv[0]}'"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'v.widerspruchvom '
'from "OBJ_MASTER" om '
'join vorgang v on om."GISPADID"=v.gispadid '
'where '
'v.widerspruchvom is not null and '
f'om."KENNUNG"={identifier}'
)
fetch_result = tmp_cursor.fetchall()
intervention.legal.revocations.all().delete()
for result in fetch_result:
revoc = Revocation.objects.get_or_create(
date=timezone.make_aware(result[0]).date(),
legal=intervention.legal,
comment="Migrierter Widerspruch"
)
tmp_cursor.close()
return intervention
def _migrate_recorded(self, instance, db_result):
""" Recording must be run after all data (including compensations) has been migrated initially!
"""
identifier = f"'{db_result[0]}'"
tmp_cursor = self.db_connection.cursor()
tmp_cursor.execute(
'select '
'v.verzeichnet, '
'om.alteintrag '
'from "OBJ_MASTER" om '
'left join vorgang v on om."GISPADID"=v.gispadid '
'where '
f'om."KENNUNG"={identifier} '
)
recorded_state_result = tmp_cursor.fetchone()
recorded_str = "'%verzeichnet%'"
tmp_cursor.execute(
'select '
'lb.erstelltam, '
'lb.erstelltvon '
'from "OBJ_MASTER" om '
'join logbuch lb on om."GISPADID"=lb.gispadid '
'where '
f'bemerkung like {recorded_str} and '
f'om."KENNUNG"={identifier} '
'order by lb.erstelltam limit 1'
)
recorded_state_ts_user = tmp_cursor.fetchone()
if instance.recorded is not None:
# Drop old info about recording
instance.recorded.delete()
instance.recorded = None
if recorded_state_result[0] is None:
# There are old entries which hold alteintrag=false but are old entries.
# This might be due to problems in the migration process in the past. Therefore,
# if there is no entry in table vorgang we act as if this is an old entry anyway,
# which needs to be recorded
to_be_recorded = True
else:
to_be_recorded = recorded_state_result[0] or recorded_state_result[1]
if to_be_recorded:
quality_checker = instance.quality_check()
if quality_checker.valid:
if recorded_state_result[1]:
recorded_on = instance.created.timestamp
recorded_by = instance.created.user.username
else:
if recorded_state_ts_user is None:
# This means the entry is not an old entry but has not been recorded in the old system.
# Nothing to do here!
return instance
recorded_on = timezone.make_aware(recorded_state_ts_user[0])
recorded_by = recorded_state_ts_user[1]
user = self._get_migrate_user(recorded_by)
# If user recorded an entry, he/she is an ets member
user.groups.add(
Group.objects.get(name=ETS_GROUP)
)
action = instance.set_recorded(user)
action.timestamp = recorded_on
action.save()
tmp_cursor.close()
return instance