Search in sources :

Example 46 with ValidationException

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

the class AbstractBasicVOService method insert.

/**
 */
public T insert(T 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, vo.getClass());
    }
    ensureValid(vo);
    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)

Example 47 with ValidationException

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

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

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

the class JsonDataImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    JsonDataVO vo = null;
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        isNew = true;
        vo = new JsonDataVO();
        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());
            }
            if (isNew) {
                service.insert(vo);
            } else {
                service.update(vo.getId(), vo);
            }
            addSuccessMessage(isNew, "emport.jsondata.prefix", xid);
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.jsondata.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 49 with ValidationException

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

the class MailingListImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    MailingList vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new MailingList();
        vo.setXid(xid);
    }
    try {
        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.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        addSuccessMessage(isnew, "emport.mailingList.prefix", xid);
    } catch (ValidationException e) {
        setValidationMessages(e.getValidationResult(), "emport.mailingList.prefix", xid);
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.mailingList.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.mailingList.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 50 with ValidationException

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

the class PublisherImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    PublisherVO 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.publisher.missingType", xid, ModuleRegistry.getPublisherDefinitionTypes());
        else {
            PublisherDefinition<?> def = ModuleRegistry.getPublisherDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.publisher.invalidType", xid, typeStr, ModuleRegistry.getPublisherDefinitionTypes());
            else {
                vo = def.baseCreatePublisherVO();
                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.publisher.prefix", xid);
                // Handle embedded points (pre Mango 4.3 this was the case)
                JsonArray arr = json.getJsonArray("points");
                if (arr != null) {
                    for (JsonValue jv : arr) {
                        String dataPointXid = jv.getJsonValue("dataPointId").toString();
                        try {
                            DataPointVO dataPointVO = dataPointService.get(dataPointXid);
                            PublishedPointVO point = vo.getDefinition().createPublishedPointVO(vo, dataPointVO);
                            point.setName(dataPointVO.getName());
                            ctx.getReader().readInto(point, jv.toJsonObject());
                            point.setXid(publishedPointService.generateUniqueXid());
                            publishedPointService.insert(point);
                            addSuccessMessage(isnew, "emport.publishedPoint.prefix", point.getXid());
                        } catch (NotFoundException e) {
                            addFailureMessage("emport.publisher.prefix", xid, new TranslatableMessage("emport.error.missingPoint", dataPointXid));
                        } catch (ValidationException e) {
                            for (ProcessMessage m : e.getValidationResult().getMessages()) {
                                addFailureMessage(new ProcessMessage("emport.publisher.prefix", new TranslatableMessage("literal", xid), m));
                            }
                        }
                    }
                }
            } else {
                addFailureMessage("emport.publisher.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.publisher.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publisher.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publisher.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) JsonValue(com.serotonin.json.type.JsonValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO)

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