db-migration #10

Merged
hsh-michaelhoennig merged 74 commits from db-migration into master 2024-01-23 15:11:24 +01:00
Showing only changes of commit 37e867c33d - Show all commits

View File

@ -57,7 +57,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@Import({ Context.class, JpaAttempt.class }) @Import({ Context.class, JpaAttempt.class })
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ImportBusinessPartners extends ContextBasedTest { public class ImportOfficeTables extends ContextBasedTest {
private static Map<Integer, HsOfficeContactEntity> contacts = new HashMap<>(); private static Map<Integer, HsOfficeContactEntity> contacts = new HashMap<>();
private static Map<Integer, HsOfficePersonEntity> persons = new HashMap<>(); private static Map<Integer, HsOfficePersonEntity> persons = new HashMap<>();
@ -81,8 +81,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importBusinessPartners() { void importBusinessPartners() {
try (Reader reader = resourceReader("migration/business-partners.csv")) { try (Reader reader = resourceReader("migration/business-partners.csv")) {
final var records = readAllLines(reader); final var lines = readAllLines(reader);
importBusinessPartners(records); importBusinessPartners(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -100,8 +100,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importContacts() { void importContacts() {
try (Reader reader = resourceReader("migration/contacts.csv")) { try (Reader reader = resourceReader("migration/contacts.csv")) {
final var records = readAllLines(reader); final var lines = readAllLines(reader);
importContacts(records); importContacts(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -149,8 +149,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importSepaMandates() { void importSepaMandates() {
try (Reader reader = resourceReader("migration/sepa-mandates.csv")) { try (Reader reader = resourceReader("migration/sepa-mandates.csv")) {
final var records = readAllLines(reader); final var lines = readAllLines(reader);
importSepaMandates(records); importSepaMandates(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -194,106 +194,121 @@ public class ImportBusinessPartners extends ContextBasedTest {
.build(); .build();
try (CSVReader csvReader = new CSVReaderBuilder(reader) try (CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(1) //.withSkipLines(1)
.withCSVParser(parser) .withCSVParser(parser)
.build()) { .build()) {
return csvReader.readAll(); return csvReader.readAll();
} }
} }
private void importBusinessPartners(final List<String[]> records) { private void importBusinessPartners(final String[] header, final List<String[]> records) {
final var columns = new Columns(header);
records.stream() records.stream()
.map(this::trimAll) .map(this::trimAll)
.forEach(record -> { .forEach(row -> {
final var record = new Record(columns, row);
// 0:bp_id;1:member_id;2:member_code;3:member_since;4:member_until;5:member_role;6:author_contract;7:nondisc_contract;8:free;9:exempt_vat;10:indicator_vat;11:uid_vat
final var person = HsOfficePersonEntity.builder() final var person = HsOfficePersonEntity.builder()
.personType(HsOfficePersonType.UNKNOWN) // TODO .personType(HsOfficePersonType.UNKNOWN) // TODO
.build(); .build();
persons.put(toInt(record[0]), person); persons.put(toInt(record.get("bp_id")), person);
final var partner = HsOfficePartnerEntity.builder() final var partner = HsOfficePartnerEntity.builder()
.details(HsOfficePartnerDetailsEntity.builder().build()) .details(HsOfficePartnerDetailsEntity.builder().build())
.contact(HsOfficeContactEntity.builder().build()) .contact(HsOfficeContactEntity.builder().build())
.person(person) .person(person)
.build(); .build();
partners.put(toInt(record[0]), partner); partners.put(toInt(row[0]), partner);
final var debitor = HsOfficeDebitorEntity.builder() final var debitor = HsOfficeDebitorEntity.builder()
.partner(partner) .partner(partner)
.debitorNumber(toInt(record[1])) .debitorNumber(toInt(row[1]))
// .memberCode(record[2]) TODO // .memberCode(record[2]) TODO
.partner(partner) .partner(partner)
.billingContact(partner.getContact()) .billingContact(partner.getContact())
// .free(toBool(record[8])) TODO // .free(toBool(record[8])) TODO
.vatBusiness("GROSS".equals(record[10])) .vatBusiness("GROSS".equals(row[10]))
.vatId(record[11]) .vatId(row[11])
.build(); .build();
debitors.put(toInt(record[0]), debitor); debitors.put(toInt(row[0]), debitor);
partners.put(toInt(record[0]), partner); partners.put(toInt(row[0]), partner);
if (isNotBlank(record[3])) { if (isNotBlank(row[3])) {
final var membership = HsOfficeMembershipEntity.builder() final var membership = HsOfficeMembershipEntity.builder()
.partner(partner) .partner(partner)
.memberNumber(toInt(record[1])) .memberNumber(toInt(row[1]))
.validity(toPostgresDateRange(localDate(record[3]), localDate(record[4]))) .validity(toPostgresDateRange(localDate(row[3]), localDate(row[4])))
.reasonForTermination( .reasonForTermination(
isBlank(record[4]) isBlank(row[4])
? HsOfficeReasonForTermination.NONE ? HsOfficeReasonForTermination.NONE
: HsOfficeReasonForTermination.UNKNOWN) // TODO : HsOfficeReasonForTermination.UNKNOWN) // TODO
.mainDebitor(debitor) .mainDebitor(debitor)
.build(); .build();
memberships.put(toInt(record[0]), membership); memberships.put(toInt(row[0]), membership);
} }
}); });
} }
private void importSepaMandates(final List<String[]> records) { private void importSepaMandates(final String[] header, final List<String[]> records) {
final var columns = new Columns(header);
records.stream() records.stream()
.map(this::trimAll) .map(this::trimAll)
.forEach(record -> { .forEach(row -> {
final var debitor = debitors.get(toInt(record[1])); final var record = new Record(columns, row);
final var debitor = debitors.get(toInt(row[1]));
final var sepaMandate = HsOfficeSepaMandateEntity.builder() final var sepaMandate = HsOfficeSepaMandateEntity.builder()
.debitor(debitor) .debitor(debitor)
.bankAccount(HsOfficeBankAccountEntity.builder() .bankAccount(HsOfficeBankAccountEntity.builder()
.holder(record[2]) .holder(row[2])
// .bankName(record[3]) // TODO // .bankName(record[3]) // TODO
.iban(record[4]) .iban(row[4])
.bic(record[5]) .bic(row[5])
.build()) .build())
.reference(record[6]) .reference(row[6])
.agreement(LocalDate.parse(record[7])) .agreement(LocalDate.parse(row[7]))
.validity(toPostgresDateRange( .validity(toPostgresDateRange(
toLocalDate(record[8]), toLocalDate(row[8]),
toLocalDate(record[9]))) toLocalDate(row[9])))
.build(); .build();
sepaMandates.put(toInt(record[0]), sepaMandate); sepaMandates.put(toInt(row[0]), sepaMandate);
bankAccounts.put(toInt(record[0]), sepaMandate.getBankAccount()); bankAccounts.put(toInt(row[0]), sepaMandate.getBankAccount());
}); });
} }
private void importContacts(final List<String[]> records) { private void importContacts(final String[] header, final List<String[]> records) {
final var columns = new Columns(header);
records.stream() records.stream()
.map(this::trimAll) .map(this::trimAll)
.forEach(record -> { .forEach(row -> {
if (isNotBlank(record[17]) && record[17].contains("billing")) { final var record = new Record(columns, row);
final var partner = partners.get(toInt(record[1])); if (isNotBlank(row[17]) && row[17].contains("billing")) {
final var partner = partners.get(toInt(row[1]));
final var person = partner.getPerson(); final var person = partner.getPerson();
person.setTradeName(record[6]); person.setTradeName(row[6]);
// TODO: title+salutation // TODO: title+salutation
person.setFamilyName(record[3]); person.setFamilyName(row[3]);
person.setGivenName(record[4]); person.setGivenName(row[4]);
initContact(partner.getContact(), record); initContact(partner.getContact(), row);
} else { } else {
initContact(new HsOfficeContactEntity(), record); initContact(new HsOfficeContactEntity(), row);
// TODO: create relationship // TODO: create relationship
} }
}); });
@ -416,4 +431,40 @@ public class ImportBusinessPartners extends ContextBasedTest {
return Files.newBufferedReader(filePath); return Files.newBufferedReader(filePath);
} }
private List<String[]> withoutFirstElement(final List<String[]> records) {
return records.subList(1, records.size()-1);
}
}
class Columns {
private final List<String> columnNames;
public Columns(final String[] header) {
columnNames = List.of(header);
}
int indexOf(final String columnName) {
int index = columnNames.indexOf(columnName);
if ( index < 0 ) {
throw new RuntimeException("column name '" + columnName + "' not found in: " + columnName);
}
return index;
}
}
class Record {
private final Columns columns;
private final String[] row;
public Record(final Columns columns, final String[] row) {
this.columns = columns;
this.row = row;
}
String get(final String columnName) {
return row[columns.indexOf(columnName)];
}
} }