@Test public void testNetworkDeviceAndPort() { GenericCMSEntity device = createDevice(); CMSClientContext context = new CMSClientContext(); cmsdbService.create(device, context); GenericCMSEntity port = createPort(device, context); context.setPath("NetworkDevice", device.get_id(), "networkPorts"); cmsdbService.create(port, context); GenericCMSEntity getDevice = null; getDevice = cmsdbService.get(device.get_id(), "NetworkDevice", context); Assert.assertEquals(1, ((List<?>) getDevice.getFieldValue("networkPorts")).size()); cmsdbService.deleteRelationship(device, "networkPorts", port, context); GenericCMSEntity getPort = cmsdbService.get(port.get_id(), "NetworkPort", context); Assert.assertNull(getPort); getDevice = cmsdbService.get(device.get_id(), "NetworkDevice", context); Assert.assertEquals(0, ((List<?>) getDevice.getFieldValue("networkPorts")).size()); }
public <T extends ICMSEntity, K extends ICMSEntity> void deleteRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); Preconditions.checkArgument(!StringUtils.isEmpty(refField), String.format("refField could not be null!")); T getFrom = getEntity(fromEntity, context); if (getFrom == null) { return; } K getTo = getEntity(toEntity, context); if (getTo == null) { return; } final String fromType = fromEntity.get_type(); MetaClass meta = getMetadata(fromType); final MetaField field = meta.getField(refField); if (! (field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relationship = (MetaRelationship)field; if (relationship.getRelationType().equals(RelationTypeEnum.Inner) || relationship.getRelationType().equals(RelationTypeEnum.Embedded)) { service.delete(toEntity, context); } else { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.setFieldValue(refField, Arrays.asList(toEntity)); } else { opEntity.setFieldValue(refField, toEntity); } final CMSClientContext fContext = context; retryRelationshipOperation(service, logger, context, new Callable<Integer>() { @Override public Integer call() throws Exception { String query = String.format(RELATIONSHIP_QUERY_TEMPLATE, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_id()); CMSQuery cmsQuery = new CMSQuery(query); CMSQueryResult<GenericCMSEntity> ge = service.query(cmsQuery, fContext); if (ge.getEntities().size() == 0) { return 0; } service.deleteField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "delete"); } }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void deleteRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); Preconditions.checkArgument(!StringUtils.isEmpty(refField), String.format("refField could not be null!")); T getFrom = getEntity(fromEntity, context); if (getFrom == null) { return; } K getTo = getEntity(toEntity, context); if (getTo == null) { return; } final String fromType = fromEntity.get_type(); MetaClass meta = getMetadata(fromType); final MetaField field = meta.getField(refField); if (! (field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relationship = (MetaRelationship)field; if (relationship.getRelationType().equals(RelationTypeEnum.Inner) || relationship.getRelationType().equals(RelationTypeEnum.Embedded)) { service.delete(toEntity, context); } else { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.setFieldValue(refField, Arrays.asList(toEntity)); } else { opEntity.setFieldValue(refField, toEntity); } final CMSClientContext fContext = context; retryRelationshipOperation(service, logger, context, new Callable<Integer>() { @Override public Integer call() throws Exception { String query = String.format(RELATIONSHIP_QUERY_TEMPLATE, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_id()); CMSQuery cmsQuery = new CMSQuery(query); CMSQueryResult<GenericCMSEntity> ge = service.query(cmsQuery, fContext); if (ge.getEntities().size() == 0) { return 0; } service.deleteField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "delete"); } } }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void deleteRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); Preconditions.checkArgument(!StringUtils.isEmpty(refField), String.format("refField could not be null!")); T getFrom = getEntity(fromEntity, context); if (getFrom == null) { return; } K getTo = getEntity(toEntity, context); if (getTo == null) { return; } final String fromType = fromEntity.get_type(); MetaClass meta = getMetadata(fromType); final MetaField field = meta.getField(refField); if (! (field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relationship = (MetaRelationship)field; if (relationship.getRelationType().equals(RelationTypeEnum.Inner) || relationship.getRelationType().equals(RelationTypeEnum.Embedded)) { service.delete(toEntity, context); } else { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.setFieldValue(refField, Arrays.asList(toEntity)); } else { opEntity.setFieldValue(refField, toEntity); } final CMSClientContext fContext = context; retryRelationshipOperation(service, logger, context, new Callable<Integer>() { @Override public Integer call() throws Exception { String query = String.format(RELATIONSHIP_QUERY_TEMPLATE, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_id()); CMSQuery cmsQuery = new CMSQuery(query); CMSQueryResult<GenericCMSEntity> ge = service.query(cmsQuery, fContext); if (ge.getEntities().size() == 0) { return 0; } service.deleteField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "delete"); } } RelationshipService(CMSClientService service); }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void deleteRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); Preconditions.checkArgument(!StringUtils.isEmpty(refField), String.format("refField could not be null!")); T getFrom = getEntity(fromEntity, context); if (getFrom == null) { return; } K getTo = getEntity(toEntity, context); if (getTo == null) { return; } final String fromType = fromEntity.get_type(); MetaClass meta = getMetadata(fromType); final MetaField field = meta.getField(refField); if (! (field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relationship = (MetaRelationship)field; if (relationship.getRelationType().equals(RelationTypeEnum.Inner) || relationship.getRelationType().equals(RelationTypeEnum.Embedded)) { service.delete(toEntity, context); } else { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.setFieldValue(refField, Arrays.asList(toEntity)); } else { opEntity.setFieldValue(refField, toEntity); } final CMSClientContext fContext = context; retryRelationshipOperation(service, logger, context, new Callable<Integer>() { @Override public Integer call() throws Exception { String query = String.format(RELATIONSHIP_QUERY_TEMPLATE, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_id()); CMSQuery cmsQuery = new CMSQuery(query); CMSQueryResult<GenericCMSEntity> ge = service.query(cmsQuery, fContext); if (ge.getEntities().size() == 0) { return 0; } service.deleteField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "delete"); } } RelationshipService(CMSClientService service); void createRelationship(final T fromEntity, final String refField,
            final K toEntity, CMSClientContext context); @Deprecated void createRelationship(T fromEntity, RelationshipField<T, K> field, K toEntity, CMSClientContext context); void deleteRelationship(final T fromEntity, final String refField,
            final K toEntity, CMSClientContext context); @Deprecated void deleteRelationship(T fromEntity, RelationshipField<T, K>field, K toEntity, CMSClientContext context); MetaClass getMetadata(String fromType); }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void deleteRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); Preconditions.checkArgument(!StringUtils.isEmpty(refField), String.format("refField could not be null!")); T getFrom = getEntity(fromEntity, context); if (getFrom == null) { return; } K getTo = getEntity(toEntity, context); if (getTo == null) { return; } final String fromType = fromEntity.get_type(); MetaClass meta = getMetadata(fromType); final MetaField field = meta.getField(refField); if (! (field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relationship = (MetaRelationship)field; if (relationship.getRelationType().equals(RelationTypeEnum.Inner) || relationship.getRelationType().equals(RelationTypeEnum.Embedded)) { service.delete(toEntity, context); } else { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.setFieldValue(refField, Arrays.asList(toEntity)); } else { opEntity.setFieldValue(refField, toEntity); } final CMSClientContext fContext = context; retryRelationshipOperation(service, logger, context, new Callable<Integer>() { @Override public Integer call() throws Exception { String query = String.format(RELATIONSHIP_QUERY_TEMPLATE, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_id()); CMSQuery cmsQuery = new CMSQuery(query); CMSQueryResult<GenericCMSEntity> ge = service.query(cmsQuery, fContext); if (ge.getEntities().size() == 0) { return 0; } service.deleteField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "delete"); } } RelationshipService(CMSClientService service); void createRelationship(final T fromEntity, final String refField,
            final K toEntity, CMSClientContext context); @Deprecated void createRelationship(T fromEntity, RelationshipField<T, K> field, K toEntity, CMSClientContext context); void deleteRelationship(final T fromEntity, final String refField,
            final K toEntity, CMSClientContext context); @Deprecated void deleteRelationship(T fromEntity, RelationshipField<T, K>field, K toEntity, CMSClientContext context); MetaClass getMetadata(String fromType);  }