Search in sources :

Example 31 with NotFoundException

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

the class EventTypesRestController method getEventTypes.

/**
 */
private List<EventTypeVOModel<?, ?, ?>> getEventTypes(String typeName, PermissionHolder user) throws NotFoundException {
    // track if the type was a default type
    List<EventTypeVOModel<?, ?, ?>> types = new ArrayList<>();
    boolean found = false;
    switch(typeName) {
        case EventTypeNames.DATA_POINT:
        case EventTypeNames.DATA_SOURCE:
        case EventTypeNames.PUBLISHER:
            throw new BadRequestException();
        case EventTypeNames.SYSTEM:
            found = true;
            for (SystemEventTypeDefinition def : ModuleRegistry.getDefinitions(SystemEventTypeDefinition.class)) {
                EventTypeVO type = SystemEventType.getEventType(def.getTypeName());
                if (type.getEventType().hasPermission(user, permissionService)) {
                    SystemEventTypeModel model = modelMapper.map(type.getEventType(), SystemEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), true, def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
            }
            break;
        case EventTypeNames.AUDIT:
            found = true;
            for (EventTypeVO vo : AuditEventType.getRegisteredEventTypes()) {
                AuditEventType aet = (AuditEventType) vo.getEventType();
                if (aet.hasPermission(user, permissionService)) {
                    AuditEventTypeModel aetm = new AuditEventTypeModel(aet);
                    EventTypeVOModel<?, ?, ?> audit = new EventTypeVOModel<>(aetm, vo.getDescription(), vo.getAlarmLevel(), true, false, false);
                    types.add(audit);
                }
            }
            break;
    }
    if (!found) {
        // Module defined
        for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
            if (StringUtils.equals(typeName, def.getTypeName())) {
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithSubtype(user, permissionService)) {
                    EventType eventType = type.getEventType();
                    AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(eventType, AbstractEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
        }
    }
    if (!found)
        throw new NotFoundException();
    return types;
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) AuditEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.AuditEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel)

Example 32 with NotFoundException

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

the class EventTypesRestController method getEventTypesForSubtypeAndReferenceId1.

/**
 * Generate a list of all event types generalized by sub-type and referenceId1
 */
private List<EventTypeVOModel<?, ?, ?>> getEventTypesForSubtypeAndReferenceId1(String typeName, String subtype, Integer referenceId1, PermissionHolder user) throws NotFoundException {
    // track if the type was a default type
    List<EventTypeVOModel<?, ?, ?>> types = new ArrayList<>();
    boolean found = false;
    switch(typeName) {
        case EventTypeNames.DATA_POINT:
            // There is no subtype for data points
            if (subtype != null)
                throw new BadRequestException();
            DataPointVO dp = this.dataPointService.get(referenceId1);
            for (AbstractPointEventDetectorVO vo : eventDetectorDao.getWithSource(dp.getId(), dp)) {
                AbstractPointEventDetectorModel<?> edm = modelMapper.map(vo, AbstractPointEventDetectorModel.class, user);
                EventTypeVO type = vo.getEventType();
                DataPointEventType eventType = (DataPointEventType) type.getEventType();
                DataPointEventTypeModel model = new DataPointEventTypeModel(eventType, modelMapper.map(dp, DataPointModel.class, user), edm);
                types.add(new EventTypeVOModel<DataPointEventType, DataPointModel, AbstractPointEventDetectorModel<?>>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.DATA_SOURCE:
            // There is no subtype for data sources
            if (subtype != null)
                throw new BadRequestException();
            DataSourceVO ds = dataSourceDao.get(referenceId1);
            if (ds == null)
                throw new NotFoundException();
            permissionService.ensurePermission(user, ds.getReadPermission());
            AbstractDataSourceModel<?> dsModel = modelMapper.map(ds, AbstractDataSourceModel.class, user);
            for (EventTypeVO type : ds.getEventTypes()) {
                DataSourceEventType eventType = (DataSourceEventType) type.getEventType();
                DataSourceEventTypeModel model = new DataSourceEventTypeModel(eventType, dsModel);
                types.add(new EventTypeVOModel<DataSourceEventType, AbstractDataSourceModel<?>, String>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.PUBLISHER:
            // There is no subtype for publishers
            if (subtype != null)
                throw new BadRequestException();
            // There are no permissions for publishers
            if (!permissionService.hasAdminRole(user))
                throw new PermissionException(new TranslatableMessage("permission.exception.doesNotHaveRequiredPermission", user), user);
            PublisherVO pub = publisherDao.get(referenceId1);
            if (pub == null)
                throw new NotFoundException();
            for (EventTypeVO type : pub.getEventTypes()) {
                PublisherEventType eventType = (PublisherEventType) type.getEventType();
                AbstractPublisherModel<?, ?> publisherModel = modelMapper.map(pub, AbstractPublisherModel.class, user);
                PublisherEventTypeModel model = new PublisherEventTypeModel(eventType, publisherModel);
                types.add(new EventTypeVOModel<PublisherEventType, AbstractPublisherModel<?, ?>, String>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.SYSTEM:
            // System
            for (SystemEventTypeDefinition def : ModuleRegistry.getDefinitions(SystemEventTypeDefinition.class)) {
                if (!StringUtils.equals(def.getTypeName(), subtype))
                    continue;
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId2(user, subtype, referenceId1)) {
                    SystemEventType eventType = (SystemEventType) type.getEventType();
                    SystemEventTypeModel model = modelMapper.map(eventType, SystemEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), true, def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
            break;
        case EventTypeNames.AUDIT:
            // Audit does not yet support reference id 2
            throw new BadRequestException();
    }
    if (!found) {
        // Module defined
        for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
            if (StringUtils.equals(typeName, def.getTypeName())) {
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId2(user, subtype, referenceId1, permissionService)) {
                    EventType eventType = type.getEventType();
                    if (!StringUtils.equals(eventType.getEventSubtype(), subtype))
                        continue;
                    if (!eventType.hasPermission(user, permissionService))
                        continue;
                    AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(eventType, AbstractEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
        }
    }
    if (!found)
        throw new NotFoundException();
    return types;
}
Also used : AbstractPointEventDetectorModel(com.infiniteautomation.mango.rest.latest.model.event.detectors.AbstractPointEventDetectorModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AbstractDataSourceModel(com.infiniteautomation.mango.rest.latest.model.datasource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) PublisherEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.PublisherEventTypeModel) ArrayList(java.util.ArrayList) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) AbstractPublisherModel(com.infiniteautomation.mango.rest.latest.model.publisher.AbstractPublisherModel) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) DataPointModel(com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel) DataSourceEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataSourceEventTypeModel) DataPointEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel)

Example 33 with NotFoundException

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

the class JsonDataRestController method deletePartialJsonData.

@ApiOperation(value = "Partially Delete JSON Data", notes = "{path} is the path to data with dots data.member.submember")
@ApiResponses({ @ApiResponse(code = 201, message = "Data Deleted", response = JsonDataModel.class), @ApiResponse(code = 401, message = "Unauthorized Access", response = ResponseEntity.class), @ApiResponse(code = 403, message = "Data Doesn't Exists") })
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}/{path:.*}")
public JsonDataModel deletePartialJsonData(@ApiParam(value = "XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Data path using dots as separator", required = true, allowMultiple = false) @PathVariable String path, @AuthenticationPrincipal PermissionHolder user) {
    String[] pathParts;
    if (path == null || (pathParts = path.split("\\.")).length == 0) {
        // Delete the whole thing
        return map.apply(this.service.delete(xid), user);
    } else {
        JsonDataVO vo = service.get(xid);
        // Delete something from the map
        JsonNode existingData = vo.getJsonData();
        boolean deleted = deleteNode(existingData, pathParts);
        if (!deleted) {
            throw new NotFoundException();
        }
        return map.apply(service.update(xid, vo), user);
    }
}
Also used : JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with NotFoundException

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

the class JsonDataRestController method modifyJsonData.

/**
 * Helper method
 */
private ResponseEntity<JsonDataModel> modifyJsonData(MapOperation operation, String xid, String[] pathParts, MangoPermission readPermissions, MangoPermission editPermissions, String name, JsonNode data, PermissionHolder user, UriComponentsBuilder builder, HttpServletRequest request, HttpStatus successStatus) {
    // check we are using this method only for replace and append
    if (operation != MapOperation.REPLACE && operation != MapOperation.APPEND)
        throw new IllegalArgumentException();
    JsonNode dataToReturn = data;
    JsonDataVO vo;
    try {
        vo = service.get(xid);
        // Replace the data
        vo.setName(name);
        vo.setReadPermission(readPermissions);
        vo.setEditPermission(editPermissions);
        JsonNode existingData = vo.getJsonData();
        if (operation == MapOperation.REPLACE) {
            JsonNode newData = replaceNode(existingData, pathParts, data);
            vo.setJsonData(newData);
        } else if (operation == MapOperation.APPEND) {
            dataToReturn = mergeNode(existingData, pathParts, data);
        }
        this.service.update(xid, vo);
    } catch (NotFoundException e) {
        // can't append/merge to a non-existing object or replace data at a path of a non existing object
        if (operation == MapOperation.APPEND || pathParts.length > 0) {
            throw e;
        }
        // Going to create a new one
        vo = new JsonDataVO();
        vo.setXid(xid);
        vo.setName(name);
        vo.setReadPermission(readPermissions);
        vo.setEditPermission(editPermissions);
        vo.setJsonData(data);
        this.service.insert(vo);
    }
    // can't modify the vo data here as it will be sent out over websocket asynchronously
    JsonDataVO copied = (JsonDataVO) vo.copy();
    // return only the data that was saved, i.e. the data that we supplied a path to
    copied.setJsonData(dataToReturn);
    URI location = builder.path("/json-data/{xid}").buildAndExpand(new Object[] { vo.getXid() }).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(this.map.apply(copied, user), headers, successStatus);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI)

Example 35 with NotFoundException

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

the class ModulesRestController method markModuleForDeletion.

@ApiOperation(value = "Set Marked For Deletion state of Module", notes = "Marking a module for deletion will un-install it upon restart")
@RequestMapping(method = RequestMethod.PUT, value = "/deletion-state")
public ModuleModel markModuleForDeletion(@ApiParam(value = "Deletion State", required = true, defaultValue = "false", allowMultiple = false) @RequestParam(name = "delete", required = true) boolean delete, @ApiParam(value = "Module model") @RequestBody() ModuleModel model, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request) {
    permissionService.ensureAdminRole(user);
    Module module = ModuleRegistry.getModule(model.getName());
    if (module == null)
        throw new NotFoundException();
    module.setMarkedForDeletion(delete);
    if (module.isMarkedForDeletion() != delete)
        throw new ModuleRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("rest.modules.error.dependencyFailure"));
    return new ModuleModel(module);
}
Also used : ModuleRestException(com.infiniteautomation.mango.rest.latest.exception.ModuleRestException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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