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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations