Search in sources :

Example 1 with RestValidationFailedException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-core-public by infiniteautomation.

the class AbstractPublisherModel method validate.

/*
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.AbstractRestModel#validate(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)
	 */
public void validate(RestProcessResult<?> result) throws RestValidationFailedException {
    ProcessResult validation = new ProcessResult();
    this.data.validate(validation);
    if (validation.getHasMessages()) {
        result.addValidationMessages(validation);
        throw new RestValidationFailedException(this, result);
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)

Example 2 with RestValidationFailedException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-core-public by infiniteautomation.

the class AbstractDataSourceModel method validate.

/*
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.AbstractRestModel#validate(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)
	 */
public void validate(RestProcessResult<?> result) throws RestValidationFailedException {
    ProcessResult validation = new ProcessResult();
    this.data.validate(validation);
    if (validation.getHasMessages()) {
        result.addValidationMessages(validation);
        throw new RestValidationFailedException(this, result);
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)

Example 3 with RestValidationFailedException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.

the class WatchListRestController method update.

@ApiOperation(value = "Update a WatchList", notes = "", response = WatchListModel.class)
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/{xid}")
public ResponseEntity<WatchListModel> update(@PathVariable String xid, @RequestBody WatchListModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<WatchListModel> result = new RestProcessResult<WatchListModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (!result.isOk()) {
        return result.createResponseEntity();
    }
    WatchListVO wl = this.dao.getByXid(xid);
    if (wl == null) {
        result.addRestMessage(getDoesNotExistMessage());
        return result.createResponseEntity();
    }
    if (!hasEditPermission(user, wl)) {
        result.addRestMessage(getUnauthorizedMessage());
        return result.createResponseEntity();
    }
    WatchListVO update = model.getData();
    // Set the id
    update.setId(wl.getId());
    // Add the user
    update.setUserId(wl.getUserId());
    // Setup the Points
    if (model.getPoints() != null)
        for (WatchListDataPointModel pm : model.getPoints()) update.getPointList().add(pm.getDataPointVO());
    if (!model.validate()) {
        result.addRestMessage(this.getValidationFailedError());
        return result.createResponseEntity(model);
    }
    try {
        String initiatorId = request.getHeader("initiatorId");
        this.dao.save(update, initiatorId);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity(model);
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) WatchListDataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel) User(com.serotonin.m2m2.vo.User) WatchListModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with RestValidationFailedException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.

the class WatchListRestController method createNew.

@ApiOperation(value = "Create New WatchList", notes = "", response = WatchListModel.class)
@ApiResponses({ @ApiResponse(code = 201, message = "User Created", response = WatchListModel.class), @ApiResponse(code = 401, message = "Unauthorized Access", response = ResponseEntity.class), @ApiResponse(code = 409, message = "WatchList Already Exists") })
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" })
public ResponseEntity<WatchListModel> createNew(@ApiParam(value = "Watchlist to save", required = true) @RequestBody WatchListModel model, UriComponentsBuilder builder, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<WatchListModel> result = new RestProcessResult<WatchListModel>(HttpStatus.CREATED);
    User user = this.checkUser(request, result);
    if (!result.isOk()) {
        return result.createResponseEntity();
    }
    WatchListVO wl = model.getData();
    // Check XID if blank and generate one
    if (StringUtils.isBlank(wl.getXid())) {
        wl.setXid(this.dao.generateUniqueXid());
    }
    // Add the user
    wl.setUserId(user.getId());
    // Setup the Points
    if (model.getPoints() != null)
        for (WatchListDataPointModel pm : model.getPoints()) wl.getPointList().add(pm.getDataPointVO());
    // Ready to validate and then save
    if (!model.validate()) {
        result.addRestMessage(this.getValidationFailedError());
        return result.createResponseEntity(model);
    }
    try {
        String initiatorId = request.getHeader("initiatorId");
        this.dao.save(wl, initiatorId);
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
    }
    return result.createResponseEntity(new WatchListModel(wl, this.dao.getPointSummaries(wl.getId())));
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) WatchListDataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel) User(com.serotonin.m2m2.vo.User) WatchListModel(com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) WatchListVO(com.serotonin.m2m2.watchlist.WatchListVO) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with RestValidationFailedException

use of com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException in project ma-modules-public by infiniteautomation.

the class PointValueRestController method putPointValue.

/**
 * Update a point value in the system
 *
 * @param pvt
 * @param xid
 * @param builder
 * @return
 * @throws RestValidationFailedException
 */
@ApiOperation(value = "Update an existing data point's value", notes = "Data point must exist and be enabled")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}", produces = { "application/json" }, consumes = { "application/json" })
public ResponseEntity<PointValueTimeModel> putPointValue(HttpServletRequest request, @RequestBody(required = true) PointValueTimeModel model, @PathVariable String xid, @ApiParam(value = "Return converted value using displayed unit", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean unitConversion, UriComponentsBuilder builder) throws RestValidationFailedException {
    RestProcessResult<PointValueTimeModel> result = new RestProcessResult<PointValueTimeModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        RestProcessResult<PointValueTimeModel> setResult = setPointValue(user, xid, model, unitConversion, builder);
        if (setResult.getHighestStatus().value() == HttpStatus.CREATED.value())
            return setResult.createResponseEntity(model);
        else
            return setResult.createResponseEntity();
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)22 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)21 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 User (com.serotonin.m2m2.vo.User)20 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)12 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)10 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)7 URI (java.net.URI)6 UserModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel)5 IOException (java.io.IOException)5 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)4 WatchListVO (com.serotonin.m2m2.watchlist.WatchListVO)4 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 RestValidationMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage)3 WatchListDataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListDataPointModel)3 WatchListModel (com.serotonin.m2m2.web.mvc.rest.v1.model.WatchListModel)3 ThreadPoolSettingsModel (com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel)3 UserCommentModel (com.serotonin.m2m2.web.mvc.rest.v1.model.comment.UserCommentModel)3 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)3