Skip to content

Commit

Permalink
Merge branch 'dataverse-affiliation-refactoring' into dataverse-v4.17…
Browse files Browse the repository at this point in the history
…-SP-bug-fixes
  • Loading branch information
manikon committed Nov 11, 2019
2 parents 47969ce + f22e3a3 commit c4b48fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import edu.harvard.iq.dataverse.RoleAssigneeServiceBean;
import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord;
import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean;
import org.apache.commons.lang.StringUtils;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.*;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

Expand Down Expand Up @@ -50,14 +52,24 @@ public AffiliationGroup getByDisplayName(String displayname) {
}
}

public AffiliationGroup getByEmailDomain(String emaildomain) {
try {
TypedQuery<AffiliationGroup> namedQuery = em.createNamedQuery("AffiliationGroup.findByEmailDomain", AffiliationGroup.class);
namedQuery.setParameter("emailDomain", "%" + emaildomain.toUpperCase() + "%");
return namedQuery.getSingleResult();
} catch (NoResultException nre) {
return null;
public AffiliationGroup find(String userEmail) {
String topLevelDomain = null;
String domain = userEmail.substring(userEmail.indexOf("@")+1).trim();
long count = domain.chars().filter(ch -> ch == '.').count();
if (count > 1) {
int secondLastIndex = StringUtils.lastIndexOf(domain, '.', domain.lastIndexOf(".") - 1);
topLevelDomain = domain.substring(secondLastIndex + 1);
} else {
topLevelDomain = domain;
}
AffiliationGroup group = matchByTopLevelEmailDomain(topLevelDomain);
if (group != null) {
String emaildomain = group.getEmaildomain();
List<String> domainValues = Arrays.asList(emaildomain.split("\\s*,\\s*"));
if (domainValues.contains(topLevelDomain))
return group;
}
return null;
}

public List<AffiliationGroup> findAll() {
Expand Down Expand Up @@ -121,4 +133,14 @@ public Long getAffiliationGroupsCount() {
public AffiliationGroup find(Object pk) {
return (AffiliationGroup) em.find(AffiliationGroup.class, pk);
}

private AffiliationGroup matchByTopLevelEmailDomain(String emaildomain) {
try {
TypedQuery<AffiliationGroup> namedQuery = em.createNamedQuery("AffiliationGroup.findByEmailDomain", AffiliationGroup.class);
namedQuery.setParameter("emailDomain", "%" + emaildomain.toUpperCase() + "%");
return namedQuery.getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,8 @@ public void validateUserEmail(FacesContext context, UIComponent toValidate, Obje
logger.info("Email is not valid: " + userEmail);
return;
}

String topLevelDomain = null;
String domain = userEmail.substring(userEmail.indexOf("@")+1).trim();
long count = domain.chars().filter(ch -> ch == '.').count();
if (count > 1) {
int secondLastIndex = StringUtils.lastIndexOf(domain, '.', domain.lastIndexOf(".") - 1);
topLevelDomain = domain.substring(secondLastIndex + 1);
} else {
topLevelDomain = domain;
}
AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(topLevelDomain);
String emaildomain = group.getEmaildomain();
List<String> domainValues = Arrays.asList(emaildomain.split("\\s*,\\s*"));
if (group == null || !domainValues.contains(topLevelDomain)) {
AffiliationGroup group = affiliationGroupServiceBean.find(userEmail);
if (group == null) {
((UIInput) toValidate).setValid(true);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, BundleUtil.getStringFromBundle("user.email.domain.invalid"), null);
context.addMessage(toValidate.getClientId(context), message);
Expand Down Expand Up @@ -324,8 +312,7 @@ public String save() {
PasswordEncryption.getLatestVersionNumber());

String userEmail = userDisplayInfo.getEmailAddress();
String domain = userEmail.substring(userEmail.indexOf("@")+1).trim();
AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(domain);
AffiliationGroup group = affiliationGroupServiceBean.find(userEmail);
String affiliation = (group == null) ? "OTHER" : group.getDisplayName();
userDisplayInfo.setAffiliation(affiliation);

Expand Down Expand Up @@ -760,8 +747,7 @@ public List<String> getAffiliationList() {
if (editMode == EditMode.SUPPORT) {
if (sendFeedbackDialog.isLoggedIn()) {
String userEmail = sendFeedbackDialog.loggedInUserEmail();
String domain = userEmail.substring(userEmail.indexOf("@")+1).trim();
AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(domain);
AffiliationGroup group = affiliationGroupServiceBean.find(userEmail);
if(group != null) {
affiliation = group.getDisplayName();
}
Expand Down

0 comments on commit c4b48fc

Please sign in to comment.