use of org.plos.ned_client.model.Alert in project wombat by PLOS.
the class AlertService method createAlertForSearch.
private Alert createAlertForSearch(String name, String query, String frequency) {
Alert alert = new Alert();
alert.setSource(ALERT_SOURCE);
alert.setFrequency(frequency);
alert.setName(name);
alert.setQuery(query);
return alert;
}
use of org.plos.ned_client.model.Alert 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.plos.ned_client.model.Alert 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.plos.ned_client.model.Alert in project wombat by PLOS.
the class AlertService method removeSubjectAlert.
/**
* Remove the subject alert for the loggedin user for the journal.
* After removing the subject, if other subjects exist in the alert object for the journal,
* then it uses PUT individuals/{userId}/alerts/{alertId} to modify the alert object,
* otherwise it uses DELETE individuals/{userId}/alerts/{alertId} to remove the
* alert object.
*
* @param authId The authentication ID of the logged in user.
* @param journalKey The journal key.
* @param subjectName The subject to remove alert for.
* @throws IOException
*/
public void removeSubjectAlert(String authId, String journalKey, String subjectName) throws IOException {
String userId = userApi.getUserIdFromAuthId(authId);
List<Alert> alerts = fetchAlerts(userId);
Alert alert = findMatchingAlert(alerts, journalKey).orElseThrow(() -> new AlertException("no subject alert found"));
RemoveResult result = removeSubjectFromAlert(alert, subjectName);
if (result == RemoveResult.NOT_FOUND) {
throw new AlertException("matching subject alert not found");
}
String alertId = alert.getId().toString();
ApiAddress address = buildAlertAddress(userId).addToken(alertId).build();
if (result == RemoveResult.EMPTY_AFTER_REMOVE) {
userApi.deleteObject(address);
} else {
userApi.putObject(address, alert);
}
}
use of org.plos.ned_client.model.Alert in project wombat by PLOS.
the class AlertService method addSubjectAlert.
/**
* Add the subject alert for the loggedin user for the journal.
* If the alert object exists for the journal it uses PUT individuals/{userId}/alerts/{alertId}
* to modify that alert object, otherwise it uses POST individuals/{userId}/alerts
* to create a new alert object.
*
* @param authId The authentication ID of the logged in user.
* @param journalKey The journal key.
* @param subjectName The subject to add alert for.
* @throws IOException
*/
public void addSubjectAlert(String authId, String journalKey, String subjectName) throws IOException {
String userId = userApi.getUserIdFromAuthId(authId);
List<Alert> alerts = fetchAlerts(userId);
Alert alert = findMatchingAlert(alerts, journalKey).map(a -> addSubjectToAlert(a, subjectName)).orElseGet(() -> createAlertForSubject(journalKey, subjectName));
ApiAddress.Builder builder = buildAlertAddress(userId);
if (alert.getId() != null) {
String alertId = alert.getId().toString();
userApi.putObject(builder.addToken(alertId).build(), alert);
} else {
userApi.postObject(builder.build(), alert);
}
}
Aggregations