Search in sources :

Example 1 with Alert

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;
}
Also used : Alert(org.plos.ned_client.model.Alert)

Example 2 with 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;
    }
}
Also used : AlertQuery(org.ambraproject.wombat.model.AlertQuery) Alert(org.plos.ned_client.model.Alert)

Example 3 with Alert

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();
}
Also used : AlertQuery(org.ambraproject.wombat.model.AlertQuery) Alert(org.plos.ned_client.model.Alert)

Example 4 with Alert

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);
    }
}
Also used : Alert(org.plos.ned_client.model.Alert) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress)

Example 5 with 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);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) TypeToken(com.google.gson.reflect.TypeToken) Iterator(java.util.Iterator) Autowired(org.springframework.beans.factory.annotation.Autowired) MultiValueMap(org.springframework.util.MultiValueMap) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) IOException(java.io.IOException) HashMap(java.util.HashMap) Objects(java.util.Objects) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Alert(org.plos.ned_client.model.Alert) Type(java.lang.reflect.Type) Gson(com.google.gson.Gson) AlertQuery(org.ambraproject.wombat.model.AlertQuery) Map(java.util.Map) UserApi(org.ambraproject.wombat.service.remote.UserApi) Optional(java.util.Optional) LinkedList(java.util.LinkedList) Alert(org.plos.ned_client.model.Alert) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress)

Aggregations

Alert (org.plos.ned_client.model.Alert)7 AlertQuery (org.ambraproject.wombat.model.AlertQuery)4 ApiAddress (org.ambraproject.wombat.service.remote.ApiAddress)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 UserApi (org.ambraproject.wombat.service.remote.UserApi)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 MultiValueMap (org.springframework.util.MultiValueMap)1