-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for 116 #236
Fix for 116 #236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,7 @@ class EmlImportService { | |
def extractContactsFromEml(eml, dataResource){ | ||
|
||
def contacts = [] | ||
def primaryContacts = [] | ||
|
||
emlFields.each { name, accessor -> | ||
def val = accessor(eml) | ||
|
@@ -123,10 +124,10 @@ class EmlImportService { | |
} | ||
} | ||
|
||
//add a contacts... | ||
//add contacts... | ||
if (eml.dataset.creator){ | ||
eml.dataset.creator.each { | ||
def contact = addContact(it) | ||
def contact = addOrUpdateContact(it) | ||
if (contact){ | ||
contacts << contact | ||
} | ||
|
@@ -137,36 +138,77 @@ class EmlImportService { | |
&& eml.dataset.metadataProvider.electronicMailAddress != eml.dataset.creator.electronicMailAddress){ | ||
|
||
eml.dataset.metadataProvider.each { | ||
def contact = addContact(it) | ||
def contact = addOrUpdateContact(it) | ||
if (contact){ | ||
contacts << contact | ||
} | ||
} | ||
} | ||
|
||
contacts | ||
// Add additional contacts | ||
if (eml.dataset.contact){ | ||
eml.dataset.contact.each { | ||
def contact = addOrUpdateContact(it) | ||
if (contact){ | ||
contacts << contact | ||
primaryContacts << contact | ||
} | ||
} | ||
} | ||
|
||
if (eml.dataset.associatedParty){ | ||
eml.dataset.associatedParty.each { | ||
def contact = addOrUpdateContact(it) | ||
if (contact){ | ||
contacts << contact | ||
} | ||
} | ||
} | ||
|
||
[contacts: contacts, primaryContacts: primaryContacts] | ||
} | ||
|
||
private def addContact(emlElement){ | ||
def contact = Contact.findByEmail(emlElement.electronicMailAddress) | ||
if (!contact){ | ||
private def addOrUpdateContact(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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inclusion of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not the case in our 500 contacts. Looking to the eml expecification: The only case I can imagine is a contact with only organizationName and no email and no individualName that seems to me something quite useless for a "contact" (Are you suggesting that we should take into account the case of "contact: organizationName: CSIRO"?). |
||
contact = new Contact() | ||
contact.firstName = emlElement.individualName.givenName | ||
contact.lastName = emlElement.individualName.surName | ||
contact.email = emlElement.electronicMailAddress | ||
contact.setUserLastModified(collectoryAuthService.username()) | ||
Contact.withTransaction { | ||
if (contact.validate()) { | ||
contact.save(flush: true, failOnError: true) | ||
return contact | ||
} else { | ||
contact.errors.each { | ||
log.error("Problem creating contact: " + it) | ||
} | ||
return null | ||
} else { | ||
return null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adam-collins I added this to prevent the NPE if a contact does not have name or email |
||
} | ||
|
||
// Update the contact details | ||
contact.firstName = emlElement.individualName.givenName | ||
contact.lastName = emlElement.individualName.surName | ||
// 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()) { | ||
contact.save(flush: true, failOnError: true) | ||
return contact | ||
} else { | ||
contact.errors.each { | ||
log.error("Problem creating contact: " + it) | ||
} | ||
return null | ||
} | ||
} | ||
|
||
contact | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,12 +119,16 @@ class IptService { | |
} | ||
} | ||
|
||
def emails = old.getContacts().collect { it.contact.email } | ||
|
||
//sync contacts | ||
update.contacts.each { contact -> | ||
if (!emails.contains(contact.email)) { | ||
old.addToContacts(contact, null, false, true, collectoryAuthService.username()) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not familiar with the context so this might be misinformed. The firstName/lastName comparison may be a problem. For example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's problematic, but as email is not mandatory and we don't have a primary key, we should use the names. For instance, this is the IPT: I was thinking that probably a better solution for us to maintain contacts in sync, is to always remove all I was thinking of adding in a future PR and doing this PR something similar to what we have but improved and less drastic and easy to review. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this PR, when merging new information, it must not exclude more new contacts that it already does. I now understand it is already excluding those without an email or those that share an email (given that it is not the primary key). At the very least, this name comparison line needs a change. I'll leave it to you if you want to address it in this PR or remove this line and address it a new PR. I agree with your suggestion. It is preferable to:
Having looked at the code a little longer I have another question. Previously the creator contact was added as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently we have a list of primary contacts and we check if it should be marked as primary or not. See below the part: This PR already addresses the original issues described in #116. Currently the drs with contacts without email are all assigned (incorrectly) to the first contact without email. In our case: |
||
} | ||
if (!existingContact) { | ||
// Add new contact | ||
boolean isPrimaryContact = update.primaryContacts.contains(contact) | ||
old.addToContacts(contact, null, false, isPrimaryContact, collectoryAuthService.username()) | ||
} | ||
} | ||
} | ||
|
@@ -142,7 +146,8 @@ class IptService { | |
DataResource.withTransaction { | ||
update.resource.save(flush: true, failOnError: true) | ||
update.contacts.each { contact -> | ||
update.resource.addToContacts(contact, null, false, true, collectoryAuthService.username()) | ||
boolean isPrimaryContact = update.primaryContacts.contains(contact) | ||
update.resource.addToContacts(contact, null, false, isPrimaryContact, collectoryAuthService.username()) | ||
} | ||
} | ||
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 { | |
resource.isShareableWithGBIF = isShareableWithGBIF | ||
|
||
def contacts = [] | ||
def primaryContacts = [] | ||
if (eml != null && eml != "") { | ||
contacts = retrieveEml(resource, eml) | ||
def result = retrieveEml(resource, eml) | ||
contacts = result.contacts | ||
primaryContacts = result.primaryContacts | ||
} | ||
|
||
[resource: resource, contacts: contacts] | ||
[resource: resource, contacts: contacts, primaryContacts: primaryContacts] | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change to only use
eml.dataset.contact
s asprimaryContacts
is OK?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what my data manager suggested.