Search in sources :

Example 6 with ValidationException

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

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)

Example 7 with ValidationException

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

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(true);
    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 8 with ValidationException

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

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 9 with ValidationException

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

the class ImportTask method processUpdatedDetectors.

/**
 * Since detectors can be attached to a data point we will import them in bulk here.  This will
 *  remove any fully imported detectors and their container after there are no more detectors to import
 *  for that point.
 */
private void processUpdatedDetectors(Map<String, DataPointWithEventDetectors> eventDetectorMap) {
    Iterator<String> it = eventDetectorMap.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        DataPointWithEventDetectors dp = eventDetectorMap.get(key);
        // The content of the event detectors lists may have duplicates and the DataPointVO may be out of date,
        // but we can assume that all the event detectors for a point will exist in this list.
        ListIterator<AbstractPointEventDetectorVO> listIt = dp.getEventDetectors().listIterator();
        while (listIt.hasNext()) {
            AbstractPointEventDetectorVO ed = listIt.next();
            try {
                if (ed.isNew()) {
                    eventDetectorService.insertAndReload(ed, false);
                    importContext.addSuccessMessage(true, "emport.eventDetector.prefix", ed.getXid());
                } else {
                    eventDetectorService.updateAndReload(ed.getXid(), ed, false);
                    importContext.addSuccessMessage(false, "emport.eventDetector.prefix", ed.getXid());
                }
                // Reload into the RT
                dataPointService.reloadDataPoint(dp.getDataPoint().getXid());
            } catch (ValidationException e) {
                importContext.copyValidationMessages(e.getValidationResult(), "emport.eventDetector.prefix", ed.getXid());
            } catch (Exception e) {
                addException(e);
                LOG.error("Event detector import failed.", e);
            } finally {
                // To avoid being stuck in the loop, removing the item from the lists even if it caused an issue or not.
                listIt.remove();
            }
        }
        if (dp.getEventDetectors().size() == 0) {
            it.remove();
        }
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException)

Example 10 with ValidationException

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

the class DataPointModel method toVO.

