Search in sources :

Example 36 with ValidationException

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

the class WatchListEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext, PermissionHolder importer) throws JsonException {
    JsonObject watchListJson = jsonValue.toJsonObject();
    String xid = watchListJson.getString("xid");
    WatchListVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new WatchListVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, watchListJson);
        // Ensure we have a default permission since null is valid in Mango 3.x
        if (vo.getReadPermission() == null) {
            vo.setReadPermission(new MangoPermission());
        }
        if (vo.getEditPermission() == null) {
            vo.setEditPermission(new MangoPermission());
        }
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        importContext.addSuccessMessage(isnew, "emport.watchList.prefix", xid);
    } catch (ValidationException e) {
        importContext.copyValidationMessages(e.getValidationResult(), "emport.watchList.prefix", xid);
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : 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 37 with ValidationException

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

the class PermissionServiceTest method validatePermission.

private void validatePermission(MangoPermission oldPermission, MangoPermission newPermission) {
    ProcessResult result = new ProcessResult();
    permissionService.validatePermission(result, "permission", Common.getUser(), oldPermission, newPermission);
    if (!result.isValid()) {
        throw new ValidationException(result);
    }
}
Also used : ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 38 with ValidationException

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

the class DataPointServiceTest method testDataTypeValidation.

@Test
public void testDataTypeValidation() {
    DataPointVO vo = newVO(editUser);
    vo.setPointLocator(new MockPointLocatorVO(DataType.fromName("UNKNOWN"), false));
    try {
        service.insert(vo);
        fail("Should throw " + ValidationException.class.getSimpleName());
    } catch (ValidationException e) {
        Assert.assertTrue(e.getValidationResult().hasContextualMessage("dataType"));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Test(org.junit.Test)

Example 39 with ValidationException

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

the class NumericPointValueWithDifferentSeriesIdTest method createMockDataPoints.

@Override
protected List<IDataPoint> createMockDataPoints(int count) {
    List<IDataPoint> points = super.createMockDataPoints(count);
    // Change series id
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointDao dao = Common.getBean(DataPointDao.class);
    for (IDataPoint point : points) {
        DataPointVO vo = (DataPointVO) point;
        vo.setSeriesId(dao.insertNewTimeSeries());
        try {
            service.update(vo.getId(), vo);
        } catch (ValidationException e) {
            String failureMessage = "";
            for (ProcessMessage m : e.getValidationResult().getMessages()) {
                String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
                failureMessage += messagePart;
            }
            fail(failureMessage);
        }
    }
    return points;
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 40 with ValidationException

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

the class MangoTestBase method createMockPublisher.

/**
 * Create a publisher with points
 */
public MockPublisherVO createMockPublisher(boolean enabled, List<MockPublishedPointVO> points) {
    MockPublisherVO publisherVO = (MockPublisherVO) ModuleRegistry.getPublisherDefinition(MockPublisherDefinition.TYPE_NAME).baseCreatePublisherVO();
    publisherVO.setName(UUID.randomUUID().toString());
    publisherVO.setEnabled(enabled);
    PublisherService publisherService = Common.getBean(PublisherService.class);
    try {
        MockPublisherVO pub = (MockPublisherVO) publisherService.insert(publisherVO);
        PublishedPointService publishedPointService = Common.getBean(PublishedPointService.class);
        for (MockPublishedPointVO point : points) {
            publishedPointService.insert(point);
        }
        return pub;
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) PublisherService(com.infiniteautomation.mango.spring.service.PublisherService) PublishedPointService(com.infiniteautomation.mango.spring.service.PublishedPointService) MockPublisherVO(com.serotonin.m2m2.vo.publish.mock.MockPublisherVO) MockPublishedPointVO(com.serotonin.m2m2.vo.publish.mock.MockPublishedPointVO)

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