Search in sources :

Example 1 with UserCommentModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel in project ma-modules-public by infiniteautomation.

the class UserCommentRestController method createNewUserComment.

/**
 * Create a new User Comment
 *
 * The timestamp and UserID are optional
 * Username is not used for input
 *
 * @param model
 * @param request
 * @return
 * @throws RestValidationFailedException
 */
@ApiOperation(value = "Create New User Comment", notes = "")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" })
public ResponseEntity<UserCommentModel> createNewUserComment(@ApiParam(value = "User Comment to save", required = true) @RequestBody(required = true) UserCommentModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.CREATED);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // Assign a userId if there isn't one
        if (model.getUserId() == 0) {
            model.setUserId(user.getId());
            model.setUsername(user.getUsername());
        }
        // Don't let non admin users create notes from other people
        if (!hasEditPermission(model.getData(), user)) {
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        if (model.getTimestamp() <= 0) {
            model.setTimestamp(System.currentTimeMillis());
        }
        if (model.validate()) {
            try {
                String initiatorId = request.getHeader("initiatorId");
                UserCommentDao.instance.save(model.getData(), initiatorId);
                LOG.info("User with name/id: " + user.getUsername() + "/" + user.getId() + " created a User Comment for user: " + model.getData().getUserId());
                URI location = builder.path("v1/comments/{xid}").buildAndExpand(model.getXid()).toUri();
                result.addRestMessage(getResourceCreatedMessage(location));
                return result.createResponseEntity(model);
            } catch (Exception e) {
                result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
                return result.createResponseEntity();
            }
        } else {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) URI(java.net.URI) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserCommentModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel in project ma-modules-public by infiniteautomation.

the class UserCommentRestController method deleteUserComment.

@ApiOperation(value = "Delete A User Comment by XID")
@RequestMapping(method = RequestMethod.DELETE, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<UserCommentModel> deleteUserComment(@ApiParam(value = "xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        UserCommentVO u = UserCommentDao.instance.getByXid(xid);
        if (u == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        if (hasEditPermission(u, user)) {
            // Delete it
            String initiatorId = request.getHeader("initiatorId");
            UserCommentDao.instance.delete(u.getId(), initiatorId);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to delete user comment : " + u.getUsername());
            result.addRestMessage(this.getUnauthorizedMessage());
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserCommentModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel in project ma-modules-public by infiniteautomation.

the class UserCommentRestController method getUserComment.

@ApiOperation(value = "Get user comment by xid", notes = "Returns the user comment specified by the given xid")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<UserCommentModel> getUserComment(@ApiParam(value = "Valid xid", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        UserCommentVO u = UserCommentDao.instance.getByXid(xid);
        if (u == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            UserCommentModel model = new UserCommentModel(u);
            return result.createResponseEntity(model);
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with UserCommentModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel in project ma-modules-public by infiniteautomation.

the class UserCommentRestController method updateUserComment.

@ApiOperation(value = "Updates a user comment")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/{xid}")
public ResponseEntity<UserCommentModel> updateUserComment(@PathVariable String xid, @RequestBody(required = true) UserCommentModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<UserCommentModel> result = new RestProcessResult<UserCommentModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        UserCommentVO u = UserCommentDao.instance.getByXid(xid);
        if (u == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            // Change the owner
            if (model.getUserId() == 0) {
                model.setUserId(user.getId());
                model.setUsername(user.getUsername());
            }
            // Check permissions
            if (hasEditPermission(model.getData(), user)) {
                // Validate and Update
                if (!model.validate()) {
                    result.addRestMessage(this.getValidationFailedError());
                } else {
                    UserCommentDao.instance.save(model.getData());
                    URI location = builder.path("v1/comments/{xid}").buildAndExpand(model.getXid()).toUri();
                    result.addRestMessage(getResourceUpdatedMessage(location));
                }
                return result.createResponseEntity(model);
            } else {
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) URI(java.net.URI) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with UserCommentModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel in project ma-modules-public by infiniteautomation.

the class EventInstanceModel method setComments.

@JsonSetter
public void setComments(List<UserCommentModel> commentModels) {
    List<UserCommentVO> comments = this.data.getEventComments();
    if (comments == null) {
        comments = new ArrayList<UserCommentVO>();
        this.data.setEventComments(comments);
    }
    for (UserCommentModel model : commentModels) {
        comments.add(model.getDataAsComment());
    }
}
Also used : UserCommentModel(com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter)

Aggregations

UserCommentModel (com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel)6 UserCommentVO (com.serotonin.m2m2.vo.comment.UserCommentVO)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 User (com.serotonin.m2m2.vo.User)3 URI (java.net.URI)2 JsonGetter (com.fasterxml.jackson.annotation.JsonGetter)1 JsonSetter (com.fasterxml.jackson.annotation.JsonSetter)1 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)1 ArrayList (java.util.ArrayList)1