Search in sources :

Example 51 with ValidationException

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

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

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

the class SystemSettingsImporter method importImpl.

@Override
protected void importImpl() {
    try {
        Map<String, Object> settings = new HashMap<String, Object>();
        // Finish reading it in.
        for (String key : json.keySet()) {
            JsonValue value = json.get(key);
            // Don't import null values or database schemas
            if ((value != null) && (!key.startsWith(SystemSettingsDao.DATABASE_SCHEMA_VERSION))) {
                Object o = value.toNative();
                if (o instanceof String) {
                    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(key);
                    if (def != null) {
                        // Legacy permission import
                        try {
                            Set<String> xids = PermissionService.explodeLegacyPermissionGroups((String) o);
                            Set<Set<Role>> roles = new HashSet<>();
                            for (String xid : xids) {
                                RoleVO role = roleService.get(xid);
                                if (role != null) {
                                    roles.add(Collections.singleton(role.getRole()));
                                } else {
                                    roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
                                }
                            }
                            permissionService.update(new MangoPermission(roles), def);
                            addSuccessMessage(false, "emport.permission.prefix", key);
                        } catch (ValidationException e) {
                            setValidationMessages(e.getValidationResult(), "emport.permission.prefix", key);
                            return;
                        }
                    } else {
                        // Could be an export code so try and convert it
                        Integer id = SystemSettingsDao.getInstance().convertToValueFromCode(key, (String) o);
                        if (id != null)
                            settings.put(key, id);
                        else
                            settings.put(key, o);
                    }
                } else {
                    settings.put(key, o);
                }
            }
        }
        // Now validate it. Use a new response object so we can distinguish errors in this vo
        // from
        // other errors.
        ProcessResult voResponse = new ProcessResult();
        SystemSettingsDao.getInstance().validate(settings, voResponse, user);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        else {
            SystemSettingsDao.getInstance().updateSettings(settings);
            addSuccessMessage(false, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        }
    } catch (Exception e) {
        addFailureMessage("emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()), e.getMessage());
    }
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) HashMap(java.util.HashMap) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 53 with ValidationException

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

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)

Example 54 with ValidationException

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

the class RoleImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    String name = json.getString("name");
    RoleVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new RoleVO(Common.NEW_ID, xid, name);
    }
    try {
        // Read into the VO to get all properties
        ctx.getReader().readInto(vo, json);
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        addSuccessMessage(isnew, "emport.role.prefix", xid);
    } catch (ValidationException e) {
        setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
    } catch (JsonException e) {
        addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : JsonException(com.serotonin.json.JsonException) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 55 with ValidationException

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

the class SystemSettingsImporter method importImpl.

@Override
protected void importImpl() {
    try {
        Map<String, Object> settings = new HashMap<String, Object>();
        // Finish reading it in.
        for (String key : json.keySet()) {
            JsonValue value = json.get(key);
            // Don't import null values or database schemas
            if ((value != null) && (!key.startsWith(SystemSettingsDao.DATABASE_SCHEMA_VERSION))) {
                Object o = value.toNative();
                if (o instanceof String) {
                    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(key);
                    if (def != null) {
                        // Legacy permission import
                        try {
                            Set<String> xids = PermissionService.explodeLegacyPermissionGroups((String) o);
                            Set<Set<Role>> roles = new HashSet<>();
                            for (String xid : xids) {
                                RoleVO role = roleService.get(xid);
                                if (role != null) {
                                    roles.add(Collections.singleton(role.getRole()));
                                } else {
                                    roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
                                }
                            }
                            permissionService.update(new MangoPermission(roles), def);
                            addSuccessMessage(false, "emport.permission.prefix", key);
                        } catch (ValidationException e) {
                            setValidationMessages(e.getValidationResult(), "emport.permission.prefix", key);
                            return;
                        }
                    } else {
                        // Could be an export code so try and convert it
                        Integer id = SystemSettingsDao.getInstance().convertToValueFromCode(key, (String) o);
                        if (id != null)
                            settings.put(key, id);
                        else
                            settings.put(key, o);
                    }
                } else {
                    settings.put(key, o);
                }
            }
        }
        // Now validate it. Use a new response object so we can distinguish errors in this vo
        // from
        // other errors.
        ProcessResult voResponse = new ProcessResult();
        SystemSettingsDao.getInstance().validate(settings, voResponse, user);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        else {
            SystemSettingsDao.getInstance().updateSettings(settings);
            addSuccessMessage(false, "emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()));
        }
    } catch (Exception e) {
        addFailureMessage("emport.systemSettings.prefix", new TranslatableMessage("header.systemSettings").translate(Common.getTranslations()), e.getMessage());
    }
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) HashMap(java.util.HashMap) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) JsonObject(com.serotonin.json.type.JsonObject) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

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