import legacy-ids for contact, partner + sepamandate

This commit is contained in:
Michael Hoennig 2024-01-06 18:07:09 +01:00
parent 6474d0c4d9
commit 1e4a18b16e
5 changed files with 46 additions and 8 deletions

View File

@ -3,7 +3,7 @@ package net.hostsharing.hsadminng.hs.office.contact;
import lombok.*;
import lombok.experimental.FieldNameConstants;
import net.hostsharing.hsadminng.errors.DisplayName;
import net.hostsharing.hsadminng.hs.office.migration.HasUuid;
import net.hostsharing.hsadminng.repository.HasUuid;
import net.hostsharing.hsadminng.stringify.Stringify;
import net.hostsharing.hsadminng.stringify.Stringifyable;
import org.hibernate.annotations.GenericGenerator;

View File

@ -5,6 +5,7 @@ import net.hostsharing.hsadminng.errors.DisplayName;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
import net.hostsharing.hsadminng.hs.office.migration.HasUuid;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import net.hostsharing.hsadminng.repository.HasUuid;
import net.hostsharing.hsadminng.stringify.Stringify;
import net.hostsharing.hsadminng.stringify.Stringifyable;
import org.hibernate.annotations.NotFound;

View File

@ -6,7 +6,7 @@ import lombok.*;
import net.hostsharing.hsadminng.errors.DisplayName;
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.migration.HasUuid;
import net.hostsharing.hsadminng.repository.HasUuid;
import net.hostsharing.hsadminng.stringify.Stringify;
import net.hostsharing.hsadminng.stringify.Stringifyable;
import org.hibernate.annotations.Type;

View File

@ -0,0 +1,7 @@
package net.hostsharing.hsadminng.repository;
import java.util.UUID;
public interface HasUuid {
UUID getUuid();
}

View File

@ -15,6 +15,7 @@ import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonType;
import net.hostsharing.hsadminng.hs.office.sepamandate.HsOfficeSepaMandateEntity;
import net.hostsharing.hsadminng.repository.HasUuid;
import net.hostsharing.test.JpaAttempt;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
@ -24,7 +25,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.Commit;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.ReflectionUtils;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
@ -36,7 +43,6 @@ 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;
@ -72,6 +78,9 @@ public class ImportOfficeTables extends ContextBasedTest {
@PersistenceContext
EntityManager em;
@Autowired
TransactionTemplate txTemplate;
@Autowired
JpaAttempt jpaAttempt;
@ -189,21 +198,46 @@ public class ImportOfficeTables extends ContextBasedTest {
@Test
@Order(10)
@Transactional
@Commit
//@Rollback(false)
void persistEntities() {
context("superuser-alex@hostsharing.net"); // TODO: use real user for actual import
contacts.forEach((id, contact) -> em.persist(contact));
updateLegacyIds(contacts, "hs_office_contact_legacy_id", "contact_id");
persons.forEach((id, person) -> em.persist(person));
partners.forEach((id, partner) -> em.persist(partner));
updateLegacyIds(partners, "hs_office_partner_legacy_id", "bp_id");
debitors.forEach((id, debitor) -> em.persist(debitor));
memberships.forEach((id, membership) -> em.persist(membership));
bankAccounts.forEach((id, account) -> em.persist(account));
sepaMandates.forEach((id, mandate) -> em.persist(mandate));
updateLegacyIds(sepaMandates, "hs_office_sepamandate_legacy_id", "sepa_mandate_id");
// TODO: coopshares+coopassets
em.flush();
}
private <E extends HasUuid> void updateLegacyIds(Map<Integer, E> entities, final String legacyIdTable, final String legacyIdColumn) {
entities.forEach((id, entity) -> em.createNativeQuery("""
UPDATE ${legacyIdTable}
SET ${legacyIdColumn} = :legacyId
WHERE uuid = :uuid
"""
.replace("${legacyIdTable}", legacyIdTable)
.replace("${legacyIdColumn}", legacyIdColumn))
.setParameter("legacyId", id)
.setParameter("uuid", entity.getUuid())
.executeUpdate()
);
}
public List<String[]> readAllLines(Reader reader) throws Exception {
final var parser = new CSVParserBuilder()
@ -423,10 +457,6 @@ public class ImportOfficeTables extends ContextBasedTest {
return isNotBlank(value) ? Integer.parseInt(value.trim()) : 0;
}
private static Integer toInteger(final String value) {
return isNotBlank(value) ? Integer.parseInt(value.trim()) : null;
}
private LocalDate localDate(final String dateStringNullOrBlank) {
if (isNotBlank(dateStringNullOrBlank)) {
return LocalDate.parse(dateStringNullOrBlank);