-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoing in prep for inquiry mailing.
- Loading branch information
1 parent
1b8caa7
commit b9e0639
Showing
4 changed files
with
168 additions
and
55 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
housekeeping/src/main/groovy/whelk/housekeeping/InquirySender.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package whelk.housekeeping | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Log4j2 | ||
import whelk.Whelk | ||
|
||
import java.sql.Connection | ||
import java.sql.PreparedStatement | ||
import java.sql.ResultSet | ||
import java.sql.Timestamp | ||
import java.time.Instant | ||
import java.time.ZoneOffset | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.time.temporal.ChronoUnit | ||
|
||
@CompileStatic | ||
@Log4j2 | ||
class InquirySender extends HouseKeeper { | ||
private final String STATE_KEY = "CXZ inquiry email sender" | ||
private String status = "OK" | ||
private final Whelk whelk | ||
|
||
public InquirySender(Whelk whelk) { | ||
this.whelk = whelk | ||
} | ||
|
||
@Override | ||
String getName() { | ||
return "Inquiry sender" | ||
} | ||
|
||
@Override | ||
String getStatusDescription() { | ||
return status | ||
} | ||
|
||
public String getCronSchedule() { | ||
return "* * * * *" | ||
} | ||
|
||
@Override | ||
void trigger() { | ||
|
||
Timestamp from = Timestamp.from(Instant.now().minus(1, ChronoUnit.DAYS)) | ||
Map sendState = whelk.getStorage().getState(STATE_KEY) | ||
if (sendState && sendState.notifiedChangesUpTo) | ||
from = Timestamp.from( ZonedDateTime.parse( (String) sendState.notifiedChangesUpTo, DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant() ) | ||
|
||
Connection connection | ||
PreparedStatement statement | ||
ResultSet resultSet | ||
|
||
Instant notifiedChangesUpTo = from.toInstant() | ||
|
||
connection = whelk.getStorage().getOuterConnection() | ||
connection.setAutoCommit(false) | ||
try { | ||
String sql = "SELECT modified, data FROM lddb WHERE data#>>'{@graph,1,@type}' IN ('InquiryAction', 'ChangeNotice') AND modified > ?;" | ||
connection.setAutoCommit(false) | ||
statement = connection.prepareStatement(sql) | ||
statement.setTimestamp(1, from) | ||
statement.setFetchSize(512) | ||
resultSet = statement.executeQuery() | ||
|
||
while (resultSet.next()) { | ||
String data = resultSet.getString("data") | ||
|
||
// TODO: Send email! | ||
|
||
Instant lastChangeObservationForInstance = resultSet.getTimestamp("modified").toInstant() | ||
if (lastChangeObservationForInstance.isAfter(notifiedChangesUpTo)) | ||
notifiedChangesUpTo = lastChangeObservationForInstance | ||
} | ||
} catch (Throwable e) { | ||
status = "Failed with:\n" + e + "\nat:\n" + e.getStackTrace().toString() | ||
throw e | ||
} finally { | ||
connection.close() | ||
if (notifiedChangesUpTo.isAfter(from.toInstant())) { | ||
Map newState = new HashMap() | ||
newState.notifiedChangesUpTo = notifiedChangesUpTo.atOffset(ZoneOffset.UTC).toString() | ||
whelk.getStorage().putState(STATE_KEY, newState) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
housekeeping/src/main/groovy/whelk/housekeeping/NotificationUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package whelk.housekeeping | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Log4j2 | ||
import org.simplejavamail.api.email.Email | ||
import org.simplejavamail.api.mailer.Mailer | ||
import org.simplejavamail.email.EmailBuilder | ||
import org.simplejavamail.mailer.MailerBuilder | ||
import whelk.Whelk | ||
import whelk.util.PropertyLoader | ||
|
||
@CompileStatic | ||
@Log4j2 | ||
class NotificationUtils { | ||
|
||
/** | ||
* Get all user settings and arrange them by requested library-uri, so that you | ||
* might for example start with a uri https://libris.kb.se/library/Utb1 and from it | ||
* get a list of user(-settings)s that subscribes to updates for things held by | ||
* that library | ||
*/ | ||
static Map<String, List<Map>> getAllSubscribingUsers(Whelk whelk) { | ||
Map<String, List<Map>> libraryToUserSettings = new HashMap<>() | ||
List<Map> allUserSettingStrings = whelk.getStorage().getAllUserData() | ||
for (Map settings : allUserSettingStrings) { | ||
if (!settings["notificationEmail"]) | ||
continue | ||
settings?.requestedNotifications?.each { request -> | ||
if (!request instanceof Map) | ||
return | ||
if (!request["heldBy"]) | ||
return | ||
|
||
String heldBy = request["heldBy"] | ||
if (!libraryToUserSettings.containsKey(heldBy)) | ||
libraryToUserSettings.put(heldBy, []) | ||
libraryToUserSettings[heldBy].add(settings) | ||
} | ||
} | ||
return libraryToUserSettings | ||
} | ||
|
||
static Mailer mailer = null | ||
static String senderAddress | ||
static synchronized void sendEmail(String recipient, String subject, String body) { | ||
if (mailer == null) { | ||
Properties props = PropertyLoader.loadProperties("secret") | ||
if (props.containsKey("smtpServer") && | ||
props.containsKey("smtpPort") && | ||
props.containsKey("smtpSender") && | ||
props.containsKey("smtpUser") && | ||
props.containsKey("smtpPassword")) | ||
mailer = MailerBuilder | ||
.withSMTPServer( | ||
(String) props.get("smtpServer"), | ||
Integer.parseInt((String)props.get("smtpPort")), | ||
(String) props.get("smtpUser"), | ||
(String) props.get("smtpPassword") | ||
).buildMailer() | ||
senderAddress = props.get("smtpSender") | ||
} | ||
|
||
if (mailer) { | ||
Email email = EmailBuilder.startingBlank() | ||
.to(recipient) | ||
.withSubject(subject) | ||
.from(senderAddress) | ||
.withPlainText(body) | ||
.buildEmail() | ||
|
||
log.info("Sending notification (cxz) email to " + recipient) | ||
mailer.sendMail(email) | ||
} else { | ||
log.info("Should now have sent notification (cxz) email to " + recipient + " but SMTP is not configured.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters