import legacy hsadmin db (WIP)

This commit is contained in:
Michael Hoennig 2022-10-29 10:59:03 +02:00
parent bc5ee0a624
commit 205311f85b

View File

@ -0,0 +1,78 @@
package net.hostsharing.hsadminng.hs.office.migration;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import org.junit.jupiter.api.Test;
import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class ImportBusinessPartners {
private Map<Integer, HsOfficePartnerEntity> partners = new HashMap<>();
private Map<Integer, HsOfficeMembershipEntity> members = new HashMap<>();
@Test
void importsBusinessPartners() throws Exception {
try (Reader reader = resourceReader("migration/business_partners.csv")) {
final var records = readAllLines(reader);
importsBusinessPartners(records);
assertThat(records).hasSize(4);
}
}
public List<String[]> readAllLines(Reader reader) throws Exception {
final var parser = new CSVParserBuilder()
.withSeparator(';')
.withQuoteChar('"')
.build();
try (CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(1)
.withCSVParser(parser)
.build()) {
return csvReader.readAll();
}
}
private void importsBusinessPartners(final List<String[]> records) {
records.forEach( record -> {
HsOfficePersonEntity.builder()
.tradeName(record[])
.build();
});
}
private Reader resourceReader(@NotNull final String resourcePath) {
return new InputStreamReader(getClass().getClassLoader().getResourceAsStream(resourcePath));
}
private Reader fileReader(@NotNull final Path filePath) throws IOException {
// Path path = Paths.get(
// ClassLoader.getSystemResource("csv/twoColumn.csv").toURI())
// );
return Files.newBufferedReader(filePath);
}
}