Search in sources :

Example 1 with Type

use of edu.harvard.iq.dataverse.UserNotification.Type in project dataverse by IQSS.

the class Notifications method getAllNotificationsForUser.

@GET
@Path("all")
public Response getAllNotificationsForUser() {
    User user;
    try {
        user = findUserOrDie();
    } catch (WrappedResponse ex) {
        return error(Response.Status.UNAUTHORIZED, "You must supply an API token.");
    }
    if (user == null) {
        return error(Response.Status.BAD_REQUEST, "A user could not be found based on the API token.");
    }
    if (!(user instanceof AuthenticatedUser)) {
        // It's unlikely we'll reach this error. A Guest doesn't have an API token and would have been blocked above.
        return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
    }
    AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    List<UserNotification> notifications = userNotificationSvc.findByUser(authenticatedUser.getId());
    for (UserNotification notification : notifications) {
        NullSafeJsonBuilder notificationObjectBuilder = jsonObjectBuilder();
        JsonArrayBuilder reasonsForReturn = Json.createArrayBuilder();
        Type type = notification.getType();
        notificationObjectBuilder.add("id", notification.getId());
        notificationObjectBuilder.add("type", type.toString());
        /* FIXME - Re-add reasons for return if/when they are added to the notifications page.
            
            if (Type.RETURNEDDS.equals(type) || Type.SUBMITTEDDS.equals(type)) {
                JsonArrayBuilder reasons = getReasonsForReturn(notification);
                for (JsonValue reason : reasons.build()) {
                    reasonsForReturn.add(reason);
                }
                notificationObjectBuilder.add("reasonsForReturn", reasonsForReturn);
            }
            */
        jsonArrayBuilder.add(notificationObjectBuilder);
    }
    JsonObjectBuilder result = Json.createObjectBuilder().add("notifications", jsonArrayBuilder);
    return ok(result);
}
Also used : Type(edu.harvard.iq.dataverse.UserNotification.Type) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) UserNotification(edu.harvard.iq.dataverse.UserNotification) JsonArrayBuilder(javax.json.JsonArrayBuilder) NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

UserNotification (edu.harvard.iq.dataverse.UserNotification)1 Type (edu.harvard.iq.dataverse.UserNotification.Type)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 User (edu.harvard.iq.dataverse.authorization.users.User)1 NullSafeJsonBuilder (edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1