Search in sources :

Example 1 with ValidationException

use of com.serotonin.m2m2.vo.exception.ValidationException in project ma-core-public by infiniteautomation.

the class Validatable method ensureValid.

/**
 * Validates the object and throws a ValidationException if it is not valid
 *
 * @throws ValidationException
 */
public default void ensureValid() throws ValidationException {
    ProcessResult response = new ProcessResult();
    this.validate(response);
    if (response.getHasMessages()) {
        throw new ValidationException(response);
    }
}
Also used : ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 2 with ValidationException

use of com.serotonin.m2m2.vo.exception.ValidationException in project ma-modules-public by infiniteautomation.

the class SystemSettingsRestController method update.

@ApiOperation(value = "Update an existing System Setting", notes = "If no type is provided, String is assumed")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/{key}")
public ResponseEntity<Object> update(@PathVariable String key, @ApiParam(value = "Updated model", required = true) @RequestBody(required = true) Object model, @ApiParam(value = "Setting Type", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "STRING") SystemSettingTypeEnum type, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<Object> result = new RestProcessResult<Object>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        Map<String, Object> settings = new HashMap<String, Object>();
        settings.put(key, model);
        ProcessResult response = new ProcessResult();
        this.dao.validate(settings, response);
        if (response.getHasMessages()) {
            throw new ValidationException(response);
        }
        switch(type) {
            case BOOLEAN:
                dao.setBooleanValue(key, (Boolean) model);
                break;
            case INTEGER:
                dao.setIntValue(key, (Integer) model);
                break;
            case JSON:
                try {
                    dao.setJsonObjectValue(key, model);
                } catch (Exception e) {
                    result.addRestMessage(this.getInternalServerErrorMessage(e.getMessage()));
                    return result.createResponseEntity();
                }
                break;
            case STRING:
            default:
                // Potentially convert value from its code
                Integer code = this.dao.convertToValueFromCode(key, (String) model);
                if (code != null)
                    dao.setIntValue(key, code);
                else
                    dao.setValue(key, (String) model);
                break;
        }
        // J.W. WTF is this for?
        // Put a link to the updated data in the header
        URI location = builder.path("/v1/system-settings/{key}").buildAndExpand(key).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) HashMap(java.util.HashMap) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) URI(java.net.URI) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ValidationException

use of com.serotonin.m2m2.vo.exception.ValidationException in project ma-modules-public by infiniteautomation.

the class SystemSettingsRestController method updateMany.

@ApiOperation(value = "Update Many System Settings", notes = "Admin Privs Required")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" })
public ResponseEntity<Map<String, Object>> updateMany(@ApiParam(value = "Updated settings", required = true) @RequestBody(required = true) Map<String, Object> settings, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<Map<String, Object>> result = new RestProcessResult<Map<String, Object>>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        ProcessResult response = new ProcessResult();
        // Convert incoming ExportCodes to int values
        settings = this.dao.convertCodesToValues(settings);
        this.dao.validate(settings, response);
        if (response.getHasMessages()) {
            throw new ValidationException(response);
        }
        this.dao.updateSettings(settings);
        // J.W. WTF is this for?
        // Put a link to the updated data in the header
        URI location = builder.path("/v1/system-settings").buildAndExpand().toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(settings);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) HashMap(java.util.HashMap) Map(java.util.Map) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 ValidationException (com.serotonin.m2m2.vo.exception.ValidationException)3 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Map (java.util.Map)1