use record[named-field] for column access

This commit is contained in:
Michael Hoennig 2024-01-05 14:39:00 +01:00
parent a042eff8bc
commit 1b3eef0e14

View File

@ -36,10 +36,12 @@ import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.stream;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@ -124,7 +126,7 @@ public class ImportOfficeTables extends ContextBasedTest {
assertThat(partners.toString()).isEqualToIgnoringWhitespace("""
{
7=partner(Michael, Mellies: Herr Michael Mellies ),
7=partner(Mellies, Michael: Herr Michael Mellies ),
10=partner(JM e.K.: Frau Dr. Jenny Meyer , JM e.K.),
12=partner(Test PS: Paule Schmidt , Test PS)
}
@ -139,21 +141,21 @@ public class ImportOfficeTables extends ContextBasedTest {
""");
assertThat(persons.toString()).isEqualToIgnoringWhitespace("""
{
7=person(personType='UNKNOWN', tradeName='', familyName='Michael', givenName='Mellies'),
10=person(personType='UNKNOWN', tradeName='JM e.K.', familyName='Jenny', givenName='Meyer'),
12=person(personType='UNKNOWN', tradeName='Test PS', familyName='Paule', givenName='Schmidt')
7=person(personType='UNKNOWN', tradeName='', familyName='Mellies', givenName='Michael'),
10=person(personType='UNKNOWN', tradeName='JM e.K.', familyName='Meyer', givenName='Jenny'),
12=person(personType='UNKNOWN', tradeName='Test PS', familyName='Schmidt', givenName='Paule')
}
""");
assertThat(debitors.toString()).isEqualToIgnoringWhitespace("""
{
7=debitor(10007: Michael, Mellies),
7=debitor(10007: Mellies, Michael),
10=debitor(10010: JM e.K.),
12=debitor(11012: Test PS)
}
""");
assertThat(memberships.toString()).isEqualToIgnoringWhitespace("""
{
7=Membership(10007, Michael, Mellies, 10007, [2000-12-06,), NONE),
7=Membership(10007, Mellies, Michael, 10007, [2000-12-06,), NONE),
10=Membership(10010, JM e.K., 10010, [2000-12-06,2016-01-01), UNKNOWN),
12=Membership(11012, Test PS, 11012, [2021-04-01,), NONE)
}
@ -210,7 +212,6 @@ public class ImportOfficeTables extends ContextBasedTest {
.build();
try (CSVReader csvReader = new CSVReaderBuilder(reader)
//.withSkipLines(1)
.withCSVParser(parser)
.build()) {
return csvReader.readAll();
@ -223,48 +224,50 @@ public class ImportOfficeTables extends ContextBasedTest {
records.stream()
.map(this::trimAll)
.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
.map(row -> new Record(columns, row))
.forEach(rec -> {
final var person = HsOfficePersonEntity.builder()
.personType(HsOfficePersonType.UNKNOWN) // TODO
.build();
persons.put(toInt(record.get("bp_id")), person);
persons.put(toInt(rec.get("bp_id")), person);
final var partner = HsOfficePartnerEntity.builder()
.details(HsOfficePartnerDetailsEntity.builder().build())
.contact(HsOfficeContactEntity.builder().build())
.person(person)
.build();
partners.put(toInt(row[0]), partner);
partners.put(toInt(rec.get("bp_id")), partner);
final var debitor = HsOfficeDebitorEntity.builder()
.partner(partner)
.debitorNumber(toInt(row[1]))
// .memberCode(record[2]) TODO
.debitorNumber(toInt(rec.get("member_id")))
// .memberCode(rec.get("member_code")) TODO
.partner(partner)
.billingContact(partner.getContact())
// .free(toBool(record[8])) TODO
.vatBusiness("GROSS".equals(row[10]))
.vatId(row[11])
// .memberRoles(toBool(rec.get("member_role")) TODO
// .authorContract(toBool(rec.get("author_contract")) TODO
// .nonDisclosureContract(toBool(rec.get("nondisc_contract")) TODO
// .free(toBool(rec.get("free")) TODO
// .vatExempt(toBool(rec.get("exempt_vat")) TODO
.vatBusiness("GROSS".equals(rec.get("indicator_vat")))
.vatId(rec.get("uid_vat"))
.build();
debitors.put(toInt(row[0]), debitor);
debitors.put(toInt(rec.get("bp_id")), debitor);
partners.put(toInt(row[0]), partner);
partners.put(toInt(rec.get("bp_id")), partner);
if (isNotBlank(row[3])) {
if (isNotBlank(rec.get("member_since"))) {
final var membership = HsOfficeMembershipEntity.builder()
.partner(partner)
.memberNumber(toInt(row[1]))
.validity(toPostgresDateRange(localDate(row[3]), localDate(row[4])))
.memberNumber(toInt(rec.get("member_id")))
.validity(toPostgresDateRange(localDate(rec.get("member_since")), localDate(rec.get("member_until"))))
.reasonForTermination(
isBlank(row[4])
isBlank(rec.get("member_until"))
? HsOfficeReasonForTermination.NONE
: HsOfficeReasonForTermination.UNKNOWN) // TODO
.mainDebitor(debitor)
.build();
memberships.put(toInt(row[0]), membership);
memberships.put(toInt(rec.get("bp_id")), membership);
}
});
}
@ -275,29 +278,27 @@ public class ImportOfficeTables extends ContextBasedTest {
records.stream()
.map(this::trimAll)
.forEach(row -> {
final var record = new Record(columns, row);
final var debitor = debitors.get(toInt(row[1]));
.map(row -> new Record(columns, row))
.forEach(rec -> {
final var debitor = debitors.get(toInt(rec.get("bp_id")));
final var sepaMandate = HsOfficeSepaMandateEntity.builder()
.debitor(debitor)
.bankAccount(HsOfficeBankAccountEntity.builder()
.holder(row[2])
// .bankName(record[3]) // TODO
.iban(row[4])
.bic(row[5])
.holder(rec.get("bank_customer"))
// .bankName(rec.get("bank_name")) // TODO
.iban(rec.get("bank_iban"))
.bic(rec.get("bank_bic"))
.build())
.reference(row[6])
.agreement(LocalDate.parse(row[7]))
.reference(rec.get("mandat_ref"))
.agreement(LocalDate.parse(rec.get("mandat_signed")))
.validity(toPostgresDateRange(
toLocalDate(row[8]),
toLocalDate(row[9])))
toLocalDate(rec.get("mandat_since")),
toLocalDate(rec.get("mandat_until"))))
.build();
sepaMandates.put(toInt(row[0]), sepaMandate);
bankAccounts.put(toInt(row[0]), sepaMandate.getBankAccount());
sepaMandates.put(toInt(rec.get("sepa_mandat_id")), sepaMandate);
bankAccounts.put(toInt(rec.get("sepa_mandat_id")), sepaMandate.getBankAccount());
});
}
@ -307,36 +308,34 @@ public class ImportOfficeTables extends ContextBasedTest {
records.stream()
.map(this::trimAll)
.forEach(row -> {
.map(row -> new Record(columns, row))
.forEach(rec -> {
if (isNotBlank(rec.get("roles")) && rec.get("roles").contains("billing")) {
final var record = new Record(columns, row);
if (isNotBlank(row[17]) && row[17].contains("billing")) {
final var partner = partners.get(toInt(row[1]));
final var partner = partners.get(toInt(rec.get("bp_id")));
final var person = partner.getPerson();
person.setTradeName(row[6]);
person.setTradeName(rec.get("firma"));
// TODO: title+salutation
person.setFamilyName(row[3]);
person.setGivenName(row[4]);
person.setGivenName(rec.get("first_name"));
person.setFamilyName(rec.get("last_name"));
initContact(partner.getContact(), row);
initContact(partner.getContact(), rec);
} else {
initContact(new HsOfficeContactEntity(), row);
initContact(new HsOfficeContactEntity(), rec);
// TODO: create relationship
}
});
}
private void initContact(final HsOfficeContactEntity contact, final String[] record) {
contacts.put(toInt(record[0]), contact);
private void initContact(final HsOfficeContactEntity contact, final Record rec) {
contacts.put(toInt(rec.get("contact_id")), contact);
contact.setLabel(toLabel(record[2], record[5], record[3], record[4], record[6]));
contact.setEmailAddresses(record[16]);
contact.setPostalAddress(toAddress(record));
contact.setPhoneNumbers(toPhoneNumbers(record));
contact.setLabel(toLabel(rec.get("salut"), rec.get("title"), rec.get("first_name"), rec.get("last_name"), rec.get("firma")));
contact.setEmailAddresses(rec.get("email"));
contact.setPostalAddress(toAddress(rec));
contact.setPhoneNumbers(toPhoneNumbers(rec));
}
private String[] trimAll(final String[] record) {
@ -347,45 +346,44 @@ public class ImportOfficeTables extends ContextBasedTest {
}
return record;
}
private String toPhoneNumbers(final String[] record) {
private String toPhoneNumbers(final Record rec) {
final var result = new StringBuilder("{\n");
if (isNotBlank(record[12]))
result.append(" \"private\": " + "\"" + record[12] + "\",\n");
if (isNotBlank(record[13]))
result.append(" \"office\": " + "\"" + record[13] + "\",\n");
if (isNotBlank(record[14]))
result.append(" \"mobile\": " + "\"" + record[14] + "\",\n");
if (isNotBlank(record[15]))
result.append(" \"fax\": " + "\"" + record[15] + "\",\n");
if (isNotBlank(rec.get("phone_private")))
result.append(" \"private\": " + "\"" + rec.get("phone_private") + "\",\n");
if (isNotBlank(rec.get("phone_office")))
result.append(" \"office\": " + "\"" + rec.get("phone_office") + "\",\n");
if (isNotBlank(rec.get("phone_mobile")))
result.append(" \"mobile\": " + "\"" + rec.get("phone_mobile") + "\",\n");
if (isNotBlank(rec.get("fax")))
result.append(" \"fax\": " + "\"" + rec.get("fax") + "\",\n");
return (result + "}").replace("\",\n}", "\"\n}");
}
private String toAddress(final String[] record) {
private String toAddress(final Record rec) {
final var result = new StringBuilder();
final var name = toName(record[2], record[5], record[3], record[4]);
final var name = toName(rec.get("salut"), rec.get("title"), rec.get("first_name"), rec.get("last_name"));
if (isNotBlank(name))
result.append(name + "\n");
if (isNotBlank(record[6]))
result.append(record[6] + "\n");
if (isNotBlank(record[7]))
result.append("c/o " + record[7] + "\n");
if (isNotBlank(record[8]))
result.append(record[8] + "\n");
final var zipcodeAndCity = toZipcodeAndCity(record);
if (isNotBlank(rec.get("firma")))
result.append(rec.get("firma") + "\n");
if (isNotBlank(rec.get("co")))
result.append("c/o " + rec.get("co") + "\n");
if (isNotBlank(rec.get("street")))
result.append(rec.get("street") + "\n");
final var zipcodeAndCity = toZipcodeAndCity(rec);
if (isNotBlank(zipcodeAndCity))
result.append(zipcodeAndCity + "\n");
return result.toString();
}
private String toZipcodeAndCity(final String[] record) {
private String toZipcodeAndCity(final Record rec) {
final var result = new StringBuilder();
if (isNotBlank(record[11]))
result.append(record[11] + " ");
if (isNotBlank(record[9]))
result.append(record[9] + " ");
if (isNotBlank(record[10]))
result.append(record[10]);
if (isNotBlank(rec.get("country")))
result.append(rec.get("country") + " ");
if (isNotBlank(rec.get("zipcode")))
result.append(rec.get("zipcode") + " ");
if (isNotBlank(rec.get("city")))
result.append(rec.get("city"));
return result.toString();
}
@ -448,7 +446,7 @@ public class ImportOfficeTables extends ContextBasedTest {
}
private static String[] justHeader(final List<String[]> lines) {
return lines.get(0);
return stream(lines.getFirst()).map(String::trim).toArray(String[]::new);
}
private List<String[]> withoutHeader(final List<String[]> records) {
@ -468,7 +466,7 @@ class Columns {
int indexOf(final String columnName) {
int index = columnNames.indexOf(columnName);
if ( index < 0 ) {
throw new RuntimeException("column name '" + columnName + "' not found in: " + columnName);
throw new RuntimeException("column name '" + columnName + "' not found in: " + columnNames);
}
return index;
}