Skip to content

Commit 1cc1d4f

Browse files
authored
Merge pull request #236 from AtlasOfLivingAustralia/fix-116
Fix for 116
2 parents 95acd75 + 54e163f commit 1cc1d4f

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

grails-app/services/au/org/ala/collectory/DataImportService.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class DataImportService {
150150
if (file.getName().startsWith(EML_FILE)) {
151151
//open the XML file that contains the EML details for the GBIF resource
152152
def xml = new XmlSlurper().parseText(zipFile.getInputStream(file).getText("UTF-8"))
153-
contacts = emlImportService.extractContactsFromEml(xml, dataResource)
153+
def result = emlImportService.extractContactsFromEml(xml, dataResource)
154+
contacts = result.contacts
154155
}
155156
}
156157
}

grails-app/services/au/org/ala/collectory/EmlImportService.groovy

+62-20
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class EmlImportService {
115115
def extractContactsFromEml(eml, dataResource){
116116

117117
def contacts = []
118+
def primaryContacts = []
118119

119120
emlFields.each { name, accessor ->
120121
def val = accessor(eml)
@@ -123,10 +124,10 @@ class EmlImportService {
123124
}
124125
}
125126

126-
//add a contacts...
127+
//add contacts...
127128
if (eml.dataset.creator){
128129
eml.dataset.creator.each {
129-
def contact = addContact(it)
130+
def contact = addOrUpdateContact(it)
130131
if (contact){
131132
contacts << contact
132133
}
@@ -137,36 +138,77 @@ class EmlImportService {
137138
&& eml.dataset.metadataProvider.electronicMailAddress != eml.dataset.creator.electronicMailAddress){
138139

139140
eml.dataset.metadataProvider.each {
140-
def contact = addContact(it)
141+
def contact = addOrUpdateContact(it)
141142
if (contact){
142143
contacts << contact
143144
}
144145
}
145146
}
146147

147-
contacts
148+
// Add additional contacts
149+
if (eml.dataset.contact){
150+
eml.dataset.contact.each {
151+
def contact = addOrUpdateContact(it)
152+
if (contact){
153+
contacts << contact
154+
primaryContacts << contact
155+
}
156+
}
157+
}
158+
159+
if (eml.dataset.associatedParty){
160+
eml.dataset.associatedParty.each {
161+
def contact = addOrUpdateContact(it)
162+
if (contact){
163+
contacts << contact
164+
}
165+
}
166+
}
167+
168+
[contacts: contacts, primaryContacts: primaryContacts]
148169
}
149170

150-
private def addContact(emlElement){
151-
def contact = Contact.findByEmail(emlElement.electronicMailAddress)
152-
if (!contact){
171+
private def addOrUpdateContact(emlElement) {
172+
def contact = null
173+
if (emlElement.electronicMailAddress && !emlElement.electronicMailAddress.isEmpty()) {
174+
String email = emlElement.electronicMailAddress.text().trim()
175+
contact = Contact.findByEmail(email)
176+
} else if (emlElement.individualName.givenName && emlElement.individualName.surName) {
177+
contact = Contact.findByFirstNameAndLastName(emlElement.individualName.givenName, emlElement.individualName.surName)
178+
} else if (emlElement.individualName.surName) {
179+
// surName is mandatory
180+
contact = Contact.findByLastName(emlElement.individualName.surName)
181+
}
182+
183+
// Create the contact if it doesn't exist and it's a individualName with email or surName
184+
// to prevent empty contacts (e.g. with emlElement.organizationName only)
185+
boolean hasEmail = emlElement?.electronicMailAddress?.text()?.trim()?.isEmpty() == false
186+
boolean hasName = emlElement?.individualName?.surName?.text()?.trim()?.isEmpty() == false
187+
188+
if (!contact && (hasEmail || hasName)) {
153189
contact = new Contact()
154-
contact.firstName = emlElement.individualName.givenName
155-
contact.lastName = emlElement.individualName.surName
156-
contact.email = emlElement.electronicMailAddress
157-
contact.setUserLastModified(collectoryAuthService.username())
158-
Contact.withTransaction {
159-
if (contact.validate()) {
160-
contact.save(flush: true, failOnError: true)
161-
return contact
162-
} else {
163-
contact.errors.each {
164-
log.error("Problem creating contact: " + it)
165-
}
166-
return null
190+
} else {
191+
return null
192+
}
193+
194+
// Update the contact details
195+
contact.firstName = emlElement.individualName.givenName
196+
contact.lastName = emlElement.individualName.surName
197+
// some email has leading/trailing spaces causing the email constrain regexp to fail, lets trim
198+
contact.email = emlElement.electronicMailAddress.text().trim()
199+
contact.setUserLastModified(collectoryAuthService.username())
200+
Contact.withTransaction {
201+
if (contact.validate()) {
202+
contact.save(flush: true, failOnError: true)
203+
return contact
204+
} else {
205+
contact.errors.each {
206+
log.error("Problem creating contact: " + it)
167207
}
208+
return null
168209
}
169210
}
211+
170212
contact
171213
}
172214
}

grails-app/services/au/org/ala/collectory/IptService.groovy

+15-7
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,16 @@ class IptService {
119119
}
120120
}
121121

122-
def emails = old.getContacts().collect { it.contact.email }
123-
124122
//sync contacts
125123
update.contacts.each { contact ->
126-
if (!emails.contains(contact.email)) {
127-
old.addToContacts(contact, null, false, true, collectoryAuthService.username())
124+
def existingContact = old.getContacts().find {
125+
(it.contact.email && !it.contact.email.isEmpty() && it.contact.email == contact.email) ||
126+
(it.contact.firstName == contact.firstName && it.contact.lastName == contact.lastName)
127+
}
128+
if (!existingContact) {
129+
// Add new contact
130+
boolean isPrimaryContact = update.primaryContacts.contains(contact)
131+
old.addToContacts(contact, null, false, isPrimaryContact, collectoryAuthService.username())
128132
}
129133
}
130134
}
@@ -142,7 +146,8 @@ class IptService {
142146
DataResource.withTransaction {
143147
update.resource.save(flush: true, failOnError: true)
144148
update.contacts.each { contact ->
145-
update.resource.addToContacts(contact, null, false, true, collectoryAuthService.username())
149+
boolean isPrimaryContact = update.primaryContacts.contains(contact)
150+
update.resource.addToContacts(contact, null, false, isPrimaryContact, collectoryAuthService.username())
146151
}
147152
}
148153
activityLogService.log username, admin, Action.CREATE, "Created new IPT data resource for provider " + provider.uid + " with uid " + update.resource.uid + " for dataset " + update.resource.websiteUrl
@@ -203,11 +208,14 @@ class IptService {
203208
resource.isShareableWithGBIF = isShareableWithGBIF
204209

205210
def contacts = []
211+
def primaryContacts = []
206212
if (eml != null && eml != "") {
207-
contacts = retrieveEml(resource, eml)
213+
def result = retrieveEml(resource, eml)
214+
contacts = result.contacts
215+
primaryContacts = result.primaryContacts
208216
}
209217

210-
[resource: resource, contacts: contacts]
218+
[resource: resource, contacts: contacts, primaryContacts: primaryContacts]
211219
}
212220

213221
/**

0 commit comments

Comments
 (0)