Search in sources :

Example 1 with AlreadyExistsRestException

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);
}
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 2 with AlreadyExistsRestException

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);
}
Also used : AlreadyExistsRestException(com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException) VirtualSerialPortConfig(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AlreadyExistsRestException

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);
}
Also used : 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)

Aggregations

AlreadyExistsRestException (com.infiniteautomation.mango.rest.v2.exception.AlreadyExistsRestException)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 URI (java.net.URI)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 VirtualSerialPortConfig (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1