use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.
the class OrcidProfileManagerImpl method addSourceToEmails.
/**
* Preserves existing source for existing emails, and adds specified source
* for new emails
*
* @param orcidProfile
* The incoming profile
* @param existingProfileEntity
* The existing profile entity from the DB
* @param amenderOrcid
* The source of new emails (from the security context)
*/
private void addSourceToEmails(OrcidProfile orcidProfile, ProfileEntity existingProfileEntity, String amenderOrcid) {
Map<String, EmailEntity> existingMap = new HashMap<>();
Set<EmailEntity> existingEmails = existingProfileEntity.getEmails();
if (existingEmails != null) {
existingMap = EmailEntity.mapByLowerCaseEmail(existingEmails);
}
OrcidBio orcidBio = orcidProfile.getOrcidBio();
if (orcidBio != null) {
ContactDetails contactDetails = orcidBio.getContactDetails();
if (contactDetails != null) {
SourceEntity entity = sourceManager.retrieveSourceEntity();
for (Email email : contactDetails.getEmail()) {
EmailEntity existingEmail = existingMap.get(email.getValue().toLowerCase());
if (existingEmail == null) {
if (entity.getSourceClient() != null && !PojoUtil.isEmpty(entity.getSourceClient().getId())) {
email.setSourceClientId(entity.getSourceClient().getId());
} else if (entity.getSourceProfile() != null && !PojoUtil.isEmpty(entity.getSourceProfile().getId())) {
email.setSource(entity.getSourceProfile().getId());
}
} else {
email.setSource(existingEmail.getSourceId());
email.setSourceClientId(existingEmail.getClientSourceId());
}
}
}
}
}
use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.
the class MembersManagerImpl method createMember.
@Override
public Member createMember(Member member) throws IllegalArgumentException {
if (emailManager.emailExists(member.getEmail().getValue())) {
throw new IllegalArgumentException("Email already exists");
}
Date now = new Date();
String orcid = orcidGenerationManager.createNewOrcid();
ProfileEntity newRecord = new ProfileEntity();
newRecord.setId(orcid);
newRecord.setOrcidType(OrcidType.GROUP);
try {
newRecord.setHashedOrcid(encryptionManager.sha256Hash(orcid));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
newRecord.setActivitiesVisibilityDefault(Visibility.PRIVATE);
newRecord.setClaimed(true);
newRecord.setCreationMethod(CreationMethod.DIRECT.value());
newRecord.setDateCreated(now);
newRecord.setEnableDeveloperTools(false);
newRecord.setEnableNotifications(DefaultPreferences.NOTIFICATIONS_ENABLED);
newRecord.setEncryptedPassword(null);
newRecord.setGroupType(MemberType.fromValue(member.getType().getValue()));
newRecord.setLastModified(now);
newRecord.setLocale(org.orcid.jaxb.model.common_v2.Locale.EN);
newRecord.setRecordLocked(false);
newRecord.setReviewed(false);
newRecord.setSalesforeId(PojoUtil.isEmpty(member.getSalesforceId()) ? null : member.getSalesforceId().getValue());
newRecord.setSendChangeNotifications(false);
newRecord.setSendEmailFrequencyDays(Float.valueOf(DefaultPreferences.SEND_EMAIL_FREQUENCY_DAYS));
newRecord.setSendMemberUpdateRequests(DefaultPreferences.SEND_MEMBER_UPDATE_REQUESTS);
newRecord.setSendOrcidNews(false);
newRecord.setSubmissionDate(now);
newRecord.setUsedRecaptchaOnRegistration(false);
// Set primary email
EmailEntity emailEntity = new EmailEntity();
emailEntity.setId(member.getEmail().getValue());
emailEntity.setProfile(newRecord);
emailEntity.setPrimary(true);
emailEntity.setCurrent(true);
emailEntity.setVerified(true);
// Email is private by default
emailEntity.setVisibility(Visibility.PRIVATE);
SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
String sourceId = sourceEntity.getSourceProfile().getId();
emailEntity.setSourceId(sourceId);
Set<EmailEntity> emails = new HashSet<>();
emails.add(emailEntity);
// Add all emails to record
newRecord.setEmails(emails);
// Set the name
RecordNameEntity recordNameEntity = new RecordNameEntity();
recordNameEntity.setDateCreated(now);
recordNameEntity.setLastModified(now);
recordNameEntity.setProfile(newRecord);
recordNameEntity.setVisibility(Visibility.PUBLIC);
recordNameEntity.setCreditName(member.getGroupName().getValue());
newRecord.setRecordNameEntity(recordNameEntity);
// Set authority
OrcidGrantedAuthority authority = new OrcidGrantedAuthority();
authority.setProfileEntity(newRecord);
authority.setAuthority(OrcidWebRole.ROLE_GROUP.getAuthority());
Set<OrcidGrantedAuthority> authorities = new HashSet<OrcidGrantedAuthority>(1);
authorities.add(authority);
newRecord.setAuthorities(authorities);
profileDao.persist(newRecord);
profileDao.flush();
member.setGroupOrcid(Text.valueOf(orcid));
return member;
}
use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.
the class MembersManagerImpl method updateMemeber.
@Override
public Member updateMemeber(Member member) throws IllegalArgumentException {
String memberId = member.getGroupOrcid().getValue();
String name = member.getGroupName().getValue();
String email = member.getEmail().getValue();
String salesForceId = member.getSalesforceId() == null ? null : member.getSalesforceId().getValue();
MemberType memberType = MemberType.fromValue(member.getType().getValue());
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
boolean memberChangedType = false;
ProfileEntity memberEntity = profileDao.find(member.getGroupOrcid().getValue());
memberEntity.setLastModified(new Date());
memberEntity.setIndexingStatus(IndexingStatus.PENDING);
memberEntity.getRecordNameEntity().setCreditName(name);
memberEntity.setSalesforeId(salesForceId);
if (!memberType.equals(memberEntity.getGroupType())) {
memberEntity.setGroupType(memberType);
memberChangedType = true;
}
EmailEntity primaryEmail = memberEntity.getPrimaryEmail();
if (!email.equals(primaryEmail.getId())) {
if (emailManager.emailExists(email)) {
throw new IllegalArgumentException("Email already exists");
}
Date now = new Date();
EmailEntity newPrimaryEmail = new EmailEntity();
newPrimaryEmail.setLastModified(now);
newPrimaryEmail.setDateCreated(now);
newPrimaryEmail.setCurrent(true);
newPrimaryEmail.setId(email);
newPrimaryEmail.setPrimary(true);
newPrimaryEmail.setVerified(true);
newPrimaryEmail.setVisibility(Visibility.PRIVATE);
memberEntity.setPrimaryEmail(newPrimaryEmail);
}
profileDao.merge(memberEntity);
if (memberChangedType) {
updateClientTypeDueMemberTypeUpdate(memberId, memberType);
}
}
});
clearCache();
return member;
}
use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.
the class ProfileDaoTest method findEmailsUnverfiedDaysTest.
@Test
@Transactional
@Rollback(true)
public void findEmailsUnverfiedDaysTest() {
String orcid = "9999-9999-9999-999X";
ProfileEntity profile = new ProfileEntity();
profile.setId(orcid);
profile.setClaimed(true);
profileDao.persist(profile);
// Created today
EmailEntity unverified_1 = new EmailEntity();
unverified_1.setDateCreated(new Date());
unverified_1.setLastModified(new Date());
unverified_1.setProfile(profile);
unverified_1.setVerified(false);
unverified_1.setVisibility(Visibility.PUBLIC);
unverified_1.setPrimary(false);
unverified_1.setCurrent(true);
unverified_1.setId("unverified_1@test.orcid.org");
// Created a week ago
EmailEntity unverified_2 = new EmailEntity();
unverified_2.setDateCreated(LocalDateTime.now().minusDays(7).toDate());
unverified_2.setLastModified(LocalDateTime.now().minusDays(7).toDate());
unverified_2.setProfile(profile);
unverified_2.setVerified(false);
unverified_2.setVisibility(Visibility.PUBLIC);
unverified_2.setPrimary(false);
unverified_2.setCurrent(true);
unverified_2.setId("unverified_2@test.orcid.org");
// Created 15 days ago
EmailEntity unverified_3 = new EmailEntity();
unverified_3.setDateCreated(LocalDateTime.now().minusDays(15).toDate());
unverified_3.setLastModified(LocalDateTime.now().minusDays(15).toDate());
unverified_3.setProfile(profile);
unverified_3.setVerified(false);
unverified_3.setVisibility(Visibility.PUBLIC);
unverified_3.setPrimary(false);
unverified_3.setCurrent(true);
unverified_3.setId("unverified_3@test.orcid.org");
// Created 7 days ago and verified
EmailEntity verified_1 = new EmailEntity();
verified_1.setDateCreated(LocalDateTime.now().minusDays(7).toDate());
verified_1.setLastModified(LocalDateTime.now().minusDays(7).toDate());
verified_1.setProfile(profile);
verified_1.setVerified(true);
verified_1.setVisibility(Visibility.PUBLIC);
verified_1.setPrimary(false);
verified_1.setCurrent(true);
verified_1.setId("verified_1@test.orcid.org");
// Created 15 days ago and verified
EmailEntity verified_2 = new EmailEntity();
verified_2.setDateCreated(LocalDateTime.now().minusDays(15).toDate());
verified_2.setLastModified(LocalDateTime.now().minusDays(15).toDate());
verified_2.setProfile(profile);
verified_2.setVerified(true);
verified_2.setVisibility(Visibility.PUBLIC);
verified_2.setPrimary(false);
verified_2.setCurrent(true);
verified_2.setId("verified_2@test.orcid.org");
emailDao.removeAll();
emailDao.persist(unverified_1);
emailDao.persist(unverified_2);
emailDao.persist(unverified_3);
emailDao.persist(verified_1);
emailDao.persist(verified_2);
List<Pair<String, Date>> results = profileDao.findEmailsUnverfiedDays(7, 100, EmailEventType.VERIFY_EMAIL_7_DAYS_SENT);
assertNotNull(results);
assertEquals(2, results.size());
boolean found1 = false, found2 = false;
for (Pair<String, Date> element : results) {
assertNotNull(element.getRight());
if (element.getLeft().equals("unverified_2@test.orcid.org")) {
found1 = true;
} else if (element.getLeft().equals("unverified_3@test.orcid.org")) {
found2 = true;
} else {
fail("Unexpected email id: " + element.getRight());
}
}
assertTrue(found1);
assertTrue(found2);
// Put an email event on 'unverified_2@test.orcid.org' and verify there is only one result
emailEventDao.persist(new EmailEventEntity("unverified_2@test.orcid.org", EmailEventType.VERIFY_EMAIL_7_DAYS_SENT));
results = profileDao.findEmailsUnverfiedDays(7, 100, EmailEventType.VERIFY_EMAIL_7_DAYS_SENT);
assertNotNull(results);
assertEquals(1, results.size());
assertEquals("unverified_3@test.orcid.org", results.get(0).getLeft());
// Put an email event on 'unverified_3@test.orcid.org' and verify there is no result anymore
emailEventDao.persist(new EmailEventEntity("unverified_3@test.orcid.org", EmailEventType.VERIFY_EMAIL_TOO_OLD));
results = profileDao.findEmailsUnverfiedDays(7, 100, EmailEventType.VERIFY_EMAIL_7_DAYS_SENT);
assertNotNull(results);
assertTrue(results.isEmpty());
}
use of org.orcid.persistence.jpa.entities.EmailEntity in project ORCID-Source by ORCID.
the class JpaJaxbEmailAdapterTest method getEmailEntity.
private EmailEntity getEmailEntity() {
EmailEntity result = new EmailEntity();
result.setId("email@test.orcid.org");
result.setCurrent(true);
result.setPrimary(true);
result.setProfile(new ProfileEntity("0000-0000-0000-0000"));
result.setVerified(true);
result.setDateCreated(new Date());
result.setLastModified(new Date());
result.setVisibility(org.orcid.jaxb.model.common_v2.Visibility.PRIVATE);
result.setClientSourceId("APP-000000001");
return result;
}
Aggregations