Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vjrj committed Oct 29, 2024
1 parent 63fd634 commit 801c1da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
45 changes: 40 additions & 5 deletions grails-app/services/au/org/ala/collectory/EmlImportService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class EmlImportService {
}
}

//add a contacts...
//add contacts...
if (eml.dataset.creator){
eml.dataset.creator.each {
def contact = addContact(it)
Expand All @@ -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()) {
Expand Down
9 changes: 6 additions & 3 deletions grails-app/services/au/org/ala/collectory/IptService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down

0 comments on commit 801c1da

Please sign in to comment.