@Override
public DataPointVO toVO() {
    DataPointVO point = new DataPointVO();
    if (xid != null) {
        point.setXid(xid);
    }
    if (name != null) {
        point.setName(name);
    }
    if (enabled != null) {
        point.setEnabled(enabled);
    }
    if (deviceName != null) {
        point.setDeviceName(deviceName);
    }
    // TODO Use ModelMapper.unmap
    if (readPermission != null) {
        point.setReadPermission(readPermission.getPermission());
    }
    // TODO Use ModelMapper.unmap
    if (editPermission != null) {
        point.setEditPermission(editPermission.getPermission());
    }
    // TODO Use ModelMapper.unmap
    if (setPermission != null) {
        point.setSetPermission(setPermission.getPermission());
    }
    // TODO Use ModelMapper.unmap
    if (StringUtils.isNotEmpty(dataSourceXid)) {
        DataSourceVO ds = DataSourceDao.getInstance().getByXid(dataSourceXid);
        if (ds != null) {
            point.setDataSourceId(ds.getId());
            point.setDataSourceName(ds.getName());
            point.setDataSourceXid(ds.getXid());
            point.setDataSourceTypeName(ds.getDefinition().getDataSourceTypeName());
        }
    }
    // TODO Use ModelMapper.unmap
    if (point.getDataSourceId() <= 0 && dataSourceId != null && dataSourceId > 0) {
        point.setDataSourceId(dataSourceId);
    }
    if (purgeOverride != null) {
        point.setPurgeOverride(purgeOverride);
        // Ensure that a purge period must be supplied
        if (purgeOverride) {
            point.setPurgePeriod(-1);
            point.setPurgeType(-1);
        }
    }
    if (purgePeriod != null) {
        point.setPurgePeriod(purgePeriod.getPeriods());
        point.setPurgeType(Common.TIME_PERIOD_CODES.getId(purgePeriod.getPeriodType()));
    }
    if (unit != null) {
        try {
            point.setUnit(JUnitUtil.parseLocal(unit));
        } catch (IllegalArgumentException e) {
            // TODO unmap
            ProcessResult result = new ProcessResult();
            result.addContextualMessage("unit", "validate.unitInvalid", e.getMessage());
            throw new ValidationException(result);
        }
    }
    if (useIntegralUnit != null) {
        point.setUseIntegralUnit(useIntegralUnit);
    }
    if (integralUnit != null) {
        try {
            point.setIntegralUnit(JUnitUtil.parseLocal(integralUnit));
        } catch (IllegalArgumentException e) {
            // TODO unmap
            ProcessResult result = new ProcessResult();
            result.addContextualMessage("integralUnit", "validate.unitInvalid", e.getMessage());
            throw new ValidationException(result);
        }
    }
    if (useRenderedUnit != null) {
        point.setUseRenderedUnit(useRenderedUnit);
    }
    if (this.renderedUnit != null) {
        try {
            point.setRenderedUnit(JUnitUtil.parseLocal(renderedUnit));
        } catch (IllegalArgumentException e) {
            // TODO unmap
            ProcessResult result = new ProcessResult();
            result.addContextualMessage("renderedUnit", "validate.unitInvalid", e.getMessage());
            throw new ValidationException(result);
        }
    }
    if (chartColour != null) {
        point.setChartColour(chartColour);
    }
    if (plotType != null) {
        point.setPlotType(DataPointVO.PLOT_TYPE_CODES.getId(plotType));
    }
    if (this.tags != null) {
        Map<String, String> existingTags = point.getTags();
        if (!this.mergeTags || existingTags == null) {
            // existingTags is only null if someone tried to use mergeTags when creating a data point
            point.setTags(this.tags);
        } else {
            Map<String, String> mergedTags = new HashMap<>(existingTags);
            for (Entry<String, String> entry : this.tags.entrySet()) {
                String tagKey = entry.getKey();
                String tagValue = entry.getValue();
                if (tagValue == null) {
                    mergedTags.remove(tagKey);
                } else {
                    mergedTags.put(tagKey, tagValue);
                }
            }
            point.setTags(mergedTags);
        }
    } else {
        // TODO unmap
        if (id != null && id > 0) {
            point.setTags(DataPointTagsDao.getInstance().getTagsForDataPointId(id));
        } else if (xid != null) {
            Integer id = DataPointDao.getInstance().getIdByXid(xid);
            if (id != null) {
                point.setTags(DataPointTagsDao.getInstance().getTagsForDataPointId(id));
            }
        }
    }
    if (this.loggingProperties != null) {
        loggingProperties.copyPropertiesTo(point);
    }
    if (this.textRenderer != null) {
        point.setTextRenderer(textRenderer.toVO());
    }
    if (pointLocator != null) {
        point.setPointLocator(pointLocator.toVO());
    }
    if (this.rollup != null) {
        point.setRollup(Common.ROLLUP_CODES.getId(this.rollup));
    }
    if (this.simplifyType != null) {
        point.setSimplifyType(DataPointVO.SIMPLIFY_TYPE_CODES.getId(this.simplifyType));
    }
    if (this.simplifyTolerance != null) {
        point.setSimplifyTolerance(simplifyTolerance);
    }
    if (this.simplifyTarget != null) {
        point.setSimplifyTarget(simplifyTarget);
    }
    if (this.preventSetExtremeValues != null) {
        point.setPreventSetExtremeValues(this.preventSetExtremeValues);
    }
    if (this.setExtremeLowLimit != null) {
        point.setSetExtremeLowLimit(this.setExtremeLowLimit);
    }
    if (this.setExtremeHighLimit != null) {
        point.setSetExtremeHighLimit(this.setExtremeHighLimit);
    }
    if (this.data != null) {
        point.setData(data);
    }
    return point;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) HashMap(java.util.HashMap) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

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