Skip to content

Commit

Permalink
Changed the code to use triples
Browse files Browse the repository at this point in the history
  • Loading branch information
Camelia-Orcid committed Nov 14, 2023
1 parent aa1f1cb commit 05c8a6a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public interface ProfileDao extends GenericDao<ProfileEntity, String> {

public List<Triple<String, Boolean, Date>> findEmailsUnverfiedDays(int daysUnverified, int maxResults);

public List<Triple<String, Boolean, String>> findEmailsUnverifiedDaysByEventType(int daysUnverified, int tooOldNumberOfDays);

String retrieveOrcidType(String orcid);

List<Object[]> findInfoForDecryptionAnalysis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProfileDaoImpl extends GenericDaoImpl<ProfileEntity, String> implements ProfileDao {

private static final String PRIVATE_VISIBILITY = "PRIVATE";

private static final Logger LOGGER = LoggerFactory.getLogger(ProfileDaoImpl.class);

@Value("${org.orcid.postgres.query.timeout:30000}")
private Integer queryTimeout;
Expand Down Expand Up @@ -150,7 +155,32 @@ public List<Triple<String, Boolean, Date>> findEmailsUnverfiedDays(int daysUnver
results.add(pair);
});
return results;
}
}


@SuppressWarnings("unchecked")
@Override
public List<Triple<String, Boolean, String>> findEmailsUnverifiedDaysByEventType(int daysUnverified, int tooOldNumberOfDays) {
StringBuilder queryString = new StringBuilder("SELECT e.email, e.is_primary, ev.email_event_type FROM email e ");
queryString.append("LEFT JOIN email_event ev ON e.email = ev.email ");
queryString.append("JOIN profile p on p.orcid = e.orcid and p.claimed = true ");
queryString.append("AND p.deprecated_date is null AND p.profile_deactivation_date is null AND p.account_expiry is null ");
queryString.append("where e.is_verified = false ");
queryString.append("and e.date_created between (now() - CAST('").append(tooOldNumberOfDays)
.append("' AS INTERVAL DAY)) and (now() - CAST('").append(daysUnverified).append("' AS INTERVAL DAY)) ");
queryString.append("and e.date_created < (now() - CAST('").append(daysUnverified).append("' AS INTERVAL DAY)) ");
queryString.append("and (e.source_id = e.orcid OR e.source_id is null)");
queryString.append(" ORDER BY e.last_modified");

Query query = entityManager.createNativeQuery(queryString.toString());
List<Object[]> dbInfo = query.getResultList();
List<Triple<String, Boolean, String>> results = new ArrayList<Triple<String, Boolean, String>>();
dbInfo.stream().forEach(element -> {
Triple<String, Boolean, String> pair = Triple.of((String) element[0], (Boolean) element[1], (String) element[2]);
results.add(pair);
});
return results;
}

@SuppressWarnings("unchecked")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import liquibase.repackaged.org.apache.commons.lang3.StringUtils;

