Skip to content
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

feature: Add affiliation when a new user registers #6942

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
import org.orcid.core.manager.ProfileEntityCacheManager;
import org.orcid.core.manager.ProfileEntityManager;
import org.orcid.core.manager.RegistrationManager;
import org.orcid.core.manager.v3.AffiliationsManager;
import org.orcid.core.manager.v3.EmailManager;
import org.orcid.core.manager.v3.NotificationManager;
import org.orcid.core.manager.v3.RecordNameManager;
import org.orcid.core.security.OrcidWebRole;
import org.orcid.core.togglz.Features;
import org.orcid.core.utils.VerifyRegistrationToken;
import org.orcid.jaxb.model.common.AvailableLocales;
import org.orcid.jaxb.model.common_v2.OrcidType;
import org.orcid.jaxb.model.common_v2.Visibility;
import org.orcid.jaxb.model.message.CreationMethod;
import org.orcid.jaxb.model.v3.release.record.Affiliation;
import org.orcid.jaxb.model.v3.release.record.Employment;
import org.orcid.jaxb.model.v3.release.record.FamilyName;
import org.orcid.jaxb.model.v3.release.record.GivenNames;
import org.orcid.jaxb.model.v3.release.record.Name;
Expand Down Expand Up @@ -90,6 +94,9 @@ public class RegistrationManagerImpl implements RegistrationManager {

@Resource(name = "recordNameManagerV3")
private RecordNameManager recordNameManager;

@Resource(name = "affiliationsManagerV3")
private AffiliationsManager affiliationsManager;

@Required
public void setEncryptionManager(EncryptionManager encryptionManager) {
Expand Down Expand Up @@ -290,7 +297,12 @@ private String createMinimalProfile(Registration registration, boolean usedCaptc
name.setGivenNames(new GivenNames(registration.getGivenNames().getValue()));
}
recordNameManager.createRecordName(orcid, name);


if (Features.EVENTS.isActive() && registration.getAffiliationForm() != null) {
DanielPalafox marked this conversation as resolved.
Show resolved Hide resolved
Affiliation affiliation = registration.getAffiliationForm().toAffiliation();
affiliationsManager.createEmploymentAffiliation(orcid, (Employment) affiliation, false);
}

// Create email frequency entity
boolean sendQuarterlyTips = (registration.getSendOrcidNews() == null) ? false : registration.getSendOrcidNews().getValue();
emailFrequencyManager.createOnRegister(orcid, SendEmailFrequency.WEEKLY, SendEmailFrequency.WEEKLY, SendEmailFrequency.WEEKLY, sendQuarterlyTips);
Expand Down
10 changes: 10 additions & 0 deletions orcid-core/src/main/java/org/orcid/pojo/ajaxForm/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Registration implements ErrorsInterface, Serializable {

private String linkType;

private AffiliationForm affiliationForm;

public Registration() {
errors = new ArrayList<String>();
password = new Text();
Expand Down Expand Up @@ -241,4 +243,12 @@ public String getLinkType() {
public void setLinkType(String linkType) {
this.linkType = linkType;
}

public AffiliationForm getAffiliationForm() {
return affiliationForm;
}

public void setAffiliationForm(AffiliationForm affiliationForm) {
this.affiliationForm = affiliationForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@
import org.orcid.core.common.manager.EmailFrequencyManager;
import org.orcid.core.manager.RegistrationManager;
import org.orcid.core.manager.SourceManager;
import org.orcid.core.manager.TwoFactorAuthenticationManager;
import org.orcid.core.manager.read_only.EmailManagerReadOnly;
import org.orcid.core.manager.v3.AffiliationsManager;
import org.orcid.core.manager.v3.ProfileEntityManager;
import org.orcid.core.manager.v3.ProfileHistoryEventManager;
import org.orcid.core.profile.history.ProfileHistoryEventType;
import org.orcid.jaxb.model.message.CreationMethod;
import org.orcid.jaxb.model.record_v2.Emails;
import org.orcid.jaxb.model.v3.release.common.Source;
import org.orcid.jaxb.model.v3.release.record.AffiliationType;
import org.orcid.jaxb.model.v3.release.record.summary.EmploymentSummary;
import org.orcid.persistence.constants.SendEmailFrequency;
import org.orcid.persistence.dao.ProfileDao;
import org.orcid.persistence.jpa.entities.ClientDetailsEntity;
import org.orcid.persistence.jpa.entities.ProfileEntity;
import org.orcid.persistence.jpa.entities.SourceEntity;
import org.orcid.pojo.ajaxForm.AffiliationForm;
import org.orcid.pojo.ajaxForm.Date;
import org.orcid.pojo.ajaxForm.Registration;
import org.orcid.pojo.ajaxForm.Text;
import org.orcid.test.DBUnitTest;
Expand All @@ -54,6 +60,7 @@
@ContextConfiguration(locations = { "classpath:test-orcid-core-context.xml" })
public class RegistrationManagerImplTest extends DBUnitTest {

private static final String CLIENT_1_ID = "4444-4444-4444-4498";
private static final String CLIENT_ID_AUTODEPRECATE_ENABLED = "APP-5555555555555555";
private static final String CLIENT_ID_AUTODEPRECATE_DISABLED = "APP-5555555555555556";

Expand All @@ -65,13 +72,13 @@ public class RegistrationManagerImplTest extends DBUnitTest {

@Resource
EmailManagerReadOnly emailManager;

@Resource
SourceManager sourceManager;


@Mock
SourceManager mockSourceManager;


@Mock
org.orcid.core.manager.v3.SourceManager mockSourceManagerV3;

@Mock
EmailFrequencyManager mockEmailFrequencyManager;

Expand All @@ -89,24 +96,26 @@ public class RegistrationManagerImplTest extends DBUnitTest {

@Resource(name = "notificationManagerV3")
org.orcid.core.manager.v3.NotificationManager notificationV3Manager;

@Mock
private TwoFactorAuthenticationManager mockTwoFactorAuthenticationManager;

@Resource
TwoFactorAuthenticationManager twoFactorAuthenticationManager;


@Resource(name = "affiliationsManagerV3")
private AffiliationsManager affiliationsManager;

@BeforeClass
public static void initDBUnitData() throws Exception {
initDBUnitData(Arrays.asList("/data/SourceClientDetailsEntityData.xml"));
}
initDBUnitData(Arrays.asList("/data/SourceClientDetailsEntityData.xml", "/data/ProfileEntityData.xml", "/data/RecordNameEntityData.xml", "/data/OrgsEntityData.xml"));
}

@Before
public void before() {
MockitoAnnotations.initMocks(this);
TargetProxyHelper.injectIntoProxy(registrationManager, "emailFrequencyManager", mockEmailFrequencyManager);
TargetProxyHelper.injectIntoProxy(registrationManager, "emailFrequencyManager", mockEmailFrequencyManager);
TargetProxyHelper.injectIntoProxy(affiliationsManager, "sourceManager", mockSourceManagerV3);
TargetProxyHelper.injectIntoProxy(affiliationsManager, "notificationManager", mockV3NotificationManager);
when(mockSourceManager.retrieveSourceEntity()).thenReturn(new SourceEntity(new ClientDetailsEntity(CLIENT_ID_AUTODEPRECATE_ENABLED)));

when(mockSourceManagerV3.retrieveActiveSource()).thenReturn(new Source("4444-4444-4444-4441"));
when(mockV3NotificationManager.sendAmendEmail(Mockito.anyString(), Mockito.any(), Mockito.anyList())).thenReturn(null);
when(mockEmailFrequencyManager.createOnRegister(Mockito.anyString(), Mockito.any(SendEmailFrequency.class), Mockito.any(SendEmailFrequency.class), Mockito.any(SendEmailFrequency.class), Mockito.anyBoolean())).thenReturn(true);

TargetProxyHelper.injectIntoProxy(registrationManager, "notificationManager", mockV3NotificationManager);
doNothing().when(mockV3NotificationManager).sendAutoDeprecateNotification(Mockito.anyString(), Mockito.anyString());

Expand Down Expand Up @@ -453,7 +462,21 @@ public void testRegisterCleanSpaceCharsOnEmailsTest() {
}

}


@Test
public void testRegisterWithAffiliationTest() {
String email = "new_user_" + System.currentTimeMillis() + "@test.orcid.org";
Registration registrationForm = createRegistrationForm(email, true);
registrationForm.setAffiliationForm(getAffiliationForm());

String userOrcid = registrationManager.createMinimalRegistration(registrationForm, true, java.util.Locale.ENGLISH, "0.0.0.0");
assertNotNull(userOrcid);
assertTrue(OrcidStringUtils.isValidOrcid(userOrcid));
List<EmploymentSummary> employmentSummaryList = affiliationsManager.getEmploymentSummaryList(userOrcid);
assertNotNull(employmentSummaryList);
assertEquals(1, employmentSummaryList.size());
}

private Registration createRegistrationForm(String email, boolean claimed) {
Registration registration = new Registration();
registration.setPassword(Text.valueOf("password"));
Expand All @@ -474,4 +497,45 @@ private Registration createRegistrationFormMultipleEmails(String email, List<Tex
registration.setCreationType(Text.valueOf(CreationMethod.DIRECT.value()));
return registration;
}

protected AffiliationForm getAffiliationForm() {
Date created = new Date();

AffiliationForm form = new AffiliationForm();
form.setAffiliationType(Text.valueOf(AffiliationType.EMPLOYMENT.value()));
created.setDay(String.valueOf(created.getDay()));
created.setMonth(String.valueOf(created.getMonth()));
created.setYear(String.valueOf(created.getYear()));
form.setCreatedDate(created);

Date lastModified = new Date();
lastModified.setDay(String.valueOf(created.getDay()));
lastModified.setMonth(String.valueOf(created.getMonth()));
lastModified.setYear(String.valueOf(created.getYear()));
form.setLastModified(lastModified);

form.setDepartmentName(Text.valueOf("department-name"));

form.setRoleTitle(Text.valueOf("role-title"));

Date startDate = new Date();
startDate.setDay("31");
startDate.setMonth("12");
startDate.setYear("2019");
form.setStartDate(startDate);

org.orcid.pojo.ajaxForm.Visibility v = new org.orcid.pojo.ajaxForm.Visibility();
v.setVisibility(org.orcid.jaxb.model.v3.release.common.Visibility.PRIVATE);
form.setVisibility(v);

form.setSource(CLIENT_1_ID);

form.setCity(Text.valueOf("city"));
form.setCountry(Text.valueOf("US"));
form.setRegion(Text.valueOf("region"));
form.setAffiliationName(Text.valueOf("org-1"));
form.setOrgDisambiguatedId(Text.valueOf("1"));

return form;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.orcid.frontend.web.controllers;

import java.io.UnsupportedEncodingException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -14,6 +19,7 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.orcid.core.constants.EmailConstants;
import org.orcid.core.constants.OrcidOauth2Constants;
Expand Down Expand Up @@ -45,6 +51,8 @@
import org.orcid.jaxb.model.v3.release.search.Search;
import org.orcid.persistence.constants.SendEmailFrequency;
import org.orcid.pojo.Redirect;
import org.orcid.pojo.ajaxForm.AffiliationForm;
import org.orcid.pojo.ajaxForm.Date;
import org.orcid.pojo.ajaxForm.PojoUtil;
import org.orcid.pojo.ajaxForm.Registration;
import org.orcid.pojo.ajaxForm.RequestInfoForm;
Expand Down Expand Up @@ -321,6 +329,28 @@ public void validateRegistrationFields(HttpServletRequest request, Registration
regEmailValidate(request, reg, false, false);
registerTermsOfUseValidate(reg);

if (Features.EVENTS.isActive() && reg.getAffiliationForm() != null) {
DanielPalafox marked this conversation as resolved.
Show resolved Hide resolved
AffiliationForm affiliationForm = reg.getAffiliationForm();
if (!affiliationForm.getAffiliationType().getValue().equals("Employment")) {
setError(affiliationForm.getAffiliationType(), "Invalid affiliation type");
}
if (reg.getAffiliationForm().getDepartmentName() != null) {
if (affiliationForm.getDepartmentName().getValue() != null && affiliationForm.getDepartmentName().getValue().trim().length() > 1000) {
setError(affiliationForm.getDepartmentName(), "common.length_less_1000");
}
}
if (reg.getAffiliationForm().getRoleTitle() != null) {
if (!PojoUtil.isEmpty(affiliationForm.getRoleTitle()) && affiliationForm.getRoleTitle().getValue().trim().length() > 1000) {
setError(affiliationForm.getRoleTitle(), "common.length_less_1000");
}
}
if (reg.getAffiliationForm().getStartDate() != null) {
if(!validDate(affiliationForm.getStartDate())) {
setError(affiliationForm.getStartDate(), "common.dates.invalid");
}
}
}

copyErrors(reg.getActivitiesVisibilityDefault(), reg);
copyErrors(reg.getEmailConfirm(), reg);
copyErrors(reg.getEmail(), reg);
Expand Down Expand Up @@ -581,4 +611,43 @@ private void processProfileHistoryEvents(Registration registration, String newUs
}
}

protected boolean validDate(Date date) {
DateTimeFormatter[] formatters = {
new DateTimeFormatterBuilder().appendPattern("yyyy").parseDefaulting(ChronoField.MONTH_OF_YEAR, 1).parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.toFormatter(),
new DateTimeFormatterBuilder().appendPattern("yyyyMM").parseDefaulting(ChronoField.DAY_OF_MONTH, 1).toFormatter(),
new DateTimeFormatterBuilder().appendPattern("yyyyMMdd").parseStrict().toFormatter() };
String dateString = date.getYear();
// If the month is empty and day provided is an invalid date
if (StringUtils.isBlank(date.getMonth())) {
if (!StringUtils.isBlank(date.getDay())) {
return false;
}
}
else if (StringUtils.isBlank(date.getYear())) {
if (!StringUtils.isBlank(date.getDay()) && !StringUtils.isBlank(date.getMonth())) {
return false;
}
}
else {
dateString += date.getMonth();
if (!StringUtils.isBlank(date.getDay())) {
dateString += date.getDay();
}
}

for (DateTimeFormatter formatter : formatters) {
try {
LocalDate localDate = LocalDate.parse(dateString, formatter);
if (PojoUtil.isEmpty(date.getDay()) || localDate.getDayOfMonth() == Integer.parseInt(date.getDay())) {
// formatter will correct day to last valid day of month if
// it is too great
return true;
}
} catch (DateTimeParseException e) {
}
}
return false;
}

}