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
@Import({ Context.class, JpaAttempt.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, HsOfficePersonEntity> persons = new HashMap<>();
@ -81,8 +81,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importBusinessPartners() {
try (Reader reader = resourceReader("migration/business-partners.csv")) {
final var records = readAllLines(reader);
importBusinessPartners(records);
final var lines = readAllLines(reader);
importBusinessPartners(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -100,8 +100,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importContacts() {
try (Reader reader = resourceReader("migration/contacts.csv")) {
final var records = readAllLines(reader);
importContacts(records);
final var lines = readAllLines(reader);
importContacts(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -149,8 +149,8 @@ public class ImportBusinessPartners extends ContextBasedTest {
void importSepaMandates() {
try (Reader reader = resourceReader("migration/sepa-mandates.csv")) {
final var records = readAllLines(reader);
importSepaMandates(records);
final var lines = readAllLines(reader);
importSepaMandates(lines.get(0), withoutFirstElement(lines));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -194,106 +194,121 @@ public class ImportBusinessPartners extends ContextBasedTest {
.build();
try (CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(1)
//.withSkipLines(1)
.withCSVParser(parser)
.build()) {
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()
.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()
.personType(HsOfficePersonType.UNKNOWN) // TODO
.build();
persons.put(toInt(record[0]), person);
persons.put(toInt(record.get("bp_id")), person);
final var partner = HsOfficePartnerEntity.builder()
.details(HsOfficePartnerDetailsEntity.builder().build())
.contact(HsOfficeContactEntity.builder().build())
.person(person)
.build();
partners.put(toInt(record[0]), partner);
partners.put(toInt(row[0]), partner);
final var debitor = HsOfficeDebitorEntity.builder()
.partner(partner)
.debitorNumber(toInt(record[1]))
.debitorNumber(toInt(row[1]))
// .memberCode(record[2]) TODO
.partner(partner)
.billingContact(partner.getContact())
// .free(toBool(record[8])) TODO
.vatBusiness("GROSS".equals(record[10]))
.vatId(record[11])
.vatBusiness("GROSS".equals(row[10]))
.vatId(row[11])
.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()
.partner(partner)
.memberNumber(toInt(record[1]))
.validity(toPostgresDateRange(localDate(record[3]), localDate(record[4])))
.memberNumber(toInt(row[1]))
.validity(toPostgresDateRange(localDate(row[3]), localDate(row[4])))
.reasonForTermination(
isBlank(record[4])
isBlank(row[4])
? HsOfficeReasonForTermination.NONE
: HsOfficeReasonForTermination.UNKNOWN) // TODO
.mainDebitor(debitor)
.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()
.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()
.debitor(debitor)
.bankAccount(HsOfficeBankAccountEntity.builder()
.holder(record[2])
.holder(row[2])
// .bankName(record[3]) // TODO
.iban(record[4])
.bic(record[5])
.iban(row[4])
.bic(row[5])
.build())
.reference(record[6])
.agreement(LocalDate.parse(record[7]))
.reference(row[6])
.agreement(LocalDate.parse(row[7]))
.validity(toPostgresDateRange(
toLocalDate(record[8]),
toLocalDate(record[9])))
toLocalDate(row[8]),
toLocalDate(row[9])))
.build();
sepaMandates.put(toInt(record[0]), sepaMandate);
bankAccounts.put(toInt(record[0]), sepaMandate.getBankAccount());
sepaMandates.put(toInt(row[0]), sepaMandate);
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()
.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();
person.setTradeName(record[6]);
person.setTradeName(row[6]);
// TODO: title+salutation
person.setFamilyName(record[3]);
person.setGivenName(record[4]);
person.setFamilyName(row[3]);
person.setGivenName(row[4]);
initContact(partner.getContact(), record);
initContact(partner.getContact(), row);
} else {
initContact(new HsOfficeContactEntity(), record);
initContact(new HsOfficeContactEntity(), row);
// TODO: create relationship
}
});
@ -416,4 +431,40 @@ public class ImportBusinessPartners extends ContextBasedTest {
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)];
}
}