Search in sources :

Example 66 with NotFoundException

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

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 67 with NotFoundException

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

the class PublishedPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    PublishedPointVO vo = null;
    PublisherVO publisherVO = null;
    DataPointVO dataPointVO = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        String pubXid = json.getString("publisherXid");
        try {
            publisherVO = publisherService.get(pubXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badPublisherReference", xid);
            return;
        }
        String dpXid = json.getString("dataPointXid");
        try {
            dataPointVO = dataPointService.get(dpXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badDataPointReference", xid);
            return;
        }
        vo = publisherVO.getDefinition().createPublishedPointVO(publisherVO, dataPointVO);
        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.publishedPoint.prefix", xid);
            } else {
                addFailureMessage("emport.publishedPoint.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.publishedPoint.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publishedPoint.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publishedPoint.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) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO)

Example 68 with NotFoundException

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

the class DataPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    DataSourceVO dsvo = null;
    DataPointWithEventDetectors dp = null;
    if (StringUtils.isBlank(xid)) {
        xid = dataPointService.generateUniqueXid();
    } else {
        try {
            dp = dataPointService.getWithEventDetectors(xid);
        } catch (NotFoundException e) {
        }
    }
    if (dp == null) {
        // Locate the data source for the point.
        String dsxid = json.getString("dataSourceXid");
        try {
            dsvo = dataSourceService.get(dsxid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.dataPoint.badReference", xid);
            return;
        }
        DataPointVO vo = new DataPointVO();
        vo.setXid(xid);
        vo.setDataSourceId(dsvo.getId());
        vo.setDataSourceXid(dsxid);
        vo.setPointLocator(dsvo.createPointLocator());
        dp = new DataPointWithEventDetectors(vo, new ArrayList<>());
    }
    // If there is already an entry, merge the event detectors
    DataPointWithEventDetectors existingMapping = dataPointMap.get(xid);
    if (existingMapping != null) {
        dp.getEventDetectors().addAll(existingMapping.getEventDetectors());
    }
    if (dp != null) {
        try {
            // Read into the VO to get all properties
            ctx.getReader().readInto(dp.getDataPoint(), json);
            // If the name is not provided, default to the XID
            if (StringUtils.isBlank(dp.getDataPoint().getName()))
                dp.getDataPoint().setName(xid);
            // If the chart colour is null provide default of '' to handle legacy code that sets colour to null
            if (dp.getDataPoint().getChartColour() == null)
                dp.getDataPoint().setChartColour("");
            // Ensure we have a default permission since null is valid in Mango 3.x
            if (dp.getDataPoint().getReadPermission() == null) {
                dp.getDataPoint().setReadPermission(new MangoPermission());
            }
            if (dp.getDataPoint().getEditPermission() == null) {
                dp.getDataPoint().setEditPermission(new MangoPermission());
            }
            if (dp.getDataPoint().getSetPermission() == null) {
                dp.getDataPoint().setSetPermission(new MangoPermission());
            }
            // Handle embedded event detectors
            JsonArray pedArray = json.getJsonArray("eventDetectors");
            if (pedArray != null) {
                for (JsonValue jv : pedArray) {
                    JsonObject pedObject = jv.toJsonObject();
                    String pedXid = pedObject.getString("xid");
                    AbstractPointEventDetectorVO ped = null;
                    if (!StringUtils.isBlank(pedXid)) {
                        // Use the ped xid to lookup an existing ped.
                        for (AbstractPointEventDetectorVO existing : dp.getEventDetectors()) {
                            if (StringUtils.equals(pedXid, existing.getXid())) {
                                ped = existing;
                                break;
                            }
                        }
                    }
                    if (ped == null) {
                        String typeStr = pedObject.getString("type");
                        if (typeStr == null)
                            throw new TranslatableJsonException("emport.error.ped.missingAttr", "type");
                        // This assumes that all definitions used for data points are Data Point Event Detectors
                        PointEventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
                        if (def == null)
                            throw new TranslatableJsonException("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
                        else {
                            ped = def.baseCreateEventDetectorVO(dp.getDataPoint());
                            ped.setDefinition(def);
                        }
                        // Create a new one
                        ped.setId(Common.NEW_ID);
                        ped.setXid(pedXid);
                        dp.addOrReplaceDetector(ped);
                    }
                    JsonArray handlerXids = pedObject.getJsonArray("handlers");
                    if (handlerXids != null)
                        for (int k = 0; k < handlerXids.size(); k += 1) {
                            AbstractEventHandlerVO eh = EventHandlerDao.getInstance().getByXid(handlerXids.getString(k));
                            if (eh == null) {
                                throw new TranslatableJsonException("emport.eventHandler.missing", handlerXids.getString(k));
                            } else {
                                ped.addAddedEventHandler(eh);
                            }
                        }
                    ctx.getReader().readInto(ped, pedObject);
                }
            }
            boolean isNew = dp.getDataPoint().isNew();
            try {
                if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
                    if (isNew) {
                        dataPointService.insert(dp.getDataPoint());
                        // Update all our event detector source Ids
                        for (AbstractPointEventDetectorVO ed : dp.getEventDetectors()) {
                            ed.setSourceId(dp.getDataPoint().getId());
                        }
                    } else {
                        dataPointService.update(dp.getDataPoint().getId(), dp.getDataPoint());
                        // Update all our event detector source Ids
                        for (AbstractPointEventDetectorVO ed : dp.getEventDetectors()) {
                            ed.setSourceId(dp.getDataPoint().getId());
                        }
                    }
                    dataPointMap.put(xid, dp);
                    addSuccessMessage(isNew, "emport.dataPoint.prefix", xid);
                } else {
                    addFailureMessage("emport.dataPoint.runtimeManagerNotRunning", xid);
                }
            } catch (LicenseViolatedException e) {
                addFailureMessage("emport.DataPoint.notImported", e.getErrorMessage(), xid);
                addDetectorsFailureMessage(dp, xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.dataPoint.prefix", xid);
            addDetectorsFailureMessage(dp, xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, e.getMsg());
            addDetectorsFailureMessage(dp, xid);
        } catch (JsonException e) {
            addFailureMessage("emport.dataPoint.prefix", xid, getJsonExceptionMessage(e));
            addDetectorsFailureMessage(dp, xid);
        }
    }
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) DataPointWithEventDetectors(com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors) 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) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 69 with NotFoundException

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

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 70 with NotFoundException

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

the class UsersService method getByIdViaCache.

public User getByIdViaCache(int id) {
    return dao.doInTransaction((tx) -> {
        String username = dao.getXidById(id);
        if (username == null) {
            throw new NotFoundException();
        }
        User user = get(username);
        if (user.getId() != id) {
            throw new IllegalStateException("User was updated while retrieving from cache");
        }
        return user;
    });
}
Also used : User(com.serotonin.m2m2.vo.User) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Aggregations

NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)74 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)26 JsonException (com.serotonin.json.JsonException)20 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)18 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)15 User (com.serotonin.m2m2.vo.User)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)15 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)10 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)9 JsonArray (com.serotonin.json.type.JsonArray)8 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)6 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)6 FileStore (com.serotonin.m2m2.vo.FileStore)6 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 PublisherVO (com.serotonin.m2m2.vo.publish.PublisherVO)6 ApiOperation (io.swagger.annotations.ApiOperation)6 IOException (java.io.IOException)6