Search in sources :

Example 1 with UserNotification

use of edu.harvard.iq.dataverse.UserNotification 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)

Example 2 with UserNotification

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

the class MailUtilTest method setUp.

@Before
public void setUp() {
    rootDataverseName = "LibraScholar";
    userNotification = new UserNotification();
}
Also used : UserNotification(edu.harvard.iq.dataverse.UserNotification) Before(org.junit.Before)

Example 3 with UserNotification

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

the class ConfirmEmailServiceBean method sendLinkOnEmailChange.

/**
 * @todo: We expect to send two messages. One at signup and another at email
 * change.
 */
private void sendLinkOnEmailChange(AuthenticatedUser aUser, String confirmationUrl) throws ConfirmEmailException {
    String messageBody = BundleUtil.getStringFromBundle("notification.email.changeEmail", Arrays.asList(aUser.getFirstName(), confirmationUrl, ConfirmEmailUtil.friendlyExpirationTime(systemConfig.getMinutesUntilConfirmEmailTokenExpires())));
    logger.log(Level.FINE, "messageBody:{0}", messageBody);
    try {
        String toAddress = aUser.getEmail();
        try {
            Dataverse rootDataverse = dataverseService.findRootDataverse();
            if (rootDataverse != null) {
                String rootDataverseName = rootDataverse.getName();
                // FIXME: consider refactoring this into MailServiceBean.sendNotificationEmail. CONFIRMEMAIL may be the only type where we don't want an in-app notification.
                UserNotification userNotification = new UserNotification();
                userNotification.setType(UserNotification.Type.CONFIRMEMAIL);
                String subject = MailUtil.getSubjectTextBasedOnNotification(userNotification, rootDataverseName, null);
                logger.fine("sending email to " + toAddress + " with this subject: " + subject);
                mailService.sendSystemEmail(toAddress, subject, messageBody);
            }
        } catch (Exception e) {
            logger.info("The root dataverse is not present. Don't send a notification to dataverseAdmin.");
        }
    } catch (Exception ex) {
        /**
         * @todo get more specific about the exception that's thrown when
         * `asadmin create-javamail-resource` (or equivalent) hasn't been
         * run.
         */
        throw new ConfirmEmailException("Problem sending email confirmation link possibily due to mail server not being configured.");
    }
    logger.log(Level.FINE, "attempted to send mail to {0}", aUser.getEmail());
}
Also used : UserNotification(edu.harvard.iq.dataverse.UserNotification) Dataverse(edu.harvard.iq.dataverse.Dataverse) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 4 with UserNotification

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

the class DataverseUserPage method displayNotification.

public void displayNotification() {
    for (UserNotification userNotification : notificationsList) {
        switch(userNotification.getType()) {
            case ASSIGNROLE:
            case REVOKEROLE:
                // Can either be a dataverse or dataset, so search both
                Dataverse dataverse = dataverseService.find(userNotification.getObjectId());
                if (dataverse != null) {
                    userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataverse));
                    userNotification.setTheObject(dataverse);
                } else {
                    Dataset dataset = datasetService.find(userNotification.getObjectId());
                    if (dataset != null) {
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataset));
                        userNotification.setTheObject(dataset);
                    } else {
                        DataFile datafile = fileService.find(userNotification.getObjectId());
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), datafile));
                        userNotification.setTheObject(datafile);
                    }
                }
                break;
            case CREATEDV:
                userNotification.setTheObject(dataverseService.find(userNotification.getObjectId()));
                break;
            case REQUESTFILEACCESS:
                DataFile file = fileService.find(userNotification.getObjectId());
                userNotification.setTheObject(file.getOwner());
                break;
            case GRANTFILEACCESS:
            case REJECTFILEACCESS:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERUPDATED:
            case CREATEDS:
            case SUBMITTEDDS:
            case PUBLISHEDDS:
            case RETURNEDDS:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERDELETEFAILED:
                userNotification.setTheObject(fileService.findFileMetadata(userNotification.getObjectId()));
                break;
            case CREATEACC:
                userNotification.setTheObject(userNotification.getUser());
                break;
            case CHECKSUMFAIL:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case FILESYSTEMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case CHECKSUMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
        }
        userNotification.setDisplayAsRead(userNotification.isReadNotification());
        if (userNotification.isReadNotification() == false) {
            userNotification.setReadNotification(true);
            userNotificationService.save(userNotification);
        }
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) UserNotification(edu.harvard.iq.dataverse.UserNotification) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 5 with UserNotification

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

the class DataverseUserPage method remove.

public String remove(Long notificationId) {
    UserNotification userNotification = userNotificationService.find(notificationId);
    userNotificationService.delete(userNotification);
    for (UserNotification uNotification : notificationsList) {
        if (Objects.equals(uNotification.getId(), userNotification.getId())) {
            notificationsList.remove(uNotification);
            break;
        }
    }
    return null;
}
Also used : UserNotification(edu.harvard.iq.dataverse.UserNotification)

Aggregations

UserNotification (edu.harvard.iq.dataverse.UserNotification)5 Dataverse (edu.harvard.iq.dataverse.Dataverse)2 DataFile (edu.harvard.iq.dataverse.DataFile)1 Dataset (edu.harvard.iq.dataverse.Dataset)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 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Before (org.junit.Before)1