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