Search in sources :

Example 21 with ValidationException

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

the class DataSourceImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    DataSourceVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.dataSource.missingType", xid, ModuleRegistry.getDataSourceDefinitionTypes());
        else {
            DataSourceDefinition<?> def = ModuleRegistry.getDataSourceDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.dataSource.invalidType", xid, typeStr, ModuleRegistry.getDataSourceDefinitionTypes());
            else {
                vo = def.baseCreateDataSourceVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // 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.isNew();
            if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
                if (isnew) {
                    service.insert(vo);
                } else {
                    service.update(vo.getId(), vo);
                }
                addSuccessMessage(isnew, "emport.dataSource.prefix", xid);
            } else {
                addFailureMessage("emport.dataSource.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.dataSource.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.dataSource.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.dataSource.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) 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) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 22 with ValidationException

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

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    AbstractEventHandlerVO handler = null;
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            handler = service.get(xid);
        } catch (NotFoundException e) {
        // Nothing, done below
        }
    }
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    if (handler != null) {
        try {
            ctx.getReader().readInto(handler, json);
            Set<EventTypeMatcher> eventTypes = new HashSet<>(handler.getEventTypes());
            // Find the event type.
            if (et != null) {
                eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, et)));
            } else if (ets != null) {
                for (JsonValue jsonValue : ets) {
                    eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, jsonValue)));
                }
            }
            handler.setEventTypes(new ArrayList<>(eventTypes));
            boolean isnew = handler.getId() == Common.NEW_ID;
            if (isnew) {
                service.insert(handler);
                addSuccessMessage(true, "emport.eventHandler.prefix", xid);
            } else {
                service.update(handler.getId(), handler);
                addSuccessMessage(false, "emport.eventHandler.prefix", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.eventHandler.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) EventType(com.serotonin.m2m2.rt.event.type.EventType) JsonValue(com.serotonin.json.type.JsonValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) JsonArray(com.serotonin.json.type.JsonArray) HashSet(java.util.HashSet)

Example 23 with ValidationException

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

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(int id, String xid, String name, String deviceName, boolean enabled, int dataSourceId, String dataSourceXid, MangoPermission readPermission, MangoPermission setPermission, MockPointLocatorVO vo) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setId(id);
    dp.setXid(xid);
    dp.setName(name);
    dp.setDeviceName(deviceName);
    dp.setEnabled(enabled);
    dp.setPointLocator(vo);
    dp.setDataSourceId(dataSourceId);
    dp.setDataSourceXid(dataSourceXid);
    dp.setReadPermission(readPermission);
    dp.setSetPermission(setPermission);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        StringBuilder failureMessage = new StringBuilder();
        for (ProcessMessage m : e.getValidationResult().getMessages()) {
            String messagePart = m.getContextKey() + " -> " + m.getContextualMessage().translate(Common.getTranslations()) + "\n";
            failureMessage.append(messagePart);
        }
        fail(failureMessage.toString());
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 24 with ValidationException

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

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(MockDataSourceVO ds, Consumer<DataPointVO> customizer) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setName(UUID.randomUUID().toString());
    dp.setDeviceName(ds.getName());
    dp.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
    dp.setDataSourceId(ds.getId());
    customizer.accept(dp);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 25 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
 */
public MockPublisherVO createMockPublisher(boolean enabled) {
    MockPublisherVO publisherVO = (MockPublisherVO) ModuleRegistry.getPublisherDefinition(MockPublisherDefinition.TYPE_NAME).baseCreatePublisherVO();
    publisherVO.setName(UUID.randomUUID().toString());
    publisherVO.setEnabled(enabled);
    PublisherService publisherService = Common.getBean(PublisherService.class);
    try {
        return (MockPublisherVO) publisherService.insert(publisherVO);
    } 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) MockPublisherVO(com.serotonin.m2m2.vo.publish.mock.MockPublisherVO)

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