From 2f769355f376cab19b33773ab59aaa1838d0ed18 Mon Sep 17 00:00:00 2001
From: Michael Hoennig <michael@hoennig.de>
Date: Sat, 29 Oct 2022 16:52:44 +0200
Subject: [PATCH] persist contacts, persons and partners

---
 src/test/resources/migration/contacts.csv                                               |   10 ++--
 src/test/resources/application.yml                                                      |    4 +-
 src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java |   75 +++++++++++++++++++++++++++----------
 3 files changed, 62 insertions(+), 27 deletions(-)

diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
index cc7d7a9..858fbdc 100644
--- a/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
+++ b/src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
@@ -4,6 +4,7 @@
 import com.opencsv.CSVReader;
 import com.opencsv.CSVReaderBuilder;
 import net.hostsharing.hsadminng.context.Context;
+import net.hostsharing.hsadminng.context.ContextBasedTest;
 import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
 import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
 import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
@@ -14,9 +15,14 @@
 import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonType;
 import net.hostsharing.test.JpaAttempt;
 import org.junit.jupiter.api.*;
+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 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;
@@ -35,11 +41,22 @@
 @DataJpaTest
 @Import({ Context.class, JpaAttempt.class })
 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
-public class ImportBusinessPartners {
+public class ImportBusinessPartners extends ContextBasedTest {
 
+    private static Map<Integer, HsOfficeContactEntity> contacts = new HashMap<>();
+    private static Map<Integer, HsOfficePersonEntity> persons = new HashMap<>();
     private static Map<Integer, HsOfficePartnerEntity> partners = new HashMap<>();
     private static Map<Integer, HsOfficeDebitorEntity> debitors = new HashMap<>();
     private static Map<Integer, HsOfficeMembershipEntity> memberships = new HashMap<>();
+
+    @PersistenceContext
+    EntityManager em;
+
+    @Autowired
+    JpaAttempt jpaAttempt;
+
+    @MockBean
+    HttpServletRequest request;
 
     @Test
     @Order(1)
@@ -65,12 +82,16 @@
         }
     }
 
-    @AfterAll
-    static void afterAll() {
-        System.out.println(partners);
-        System.out.println(debitors);
-        System.out.println(memberships);
+    @Test
+    @Order(10)
+    void persistEntities() {
+        jpaAttempt.transacted( () -> {
+            context("superuser-alex@hostsharing.net"); // TODO: use real user
 
+            contacts.forEach((id, contact) -> em.persist(contact));
+            persons.forEach((id, person) -> em.persist(person));
+            partners.forEach((id, partner) -> em.persist(partner));
+        });
     }
 
     public List<String[]> readAllLines(Reader reader) throws Exception {
@@ -93,12 +114,15 @@
                 .map(this::trimAll)
                 .forEach(record -> {
 
+                    final var person = HsOfficePersonEntity.builder()
+                            .personType(HsOfficePersonType.UNKNOWN) // TODO
+                            .build();
+                    persons.put(toInt(record[0]), person);
+
                     final var partner = HsOfficePartnerEntity.builder()
                             .details(HsOfficePartnerDetailsEntity.builder().build())
                             .contact(HsOfficeContactEntity.builder().build())
-                            .person(HsOfficePersonEntity.builder()
-                                    .personType(HsOfficePersonType.UNKNOWN) // TODO
-                                    .build())
+                            .person(person)
                             .build();
                     partners.put(toInt(record[0]), partner);
 
@@ -136,22 +160,33 @@
                 .map(this::trimAll)
                 .forEach(record -> {
 
-                    final var partner = partners.get(toInt(record[1]));
+                    if ( isNotBlank(record[17]) &&  record[17].contains("billing") ) {
 
-                    final var contact = partner.getContact();
-                    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));
+                        final var partner = partners.get(toInt(record[1]));
 
-                    final var person = partner.getPerson();
-                    person.setTradeName(record[6]);
-                    // TODO: title+salutation
-                    person.setFamilyName(record[3]);
-                    person.setGivenName(record[4]);
+                        final var person = partner.getPerson();
+                        person.setTradeName(record[6]);
+                        // TODO: title+salutation
+                        person.setFamilyName(record[3]);
+                        person.setGivenName(record[4]);
+
+                        initContact(partner.getContact(), record);
+                    } else {
+                        initContact(new HsOfficeContactEntity(), record);
+                        // TODO: create relationship
+                    }
                 });
     }
 
+    private void initContact(final HsOfficeContactEntity contact, final String[] record) {
+        contacts.put(toInt(record[0]), 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));
+    }
+
     private String[] trimAll(final String[] record) {
         for (int i = 0; i < record.length; ++i) {
             if (record[i] != null) {
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 9915854..410e118 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -4,8 +4,8 @@
             platform: postgres
 
     datasource:
-        url: jdbc:tc:postgresql:13.7-bullseye:///spring_boot_testcontainers
-        url-local: jdbc:postgresql://localhost:5432/postgres
+        url-tc: jdbc:tc:postgresql:13.7-bullseye:///spring_boot_testcontainers
+        url: jdbc:postgresql://localhost:5432/postgres
         username: postgres
         password: password
 
diff --git a/src/test/resources/migration/contacts.csv b/src/test/resources/migration/contacts.csv
index 73a16af..341d22d 100644
--- a/src/test/resources/migration/contacts.csv
+++ b/src/test/resources/migration/contacts.csv
@@ -1,5 +1,5 @@
-contact_id;	bp_id;	salut;	first_name;	last_name;	title;	firma;	co;	street;			zipcode;city;	country;	phone_private;		phone_office;		phone_mobile;	fax;		email
-71;		7;	Herr;	Michael;	Mellies;		;	;	;	Kleine Freiheit 50;	26524;	Hage;	DE;		;			+49 4931 123456;	+49 1522 123456;;		mih@example.org
-101;		10;	Frau;	Jenny;		Meyer;		Dr.;	JM e.K.;;	Waldweg 5;		11001;	Berlin; DE;		+49 30 7777777;		+49 30 8888888;		;		+49 30 9999999; jm@example.org
-102;		10;	Herr;	Andrew;		Meyer;		;	JM e.K.;;	Waldweg 5;		11001;	Berlin; DE;		+49 30 6666666;		+49 30 5555555;		;		+49 30 9999999; am@example.org
-121;		12;	;	Paule;		Schmidt;	;	Test PS;;	;			;	;	;		;			;			;		;		ps@example.com
+contact_id;	bp_id;	salut;	first_name;	last_name;	title;	firma;	co;	street;			zipcode;city;	country;	phone_private;		phone_office;		phone_mobile;	fax;		email; roles
+71;		7;	Herr;	Michael;	Mellies;		;	;	;	Kleine Freiheit 50;	26524;	Hage;	DE;		;			+49 4931 123456;	+49 1522 123456;;		mih@example.org; billing,operation
+101;		10;	Frau;	Jenny;		Meyer;		Dr.;	JM e.K.;;	Waldweg 5;		11001;	Berlin; DE;		+49 30 7777777;		+49 30 8888888;		;		+49 30 9999999; jm@example.org; billing
+102;		10;	Herr;	Andrew;		Meyer;		;	JM e.K.;;	Waldweg 5;		11001;	Berlin; DE;		+49 30 6666666;		+49 30 5555555;		;		+49 30 9999999; am@example.org; operation
+121;		12;	;	Paule;		Schmidt;	;	Test PS;;	;			;	;	;		;			;			;		;		ps@example.com; billing,operation

--
Gitblit v1.9.3