use of com.serotonin.m2m2.vo.DataPointVO 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();
}
use of com.serotonin.m2m2.vo.DataPointVO 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.serotonin.m2m2.vo.DataPointVO 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.serotonin.m2m2.vo.DataPointVO in project ma-modules-public by infiniteautomation.
the class DataPointRestController method getDataPoint.
@ApiOperation(value = "Get data point by XID", notes = "Only points that user has read permission to are returned")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}")
public DataPointModel getDataPoint(@ApiParam(value = "Valid Data Point XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal User user) {
DataPointVO dataPoint = DataPointDao.instance.getByXid(xid);
if (dataPoint == null) {
throw new NotFoundRestException();
}
DataPointDao.instance.loadPartialRelationalData(dataPoint);
Permissions.ensureDataPointReadPermission(user, dataPoint);
return new DataPointModel(dataPoint);
}
use of com.serotonin.m2m2.vo.DataPointVO 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);
}
Aggregations