Search in sources :

Example 26 with EmailEntity

use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.

the class JpaJaxbEmailAdapterTest method fromEmailToEmailEntity.

@Test
public void fromEmailToEmailEntity() {
    EmailEntity entity = getEmailEntity();
    Email email = jpaJaxbEmailAdapter.toEmail(entity);
    assertNotNull(email);
    assertNotNull(email.getLastModifiedDate().getValue());
    assertNotNull(email.getCreatedDate().getValue());
    assertEquals("email@test.orcid.org", email.getEmail());
    assertEquals(Visibility.PRIVATE, email.getVisibility());
}
Also used : Email(org.orcid.jaxb.model.record_v2.Email) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) Test(org.junit.Test)

Example 27 with EmailEntity

use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.

the class JpaJaxbEmailAdapterTest method testEmailEntityToEmail.

@Test
public void testEmailEntityToEmail() throws JAXBException {
    Email email = getEmail();
    assertNotNull(email);
    EmailEntity entity = jpaJaxbEmailAdapter.toEmailEntity(email);
    assertNotNull(entity);
    assertNotNull(entity.getDateCreated());
    assertNotNull(entity.getLastModified());
    assertEquals("user1@email.com", entity.getId());
    assertEquals(Visibility.PUBLIC, entity.getVisibility());
    // Source
    assertNull(entity.getSourceId());
    assertNull(entity.getClientSourceId());
    assertNull(entity.getElementSourceId());
}
Also used : Email(org.orcid.jaxb.model.record_v2.Email) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) Test(org.junit.Test)

Example 28 with EmailEntity

use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.

the class RegistrationManagerImpl method createMinimalProfile.

/**
 * Creates a minimal record
 *
 * @param orcidProfile
 *            The record to create
 * @return the new record
 */
