From 801c1da8fa4f46ec2270bd22090bef50e5ce5198 Mon Sep 17 00:00:00 2001 From: vjrj Date: Mon, 15 Apr 2024 14:28:26 +0200 Subject: [PATCH] Fix #116 --- .../ala/collectory/EmlImportService.groovy | 45 ++++++++++++++++--- .../au/org/ala/collectory/IptService.groovy | 9 ++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/grails-app/services/au/org/ala/collectory/EmlImportService.groovy b/grails-app/services/au/org/ala/collectory/EmlImportService.groovy index b82dffbd..9a187224 100644 --- a/grails-app/services/au/org/ala/collectory/EmlImportService.groovy +++ b/grails-app/services/au/org/ala/collectory/EmlImportService.groovy @@ -123,7 +123,7 @@ class EmlImportService { } } - //add a contacts... + //add contacts... if (eml.dataset.creator){ eml.dataset.creator.each { def contact = addContact(it) @@ -144,16 +144,51 @@ class EmlImportService { } } + // Add additional contacts + if (eml.dataset.contact){ + eml.dataset.contact.each { + def contact = addContact(it) + if (contact){ + contacts << contact + } + } + } + + if (eml.dataset.associatedParty){ + eml.dataset.associatedParty.each { + def contact = addContact(it) + if (contact){ + contacts << contact + } + } + } + contacts } - private def addContact(emlElement){ - def contact = Contact.findByEmail(emlElement.electronicMailAddress) - if (!contact){ + private def addContact(emlElement) { + def contact = null + if (emlElement.electronicMailAddress && !emlElement.electronicMailAddress.isEmpty()) { + String email = emlElement.electronicMailAddress.text().trim() + contact = Contact.findByEmail(email) + } else if (emlElement.individualName.givenName && emlElement.individualName.surName) { + contact = Contact.findByFirstNameAndLastName(emlElement.individualName.givenName, emlElement.individualName.surName) + } else if (emlElement.individualName.surName) { + // surName is mandatory + contact = Contact.findByLastName(emlElement.individualName.surName) + } + + // Create the contact if it doesn't exist and it's a individualName with email or surName + // to prevent empty contacts (e.g. with emlElement.organizationName only) + boolean hasEmail = emlElement?.electronicMailAddress?.text()?.trim()?.isEmpty() == false + boolean hasName = emlElement?.individualName?.surName?.text()?.trim()?.isEmpty() == false + + if (!contact && (hasEmail || hasName)) { contact = new Contact() contact.firstName = emlElement.individualName.givenName contact.lastName = emlElement.individualName.surName - contact.email = emlElement.electronicMailAddress + // some email has leading/trailing spaces causing the email constrain regexp to fail, lets trim + contact.email = emlElement.electronicMailAddress.text().trim() contact.setUserLastModified(collectoryAuthService.username()) Contact.withTransaction { if (contact.validate()) { diff --git a/grails-app/services/au/org/ala/collectory/IptService.groovy b/grails-app/services/au/org/ala/collectory/IptService.groovy index 97dae984..420de440 100644 --- a/grails-app/services/au/org/ala/collectory/IptService.groovy +++ b/grails-app/services/au/org/ala/collectory/IptService.groovy @@ -119,11 +119,14 @@ class IptService { } } - def emails = old.getContacts().collect { it.contact.email } - //sync contacts update.contacts.each { contact -> - if (!emails.contains(contact.email)) { + def existingContact = old.getContacts().find { + (it.contact.email && !it.contact.email.isEmpty() && it.contact.email == contact.email) || + (it.contact.firstName == contact.firstName && it.contact.lastName == contact.lastName) + } + if (!existingContact) { + // Add new contact old.addToContacts(contact, null, false, true, collectoryAuthService.username()) } }