diff --git a/orcid-core/src/main/java/org/orcid/core/manager/impl/RegistrationManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/manager/impl/RegistrationManagerImpl.java index c00ffa055b4..5de0ae5d3ab 100644 --- a/orcid-core/src/main/java/org/orcid/core/manager/impl/RegistrationManagerImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/manager/impl/RegistrationManagerImpl.java @@ -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; @@ -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) { @@ -290,7 +297,12 @@ private String createMinimalProfile(Registration registration, boolean usedCaptc name.setGivenNames(new GivenNames(registration.getGivenNames().getValue())); } recordNameManager.createRecordName(orcid, name); - + + if (Features.REGISTRATION_2_0.isActive() && registration.getAffiliationForm() != null) { + 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); diff --git a/orcid-core/src/main/java/org/orcid/pojo/ajaxForm/Registration.java b/orcid-core/src/main/java/org/orcid/pojo/ajaxForm/Registration.java index 91f6ddb4cc1..5ef0e961f8f 100644 --- a/orcid-core/src/main/java/org/orcid/pojo/ajaxForm/Registration.java +++ b/orcid-core/src/main/java/org/orcid/pojo/ajaxForm/Registration.java @@ -52,6 +52,8 @@ public class Registration implements ErrorsInterface, Serializable { private String linkType; + private AffiliationForm affiliationForm; + public Registration() { errors = new ArrayList(); password = new Text(); @@ -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; + } } diff --git a/orcid-core/src/test/java/org/orcid/core/manager/impl/RegistrationManagerImplTest.java b/orcid-core/src/test/java/org/orcid/core/manager/impl/RegistrationManagerImplTest.java index 720306254f6..e0a6b83b2b1 100644 --- a/orcid-core/src/test/java/org/orcid/core/manager/impl/RegistrationManagerImplTest.java +++ b/orcid-core/src/test/java/org/orcid/core/manager/impl/RegistrationManagerImplTest.java @@ -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; @@ -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"; @@ -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; @@ -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()); @@ -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 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")); @@ -474,4 +497,45 @@ private Registration createRegistrationFormMultipleEmails(String email, List 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); @@ -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; + } + }