private String createMinimalProfile(Registration registration, boolean usedCaptcha, Locale locale, String ip) {
    Date now = new Date();
    String orcid = orcidGenerationManager.createNewOrcid();
    ProfileEntity newRecord = new ProfileEntity();
    newRecord.setId(orcid);
    try {
        newRecord.setHashedOrcid(encryptionManager.sha256Hash(orcid));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    newRecord.setOrcidType(OrcidType.USER);
    newRecord.setDateCreated(now);
    newRecord.setLastModified(now);
    newRecord.setSubmissionDate(now);
    newRecord.setClaimed(true);
    newRecord.setEnableDeveloperTools(false);
    newRecord.setRecordLocked(false);
    newRecord.setReviewed(false);
    newRecord.setEnableNotifications(DefaultPreferences.NOTIFICATIONS_ENABLED);
    newRecord.setUsedRecaptchaOnRegistration(usedCaptcha);
    newRecord.setUserLastIp(ip);
    newRecord.setLastLogin(now);
    if (PojoUtil.isEmpty(registration.getSendEmailFrequencyDays())) {
        newRecord.setSendEmailFrequencyDays(Float.valueOf(DefaultPreferences.SEND_EMAIL_FREQUENCY_DAYS));
    } else {
        newRecord.setSendEmailFrequencyDays(Float.valueOf(registration.getSendEmailFrequencyDays().getValue()));
    }
    if (registration.getSendMemberUpdateRequests() == null) {
        newRecord.setSendMemberUpdateRequests(DefaultPreferences.SEND_MEMBER_UPDATE_REQUESTS);
    } else {
        newRecord.setSendMemberUpdateRequests(registration.getSendMemberUpdateRequests().getValue());
    }
    newRecord.setCreationMethod(PojoUtil.isEmpty(registration.getCreationType()) ? CreationMethod.DIRECT.value() : registration.getCreationType().getValue());
    newRecord.setSendChangeNotifications(registration.getSendChangeNotifications().getValue());
    newRecord.setSendAdministrativeChangeNotifications(true);
    newRecord.setSendOrcidNews(registration.getSendOrcidNews().getValue());
    newRecord.setLocale(locale == null ? org.orcid.jaxb.model.common_v2.Locale.EN : org.orcid.jaxb.model.common_v2.Locale.fromValue(locale.toString()));
    // Visibility defaults
    newRecord.setActivitiesVisibilityDefault(Visibility.fromValue(registration.getActivitiesVisibilityDefault().getVisibility().value()));
    // Encrypt the password
    newRecord.setEncryptedPassword(encryptionManager.hashForInternalUse(registration.getPassword().getValue()));
    // Set primary email
    EmailEntity emailEntity = new EmailEntity();
    emailEntity.setId(registration.getEmail().getValue().trim());
    emailEntity.setProfile(newRecord);
    emailEntity.setPrimary(true);
    emailEntity.setCurrent(true);
    emailEntity.setVerified(false);
    // Email is private by default
    emailEntity.setVisibility(Visibility.PRIVATE);
    emailEntity.setSourceId(orcid);
    Set<EmailEntity> emails = new HashSet<>();
    emails.add(emailEntity);
    // Set additional emails
    for (Text emailAdditional : registration.getEmailsAdditional()) {
        if (!PojoUtil.isEmpty(emailAdditional)) {
            EmailEntity emailAdditionalEntity = new EmailEntity();
            emailAdditionalEntity.setId(emailAdditional.getValue().trim());
            emailAdditionalEntity.setProfile(newRecord);
            emailAdditionalEntity.setPrimary(false);
            emailAdditionalEntity.setCurrent(true);
            emailAdditionalEntity.setVerified(false);
            // Email is private by default
            emailAdditionalEntity.setVisibility(Visibility.PRIVATE);
            emailAdditionalEntity.setSourceId(orcid);
            emails.add(emailAdditionalEntity);
        }
    }
    // Add all emails to record
    newRecord.setEmails(emails);
    // Set the name
    RecordNameEntity recordNameEntity = new RecordNameEntity();
    recordNameEntity.setDateCreated(now);
    recordNameEntity.setLastModified(now);
    recordNameEntity.setProfile(newRecord);
    // Name is public by default
    recordNameEntity.setVisibility(Visibility.PUBLIC);
    if (!PojoUtil.isEmpty(registration.getFamilyNames())) {
        recordNameEntity.setFamilyName(registration.getFamilyNames().getValue().trim());
    }
    if (!PojoUtil.isEmpty(registration.getGivenNames())) {
        recordNameEntity.setGivenNames(registration.getGivenNames().getValue().trim());
    }
    newRecord.setRecordNameEntity(recordNameEntity);
    // Set authority
    OrcidGrantedAuthority authority = new OrcidGrantedAuthority();
    authority.setProfileEntity(newRecord);
    authority.setAuthority(OrcidWebRole.ROLE_USER.getAuthority());
    Set<OrcidGrantedAuthority> authorities = new HashSet<OrcidGrantedAuthority>(1);
    authorities.add(authority);
    newRecord.setAuthorities(authorities);
    profileDao.persist(newRecord);
    profileDao.flush();
    return newRecord.getId();
}
Also used : RecordNameEntity(org.orcid.persistence.jpa.entities.RecordNameEntity) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) Text(org.orcid.pojo.ajaxForm.Text) OrcidGrantedAuthority(org.orcid.persistence.jpa.entities.OrcidGrantedAuthority) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) HashSet(java.util.HashSet)

Example 29 with EmailEntity

use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.

the class EmailMessageSenderImpl method sendEmailMessages.

@Override
public void sendEmailMessages() {
    LOGGER.info("About to send email messages");
    List<Object[]> orcidsWithUnsentNotifications = notificationDaoReadOnly.findRecordsWithUnsentNotifications();
    for (final Object[] element : orcidsWithUnsentNotifications) {
        String orcid = (String) element[0];
        try {
            Float emailFrequencyDays = Float.valueOf((float) element[1]);
            Date recordActiveDate = (Date) element[2];
            LOGGER.info("Sending messages for orcid: {}", orcid);
            List<Notification> notifications = notificationManager.findNotificationsToSend(orcid, emailFrequencyDays, recordActiveDate);
            if (!notifications.isEmpty()) {
                LOGGER.info("Found {} messages to send for orcid: {}", notifications.size(), orcid);
                EmailMessage digestMessage = createDigest(orcid, notifications);
                digestMessage.setFrom(DIGEST_FROM_ADDRESS);
                EmailEntity primaryEmail = emailDao.findPrimaryEmail(orcid);
                if (primaryEmail == null) {
                    LOGGER.info("No primary email for orcid: " + orcid);
                    return;
                }
                digestMessage.setTo(primaryEmail.getId());
                boolean successfullySent = mailGunManager.sendEmail(digestMessage.getFrom(), digestMessage.getTo(), digestMessage.getSubject(), digestMessage.getBodyText(), digestMessage.getBodyHtml());
                if (successfullySent) {
                    flagAsSent(notifications);
                }
            } else {
                LOGGER.info("There are no notifications to send for orcid: {}", orcid);
            }
        } catch (RuntimeException e) {
            LOGGER.warn("Problem sending email message to user: " + orcid, e);
        }
    }
    LOGGER.info("Finished sending email messages");
}
Also used : EmailMessage(org.orcid.core.manager.EmailMessage) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) Date(java.util.Date) Notification(org.orcid.jaxb.model.notification_v2.Notification)

