Search in sources :

Example 31 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.

the class MaintenanceEventEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext, PermissionHolder importer) throws JsonException {
    MaintenanceEventsService service = Common.getBean(MaintenanceEventsService.class);
    JsonObject maintenanceEvent = jsonValue.toJsonObject();
    String xid = maintenanceEvent.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = service.generateUniqueXid();
    MaintenanceEventVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new MaintenanceEventVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, maintenanceEvent);
        // Ensure we have a default permission since null is valid in Mango 3.x
        if (vo.getTogglePermission() == null) {
            vo.setTogglePermission(new MangoPermission());
        }
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        importContext.addSuccessMessage(isnew, "emport.maintenanceEvent.prefix", xid);
    } catch (ValidationException e) {
        importContext.copyValidationMessages(e.getValidationResult(), "emport.maintenanceEvent.prefix", xid);
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonObject(com.serotonin.json.type.JsonObject) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 32 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.

the class MaintenanceEventModel method toVO.

@Override
public MaintenanceEventVO toVO() {
    MaintenanceEventVO vo = super.toVO();
    ProcessResult result = new ProcessResult();
    if (dataSources != null) {
        Set<Integer> ids = new HashSet<>();
        for (String xid : dataSources) {
            Integer id = DataSourceDao.getInstance().getIdByXid(xid);
            if (id != null)
                ids.add(id);
            else
                result.addContextualMessage("dataSources", "maintenanceEvents.validate.missingDataSource", xid);
        }
        vo.setDataSources(new ArrayList<>(ids));
    }
    if (dataPoints != null) {
        Set<Integer> ids = new HashSet<>();
        for (String xid : dataPoints) {
            Integer id = DataPointDao.getInstance().getIdByXid(xid);
            if (id != null)
                ids.add(id);
            else
                result.addContextualMessage("dataPoints", "maintenanceEvents.validate.missingDataPoint", xid);
        }
        vo.setDataPoints(new ArrayList<>(ids));
    }
    if (result.getHasMessages())
        throw new ValidationException(result);
    vo.setAlarmLevel(alarmLevel);
    vo.setScheduleType(MaintenanceEventVO.TYPE_CODES.getId(scheduleType));
    vo.setDisabled(disabled == null ? false : disabled);
    vo.setActiveYear(activeYear == null ? 0 : activeYear);
    vo.setActiveMonth(activeMonth == null ? 0 : activeMonth);
    vo.setActiveDay(activeDay == null ? 0 : activeDay);
    vo.setActiveHour(activeHour == null ? 0 : activeHour);
    vo.setActiveMinute(activeMinute == null ? 0 : activeMinute);
    vo.setActiveSecond(activeSecond == null ? 0 : activeSecond);
    vo.setActiveCron(activeCron);
    vo.setInactiveYear(inactiveYear == null ? 0 : inactiveYear);
    vo.setInactiveMonth(inactiveMonth == null ? 0 : inactiveMonth);
    vo.setInactiveDay(inactiveDay == null ? 0 : inactiveDay);
    vo.setInactiveHour(inactiveHour == null ? 0 : inactiveHour);
    vo.setInactiveMinute(inactiveMinute == null ? 0 : inactiveMinute);
    vo.setInactiveSecond(inactiveSecond == null ? 0 : inactiveSecond);
    vo.setInactiveCron(inactiveCron);
    vo.setTimeoutPeriods(timeoutPeriods == null ? 0 : timeoutPeriods);
    vo.setTimeoutPeriodType(Common.TIME_PERIOD_CODES.getId(timeoutPeriodType));
    vo.setTogglePermission(togglePermission != null ? togglePermission.getPermission() : new MangoPermission());
    return vo;
}
Also used : MaintenanceEventVO(com.serotonin.m2m2.maintenanceEvents.MaintenanceEventVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 33 with ValidationException

use of com.infiniteautomation.mango.util.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)
public Map<String, Object> updateMany(@ApiParam(value = "Updated settings", required = true) @RequestBody(required = true) Map<String, JsonNode> body, @AuthenticationPrincipal PermissionHolder user) {
    permissionService.ensureAdminRole(user);
    ProcessResult response = new ProcessResult();
    Map<String, Object> settings = convertValues(body);
    // Convert incoming ExportCodes to int values
    settings = this.dao.convertCodesToValues(settings);
    this.dao.validate(settings, response, user);
    if (response.getHasMessages()) {
        throw new ValidationException(response);
    }
    this.dao.updateSettings(settings);
    return settings;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with ValidationException

use of com.infiniteautomation.mango.util.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, value = "/{key}")
public Object update(@PathVariable String key, @ApiParam(value = "Updated model", required = true) @RequestBody(required = true) JsonNode model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder, HttpServletRequest request) {
    permissionService.ensureAdminRole(user);
    ProcessResult response = new ProcessResult();
    Map<String, Object> settings = Collections.singletonMap(key, convertValue(model));
    settings = this.dao.convertCodesToValues(settings);
    this.dao.validate(settings, response, user);
    if (response.getHasMessages()) {
        throw new ValidationException(response);
    }
    this.dao.updateSettings(settings);
    return model;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.

the class RestExceptionHandler method handleValidationException.

@ExceptionHandler({ ValidationException.class })
public ResponseEntity<Object> handleValidationException(HttpServletRequest request, HttpServletResponse response, Exception ex, WebRequest req) {
    ValidationException validationException = (ValidationException) ex;
    ProcessResult result = validationException.getValidationResult();
    // Do any potential mapping of VO property names to model property names
    Class<?> modelClass = (Class<?>) req.getAttribute(MangoRequestBodyAdvice.MODEL_CLASS, RequestAttributes.SCOPE_REQUEST);
    if (validationException.getValidatedClass() != null && modelClass != null) {
        result = mapper.mapValidationErrors(modelClass, validationException.getValidatedClass(), result);
    }
    return handleExceptionInternal(ex, new ValidationFailedRestException(result), new HttpHeaders(), HttpStatus.UNPROCESSABLE_ENTITY, req);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)

Aggregations

ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)75 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)31 JsonException (com.serotonin.json.JsonException)22 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)20 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)16 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)14 JsonValue (com.serotonin.json.type.JsonValue)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 JsonObject (com.serotonin.json.type.JsonObject)8 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)8 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)7 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)6 JsonArray (com.serotonin.json.type.JsonArray)6 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 ArrayList (java.util.ArrayList)6 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)4 PublishedPointService (com.infiniteautomation.mango.spring.service.PublishedPointService)4