use of com.infiniteautomation.mango.rest.v2.model.dataPoint.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();
}
use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.
the class DataPointRestController method updateDataPoint.
@ApiOperation(value = "Update an existing data point")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
if (dataPoint == null) {
throw new NotFoundRestException();
}
Permissions.ensureDataSourcePermission(user, dataPoint.getDataSourceId());
// check if they are trying to move it to another data source
String newDataSourceXid = model.getDataSourceXid();
if (newDataSourceXid != null && !newDataSourceXid.isEmpty() && !newDataSourceXid.equals(dataPoint.getDataSourceXid())) {
throw new BadRequestException(new TranslatableMessage("rest.error.pointChangeDataSource"));
}
DataPointPropertiesTemplateVO template = null;
if (model.isTemplateXidWasSet()) {
if (model.getTemplateXid() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template == null) {
throw new BadRequestException(new TranslatableMessage("invalidTemplateXid"));
}
}
} else if (dataPoint.getTemplateId() != null) {
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.get(dataPoint.getTemplateId());
}
DataPointDao.instance.loadPartialRelationalData(dataPoint);
model.copyPropertiesTo(dataPoint);
// load the template after copying the properties, template properties override the ones in the data point
if (template != null) {
dataPoint.withTemplate(template);
}
dataPoint.ensureValid();
// have to load any existing event detectors for the data point as we are about to replace the VO in the runtime manager
DataPointDao.instance.setEventDetectors(dataPoint);
Common.runtimeManager.saveDataPoint(dataPoint);
URI location = builder.path("/v2/data-points/{xid}").buildAndExpand(xid).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(new DataPointModel(dataPoint), headers, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel 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);
}
use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.
the class DataPointRestController method bulkDataPointOperation.
@ApiOperation(value = "Bulk get/create/update/delete data points", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> bulkDataPointOperation(@RequestBody DataPointBulkRequest requestBody, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
DataPointModel defaultBody = requestBody.getBody();
List<DataPointIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT, resourceId, user.getId(), expiration, timeout, (resource) -> {
DataPointBulkResponse bulkResponse = new DataPointBulkResponse();
int i = 0;
resource.progress(bulkResponse, i++, requests.size());
for (DataPointIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
DataPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/v2/data-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel in project ma-modules-public by infiniteautomation.
the class DataPointRestController method doQuery.
private static StreamedArrayWithTotal doQuery(ASTNode rql, User user) {
if (user.isAdmin()) {
return new StreamedVOQueryWithTotal<>(DataPointDao.instance, rql, item -> {
DataPointDao.instance.loadPartialRelationalData(item);
return new DataPointModel(item);
});
} else {
// Add some conditions to restrict based on user permissions
ConditionSortLimitWithTagKeys conditions = DataPointDao.instance.rqlToCondition(rql);
conditions.addCondition(DataPointDao.instance.userHasPermission(user));
DataPointFilter dataPointFilter = new DataPointFilter(user);
return new StreamedVOQueryWithTotal<>(DataPointDao.instance, conditions, item -> {
boolean oldFilterMatches = dataPointFilter.hasDataPointReadPermission(item);
// this is just a double check, permissions should be accounted for via SQL restrictions added by DataPointDao.userHasPermission()
if (!oldFilterMatches) {
throw new RuntimeException("Data point does not match old permission filter");
}
return true;
}, item -> {
DataPointDao.instance.loadPartialRelationalData(item);
return new DataPointModel(item);
});
}
}
Aggregations