use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.
the class DataPointRestController method bulkApplyReadPermissions.
@ApiOperation(value = "Bulk Update Read Permissions", notes = "", response = Long.class)
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" }, value = "/bulk-apply-read-permissions")
public ResponseEntity<Long> bulkApplyReadPermissions(@ApiParam(value = "Permissions", required = true) @RequestBody(required = true) String permissions, HttpServletRequest request) {
RestProcessResult<Long> result = new RestProcessResult<Long>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (!user.isAdmin()) {
LOG.warn("User " + user.getUsername() + " attempted to set bulk permissions");
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
try {
ASTNode node = parseRQLtoAST(request.getQueryString());
long changed = this.dao.bulkUpdatePermissions(node, permissions, false);
return result.createResponseEntity(changed);
} catch (InvalidRQLRestException e) {
LOG.error(e.getMessage(), e);
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
return result.createResponseEntity();
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult 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();
}
use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.
the class DataPointRestController method saveDataPoint.
@ApiOperation(value = "Create a data point", notes = "Content may be CSV or JSON")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" })
public ResponseEntity<DataPointModel> saveDataPoint(@ApiParam(value = "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();
// Check to see if the point already exists
if (!StringUtils.isEmpty(vo.getXid())) {
DataPointVO existing = this.dao.getByXid(vo.getXid());
if (existing != null) {
result.addRestMessage(HttpStatus.CONFLICT, new TranslatableMessage("rest.exception.alreadyExists", model.getXid()));
return result.createResponseEntity();
}
}
// Ensure ds exists
DataSourceVO<?> dataSource = DataSourceDao.instance.getByXid(model.getDataSourceXid());
// We will always override the DS Info with the one from the XID Lookup
if (dataSource == null) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", model.getDataSourceXid()));
return result.createResponseEntity();
} else {
vo.setDataSourceId(dataSource.getId());
vo.setDataSourceName(dataSource.getName());
}
// Check permissions
try {
if (!Permissions.hasDataSourcePermission(user, vo.getDataSourceId())) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException e) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
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) {
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 (StringUtils.isEmpty(vo.getXid()))
vo.setXid(DataPointDao.instance.generateUniqueXid());
// allow empty string, but if its null use the data source name
if (vo.getDeviceName() == null) {
vo.setDeviceName(dataSource.getName());
}
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
} else {
try {
Common.runtimeManager.saveDataPoint(vo);
} catch (LicenseViolatedException e) {
result.addRestMessage(HttpStatus.METHOD_NOT_ALLOWED, e.getErrorMessage());
}
}
// 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();
}
Aggregations