use of org.ambraproject.wombat.model.AlertQuery in project wombat by PLOS.
the class AlertService method isUserSubscribed.
/**
* Check if the logged in user is subscribed to the subject alert or not.
*
* @param authId The authentication ID of the logged in user, different from Ned ID
* @param journalKey The journal key to check for alert in.
* @param subjectName The subject for which to check for alert.
* @return whether the user is subscribed to the subject alert in this journal.
* @throws IOException
*/
public boolean isUserSubscribed(String authId, String journalKey, String subjectName) throws IOException {
String userId = userApi.getUserIdFromAuthId(authId);
List<Alert> alerts = fetchAlerts(userId);
Optional<Alert> existing = findMatchingAlert(alerts, journalKey);
if (!existing.isPresent()) {
return false;
} else {
AlertQuery query = getAlertQuery(existing.get());
List<String> filterSubjects = query.getFilterSubjectsDisjunction();
for (String subject : filterSubjects) {
if (subjectName.equalsIgnoreCase(subject)) {
return true;
}
}
return false;
}
}
use of org.ambraproject.wombat.model.AlertQuery in project wombat by PLOS.
the class AlertService method findMatchingAlert.
/**
* For the list of alerts, return the first alert object that matches the journal, or empty.
* It matches only if there is only one item in "filterJournals" matching the specified journal
* key.
*
* Besides matching for the journal key in "filterJournals" attribute of the alert,
* it also checks if the "source" is "Ambra", "frequency" is "weekly" and "name" is PLoSONE".
*
* @param alerts JSON list of alert objects.
* @param journalKey The journal key string.
* @return First matching alert object for journalKey or empty if no match.
*/
private Optional<Alert> findMatchingAlert(List<Alert> alerts, String journalKey) {
for (Alert alert : alerts) {
String source = alert.getSource();
String frequency = alert.getFrequency();
String name = alert.getName();
if (ALERT_SOURCE.equalsIgnoreCase(source) && ALERT_FREQUENCY.equalsIgnoreCase(frequency) && ALERT_NAME.equalsIgnoreCase(name)) {
AlertQuery query = getAlertQuery(alert);
List<String> filterJournals = query.getFilterJournals();
if (filterJournals.size() == 1 && journalKey.equals(filterJournals.get(0))) {
return Optional.of(alert);
}
}
}
return Optional.empty();
}
use of org.ambraproject.wombat.model.AlertQuery in project wombat by PLOS.
the class AlertService method addSubjectToAlert.
/**
* Add the subject in the "filterSubjectsDisjuction" attribute
* of "query" attribute of the alert JSON object.
*
* @param alert
* @param subjectName
*/
private Alert addSubjectToAlert(Alert alert, String subjectName) {
AlertQuery query = getAlertQuery(alert);
List<String> filterSubjects = query.getFilterSubjectsDisjunction();
for (String filterSubject : filterSubjects) {
if (subjectName.equalsIgnoreCase(filterSubject)) {
throw new AlertException("already exists");
}
}
query.setFilterSubjectsDisjunction(ImmutableList.<String>builder().addAll(filterSubjects).add(subjectName).build());
alert.setQuery(gson.toJson(query));
return alert;
}
use of org.ambraproject.wombat.model.AlertQuery in project wombat by PLOS.
the class AlertService method createAlertForSubject.
/**
* Create a new JSON alert object for the subject in that journal.
*
* @param journalKey The journal key.
* @param subjectName The subject to create alert for.
* @return JSON alert object.
*/
private Alert createAlertForSubject(String journalKey, String subjectName) {
AlertQuery query = new AlertQuery();
query.setFilterJournals(ImmutableList.of(journalKey));
query.setFilterSubjectsDisjunction(ImmutableList.of(subjectName));
Alert alert = new Alert();
alert.setSource(ALERT_SOURCE);
alert.setFrequency(ALERT_FREQUENCY);
alert.setName(ALERT_NAME);
alert.setQuery(gson.toJson(query));
return alert;
}
use of org.ambraproject.wombat.model.AlertQuery in project wombat by PLOS.
the class AlertService method removeSubjectFromAlert.
/**
* Remove a subject from the alert object.
*
* @param alert The JSON alert object.
* @param subjectName
* @return Indicates whether the subject was not found, or found and removed, and
* if yes, then whether the "filterSubjects" is empty after remove or not.
*/
private RemoveResult removeSubjectFromAlert(Alert alert, String subjectName) {
AlertQuery query = getAlertQuery(alert);
List<String> filterSubjects = new LinkedList<>(query.getFilterSubjectsDisjunction());
boolean found = false;
for (Iterator<String> it = filterSubjects.iterator(); it.hasNext(); ) {
String filterSubject = it.next();
if (subjectName.equalsIgnoreCase(filterSubject)) {
it.remove();
found = true;
}
}
if (!found) {
return RemoveResult.NOT_FOUND;
}
query.setFilterSubjectsDisjunction(filterSubjects);
alert.setQuery(gson.toJson(query));
return (filterSubjects.size() == 0 ? RemoveResult.EMPTY_AFTER_REMOVE : RemoveResult.NOT_EMPTY_AFTER_REMOVE);
}
Aggregations