Search in sources :

Example 1 with MaintenanceEventsService

use of com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService in project ma-modules-public by infiniteautomation.

the class MaintenanceEventEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext, PermissionHolder importer) throws JsonException {
    MaintenanceEventsService service = Common.getBean(MaintenanceEventsService.class);
    JsonObject maintenanceEvent = jsonValue.toJsonObject();
    String xid = maintenanceEvent.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = service.generateUniqueXid();
    MaintenanceEventVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new MaintenanceEventVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, maintenanceEvent);
        // Ensure we have a default permission since null is valid in Mango 3.x
        if (vo.getTogglePermission() == null) {
            vo.setTogglePermission(new MangoPermission());
        }
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        importContext.addSuccessMessage(isnew, "emport.maintenanceEvent.prefix", xid);
    } catch (ValidationException e) {
        importContext.copyValidationMessages(e.getValidationResult(), "emport.maintenanceEvent.prefix", xid);
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonObject(com.serotonin.json.type.JsonObject) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 2 with MaintenanceEventsService

use of com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService in project ma-modules-public by infiniteautomation.

the class MaintenanceEventType method getEventPermission.

@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
    Set<Role> allRequired = new HashSet<>();
    try {
        MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
        try {
            for (int dsId : vo.getDataSources()) {
                MangoPermission read = dataSourceService.getReadPermission(dsId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
        try {
            for (int dpId : vo.getDataPoints()) {
                MangoPermission read = dataPointService.getReadPermission(dpId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
    } catch (NotFoundException e) {
    // Ignore all of it
    }
    if (allRequired.size() == 0) {
        return MangoPermission.superadminOnly();
    } else {
        return MangoPermission.requireAllRoles(allRequired);
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) Role(com.serotonin.m2m2.vo.role.Role) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) HashSet(java.util.HashSet)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 MaintenanceEventsService (com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService)2 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)2 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)1 DataSourceService (com.infiniteautomation.mango.spring.service.DataSourceService)1 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)1 JsonException (com.serotonin.json.JsonException)1 JsonObject (com.serotonin.json.type.JsonObject)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 Role (com.serotonin.m2m2.vo.role.Role)1 HashSet (java.util.HashSet)1