Search in sources :

Example 11 with ValidationException

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

the class PermissionImporter method importImpl.

@Override
protected void importImpl() {
    Iterator<String> it = json.keySet().iterator();
    if (it.hasNext()) {
        String permissionType = it.next();
        PermissionDefinition def = ModuleRegistry.getPermissionDefinition(permissionType);
        if (def != null) {
            try {
                JsonValue v = json.get(permissionType);
                if (v != null) {
                    MangoPermission permission = ctx.getReader().read(MangoPermission.class, v);
                    service.update(permission, def);
                }
                addSuccessMessage(false, "emport.permission.prefix", permissionType);
            } catch (ValidationException e) {
                setValidationMessages(e.getValidationResult(), "emport.permission.prefix", permissionType);
            } catch (JsonException e) {
                addFailureMessage("emport.permission.prefix", permissionType, getJsonExceptionMessage(e));
            }
        }
    }
}
Also used : JsonException(com.serotonin.json.JsonException) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonValue(com.serotonin.json.type.JsonValue) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 12 with ValidationException

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

the class DataSourceVO method setAlarmLevel.

/**
 * Set an alarm level based on the sub-type of the data source event type
 * which MUST (and already is) one of the codes in getEventCodes()
 */
public void setAlarmLevel(String subType, AlarmLevels level) throws ValidationException {
    ExportCodes codes = getEventCodes();
    int eventId = codes.getId(subType);
    if (eventId == -1) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("alarmLevel", "emport.error.eventCode", subType, codes.getCodeList());
        throw new ValidationException(result);
    }
    alarmLevels.put(eventId, level);
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 13 with ValidationException

use of com.infiniteautomation.mango.util.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
 */
default void ensureValid() throws ValidationException {
    ProcessResult response = new ProcessResult();
    this.validate(response);
    if (!response.isValid()) {
        throw new ValidationException(response);
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 14 with ValidationException

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

the class PublishedPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    PublishedPointVO vo = null;
    PublisherVO publisherVO = null;
    DataPointVO dataPointVO = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        String pubXid = json.getString("publisherXid");
        try {
            publisherVO = publisherService.get(pubXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badPublisherReference", xid);
            return;
        }
        String dpXid = json.getString("dataPointXid");
        try {
            dataPointVO = dataPointService.get(dpXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badDataPointReference", xid);
            return;
        }
        vo = publisherVO.getDefinition().createPublishedPointVO(publisherVO, dataPointVO);
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            boolean isnew = vo.isNew();
            if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
                if (isnew) {
                    service.insert(vo);
                } else {
                    service.update(vo.getId(), vo);
                }
                addSuccessMessage(isnew, "emport.publishedPoint.prefix", xid);
            } else {
                addFailureMessage("emport.publishedPoint.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.publishedPoint.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publishedPoint.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publishedPoint.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO)

Example 15 with ValidationException

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

the class UsersService method insert.

@Override
public User insert(User vo) throws PermissionException, ValidationException {
    PermissionHolder currentUser = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(currentUser, vo);
    // Ensure id is not set
    if (vo.getId() != Common.NEW_ID) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("id", "validate.invalidValue");
        throw new ValidationException(result);
    }
    // Generate a username if necessary
    if (StringUtils.isEmpty(vo.getUsername()))
        vo.setUsername(dao.generateUniqueXid());
    ensureValid(vo);
    // After validation we can set the created date if necessary
    if (vo.getCreated() == null) {
        vo.setCreated(new Date());
    }
    // After validation we can set password changed date
    vo.setPasswordChangeTimestamp(Common.timer.currentTimeMillis());
    dao.insert(vo);
    return vo;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Date(java.util.Date)

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