/**
*
* @author Will Simpson
Expand Down Expand Up @@ -596,43 +598,45 @@ synchronized public void processUnverifiedEmails2Days() {
synchronized public void processUnverifiedEmails7Days() {
if(Features.SEND_ALL_VERIFICATION_EMAILS.isActive()) {
LOGGER.info("About to process unverIfied emails for 7 days reminder");
List<Triple<String, Boolean, Date>> elements = Collections.<Triple<String, Boolean, Date>> emptyList();
do {
elements = profileDaoReadOnly.findEmailsUnverfiedDays(verifyReminderAfterSevenDays, 100);
LOGGER.info("Got batch of {} profiles with unverified emails for 7 days reminder", elements.size());
LocalDateTime now = LocalDateTime.now();
Date tooOld = now.minusDays(emailTooOld).toDate();
for (Triple<String, Boolean, Date> element : elements) {
if(element.getRight() == null || element.getRight().after(tooOld)) {
List<Triple<String, Boolean, String>> elements = Collections.<Triple<String, Boolean, String>> emptyList();

elements = profileDaoReadOnly.findEmailsUnverifiedDaysByEventType(verifyReminderAfterSevenDays, emailTooOld);
LOGGER.info("Got {} profiles with email event and unverified emails for 7 days reminder", elements.size());

HashMap<String, Triple<String, Boolean, String>> hasEventTypesMap = hasEventTypes (elements, List.of(EmailEventType.VERIFY_EMAIL_7_DAYS_SENT, EmailEventType.VERIFY_EMAIL_7_DAYS_SENT_SKIPPED , EmailEventType.VERIFY_EMAIL_TOO_OLD));
for (Triple<String, Boolean, String> element : elements) {
if(!hasEventTypesMap.containsKey(element.getLeft()) )
processUnverifiedEmailsInTransaction(element.getLeft(), element.getMiddle(), EmailEventType.VERIFY_EMAIL_7_DAYS_SENT, EmailEventType.VERIFY_EMAIL_7_DAYS_SENT_SKIPPED);
} else {
// Mark is as too old to send the verification email
markUnverifiedEmailAsTooOld(element.getLeft());
}
hasEventTypesMap.put(element.getLeft(), element);
}
}
} while (!elements.isEmpty());
}
}

//helper method to get the triple map for an event type list
private HashMap<String, Triple<String, Boolean, String>> hasEventTypes(List<Triple<String, Boolean, String>> elements, List<EmailEventType> eventTypes) {
HashMap<String, Triple<String, Boolean, String>> hasEventTypesMap = new HashMap<String, Triple<String, Boolean, String>>();
for (Triple<String, Boolean, String> element : elements) {
if(eventTypes.contains(EmailEventType.valueOf(element.getRight()))) {
hasEventTypesMap.put(element.getLeft(), element);
}
}
return hasEventTypesMap;
}


synchronized public void processUnverifiedEmails28Days() {
if(Features.SEND_ALL_VERIFICATION_EMAILS.isActive()) {
LOGGER.info("About to process unverIfied emails for 28 days reminder");
List<Triple<String, Boolean, Date>> elements = Collections.<Triple<String, Boolean, Date>> emptyList();
do {
elements = profileDaoReadOnly.findEmailsUnverfiedDays(verifyReminderAfterTwentyEightDays, 100);
LOGGER.info("Got batch of {} profiles with unverified emails for 28 days reminder", elements.size());
LocalDateTime now = LocalDateTime.now();
Date tooOld = now.minusDays(emailTooOld).toDate();
for (Triple<String, Boolean, Date> element : elements) {
if(element.getRight() == null || element.getRight().after(tooOld)) {
processUnverifiedEmailsInTransaction(element.getLeft(), element.getMiddle(), EmailEventType.VERIFY_EMAIL_28_DAYS_SENT, EmailEventType.VERIFY_EMAIL_28_DAYS_SENT_SKIPPED);
} else {
// Mark is as too old to send the verification email
markUnverifiedEmailAsTooOld(element.getLeft());
}
}
} while (!elements.isEmpty());
List<Triple<String, Boolean, String>> elements = Collections.<Triple<String, Boolean, String>> emptyList();
elements = profileDaoReadOnly.findEmailsUnverifiedDaysByEventType(verifyReminderAfterTwentyEightDays, emailTooOld);
LOGGER.info("Got {} profiles with email event and unverified emails for 28 days reminder", elements.size());

HashMap<String, Triple<String, Boolean, String>> hasEventTypesMap = hasEventTypes (elements, List.of(EmailEventType.VERIFY_EMAIL_28_DAYS_SENT, EmailEventType.VERIFY_EMAIL_28_DAYS_SENT_SKIPPED , EmailEventType.VERIFY_EMAIL_TOO_OLD));
for (Triple<String, Boolean, String> element : elements) {
if(!hasEventTypesMap.containsKey(element.getLeft()) )
processUnverifiedEmailsInTransaction(element.getLeft(), element.getMiddle(), EmailEventType.VERIFY_EMAIL_28_DAYS_SENT, EmailEventType.VERIFY_EMAIL_28_DAYS_SENT_SKIPPED);
hasEventTypesMap.put(element.getLeft(), element);
}
}
}

Expand Down

0 comments on commit 05c8a6a

Please sign in to comment.