Search in sources :

Example 6 with NotFoundRestException

use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class DataPointRestController method getDataPoint.

@ApiOperation(value = "Get data point by XID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public DataPointModel getDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
    DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
    if (dataPoint == null) {
        throw new NotFoundRestException();
    }
    DataPointDao.instance.loadPartialRelationalData(dataPoint);
    Permissions.ensureDataPointReadPermission(user, dataPoint);
    return new DataPointModel(dataPoint);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with NotFoundRestException

use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method getForDataSource.

@ApiOperation(value = "Get all Event Detectors for a given data source", notes = "Must have permission for all data points", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/data-source/{xid}")
public ResponseEntity<List<AbstractEventDetectorModel<?>>> getForDataSource(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Data Source XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    DataSourceVO<?> ds = DataSourceDao.instance.getByXid(xid);
    if (ds == null)
        throw new NotFoundRestException();
    List<DataPointVO> points = DataPointDao.instance.getDataPoints(ds.getId(), null, false);
    List<AbstractEventDetectorModel<?>> models = new ArrayList<AbstractEventDetectorModel<?>>();
    for (DataPointVO dp : points) {
        // Check permissions
        if (!user.isAdmin())
            Permissions.ensureDataPointReadPermission(user, dp);
        DataPointDao.instance.setEventDetectors(dp);
        for (AbstractPointEventDetectorVO<?> ped : dp.getEventDetectors()) models.add(ped.asModel());
    }
    return new ResponseEntity<>(models, HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) AbstractEventDetectorModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with NotFoundRestException

use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method save.

@ApiOperation(value = "Create an Event Detector", notes = "Cannot already exist, must have data source permission for the point")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<AbstractEventDetectorModel<?>> save(@ApiParam(value = "Event Detector", required = true) @RequestBody(required = true) AbstractEventDetectorModel<?> model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = model.getData();
    // Set XID if required
    if (StringUtils.isEmpty(vo.getXid())) {
        vo.setXid(EventDetectorDao.instance.generateUniqueXid());
    }
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(vo.getXid());
    if (existing != null) {
        throw new AlreadyExistsRestException(vo.getXid());
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
    if (dp == null)
        throw new NotFoundRestException();
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
    ped.njbSetDataPoint(dp);
    // Validate
    ped.ensureValid();
    // Add it to the data point
    DataPointDao.instance.setEventDetectors(dp);
    dp.getEventDetectors().add(ped);
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    // Put a link to the updated data in the header?
    URI location = builder.path("/v2/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
    return getResourceCreated(vo.asModel(), location);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) AlreadyExistsRestException(com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with NotFoundRestException

use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method get.

@ApiOperation(value = "Get an Event Detector", notes = "", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public ResponseEntity<AbstractEventDetectorModel<?>> get(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = this.dao.getByXid(xid);
    if (vo == null)
        throw new NotFoundRestException();
    // Check permissions
    if (!user.isAdmin()) {
        DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
        Permissions.ensureDataPointReadPermission(user, dp);
    }
    return new ResponseEntity<>(vo.asModel(), HttpStatus.OK);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with NotFoundRestException

use of com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method update.

@ApiOperation(value = "Update an Event Detector", notes = "")
@RequestMapping(method = RequestMethod.PUT, value = { "/{xid}" })
public ResponseEntity<AbstractEventDetectorModel<?>> update(@ApiParam(value = "Valid Event Detector XID", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Event Detector", required = true) @RequestBody(required = true) AbstractEventDetectorModel<?> model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
    AbstractEventDetectorVO<?> vo = model.getData();
    // Check to see if it already exists
    AbstractEventDetectorVO<?> existing = this.dao.getByXid(xid);
    if (existing == null) {
        throw new NotFoundRestException();
    } else {
        // Set the ID
        vo.setId(existing.getId());
    }
    // Check permission
    DataPointVO dp = DataPointDao.instance.get(vo.getSourceId());
    if (dp == null)
        throw new NotFoundRestException();
    Permissions.ensureDataSourcePermission(user, dp.getDataSourceId());
    // TODO Fix this when we have other types of detectors
    AbstractPointEventDetectorVO<?> ped = (AbstractPointEventDetectorVO<?>) vo;
    ped.njbSetDataPoint(dp);
    // Validate
    ped.ensureValid();
    // Replace it on the data point, if it isn't replaced we fail.
    boolean replaced = false;
    DataPointDao.instance.setEventDetectors(dp);
    ListIterator<AbstractPointEventDetectorVO<?>> it = dp.getEventDetectors().listIterator();
    while (it.hasNext()) {
        AbstractPointEventDetectorVO<?> ed = it.next();
        if (ed.getId() == ped.getId()) {
            it.set(ped);
            replaced = true;
            break;
        }
    }
    if (!replaced)
        throw new ServerErrorException(new TranslatableMessage("rest.error.eventDetectorNotAssignedToThisPoint"));
    // Save the data point
    Common.runtimeManager.saveDataPoint(dp);
    URI location = builder.path("/v2/event-detectors/{xid}").buildAndExpand(vo.getXid()).toUri();
    return getResourceUpdated(vo.asModel(), location);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)35 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)19 ResponseEntity (org.springframework.http.ResponseEntity)17 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)12 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)6 File (java.io.File)6 URI (java.net.URI)6 User (com.serotonin.m2m2.vo.User)5 MultipartFile (org.springframework.web.multipart.MultipartFile)5 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)5 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)4 ArrayList (java.util.ArrayList)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)3 FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)3 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)3