created separate api for relations with returning entity
This commit is contained in:
parent
c619be45b6
commit
a1e8ec4c02
@ -81,8 +81,24 @@ public class EntityRelationController extends BaseController {
|
||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/relation", method = RequestMethod.POST)
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public EntityRelation saveRelation(@Parameter(description = "A JSON value representing the relation.", required = true)
|
||||
@RequestBody EntityRelation relation) throws ThingsboardException {
|
||||
public void saveRelation(@Parameter(description = "A JSON value representing the relation.", required = true)
|
||||
@RequestBody EntityRelation relation) throws ThingsboardException {
|
||||
doSave(relation);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Create Relation (saveRelationV2)",
|
||||
notes = "Creates or updates a relation between two entities in the platform. " +
|
||||
"Relations unique key is a combination of from/to entity id and relation type group and relation type. " +
|
||||
SECURITY_CHECKS_ENTITIES_DESCRIPTION)
|
||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/v2/relation", method = RequestMethod.POST)
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public EntityRelation saveRelationV2(@Parameter(description = "A JSON value representing the relation.", required = true)
|
||||
@RequestBody EntityRelation relation) throws ThingsboardException {
|
||||
return doSave(relation);
|
||||
}
|
||||
|
||||
private EntityRelation doSave(EntityRelation relation) throws ThingsboardException {
|
||||
checkNotNull(relation);
|
||||
checkCanCreateRelation(relation.getFrom());
|
||||
checkCanCreateRelation(relation.getTo());
|
||||
@ -98,12 +114,30 @@ public class EntityRelationController extends BaseController {
|
||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/relation", method = RequestMethod.DELETE, params = {FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE})
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public EntityRelation deleteRelation(@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_ID) String strFromId,
|
||||
public void deleteRelation(@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_ID) String strFromId,
|
||||
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_TYPE) String strFromType,
|
||||
@Parameter(description = RELATION_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(RELATION_TYPE) String strRelationType,
|
||||
@Parameter(description = RELATION_TYPE_GROUP_PARAM_DESCRIPTION) @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup,
|
||||
@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(TO_ID) String strToId,
|
||||
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(TO_TYPE) String strToType) throws ThingsboardException {
|
||||
doDelete(strFromId, strFromType, strRelationType, strRelationTypeGroup, strToId, strToType);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Delete Relation (deleteRelationV2)",
|
||||
notes = "Deletes a relation between two entities in the platform. " + SECURITY_CHECKS_ENTITIES_DESCRIPTION)
|
||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
|
||||
@RequestMapping(value = "/v2/relation", method = RequestMethod.DELETE, params = {FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE})
|
||||
@ResponseStatus(value = HttpStatus.OK)
|
||||
public EntityRelation deleteRelationV2(@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_ID) String strFromId,
|
||||
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_TYPE) String strFromType,
|
||||
@Parameter(description = RELATION_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(RELATION_TYPE) String strRelationType,
|
||||
@Parameter(description = RELATION_TYPE_GROUP_PARAM_DESCRIPTION) @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup,
|
||||
@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(TO_ID) String strToId,
|
||||
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(TO_TYPE) String strToType) throws ThingsboardException {
|
||||
return doDelete(strFromId, strFromType, strRelationType, strRelationTypeGroup, strToId, strToType);
|
||||
}
|
||||
|
||||
private EntityRelation doDelete(String strFromId, String strFromType, String strRelationType, String strRelationTypeGroup, String strToId, String strToType) throws ThingsboardException {
|
||||
checkParameter(FROM_ID, strFromId);
|
||||
checkParameter(FROM_TYPE, strFromType);
|
||||
checkParameter(RELATION_TYPE, strRelationType);
|
||||
|
||||
@ -103,7 +103,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
|
||||
|
||||
Mockito.reset(tbClusterService, auditLogService);
|
||||
|
||||
relation = doPost("/api/relation", relation, EntityRelation.class);
|
||||
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
|
||||
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
|
||||
mainDevice.getUuidId(), EntityType.DEVICE,
|
||||
@ -315,7 +315,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
|
||||
Device device = buildSimpleDevice("Test device 1");
|
||||
|
||||
EntityRelation relation = createFromRelation(mainDevice, device, "CONTAINS");
|
||||
relation = doPost("/api/relation", relation, EntityRelation.class);
|
||||
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
|
||||
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
|
||||
mainDevice.getUuidId(), EntityType.DEVICE,
|
||||
@ -329,7 +329,11 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
|
||||
|
||||
Mockito.reset(tbClusterService, auditLogService);
|
||||
|
||||
var deletedRelation = doDelete(url, EntityRelation.class);
|
||||
String deleteUrl = String.format("/api/v2/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
|
||||
mainDevice.getUuidId(), EntityType.DEVICE,
|
||||
"CONTAINS", device.getUuidId(), EntityType.DEVICE
|
||||
);
|
||||
var deletedRelation = doDelete(deleteUrl, EntityRelation.class);
|
||||
|
||||
testNotifyEntityAllOneTimeRelation(deletedRelation,
|
||||
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(),
|
||||
@ -523,7 +527,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
public void testCreateRelationFromTenantToDevice() throws Exception {
|
||||
EntityRelation relation = new EntityRelation(tenantAdmin.getTenantId(), mainDevice.getId(), "CONTAINS");
|
||||
relation = doPost("/api/relation", relation, EntityRelation.class);
|
||||
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
|
||||
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
|
||||
tenantAdmin.getTenantId(), EntityType.TENANT,
|
||||
@ -539,7 +543,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
public void testCreateRelationFromDeviceToTenant() throws Exception {
|
||||
EntityRelation relation = new EntityRelation(mainDevice.getId(), tenantAdmin.getTenantId(), "CONTAINS");
|
||||
relation = doPost("/api/relation", relation, EntityRelation.class);
|
||||
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
|
||||
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
|
||||
mainDevice.getUuidId(), EntityType.DEVICE,
|
||||
|
||||
@ -48,7 +48,7 @@ public class RelationEdgeTest extends AbstractEdgeTest {
|
||||
relation.setTo(asset.getId());
|
||||
relation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
edgeImitator.expectMessageAmount(1);
|
||||
relation = doPost("/api/relation", relation, EntityRelation.class);
|
||||
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
Assert.assertTrue(edgeImitator.waitForMessages());
|
||||
AbstractMessage latestMessage = edgeImitator.getLatestMessage();
|
||||
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
|
||||
@ -60,7 +60,7 @@ public class RelationEdgeTest extends AbstractEdgeTest {
|
||||
|
||||
// delete relation
|
||||
edgeImitator.expectMessageAmount(1);
|
||||
var deletedRelation = doDelete("/api/relation?" +
|
||||
var deletedRelation = doDelete("/api/v2/relation?" +
|
||||
"fromId=" + relation.getFrom().getId().toString() +
|
||||
"&fromType=" + relation.getFrom().getEntityType().name() +
|
||||
"&relationType=" + relation.getType() +
|
||||
@ -118,7 +118,7 @@ public class RelationEdgeTest extends AbstractEdgeTest {
|
||||
deviceToAssetRelation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
|
||||
edgeImitator.expectMessageAmount(1);
|
||||
deviceToAssetRelation = doPost("/api/relation", deviceToAssetRelation, EntityRelation.class);
|
||||
deviceToAssetRelation = doPost("/api/v2/relation", deviceToAssetRelation, EntityRelation.class);
|
||||
Assert.assertTrue(edgeImitator.waitForMessages());
|
||||
|
||||
EntityRelation assetToTenantRelation = new EntityRelation();
|
||||
|
||||
@ -937,7 +937,7 @@ public class VersionControlTest extends AbstractControllerTest {
|
||||
relation.setType(EntityRelation.MANAGES_TYPE);
|
||||
relation.setAdditionalInfo(JacksonUtil.newObjectNode().set("a", new TextNode("b")));
|
||||
relation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
return doPost("/api/relation", relation, EntityRelation.class);
|
||||
return doPost("/api/v2/relation", relation, EntityRelation.class);
|
||||
}
|
||||
|
||||
protected void checkImportedRuleChainData(RuleChain initialRuleChain, RuleChainMetaData initialMetaData, RuleChain importedRuleChain, RuleChainMetaData importedMetaData) {
|
||||
|
||||
@ -1693,6 +1693,10 @@ public class RestClient implements Closeable {
|
||||
restTemplate.postForLocation(baseURL + "/api/relation", relation);
|
||||
}
|
||||
|
||||
public EntityRelation saveRelationV2(EntityRelation relation) {
|
||||
return restTemplate.postForEntity(baseURL + "/api/v2/relation", relation, EntityRelation.class).getBody();
|
||||
}
|
||||
|
||||
public void deleteRelation(EntityId fromId, String relationType, RelationTypeGroup relationTypeGroup, EntityId toId) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("fromId", fromId.getId().toString());
|
||||
@ -1704,6 +1708,26 @@ public class RestClient implements Closeable {
|
||||
restTemplate.delete(baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}", params);
|
||||
}
|
||||
|
||||
public Optional<EntityRelation> deleteRelationV2(EntityId fromId, String relationType, RelationTypeGroup relationTypeGroup, EntityId toId) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("fromId", fromId.getId().toString());
|
||||
params.put("fromType", fromId.getEntityType().name());
|
||||
params.put("relationType", relationType);
|
||||
params.put("relationTypeGroup", relationTypeGroup.name());
|
||||
params.put("toId", toId.getId().toString());
|
||||
params.put("toType", toId.getEntityType().name());
|
||||
try {
|
||||
var relation = restTemplate.exchange(baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}", HttpMethod.DELETE, HttpEntity.EMPTY, EntityRelation.class, params);
|
||||
return Optional.ofNullable(relation.getBody());
|
||||
} catch (HttpClientErrorException exception) {
|
||||
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteRelations(EntityId entityId) {
|
||||
restTemplate.delete(baseURL + "/api/relations?entityId={entityId}&entityType={entityType}", entityId.getId().toString(), entityId.getEntityType().name());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user