Search in sources :

Example 11 with UnexpectedRollbackException

use of org.springframework.transaction.UnexpectedRollbackException in project vboard by voyages-sncf-technologies.

the class CommentsController method addComment.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public Comment addComment(@Valid @RequestBody CommentParams params) {
    String author = params.getAuthor();
    String pinId = params.getPinId();
    String text = params.getText();
    DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
    Comment comment = new Comment(pinId, author, text, postDateUTC);
    // Check if the author given is the same as the session user (the author param could be deleted and replaced with the session one)
    permission.ensureNewEntityAuthorMatchesSessionUser(author);
    try {
        this.logger.debug("addComment: author={} - pin={} -text={}", author, pinId, text);
        this.commentDAO.save(comment);
        Pin pin = this.pinDAO.findByPinId(pinId);
        if (pin != null) {
            pin.increaseCommentsNumber();
            this.pinDAO.save(pin);
        }
        // Increase the number of comments for the given pin in elasticsearch
        this.elsClient.addComment(comment.getPinId());
        // Update the stats (for the user, and for the author of the pin where the comment has been added)
        this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
        if (pin != null && User.getEmailFromString(pin.getAuthor()).isPresent()) {
            User userAuthor = this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get());
            if (userAuthor != null && userAuthor != permission.getSessionUserWithSyncFromDB()) {
                this.gamification.updateStats(userAuthor);
            }
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    // Send comment notifications
    notifications.addNotificationsFromComment(pinId);
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 12 with UnexpectedRollbackException

use of org.springframework.transaction.UnexpectedRollbackException in project vboard by voyages-sncf-technologies.

the class CommentsController method addCommentFromVblog.

// Comments posted from VBlog (wordpress)
@RequestMapping(value = "/vblog", method = RequestMethod.POST, consumes = { "application/x-www-form-urlencoded" })
@ResponseBody
@Valid
public Comment addCommentFromVblog(@RequestParam("text") final String text, @RequestParam("pinId") String pinId, @RequestParam("author") String author, @RequestParam("ID") final String ID, HttpServletRequest request) {
    Comment comment = this.commentDAO.findById("vblog-" + ID);
    // Should restrict the host name from wordpress (vblog)
    /*if (!request.getRemoteHost().equals(hostName.getHostName())) {
            throw new VBoardException("Unknown web site - The hostname that is using this method is not authorized: hostname: " + request.getRemoteHost());
        }*/
    User user = this.userDAO.findByEmail(author);
    if (user != null) {
        author = user.getUserString();
    }
    if (comment == null) {
        DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
        comment = new Comment("vblog-" + ID, "vblog-" + pinId, author, text, postDateUTC.toString());
        Pin pin = this.pinDAO.findByPinId("vblog-" + pinId);
        if (pin != null) {
            pin.increaseCommentsNumber();
            this.pinDAO.save(pin);
            if (User.getEmailFromString(pin.getAuthor()).isPresent()) {
                User userAuthor = this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get());
                if (userAuthor != null && userAuthor != permission.getSessionUserWithSyncFromDB()) {
                    // Update the pin's author stats
                    this.gamification.updateStats(userAuthor);
                }
            }
        }
    } else {
        comment.setText(text);
        comment.setAuthor(author);
    }
    try {
        this.logger.debug("addComment: author={} - pin={} -text={}", author, "vblog-" + pinId, text);
        this.commentDAO.save(comment);
        // Increase the number of comments for the given pin in elasticsearch
        this.elsClient.addComment(comment.getPinId());
        // Send comment notifications
        notifications.addNotificationsFromComment(comment.getPinId());
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    if (user != null) {
        // Update the user's stats
        this.gamification.updateStats(user);
    }
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 13 with UnexpectedRollbackException

use of org.springframework.transaction.UnexpectedRollbackException in project vboard by voyages-sncf-technologies.

the class GamificationController method trackConnection.

// Add a new connexion to the user's stats
@RequestMapping(value = "/connexion", method = RequestMethod.POST)
@ResponseBody
@Valid
public int trackConnection() {
    final User user = permission.getSessionUserWithSyncFromDB();
    Stats stats = this.statsDAO.findByEmail(user.getEmail());
    // If the user has no stats yet, a new one is created
    if (stats == null) {
        stats = new Stats(user.getEmail(), user.getTeam());
    }
    // Get the last stat connexion
    DateTime lastConnexion = new DateTime(DateTimeZone.UTC);
    String date = stats.getLastConnexion();
    if (date == null) {
        // Used for version compatibility (new stats should get a default one)
        stats.setLastConnexion(new DateTime(DateTimeZone.UTC).toString());
    } else {
        try {
            lastConnexion = new DateTime(date);
        }// NOPMD
         catch (IllegalArgumentException e) {
        }
    }
    // Gamification connexion can only be increased once a day.
    if (lastConnexion.plusDays(1).isBeforeNow()) {
        // Update all the stats before
        stats = this.getStats(user);
        stats.setLastConnexion(new DateTime(DateTimeZone.UTC).toString());
        // If the connexion level increased enough for the user to get a badge, a notification is sent.
        if (gamification.getLevel(stats.getConnexionNumber() * CONNEXIONS_WEIGHT) < gamification.getLevel((stats.getConnexionNumber() + 1) * CONNEXIONS_WEIGHT) && gamification.levels().contains(gamification.getLevel((stats.getConnexionNumber() + 1) * CONNEXIONS_WEIGHT))) {
            notifications.addNotificationsFromBadges(user, "\"Lecteur " + gamification.badgesMessageUser(gamification.getLevel((stats.getConnexionNumber() + 1) * CONNEXIONS_WEIGHT)) + "\"");
        }
        stats.incrementConnexionNumber();
    }
    try {
        this.statsDAO.save(stats);
        this.logger.debug("Connexion: User: {}, connexion number: {}", user.getNiceName(), stats.getConnexionNumber());
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return stats.getConnexionNumber();
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 14 with UnexpectedRollbackException

use of org.springframework.transaction.UnexpectedRollbackException in project vboard by voyages-sncf-technologies.

the class UsersController method addNewUser.

@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public // Parsing the params in the JSON body requires using a dedicated @RequestBody annotated class instead of simple @RequestParam arguments
User addNewUser(@Valid @RequestBody UserParams params) {
    final String email = params.getEmail();
    final String firstName = params.getFirst_name();
    final String lastName = params.getLast_name();
    final User newUser = new User(email, firstName, lastName);
    try {
        this.logger.debug("addNewUser: email={} - first_name={} - last_name={}", email, firstName, lastName);
        return this.userDAO.save(newUser);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 15 with UnexpectedRollbackException

use of org.springframework.transaction.UnexpectedRollbackException in project spring-data-commons by spring-projects.

the class ChainedTransactionManager method rollback.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#rollback(org.springframework.transaction.TransactionStatus)
	 */
public void rollback(TransactionStatus status) throws TransactionException {
    Exception rollbackException = null;
    PlatformTransactionManager rollbackExceptionTransactionManager = null;
    MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
    for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
        try {
            multiTransactionStatus.rollback(transactionManager);
        } catch (Exception ex) {
            if (rollbackException == null) {
                rollbackException = ex;
                rollbackExceptionTransactionManager = transactionManager;
            } else {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex.getMessage(), ex);
            }
        }
    }
    if (multiTransactionStatus.isNewSynchonization()) {
        synchronizationManager.clearSynchronization();
    }
    if (rollbackException != null) {
        throw new UnexpectedRollbackException("Rollback exception, originated at (" + rollbackExceptionTransactionManager + ") " + rollbackException.getMessage(), rollbackException);
    }
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Aggregations

UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)20 Valid (javax.validation.Valid)11 VBoardException (com.vsct.vboard.models.VBoardException)6 User (com.vsct.vboard.models.User)5 DateTime (org.joda.time.DateTime)5 TransactionStatus (org.springframework.transaction.TransactionStatus)4 Comment (com.vsct.vboard.models.Comment)3 Pin (com.vsct.vboard.models.Pin)3 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)3 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)3 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)3 TransactionSystemException (org.springframework.transaction.TransactionSystemException)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3 Method (java.lang.reflect.Method)2 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.jupiter.api.Test)2 TransactionException (org.springframework.transaction.TransactionException)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 JMSException (jakarta.jms.JMSException)1