Example 30 with EmailEntity

use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.

the class SetUpClientsAndUsers method clearRegistry.

private boolean clearRegistry(OrcidProfile existingProfile, Map<String, String> params) {
    if (existingProfile != null) {
        String orcid = params.get(ORCID);
        String email = params.get(EMAIL);
        // exception
        if (existingProfile.getOrcidBio() == null || existingProfile.getOrcidBio().getContactDetails() == null || existingProfile.getOrcidBio().getContactDetails().retrievePrimaryEmail() == null || !email.equals(existingProfile.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue())) {
            throw new ApplicationException("User with email " + params.get(EMAIL) + " must have orcid id '" + orcid + "' but it is '" + existingProfile.getOrcidId() + "'");
        }
        // Check if the profile have the same password, if not, update the
        // password
        String encryptedPassword = encryptionManager.hashForInternalUse(params.get(PASSWORD));
        if (!encryptedPassword.equals(existingProfile.getPassword())) {
            existingProfile.setPassword(params.get(PASSWORD));
            orcidProfileManager.updatePasswordInformation(existingProfile);
        }
        // Set default names
        Name name = new Name();
        name.setCreditName(new CreditName(params.get(CREDIT_NAME)));
        name.setGivenNames(new GivenNames(params.get(GIVEN_NAMES)));
        name.setFamilyName(new FamilyName(params.get(FAMILY_NAMES)));
        name.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(OrcidVisibilityDefaults.NAMES_DEFAULT.getVisibility().value()));
        if (recordNameManager.exists(orcid)) {
            recordNameManager.updateRecordName(orcid, name);
        } else {
            recordNameManager.createRecordName(orcid, name);
        }
        profileDao.updatePreferences(orcid, true, true, true, true, org.orcid.jaxb.model.common_v2.Visibility.PUBLIC, true, 1f);
        // Set default bio
        org.orcid.jaxb.model.record_v2.Biography bio = biographyManager.getBiography(orcid);
        if (bio == null || bio.getContent() == null) {
            bio = new org.orcid.jaxb.model.record_v2.Biography(params.get(BIO));
            bio.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(OrcidVisibilityDefaults.BIOGRAPHY_DEFAULT.getVisibility().value()));
            biographyManager.createBiography(orcid, bio);
        } else {
            bio.setContent(params.get(BIO));
            bio.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.fromValue(OrcidVisibilityDefaults.BIOGRAPHY_DEFAULT.getVisibility().value()));
            biographyManager.updateBiography(orcid, bio);
        }
        // Remove other names
        List<OtherNameEntity> otherNames = otherNameDao.getOtherNames(orcid, 0L);
        if (otherNames != null && !otherNames.isEmpty()) {
            for (OtherNameEntity otherName : otherNames) {
                otherNameDao.deleteOtherName(otherName);
            }
        }
        // Remove keywords
        List<ProfileKeywordEntity> keywords = profileKeywordDao.getProfileKeywords(orcid, 0L);
        if (keywords != null && !keywords.isEmpty()) {
            for (ProfileKeywordEntity keyword : keywords) {
                profileKeywordDao.deleteProfileKeyword(keyword);
            }
        }
        // Remove researcher urls
        List<ResearcherUrlEntity> rUrls = researcherUrlDao.getResearcherUrls(orcid, 0L);
        if (rUrls != null && !rUrls.isEmpty()) {
            for (ResearcherUrlEntity rUrl : rUrls) {
                researcherUrlDao.deleteResearcherUrl(orcid, rUrl.getId());
            }
        }
        // Remove external ids
        List<ExternalIdentifierEntity> extIds = externalIdentifierDao.getExternalIdentifiers(orcid, System.currentTimeMillis());
        if (extIds != null && !extIds.isEmpty()) {
            for (ExternalIdentifierEntity extId : extIds) {
                externalIdentifierDao.removeExternalIdentifier(orcid, extId.getId());
            }
        }
        // Remove addresses
        List<AddressEntity> addresses = addressDao.getAddresses(orcid, 0L);
        if (addresses != null && !addresses.isEmpty()) {
            for (AddressEntity address : addresses) {
                addressDao.deleteAddress(orcid, address.getId());
            }
        }
        // Remove emails
        List<EmailEntity> emails = emailDao.findByOrcid(orcid, profileEntityManager.getLastModified(orcid));
        if (emails != null && !emails.isEmpty()) {
            for (EmailEntity rc2Email : emails) {
                if (!params.get(EMAIL).equals(rc2Email.getId())) {
                    emailDao.removeEmail(orcid, rc2Email.getId());
                }
            }
        }
        // Remove notifications
        List<NotificationEntity> notifications = notificationDao.findByOrcid(orcid, true, 0, 10000);
        if (notifications != null && !notifications.isEmpty()) {
            for (NotificationEntity notification : notifications) {
                notificationDao.deleteNotificationItemByNotificationId(notification.getId());
                notificationDao.deleteNotificationWorkByNotificationId(notification.getId());
                notificationDao.deleteNotificationById(notification.getId());
            }
        }
        // Remove works
        List<WorkLastModifiedEntity> works = workDao.getWorkLastModifiedList(orcid);
        if (works != null && !works.isEmpty()) {
            for (WorkLastModifiedEntity work : works) {
                workDao.removeWork(orcid, work.getId());
            }
        }
        // Remove affiliations
        List<OrgAffiliationRelationEntity> affiliations = orgAffiliationRelationDao.getByUser(orcid);
        if (affiliations != null && !affiliations.isEmpty()) {
            for (OrgAffiliationRelationEntity affiliation : affiliations) {
                orgAffiliationRelationDao.remove(affiliation.getId());
            }
        }
        // Remove fundings
        List<ProfileFundingEntity> fundings = profileFundingDao.getByUser(orcid, profileEntityManager.getLastModified(orcid));
        if (fundings != null && !fundings.isEmpty()) {
            for (ProfileFundingEntity funding : fundings) {
                profileFundingDao.removeProfileFunding(orcid, funding.getId());
            }
        }
        // Remove peer reviews
        List<PeerReviewEntity> peerReviews = peerReviewDao.getByUser(orcid, profileEntityManager.getLastModified(orcid));
        if (peerReviews != null && !peerReviews.isEmpty()) {
            for (PeerReviewEntity peerReview : peerReviews) {
                peerReviewDao.removePeerReview(orcid, peerReview.getId());
            }
        }
        // Remove 3d party links
        List<OrcidOauth2TokenDetail> tokenDetails = orcidOauth2TokenDetailDao.findByUserName(orcid);
        if (tokenDetails != null && !tokenDetails.isEmpty()) {
            for (OrcidOauth2TokenDetail token : tokenDetails) {
                orcidOauth2TokenDetailDao.remove(token.getId());
            }
        }
        // Unlock just in case it is locked
        profileDao.unlockProfile(orcid);
        return true;
    }
    return false;
}
Also used : ProfileKeywordEntity(org.orcid.persistence.jpa.entities.ProfileKeywordEntity) FamilyName(org.orcid.jaxb.model.record_v2.FamilyName) ExternalIdentifierEntity(org.orcid.persistence.jpa.entities.ExternalIdentifierEntity) FamilyName(org.orcid.jaxb.model.record_v2.FamilyName) CreditName(org.orcid.jaxb.model.common_v2.CreditName) Name(org.orcid.jaxb.model.record_v2.Name) ProfileFundingEntity(org.orcid.persistence.jpa.entities.ProfileFundingEntity) WorkLastModifiedEntity(org.orcid.persistence.jpa.entities.WorkLastModifiedEntity) GivenNames(org.orcid.jaxb.model.record_v2.GivenNames) PeerReviewEntity(org.orcid.persistence.jpa.entities.PeerReviewEntity) NotificationEntity(org.orcid.persistence.jpa.entities.NotificationEntity) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) CreditName(org.orcid.jaxb.model.common_v2.CreditName) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) ApplicationException(org.orcid.core.exception.ApplicationException) OtherNameEntity(org.orcid.persistence.jpa.entities.OtherNameEntity) OrgAffiliationRelationEntity(org.orcid.persistence.jpa.entities.OrgAffiliationRelationEntity)

Aggregations

EmailEntity (org.orcid.persistence.jpa.entities.EmailEntity)45 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)21 Date (java.util.Date)14 Test (org.junit.Test)14 HashSet (java.util.HashSet)11 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)10 DBUnitTest (org.orcid.test.DBUnitTest)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Email (org.orcid.jaxb.model.v3.dev1.record.Email)6 Transactional (org.springframework.transaction.annotation.Transactional)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Set (java.util.Set)4 Email (org.orcid.jaxb.model.record_v2.Email)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2