Search in sources :

Example 41 with ValidationException

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

the class MangoTestBase method createMockPublishedPoint.

/**
 * Create a published point
 */
public MockPublishedPointVO createMockPublishedPoint(MockPublisherVO publisher, IDataPoint dataPoint, boolean enabled) {
    MockPublishedPointVO pp = publisher.getDefinition().createPublishedPointVO(publisher, dataPoint);
    pp.setName(dataPoint.getName());
    pp.setEnabled(enabled);
    PublishedPointService publishedPointService = Common.getBean(PublishedPointService.class);
    try {
        publishedPointService.insert(pp);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
    return pp;
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) PublishedPointService(com.infiniteautomation.mango.spring.service.PublishedPointService) MockPublishedPointVO(com.serotonin.m2m2.vo.publish.mock.MockPublishedPointVO)

Example 42 with ValidationException

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

the class PublisherVO method setAlarmLevel.

/**
 * Set an alarm level based on the sub-type of the publisher 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 43 with ValidationException

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

the class DataPointService method insert.

@Override
public DataPointVO insert(DataPointVO vo) throws PermissionException, ValidationException {
    PermissionHolder user = Common.getUser();
    // Ensure they can create
    ensureCreatePermission(user, 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 an Xid if necessary
    if (StringUtils.isEmpty(vo.getXid()))
        vo.setXid(dao.generateUniqueXid());
    for (DataPointChangeDefinition def : changeDefinitions) {
        def.preInsert(vo);
    }
    ensureValid(vo);
    dao.insert(vo);
    List<AbstractPointEventDetectorVO> detectors = new ArrayList<>();
    for (DataPointChangeDefinition def : changeDefinitions) {
        for (var detector : def.postInsert(vo)) {
            if (detector.isNew()) {
                log.warn("Detector added via postInsert hook was not saved");
            } else if (detector.getDataPoint().getId() != vo.getId()) {
                log.warn("Detector added via postInsert hook was for a different data point");
            } else {
                detectors.add(detector);
            }
        }
    }
    if (vo.isEnabled()) {
        // the data point cannot have detectors if it was just inserted, don't query for detectors
        getRuntimeManager().startDataPoint(new DataPointWithEventDetectors(vo, detectors));
    }
    return vo;
}
Also used : DataPointChangeDefinition(com.serotonin.m2m2.module.definitions.dataPoint.DataPointChangeDefinition) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 44 with ValidationException

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

the class DataSourceService method copy.

/**
 * Copy a data source and optionally its points
 */
public DataSourceVO copy(String xid, String copyXid, String copyName, String copyDeviceName, boolean enabled, boolean copyPoints) throws PermissionException, NotFoundException {
    DataSourceVO existing = get(xid);
    PermissionHolder user = Common.getUser();
    ensureCreatePermission(user, existing);
    // Determine the new name
    String newName;
    if (StringUtils.isEmpty(copyName)) {
        newName = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", existing.getName()), 40);
    } else {
        newName = copyName;
    }
    // Determine the new xid
    String newXid;
    if (StringUtils.isEmpty(copyXid)) {
        newXid = DataSourceDao.getInstance().generateUniqueXid();
    } else {
        newXid = copyXid;
    }
    String newDeviceName;
    if (StringUtils.isEmpty(copyDeviceName)) {
        newDeviceName = existing.getName();
    } else {
        newDeviceName = copyDeviceName;
    }
    // Ensure device name is valid
    if (StringValidation.isLengthGreaterThan(newDeviceName, 255)) {
        ProcessResult result = new ProcessResult();
        result.addMessage("deviceName", new TranslatableMessage("validate.notLongerThan", 255));
        throw new ValidationException(result);
    }
    DataSourceVO copy = (DataSourceVO) existing.copy();
    copy.setId(Common.NEW_ID);
    copy.setName(newName);
    copy.setXid(newXid);
    copy.setEnabled(enabled);
    ensureValid(copy);
    // Save it
    insert(copy);
    if (copyPoints) {
        // Copy the points from this data source
        dataPointService.copyDataSourcePoints(existing.getId(), copy.getId(), newDeviceName);
    }
    return get(newXid);
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PollingDataSourceVO(com.serotonin.m2m2.vo.dataSource.PollingDataSourceVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Example 45 with ValidationException

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

the class SystemPermissionService method update.

/**
 */
public void update(MangoPermission permission, PermissionDefinition def) throws ValidationException {
    Objects.requireNonNull(def, "Permission definition cannot be null");
    permissionService.ensureAdminRole(Common.getUser());
    ProcessResult validation = new ProcessResult();
    if (permission == null) {
        validation.addContextualMessage("permission", "validate.required");
        throw new ValidationException(validation);
    }
    permission.getRoles().stream().flatMap(Collection::stream).forEach(role -> {
        try {
            roleService.get(role.getXid());
        } catch (NotFoundException e) {
            validation.addGenericMessage("validate.role.notFound", role.getXid());
        }
    });
    if (validation.getHasMessages()) {
        throw new ValidationException(validation);
    }
    // Execute in transaction as def.setPermission may make database calls
    dao.doInTransaction(tx -> {
        dao.update(def.getPermissionTypeName(), def.getPermission(), permission);
        def.setPermission(permission);
    });
    this.eventPublisher.publishEvent(new SystemPermissionUpdated(def));
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

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