Commit 0097e724 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider

chore(access-importer): remove completely because obsolete

And it would certainly not work any more anyway
parent 5fde953a
Pipeline #94077 passed with stage
in 14 minutes and 18 seconds
module AccUtils
def make_mappable(table_name, primary_key, sanitize = false)
@acdb[table_name].map do |r|
[
r[primary_key].to_i,
sanitize ? sanitize_record(r) : r
]
end.to_h
end
# normalize hash keys from access db
# they can be with uppercase and special chars
#
def down_hkeys(row)
row.transform_keys { |key| key.to_s.underscore.to_sym }
end
def parse_int_fields(record, *keys)
record.merge(record.slice(*keys).compact.transform_values(&:to_i))
end
def parse_datetime_fields(record, *keys)
record.merge(record.slice(*keys).compact.transform_values(&:to_time))
end
def parse_float_fields(record, *keys)
record.merge(record.slice(*keys).compact.transform_values(&:to_f))
end
def parse_date_fields(record, *keys)
record.merge(record.slice(*keys).compact.transform_values(&:to_date))
end
def parse_boolean_fields(record, *keys)
record.merge(
parse_int_fields(record, *keys).slice(*keys).compact.transform_values do |val|
val == 1
end
)
end
def salutation(anrede, gender = nil)
return 'mrs' if anrede == 'Frau'
return 'mr' if anrede == 'Herr'
return 'mrs' if gender == 'female'
return 'mr' if gender == 'male'
nil
end
def email(h_email)
return generate_bogus_email if h_email.nil?
email = h_email.sub(/^\#mailto:/, '').sub(/\#$/, '')
return generate_bogus_email unless email.match?(Devise.email_regexp)
email
end
def generate_bogus_email
"unknown_email_#{Time.zone.now.to_f}@example.com"
end
def map_gender(value)
return nil unless value
return 'female' if value == 'W'
return 'male' if value == 'M'
end
def contact_attributes(haupt_person)
if haupt_person[:t_Telefon1].blank? && haupt_person[:t_Telefon2].present?
haupt_person[:t_Telefon1] = haupt_person[:t_Telefon2]
haupt_person[:t_Telefon2] = nil
end
{ contact_attributes: {
first_name: haupt_person[:t_Vorname] || 'unbekannt',
last_name: haupt_person[:t_Nachname] || 'unbekannt',
street: haupt_person[:t_Adresszeile1] || 'unbekannt',
extended: haupt_person[:t_Adresszeile2],
city: haupt_person[:city] || 'unbekannt',
postal_code: haupt_person[:postal_code] || '0000',
primary_email: haupt_person[:email],
primary_phone: haupt_person[:t_Telefon1] || 'Keine Nummer für Import vorhanden',
secondary_phone: haupt_person[:t_Telefon2]
} }
end
def import_attributes(access_name, access_id, related_records)
{ import_attributes: access_import(access_name, access_id, related_records) }
end
def access_import(access_name, access_id, related_records)
{
access_id: access_id,
base_origin_entity: access_name.to_s,
store: related_records
}
end
def language_skills_attributes(sprachen)
{ language_skills_attributes: map_sprachen_to_language_skills(sprachen) }
end
def map_sprachen_to_language_skills(sprachen)
return {} if sprachen.blank?
sprachen.map do |sprache|
[(Time.now.to_f * 1000).to_i,
{
language: sprache[:language][:lang],
level: sprache[:kenntnisstufe_ve].presence || 'basic'
}]
end.to_h
end
def now
Time.zone.now
end
def birth_year(geburtsdatum, jahrgang)
return geburtsdatum if geburtsdatum
Date.parse(jahrgang + '-06-01') if jahrgang
end
end
module AccessImportSetup
IMPORT_USER_EMAIL = 'aoz_access_importer@example.com'.freeze
IMPORT_USER_NAME = 'AOZ Import'.freeze
attr_reader :acdb
attr_reader :import_user
def initialize(path)
ObjectSpace.define_finalizer(self, self.class.finalize)
@import_user = create_or_fetch_import_user
@acdb = Mdb.open(path)
setup_class_variables(*instantiate_all_accessors)
@sprache_pro_hauptperson.add_other_accessors(@sprachen, @sprach_kenntnisse)
@einsatz_orte.add_other_accessors(@plz)
@haupt_person.add_other_accessors(@plz, @laender, @sprache_pro_hauptperson, @sprachen,
@sprach_kenntnisse)
@kontoangaben.add_other_accessors(@plz)
# don't overwrite imported accepted_at values
Volunteer.skip_callback(:save, :record_acceptance_change, if: :accepted_at?)
Client.skip_callback(:save, :record_acceptance_change, if: :accepted_at?)
end
def instantiate_all_accessors
Dir['lib/access_import/accessors/*.rb']
.map { |file| File.basename(file, '.*') }
.reject { |name| name == 'accessor' }
.map do |name|
name.camelize.constantize.new(@acdb)
end
end
def setup_class_variables(*accessors)
accessors.each do |accessor|
class_eval { attr_reader accessor.class.name.underscore.to_sym }
instance_variable_set("@#{accessor.class.name.underscore}", accessor)
end
end
# Create Import user, needed for creating records that depend on a creator user
#
def create_or_fetch_import_user
if User.with_deleted.exists?(email: IMPORT_USER_EMAIL)
return User.with_deleted.find_by(email: IMPORT_USER_EMAIL).restore
end
user = User.create!(email: IMPORT_USER_EMAIL, password: SecureRandom.hex(60), role: 'superadmin')
user.build_profile
user.profile.build_contact(first_name: IMPORT_USER_NAME, last_name: IMPORT_USER_NAME,
primary_email: IMPORT_USER_EMAIL, primary_phone: '0000', city: 'Zuerich', postal_code: '8000',
street: 'xxxxxx')
user.save!
user
end
# Shell Output methods
#
def shell_message(message)
puts message
end
def start_message(import_model)
shell_message "Start Importing #{import_model.to_s.classify.pluralize}"
end
# display amount of imports for models
def display_stats(*models)
models.each do |model|
shell_message stat_text(model)
end
end
def stat_text(model)
"Imported #{Import.where(importable_type: model.name)&.count} #{model.name.pluralize}."
end
def overall_stats
shell_message "Overall imported #{Import.count} records"
shell_message imported_stat_texts.join("\n")
end
def imported_stat_texts
[Assignment, Client, Department, GroupAssignment, GroupOfferCategory, GroupOffer, Hour, Journal,
Volunteer].map do |model|
stat_text(model)
end
end
end
# sort of singleton instance methods for the transformers
#
module AccessImportTransformers
def assignment_transform
@assignment_transform ||= AssignmentTransform.new(self, @begleitete, @freiwilligen_einsaetze,
@personen_rolle)
end
def client_transform
@client_transform ||= ClientTransform.new(self, @begleitete, @haupt_person, @familien_rollen,
@personen_rolle, @verfahrens_history)
end
def department_transform
@department_transform ||= DepartmentTransform.new(self, @einsatz_orte)
end
def einsatz_transform
@einsatz_transform ||= EinsatzTransform.new(self, @freiwilligen_einsaetze, @personen_rolle)
end
def group_offer_transform
@group_offer_transform ||= GroupOfferTransform.new(self, @freiwilligen_einsaetze, @rollen)
end
def group_assignment_transform
@group_assignment_transform ||= GroupAssignmentTransform.new(self, @begleitete,
@freiwilligen_einsaetze, @personen_rolle, @haupt_person)
end
def journal_transform
@journal_transform ||= JournalTransform.new(self, @freiwilligen_einsaetze, @journale,
@personen_rolle, @haupt_person)
end
def kurs_transform
@kurs_transform ||= KursTransform.new(self, @kurse, @begleitete, @haupt_person,
@familien_rollen, @personen_rolle, @kursarten, @freiwilligen_einsaetze, @einsatz_orte)
end
def kursart_transform
@kursart_transform ||= KursartTransform.new(self, @kursarten)
end
def volunteer_transform
@volunteer_transform ||= VolunteerTransform.new(self, @haupt_person, @personen_rolle,
@kontoangaben, @stundenerfassung)
end
def billing_expense_transform
@billing_expense_transform ||= BillingExpenseTransform.new(self, @haupt_person, @personen_rolle,
@stundenerfassung, @freiwilligen_entschaedigung)
end
def hour_transform
@hour_transform ||= HourTransform.new(self, @haupt_person, @personen_rolle, @stundenerfassung,
@freiwilligen_einsaetze)
end
end
class Ausbildungen < Accessor
def hash_all
make_mappable(:tbl_Ausbildungen, :pk_Ausbildung, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec,
:pk_Ausbildung, :fk_Hauptperson, :fk_AusbildungsTyp, :fk_FreiwilligenEinsatz)
rec[:ausbildung] = AUSBILDUNGS_TYPEN[rec[:fk_AusbildungsTyp]]
parse_datetime_fields(rec, :d_MutDatum).except(:fk_AusbildungsTyp)
end
def where_haupt_person(hauptperson_id)
all.select do |_key, ausbildung|
ausbildung[:fk_Hauptperson] == hauptperson_id.to_i
end
end
end
class Begleitete < Accessor
def hash_all
make_mappable(:tbl_Begleitete, :pk_Begleitete, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :fk_FamilienRolle, :fk_PersonenRolle, :pk_Begleitete, :z_Jahrgang)
rec = parse_datetime_fields(rec, :d_MutDatum)
rec[:gender] = map_gender(rec[:t_Geschlecht])
rec[:birth_year] = Date.ordinal(rec[:z_Jahrgang]) if rec[:z_Jahrgang]
rec[:relation] = map_familien_rolle(rec)
rec
end
def where_personen_rolle(personen_rolle_id)
all.select do |_key, personen_rolle|
personen_rolle[:fk_PersonenRolle] == personen_rolle_id.to_i
end
end
def find_with_personenrolle(begleitet_id)
begleitet = find(begleitet_id)
begleitet.merge(personen_rolle: @personen_rolle.find(begleitet[:fk_PersonenRolle]))
end
# :wife, :husband, :mother, :father, :daughter, :son, :sister, :brother, :aunt, :uncle
# 1: <keine>, 2: 'Hauptperson', 3: 'Ehepartner/in', 4: 'Kind', 5: 'Geschwister', 6: 'Eltern'
#
def map_familien_rolle(record)
return nil if [nil, 1, 2].include? record[:fk_FamilienRolle]
return handle_female(record) if record[:gender] == 'female'
handle_male(record) if record[:gender] == 'male'
end
def handle_female(record)
return 'mother' if record[:fk_FamilienRolle] == 6
return 'sister' if record[:fk_FamilienRolle] == 5
return 'wife' if record[:fk_FamilienRolle] == 3
'daughter' if record[:fk_FamilienRolle] == 4
end
def handle_male(record)
return 'father' if record[:fk_FamilienRolle] == 6
return 'brother' if record[:fk_FamilienRolle] == 5
return 'husband' if record[:fk_FamilienRolle] == 3
'son' if record[:fk_FamilienRolle] == 4
end
end
class FallstelleTeilnehmer < Accessor
def hash_all
make_mappable(:tbl_FallStelleProTeilnehmer, :pk_FallStelleProTeilnehmer, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_FallStelleProTeilnehmer, :fk_PersonenRolle, :fk_Fallstelle,
:fk_Kontaktperson)
parse_datetime_fields(rec, :d_MutDatum)
end
end
class FreiwilligenEinsaetze < Accessor
def hash_all
make_mappable(:tbl_FreiwilligenEinsätze, :pk_FreiwilligenEinsatz, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_FreiwilligenEinsatz, :fk_PersonenRolle, :fk_FreiwilligenFunktion,
:fk_Kostenträger, :fk_EinsatzOrt, :fk_Begleitete, :fk_Kurs, :fk_Semester, :fk_Lehrmittel, :z_FamilienBegleitung)
rec = parse_float_fields(rec, :z_Spesen)
rec[:cost_unit] = cost_unit(rec[:fk_Kostenträger])
rec[:fk_FreiwilligenFunktion] = 0 unless rec[:fk_FreiwilligenFunktion]
rec[:funktion] = freiwilligen_funktion(rec[:fk_FreiwilligenFunktion]).bezeichnung
rec[:lehrmittel] = LEHRMITTEL[rec[:fk_Lehrmittel]] if rec[:fk_Lehrmittel]
rec[:semester] = SEMESTER[rec[:fk_Semester]] if rec[:fk_Semester]
rec = parse_boolean_fields(rec, :b_Probezeitbericht, :b_LP1, :b_LP2, :b_Bücher)
parse_datetime_fields(rec, :d_EinsatzVon, :d_EinsatzBis, :d_Probezeit, :d_Hausbesuch,
:d_ErstUnterricht, :d_Standortgespräch, :d_MutDatum)
.except(:fk_Lehrmittel, :fk_Semester)
end
def where_begleitung
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_FreiwilligenFunktion] == FREIWILLIGEN_FUNKTION_BY_NAME.begleitung.id
end
end
def where_kurs
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_FreiwilligenFunktion] == FREIWILLIGEN_FUNKTION_BY_NAME.kurs.id
end
end
def where_animation_f
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_FreiwilligenFunktion] == FREIWILLIGEN_FUNKTION_BY_NAME.animation_f.id
end
end
def where_kurzeinsatz
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_FreiwilligenFunktion] == FREIWILLIGEN_FUNKTION_BY_NAME.kurzeinsatz.id
end
end
def where_andere
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_FreiwilligenFunktion] == FREIWILLIGEN_FUNKTION_BY_NAME.andere.id
end
end
def where_begleitete(begleitet_id)
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_Begleitete] == begleitet_id.to_i
end
end
def where_einsatz_ort(einsatz_ort_id)
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_EinsatzOrt] == einsatz_ort_id.to_i
end
end
def where_personen_rolle(personen_rolle_id)
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_PersonenRolle] == personen_rolle_id.to_i
end
end
def where_kurs(kurs_id)
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_Kurs] == kurs_id.to_i
end
end
end
class FreiwilligenEntschaedigung < Accessor
def hash_all
make_mappable(:tbl_FreiwilligenEntschädigung, :pk_FreiwilligenEntschädigung, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_FreiwilligenEntschädigung, :fk_PersonenRolle, :fk_Semester,
:z_Semesterjahr, :z_Jahr, :z_Einsätze, :z_Stunden, :z_Betrag, :z_Spesen, :z_KST, :z_AOZKonto)
rec = parse_float_fields(rec, :z_Total)
rec[:semester] = rec[:fk_Semester]
parse_datetime_fields(rec, :d_MutDatum, :d_Datum).except(:fk_Semester)
end
def where_personen_rolle(pr_id)
all.select do |_key, fw_einsatz|
fw_einsatz[:fk_PersonenRolle] == pr_id.to_i
end
end
end
class Journale < Accessor
def hash_all
make_mappable(:tbl_Journal, :pk_Journal, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_Journal, :fk_Hauptperson, :fk_JournalKategorie,
:fk_FreiwilligenEinsatz)
rec[:kategorie] = JOURNAL_KATEGORIEN[rec[:fk_JournalKategorie]]
parse_datetime_fields(rec, :d_MutDatum, :d_ErfDatum)
end
def where_haupt_person(hp_id)
all.select do |_key, journal|
journal[:fk_Hauptperson] == hp_id.to_i
end
end
end
class Kontoangaben < Accessor
def hash_all
make_mappable(:tbl_Kontoangaben, :pk_Kontoangabe, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_Kontoangabe, :fk_Hauptperson, :z_KontoArt, :fk_PlzOrt,
:z_ClearingNummer)
rec[:city] = "#{ort[:t_Ort]} #{ort[:t_PLZ]}" if ort(rec[:fk_PlzOrt]).present?
parse_datetime_fields(rec, :d_MutDatum)
end
def ort(plz_id = nil)
@ort ||= @plz.find(plz_id)
end
def where_haupt_person(hp_id)
related = all.find_all do |_key, kontoangabe|
kontoangabe[:fk_Hauptperson] == hp_id.to_i
end
return if related.blank?
return related.first.last if related.size == 1
related.sort_by { |_, angabe| angabe[:d_MutDatum] }.last.last
end
end
class Laender < Accessor
def hash_all
make_mappable(:tbl_Länder, :pk_Land, true)
end
def laender_map
@laender_map ||= CSV.read('lib/access_import/accessors/laender_map_alpha2.csv',
headers: true, converters: :numeric, header_converters: :symbol).map do |row|
[row[:pk_land], row[:alpha2]]
end.to_h
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_Land)
rec[:alpha2] = laender_map[rec[:pk_Land]]
rec
end
end
class PersonenRolle < Accessor
def hash_all
make_mappable(:tbl_Personenrollen, :pk_PersonenRolle, true)
end
def sanitize_record(rec)
rec = parse_datetime_fields(rec, :d_MutDatum, :d_Rollenbeginn, :d_Rollenende)
rec = parse_int_fields(rec, :b_EinführungsKurs, :b_Interesse, :b_SpesenVerzicht,
:fk_Hauptperson, :fk_Kostenträger, :pk_PersonenRolle, :z_AnzErw, :z_AnzKind,
:z_Familienverband, :z_Rolle)
rec[:rolle] = ACCESS_TO_OWN_ROLES_MAP[rec[:z_Rolle]]
rec[:cost_unit] = cost_unit(rec[:fk_Kostenträger])
rec
end
ACCESS_TO_OWN_ROLES_MAP = {
1 => 'Volunteer',
2 => 'Client',
3 => 'Animator',
4 => 'Participant'
}.freeze
def find_by_hauptperson(hauptperson_id)
all.find do |_, personen_rolle|
personen_rolle[:fk_Hauptperson] == hauptperson_id
end.last
end
def all_volunteers
all.select do |_, personen_rolle|
personen_rolle[:z_Rolle] == ACCESS_ROLES.volunteer
end
end
def all_clients
all.select do |_, personen_rolle|
personen_rolle[:z_Rolle] == ACCESS_ROLES.client
end
end
def all_animators
all.select do |_, personen_rolle|
personen_rolle[:z_Rolle] == ACCESS_ROLES.animator
end
end
def all_participants
all.select do |_, personen_rolle|
personen_rolle[:z_Rolle] == ACCESS_ROLES.participant
end
end
end
class Stundenerfassung < Accessor
def hash_all
make_mappable(:tbl_Stundenerfassung, :pk_Stundenerfassung, true)
end
def sanitize_record(rec)
rec = parse_int_fields(rec, :pk_Stundenerfassung, :fk_PersonenRolle, :fk_FreiwilligenEinsatz,
:fk_Semester, :z_Quartal, :z_Jahr, :z_Einsatzzahl)
rec = parse_float_fields(rec, :z_Stundenzahl, :z_Spesen)
parse_datetime_fields(rec, :d_MutDatum)
end