make contacts.csv easier readable by empty lines, comments and better numbering

This commit is contained in:
Michael Hoennig 2024-01-16 15:49:53 +01:00
parent ea2bd24035
commit 7c721f8450

View File

@ -35,9 +35,7 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
@ -45,10 +43,12 @@ import java.time.LocalDate;
import java.util.*;
import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
/*
* This 'test' includes the complete legacy 'office' data import.
@ -196,11 +196,11 @@ public class ImportOfficeTables extends ContextBasedTest {
""");
assertThat(contacts.toString()).isEqualToIgnoringWhitespace("""
{
71=contact(label='Herr Michael Mellies ', emailAddresses='mih@example.org'),
101=contact(label='Frau Dr. Jenny Meyer-Billing , JM e.K.', emailAddresses='jm-billing@example.org'),
102=contact(label='Herr Andrew Meyer-Operation , JM e.K.', emailAddresses='am-operation@example.org'),
103=contact(label='Herr Philip Meyer-Contract , JM e.K.', emailAddresses='pm-contractual@example.org'),
121=contact(label='Petra Schmidt , Test PS', emailAddresses='ps@example.com')
1101=contact(label='Herr Michael Mellies ', emailAddresses='mih@example.org'),
1201=contact(label='Frau Dr. Jenny Meyer-Billing , JM e.K.', emailAddresses='jm-billing@example.org'),
1202=contact(label='Herr Andrew Meyer-Operation , JM e.K.', emailAddresses='am-operation@example.org'),
1203=contact(label='Herr Philip Meyer-Contract , JM e.K.', emailAddresses='pm-partner@example.org'),
1301=contact(label='Petra Schmidt , Test PS', emailAddresses='ps@example.com')
}
""");
assertThat(persons.toString()).isEqualToIgnoringWhitespace("""
@ -226,9 +226,10 @@ public class ImportOfficeTables extends ContextBasedTest {
""");
assertThat(relationships.toString()).isEqualToIgnoringWhitespace("""
{
71=rel(relAnchor='Mellies, Michael', relType='TECHNICAL_CONTACT', relHolder='Mellies, Michael', contact='Herr Michael Mellies '),
102=rel(relAnchor='JM e.K.', relType='TECHNICAL_CONTACT', relHolder='JM e.K.', contact='Herr Andrew Meyer-Operation , JM e.K.'),
121=rel(relAnchor='Test PS', relType='TECHNICAL_CONTACT', relHolder='Test PS', contact='Petra Schmidt , Test PS')
1101=rel(relAnchor='Mellies, Michael', relType='OPERATIONS', relHolder='Mellies, Michael', contact='Herr Michael Mellies '),
1202=rel(relAnchor='JM e.K.', relType='OPERATIONS', relHolder='JM e.K.', contact='Herr Andrew Meyer-Operation , JM e.K.'),
1203=rel(relAnchor='JM e.K.', relType='REPRESENTATIVE', relHolder='JM e.K.', contact='Herr Philip Meyer-Contract , JM e.K.'),
1301=rel(relAnchor='Test PS', relType='REPRESENTATIVE', relHolder='Test PS', contact='Petra Schmidt , Test PS')
}
""");
}
@ -485,12 +486,28 @@ public class ImportOfficeTables extends ContextBasedTest {
.withQuoteChar('"')
.build();
try (CSVReader csvReader = new CSVReaderBuilder(reader)
final var filteredReader = skippingEmptyAndCommentLines(reader);
try (CSVReader csvReader = new CSVReaderBuilder(filteredReader)
.withCSVParser(parser)
.build()) {
return csvReader.readAll();
}
}
public static Reader skippingEmptyAndCommentLines(Reader reader) throws IOException {
try (var bufferedReader = new BufferedReader(reader);
StringWriter writer = new StringWriter()) {
String line;
while ((line = bufferedReader.readLine()) != null) {
if (!line.isBlank() && !line.startsWith("#")) {
writer.write(line);
writer.write("\n");
}
}
return new StringReader(writer.toString());
}
}
private void importBusinessPartners(final String[] header, final List<String[]> records) {
@ -658,6 +675,10 @@ public class ImportOfficeTables extends ContextBasedTest {
.forEach(rec -> {
final var contactId = rec.getInteger("contact_id");
if (rec.getString("roles").isBlank()) {
fail("empty roles assignment not allowed for contact_id: " + contactId);
}
final var partner = partners.get(rec.getInteger("bp_id"));
final var debitor = debitors.get(rec.getInteger("bp_id"));
@ -671,7 +692,7 @@ public class ImportOfficeTables extends ContextBasedTest {
contacts.put(contactId, initContact(contact, rec));
var imported = false;
if (rec.getString("roles").contains("contractual")) {
if (rec.getString("roles").contains("partner")) {
assertThat(partner.getContact()).isNull();
partner.setContact(contact);
imported = true;
@ -686,12 +707,12 @@ public class ImportOfficeTables extends ContextBasedTest {
.relAnchor(partner.getPerson())
.relHolder(person)
.contact(contact)
.relType(HsOfficeRelationshipType.TECHNICAL_CONTACT)
.relType(HsOfficeRelationshipType.OPERATIONS)
.build();
relationships.put(contactId, rel);
imported = true;
}
if (rec.getString("roles").contains("representative")) {
if (rec.getString("roles").contains("contractual")) {
final var rel = HsOfficeRelationshipEntity.builder()
.relAnchor(partner.getPerson())
.relHolder(person)
@ -808,7 +829,7 @@ public class ImportOfficeTables extends ContextBasedTest {
}
private Reader resourceReader(@NotNull final String resourcePath) {
return new InputStreamReader(getClass().getClassLoader().getResourceAsStream(resourcePath));
return new InputStreamReader(requireNonNull(getClass().getClassLoader().getResourceAsStream(resourcePath)));
}
private Reader fileReader(@NotNull final Path filePath) throws IOException {