Search in sources :

Example 26 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method deleteDataSource.

@ApiOperation(value = "Delete data source")
@RequestMapping(method = { RequestMethod.DELETE }, value = { "/{xid}" }, produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> deleteDataSource(@PathVariable String xid, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> existing = (DataSourceVO<?>) DataSourceDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(this.getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            try {
                if (!Permissions.hasDataSourcePermission(user, existing.getId())) {
                    result.addRestMessage(this.getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            } catch (PermissionException pe) {
                LOG.warn(pe.getMessage(), pe);
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            }
            Common.runtimeManager.deleteDataSource(existing.getId());
            return result.createResponseEntity(existing.asModel());
        }
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method getAllDataSources.

@ApiOperation(value = "Get all data sources", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/list")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<AbstractDataSourceModel<?>>> getAllDataSources(HttpServletRequest request) {
    RestProcessResult<List<AbstractDataSourceModel<?>>> result = new RestProcessResult<List<AbstractDataSourceModel<?>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<DataSourceVO<?>> dataSources = DataSourceDao.instance.getAll();
        List<AbstractDataSourceModel<?>> models = new ArrayList<AbstractDataSourceModel<?>>();
        for (DataSourceVO<?> ds : dataSources) {
            try {
                if (Permissions.hasDataSourcePermission(user, ds))
                    models.add(ds.asModel());
            } catch (PermissionException e) {
            // Munch Munch
            }
        }
        return result.createResponseEntity(models);
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.

the class DataPointRestController method createDataPoint.

@ApiOperation(value = "Create a new data point")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<DataPointModel> createDataPoint(@ApiParam(value = "Data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    DataSourceVO<?> dataSource = DataSourceDao.instance.getByXid(model.getDataSourceXid());
    if (dataSource == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.invalidDataSourceXid"));
    }
    Permissions.ensureDataSourcePermission(user, dataSource);
    DataPointVO dataPoint = new DataPointVO(dataSource);
    model.copyPropertiesTo(dataPoint);
    if (model.getTemplateXid() != null) {
        DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
        if (template == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.invalidTemplateXid"));
        }
        dataPoint.withTemplate(template);
    }
    dataPoint.ensureValid();
    Common.runtimeManager.saveDataPoint(dataPoint);
    URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(dataPoint.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.CREATED);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO 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 30 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-modules-public by infiniteautomation.

the class EventTypeV2RestController method getAllDataSourceEventTypes.

private void getAllDataSourceEventTypes(List<EventTypeModel> types, User user, Integer dataSourceId, Integer dataSourceEventId) {
    List<DataSourceVO<?>> dataSources = DataSourceDao.instance.getAll();
    final boolean admin = Permissions.hasAdmin(user);
    for (DataSourceVO<?> dsvo : dataSources) if (dataSourceId == null || dataSourceId.intValue() == dsvo.getId())
        for (EventTypeVO dset : (List<EventTypeVO>) dsvo.getEventTypes()) if (dataSourceEventId == null || dataSourceEventId.intValue() == dset.getTypeRef2()) {
            EventType et = dset.createEventType();
            if (admin || Permissions.hasEventTypePermission(user, et))
                types.add(et.asModel());
        }
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) ArrayList(java.util.ArrayList) List(java.util.List) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO)

Aggregations

User (com.serotonin.m2m2.vo.User)31 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 ArrayList (java.util.ArrayList)21 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)19 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)18 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)18 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)15 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)15 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 List (java.util.List)10 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)9 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)8 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)6 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)6 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)5 URI (java.net.URI)5 HashMap (java.util.HashMap)5