Search in sources :

Example 1 with DataPointModel

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

the class DataPointRestController method getDataPointById.

@ApiOperation(value = "Get data point by ID", notes = "Returned as CSV or JSON, only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv", "application/sero-json" }, value = "/by-id/{id}")
public ResponseEntity<DataPointModel> getDataPointById(@ApiParam(value = "Valid Data Point ID", required = true, allowMultiple = false) @PathVariable int id, HttpServletRequest request) {
    RestProcessResult<DataPointModel> result = new RestProcessResult<DataPointModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.get(id);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        try {
            if (Permissions.hasDataPointReadPermission(user, vo))
                return result.createResponseEntity(new DataPointModel(vo));
            else {
                LOG.warn("User: " + user.getUsername() + " tried to access data point with xid " + vo.getXid());
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.warn(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with DataPointModel

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

the class DataPointRestController method getDataPoint.

@ApiOperation(value = "Get data point by XID", notes = "Returned as CSV or JSON, only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv", "application/sero-json" }, value = "/{xid}")
public ResponseEntity<DataPointModel> getDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<DataPointModel> result = new RestProcessResult<DataPointModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        try {
            if (Permissions.hasDataPointReadPermission(user, vo))
                return result.createResponseEntity(new DataPointModel(vo));
            else {
                LOG.warn("User: " + user.getUsername() + " tried to access data point with xid " + vo.getXid());
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.warn(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with DataPointModel

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

the class DataPointRestController method queryRQL.

@ApiOperation(value = "Query Data Points", notes = "Use RQL formatted query", response = DataPointModel.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<QueryDataPageStream<DataPointVO>> queryRQL(HttpServletRequest request) {
    RestProcessResult<QueryDataPageStream<DataPointVO>> result = new RestProcessResult<QueryDataPageStream<DataPointVO>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        try {
            ASTNode node = parseRQLtoAST(request.getQueryString());
            if (user.isAdmin()) {
                // Admin Users Don't need to filter the results
                return result.createResponseEntity(getPageStream(node));
            } else {
                // Limit our results based on the fact that our permissions should be in the permissions strings
                node = addPermissionsFilter(node, user);
                DataPointStreamCallback callback = new DataPointStreamCallback(this, user);
                FilteredPageQueryStream<DataPointVO, DataPointModel, DataPointDao> stream = new FilteredPageQueryStream<DataPointVO, DataPointModel, DataPointDao>(DataPointDao.instance, this, node, callback);
                stream.setupQuery();
                return result.createResponseEntity(stream);
            }
        } catch (InvalidRQLRestException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) DataPointStreamCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointStreamCallback) QueryDataPageStream(com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) ASTNode(net.jazdw.rql.parser.ASTNode) FilteredPageQueryStream(com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with DataPointModel

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

the class DataPointRestController method updateDataPoint.

/**
 * Update a data point in the system
 * @param vo
 * @param xid
 * @param builder
 * @param request
 * @return
 */
@ApiOperation(value = "Update an existing data point", notes = "Content may be CSV or JSON")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" }, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<DataPointModel> result = new RestProcessResult<DataPointModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        boolean contentTypeCsv = false;
        if (request.getContentType().toLowerCase().contains("text/csv"))
            contentTypeCsv = true;
        DataPointVO vo = model.getData();
        DataPointVO existingDp = DataPointDao.instance.getByXid(xid);
        if (existingDp == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // We will always override the DS Info with the one from the XID Lookup
        DataSourceVO<?> dsvo = DataSourceDao.instance.getDataSource(existingDp.getDataSourceXid());
        // TODO this implies that we may need to have a different JSON Converter for data points
        // Need to set DataSourceId among other things
        vo.setDataSourceId(existingDp.getDataSourceId());
        // Check permissions
        try {
            if (!Permissions.hasDataSourcePermission(user, vo.getDataSourceId())) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        vo.setId(existingDp.getId());
        // Set all properties that are not in the template or the spreadsheet
        // Use ID to get detectors
        DataPointDao.instance.setEventDetectors(vo);
        vo.setPointFolderId(existingDp.getPointFolderId());
        if (vo.getTextRenderer() == null) {
            vo.setTextRenderer(new PlainRenderer());
        }
        if (vo.getChartColour() == null) {
            vo.setChartColour("");
        }
        // Check the Template and see if we need to use it
        if (model.getTemplateXid() != null) {
            DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
            if (template != null) {
                template.updateDataPointVO(vo);
                template.updateDataPointVO(model.getData());
            } else {
                model.addValidationMessage("validate.invalidReference", RestMessageLevel.ERROR, "templateXid");
                result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", model.getTemplateXid())));
            }
        } else {
            if (contentTypeCsv) {
                model.addValidationMessage("validate.required", RestMessageLevel.ERROR, "templateXid");
                result.addRestMessage(this.getValidationFailedError());
                return result.createResponseEntity(model);
            }
        }
        if (!model.validate()) {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        } else {
            if (dsvo == null) {
                result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", xid));
                return result.createResponseEntity();
            } else {
                // Does the old point have a different data source?
                if (existingDp.getDataSourceId() != dsvo.getId()) {
                    vo.setDataSourceId(dsvo.getId());
                    vo.setDataSourceName(dsvo.getName());
                }
            }
            Common.runtimeManager.saveDataPoint(vo);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/data-points/{xid}").buildAndExpand(vo.getXid()).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) PlainRenderer(com.serotonin.m2m2.view.text.PlainRenderer) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) URI(java.net.URI) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) RestMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with DataPointModel

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

the class DataPointRestController method query.

@ApiOperation(value = "Query Data Points", notes = "", response = DataPointModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" }, value = "/query")
public ResponseEntity<QueryDataPageStream<DataPointVO>> query(@ApiParam(value = "Query", required = true) @RequestBody(required = true) ASTNode root, HttpServletRequest request) {
    RestProcessResult<QueryDataPageStream<DataPointVO>> result = new RestProcessResult<QueryDataPageStream<DataPointVO>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        // We are going to filter the results, so we need to strip out the limit(limit,offset) or limit(limit) clause.
        DataPointStreamCallback callback = new DataPointStreamCallback(this, user);
        if (!user.isAdmin()) {
            // Limit our results based on the fact that our permissions should be in the permissions strings
            root = addPermissionsFilter(root, user);
            FilteredPageQueryStream<DataPointVO, DataPointModel, DataPointDao> stream = new FilteredPageQueryStream<DataPointVO, DataPointModel, DataPointDao>(DataPointDao.instance, this, root, callback);
            stream.setupQuery();
            return result.createResponseEntity(stream);
        } else {
            // Admin Users Don't need to filter the results
            return result.createResponseEntity(getPageStream(root));
        }
    }
    return result.createResponseEntity();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) DataPointStreamCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointStreamCallback) QueryDataPageStream(com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream) DataPointModel(com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) FilteredPageQueryStream(com.serotonin.m2m2.web.mvc.rest.v1.model.FilteredPageQueryStream) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (com.wordnik.swagger.annotations.ApiOperation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)16 User (com.serotonin.m2m2.vo.User)11 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)11 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)11 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)9 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)7 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)6 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)5 URI (java.net.URI)5 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 ResponseEntity (org.springframework.http.ResponseEntity)4 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 PlainRenderer (com.serotonin.m2m2.view.text.PlainRenderer)3 RestMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestMessage)3 DataPointStreamCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointStreamCallback)3 HttpHeaders (org.springframework.http.HttpHeaders)3 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2