Search in sources :

Example 1 with UnexpectedRollbackException

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

the class CommentsController method removeComment.

@RequestMapping(value = "", method = RequestMethod.DELETE)
@ResponseBody
@Valid
public Comment removeComment(@RequestParam(value = "id") String id) {
    Comment comment;
    try {
        comment = this.commentDAO.findById(id);
        if (comment != null) {
            // Check if the user can update this comment (or throw an exception)
            permission.ensureUserHasRightsToAlterPin(comment.getAuthor());
            this.commentDAO.delete(comment);
            String pinId = comment.getPinId();
            Pin pin = this.pinDAO.findByPinId(pinId);
            if (pin != null) {
                pin.decreaseCommentsNumber();
                this.pinDAO.save(pin);
            }
            // Decrease the number of comments for the given pin in elasticsearch
            this.elsClient.removeComment(pinId);
            this.logger.debug("deleteComment: id={}", id);
            // Update the stats
            this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
        } else {
            throw new VBoardException("Comment does not exist or already deleted");
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return comment;
}
Also used : Comment(com.vsct.vboard.models.Comment) VBoardException(com.vsct.vboard.models.VBoardException) Pin(com.vsct.vboard.models.Pin) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 2 with UnexpectedRollbackException

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

the class MessagesController method hideMessage.

@RequestMapping(value = "/remove", method = RequestMethod.POST)
@ResponseBody
@Valid
public Message hideMessage() {
    permission.ensureCurrentUserIsAdmin();
    Message previousMessage = this.messageDAO.findByActive(true);
    try {
        if (previousMessage != null) {
            previousMessage.setActive(false);
            this.messageDAO.save(previousMessage);
        }
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return previousMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 3 with UnexpectedRollbackException

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

the class MessagesController method addMessage.

// Add a new displayable message
@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
@Valid
public Message addMessage(@Valid @RequestBody MessageParams params) {
    final String content = params.getContent();
    final String type = params.getType();
    permission.ensureCurrentUserIsAdmin();
    final Message message = new Message(type, content);
    try {
        this.logger.debug("addMessage: author={} - type={} -content={}", permission.getSessionUser().getUserString(), type, content);
        // Find the previous active message (if one if found)
        final Message previousMessage = this.messageDAO.findByActive(true);
        if (previousMessage != null) {
            // Deactivate the previous active message
            previousMessage.setActive(false);
            this.messageDAO.save(previousMessage);
        }
        this.messageDAO.save(message);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return message;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 4 with UnexpectedRollbackException

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

the class PinsController method addNewPin.

@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
Pin addNewPin(@Valid @RequestBody AddNewPinParams params) {
    Pin retval;
    // Save
    String title = params.getTitle();
    String url = params.getUrl();
    String imgType = params.getImgType();
    String description = params.getDescription();
    String[] labels = params.getLabels();
    String strLabels = labels == null || labels.length == 0 ? "" : String.join(",", labels);
    String author = params.getAuthor();
    // Check if the author given is effectively the one that posted the pin
    permission.ensureNewEntityAuthorMatchesSessionUser(author);
    DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
    Pin newPin = new Pin(title, url, 0, imgType, strLabels, description, author, postDateUTC);
    // If the media is an image (!<iframe) and is a base64image (custom) (version compatibility) the image points out on its localisation (relative url)
    if (this.isMediaBase64Image(imgType)) {
        String nasURL = "/pinImg/" + newPin.getPinId() + ".png";
        newPin.setImgType(nasURL);
    }
    // If the media is a video, the content is set as it is
    if (this.isMediaVideo(imgType)) {
        newPin.setImgType(imgType);
    }
    try {
        this.logger.debug("addNewPin: title={} - url={} - likes=0 - imgType={} - description={} - labels={} - author={}", title, url, newPin.getImgType(), description, strLabels, author);
        retval = this.pinDAO.save(newPin);
        this.elsClient.addPin(newPin);
        // If the image given is not a video (iframe) and exists, the image is saved (if not url, see inside the method) in the NAS
        if (this.isMediaBase64Image(params.getImgType()) || this.isMediaInternetImage(params.getImgType())) {
            uploadsManager.savePinImage(params.getImgType(), retval.getPinId());
        }
        // Update the stats
        this.gamification.updateStats(permission.getSessionUserWithSyncFromDB());
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    // Send a notification
    this.notifications.addNotificationsFromPin(newPin.getPinId(), "a ajouté une épingle avec un label que vous suivez");
    // Add the new labels in the list of labels
    if (!isBlank(strLabels)) {
        List<String> pinLabels = Arrays.asList(strLabels.split(","));
        pinLabels.forEach(l -> this.labelDAO.save(new Label(l)));
    }
    return retval;
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

Example 5 with UnexpectedRollbackException

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

the class PinsController method addNewPinVblog.

// Post pins from Vblog (wordpress) (see previous method)
@RequestMapping(value = "/vblog", method = RequestMethod.POST, consumes = { "application/x-www-form-urlencoded" })
@ResponseBody
@Valid
public Pin addNewPinVblog(@RequestParam("title") final String title, @RequestParam("url") final String url, @RequestParam("imgType") String imgType, @RequestParam("description") final String description, @RequestParam("labels") String labels, @RequestParam("author") String author, @RequestParam("ID") final String ID) {
    // TODO only authorize wordpress (vblog) to access that url/method
    if (!this.isMediaInternetImage(imgType)) {
        imgType = null;
    }
    if (!isBlank(labels)) {
        labels = "#" + labels;
    }
    User user = this.userDAO.findByEmail(author);
    if (user != null) {
        author = user.getUserString();
    }
    Pin pin = this.pinDAO.findByPinId("vblog-" + ID);
    if (pin == null) {
        DateTime postDateUTC = new DateTime(DateTimeZone.UTC);
        pin = new Pin("vblog-" + ID, title, url, 0, imgType, labels, description, author, postDateUTC);
    } else {
        pin.setPinTitle(title);
        pin.setHrefUrl(url);
        pin.setImgType(imgType);
        pin.setLabels(labels);
        pin.setIndexableTextContent(description);
        pin.setAuthor(author);
    }
    try {
        this.logger.debug("addNewPinVblog: pinId= vblog-{} title={} - url={} - likes=0 - imgType={} - description={} - labels={} - author={}", ID, title, url, imgType, description, labels, author);
        pin = this.pinDAO.save(pin);
        this.elsClient.updatePin(pin);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    // Add the new labels in the list of labels
    if (!isBlank(labels)) {
        List<String> pinLabels = Arrays.asList(labels.split(","));
        pinLabels.forEach(l -> this.labelDAO.save(new Label(l)));
    }
    if (this.isMediaInternetImage(imgType)) {
        uploadsManager.savePinImage(imgType, pin.getPinId());
    }
    if (user != null) {
        this.gamification.updateStats(user);
    }
    return pin;
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) DateTime(org.joda.time.DateTime) Valid(javax.validation.Valid)

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