use of com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException 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.AlreadyExistsRestException in project ma-modules-public by infiniteautomation.
the class VirtualSerialPortRestV2Controller method save.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Create a virtual serial port", notes = "Cannot already exist, admin only")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" })
public ResponseEntity<VirtualSerialPortConfig> save(@ApiParam(value = "Serial Port", required = true) @RequestBody(required = true) VirtualSerialPortConfig model, @AuthenticationPrincipal User user, UriComponentsBuilder builder, HttpServletRequest request) {
// Check to see if it already exists
if (!StringUtils.isEmpty(model.getXid())) {
VirtualSerialPortConfig existing = VirtualSerialPortConfigDao.instance.getByXid(model.getXid());
if (existing != null) {
throw new AlreadyExistsRestException(model.getXid());
}
}
// Validate
model.ensureValid();
// Save it
VirtualSerialPortConfigDao.instance.save(model);
// Put a link to the updated data in the header?
URI location = builder.path("/v2/virtual-serial-ports/{xid}").buildAndExpand(model.getXid()).toUri();
return getResourceCreated(model, location);
}
use of com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException in project ma-modules-public by infiniteautomation.
the class PublisherRestV2Controller method save.
/**
* Update a publisher
* @param xid
* @param model
* @param builder
* @param request
* @return
*/
@ApiOperation(value = "Create publisher", notes = "Admin only")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "application/sero-json" }, produces = { "application/json", "application/sero-json" })
public ResponseEntity<AbstractPublisherModel<?, ?>> save(@AuthenticationPrincipal User user, @RequestBody(required = true) AbstractPublisherModel<?, ?> model, UriComponentsBuilder builder, HttpServletRequest request) {
assertAdmin(user);
PublisherVO<?> vo = model.getData();
PublisherVO<?> existing = this.dao.getByXid(vo.getXid());
if (existing != null)
throw new AlreadyExistsRestException(vo.getXid());
vo.ensureValid();
Common.runtimeManager.savePublisher(vo);
URI location = builder.path("/v2/publishers/{xid}").buildAndExpand(vo.getXid()).toUri();
return getResourceCreated(vo.asModel(), location);
}
Aggregations