Skip to content

Commit 5a42cb3

Browse files
committed
Update contacts too and take into account primary accounts
1 parent 71242f5 commit 5a42cb3

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
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

+25-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)
@@ -126,7 +127,7 @@ class EmlImportService {
126127
//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,7 +138,7 @@ 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
}
@@ -147,26 +148,27 @@ class EmlImportService {
147148
// Add additional contacts
148149
if (eml.dataset.contact){
149150
eml.dataset.contact.each {
150-
def contact = addContact(it)
151+
def contact = addOrUpdateContact(it)
151152
if (contact){
152153
contacts << contact
154+
primaryContacts << contact
153155
}
154156
}
155157
}
156158

157159
if (eml.dataset.associatedParty){
158160
eml.dataset.associatedParty.each {
159-
def contact = addContact(it)
161+
def contact = addOrUpdateContact(it)
160162
if (contact){
161163
contacts << contact
162164
}
163165
}
164166
}
165167

166-
contacts
168+
[contacts: contacts, primaryContacts: primaryContacts]
167169
}
168170

169-
private def addContact(emlElement) {
171+
private def addOrUpdateContact(emlElement) {
170172
def contact = null
171173
if (emlElement.electronicMailAddress && !emlElement.electronicMailAddress.isEmpty()) {
172174
String email = emlElement.electronicMailAddress.text().trim()
@@ -185,23 +187,26 @@ class EmlImportService {
185187

186188
if (!contact && (hasEmail || hasName)) {
187189
contact = new Contact()
188-
contact.firstName = emlElement.individualName.givenName
189-
contact.lastName = emlElement.individualName.surName
190-
// some email has leading/trailing spaces causing the email constrain regexp to fail, lets trim
191-
contact.email = emlElement.electronicMailAddress.text().trim()
192-
contact.setUserLastModified(collectoryAuthService.username())
193-
Contact.withTransaction {
194-
if (contact.validate()) {
195-
contact.save(flush: true, failOnError: true)
196-
return contact
197-
} else {
198-
contact.errors.each {
199-
log.error("Problem creating contact: " + it)
200-
}
201-
return null
190+
}
191+
192+
// Update the contact details
193+
contact.firstName = emlElement.individualName.givenName
194+
contact.lastName = emlElement.individualName.surName
195+
// some email has leading/trailing spaces causing the email constrain regexp to fail, lets trim
196+
contact.email = emlElement.electronicMailAddress.text().trim()
197+
contact.setUserLastModified(collectoryAuthService.username())
198+
Contact.withTransaction {
199+
if (contact.validate()) {
200+
contact.save(flush: true, failOnError: true)
201+
return contact
202+
} else {
203+
contact.errors.each {
204+
log.error("Problem creating contact: " + it)
202205
}
206+
return null
203207
}
204208
}
209+
205210
contact
206211
}
207212
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ class IptService {
127127
}
128128
if (!existingContact) {
129129
// Add new contact
130-
old.addToContacts(contact, null, false, true, collectoryAuthService.username())
130+
boolean isPrimaryContact = update.primaryContacts.contains(contact)
131+
old.addToContacts(contact, null, false, isPrimaryContact, collectoryAuthService.username())
131132
}
132133
}
133134
}
@@ -145,7 +146,8 @@ class IptService {
145146
DataResource.withTransaction {
146147
update.resource.save(flush: true, failOnError: true)
147148
update.contacts.each { contact ->
148-
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())
149151
}
150152
}
151153
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
@@ -206,11 +208,14 @@ class IptService {
206208
resource.isShareableWithGBIF = isShareableWithGBIF
207209

208210
def contacts = []
211+
def primaryContacts = []
209212
if (eml != null && eml != "") {
210-
contacts = retrieveEml(resource, eml)
213+
def result = retrieveEml(resource, eml)
214+
contacts = result.contacts
215+
primaryContacts = result.primaryContacts
211216
}
212217

213-
[resource: resource, contacts: contacts]
218+
[resource: resource, contacts: contacts, primaryContacts: primaryContacts]
214219
}
215220

216221
/**

0 commit comments

Comments
 (0)