Search in sources :

Example 1 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel 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 2 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel 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 3 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel 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 4 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel 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)

Example 5 with AbstractEventDetectorModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel in project ma-modules-public by infiniteautomation.

the class EventDetectorRestV2Controller method getForDataPoint.

@ApiOperation(value = "Get all Event Detectors for a given data point", notes = "", response = AbstractEventDetectorModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = "/data-point/{xid}")
public ResponseEntity<List<AbstractEventDetectorModel<?>>> getForDataPoint(@AuthenticationPrincipal User user, @ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    DataPointVO dp = DataPointDao.instance.getByXid(xid);
    if (dp == null)
        throw new NotFoundRestException();
    // Check permissions
    if (!user.isAdmin())
        Permissions.ensureDataPointReadPermission(user, dp);
    DataPointDao.instance.setEventDetectors(dp);
    List<AbstractEventDetectorModel<?>> models = new ArrayList<AbstractEventDetectorModel<?>>();
    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)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ResponseEntity (org.springframework.http.ResponseEntity)5 AbstractEventDetectorModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.detectors.AbstractEventDetectorModel)4 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)3 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)1 EventDetectorDao (com.serotonin.m2m2.db.dao.EventDetectorDao)1 AbstractEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractEventDetectorVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 FilteredPageQueryStream (com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream)1 EventDetectorStreamCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.eventDetector.EventDetectorStreamCallback)1 ASTNode (net.jazdw.rql.parser.ASTNode)1