use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method replaceNode.
JsonNode replaceNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
if (dataPath.length == 0) {
return newData;
}
String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
String fieldName = dataPath[dataPath.length - 1];
JsonNode parent = getNode(existingData, parentPath);
if (parent.isObject()) {
ObjectNode parentObject = (ObjectNode) parent;
parentObject.set(fieldName, newData);
} else if (parent.isArray()) {
ArrayNode parentArray = (ArrayNode) parent;
int index = toArrayIndex(fieldName);
parentArray.set(index, newData);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantSetFieldOfNodeType", parent.getNodeType()));
}
return existingData;
}
use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method getNode.
JsonNode getNode(final JsonNode existingData, final String[] dataPath) {
JsonNode node = existingData;
for (int i = 0; i < dataPath.length; i++) {
String fieldName = dataPath[i];
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
node = objectNode.get(fieldName);
} else if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
int index = toArrayIndex(fieldName);
node = arrayNode.get(index);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantGetFieldOfNodeType", node.getNodeType()));
}
if (node == null) {
throw new NotFoundRestException();
}
}
return node;
}
use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method deleteNode.
boolean deleteNode(final JsonNode existingData, final String[] dataPath) {
if (dataPath.length == 0)
throw new IllegalArgumentException();
String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
String fieldName = dataPath[dataPath.length - 1];
JsonNode parent = getNode(existingData, parentPath);
JsonNode deletedValue = null;
if (parent.isObject()) {
ObjectNode parentObject = (ObjectNode) parent;
deletedValue = parentObject.remove(fieldName);
} else if (parent.isArray()) {
ArrayNode parentArray = (ArrayNode) parent;
int index = toArrayIndex(fieldName);
deletedValue = parentArray.remove(index);
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantDeleteFieldOfNodeType", parent.getNodeType()));
}
return deletedValue != null;
}
use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException 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.exception.BadRequestException 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);
}
Aggregations