@Test public void testReference_Generic_allnewlycreate() { GenericCMSEntity genericAs = newGenericAppServ(); GenericCMSEntity genericSi = newGenericServiceInstance(); CMSClientContext context = new CMSClientContext(); raptorService.createRelationship(genericAs, "services", genericSi, context); assertGenericAppAndService(genericAs, genericSi, context); }
public <T extends ICMSEntity, K extends ICMSEntity> void createRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); String fromType = fromEntity.get_type(); MetaClass fromMetaClass = getMetadata(fromType); ensureFromEntity(fromEntity, refField, context); MetaField field = fromMetaClass.getField(refField); if (!(field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relation = (MetaRelationship) field; ensureToEntity(fromEntity, refField, toEntity, context, fromType); if (relation.getRelationType().equals(RelationTypeEnum.Reference)) { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.removeFieldValue(refField); opEntity.addFieldValue(refField, 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.updateEntityField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "create"); } }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void createRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); String fromType = fromEntity.get_type(); MetaClass fromMetaClass = getMetadata(fromType); ensureFromEntity(fromEntity, refField, context); MetaField field = fromMetaClass.getField(refField); if (!(field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relation = (MetaRelationship) field; ensureToEntity(fromEntity, refField, toEntity, context, fromType); if (relation.getRelationType().equals(RelationTypeEnum.Reference)) { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.removeFieldValue(refField); opEntity.addFieldValue(refField, 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.updateEntityField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "create"); } } }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void createRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); String fromType = fromEntity.get_type(); MetaClass fromMetaClass = getMetadata(fromType); ensureFromEntity(fromEntity, refField, context); MetaField field = fromMetaClass.getField(refField); if (!(field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relation = (MetaRelationship) field; ensureToEntity(fromEntity, refField, toEntity, context, fromType); if (relation.getRelationType().equals(RelationTypeEnum.Reference)) { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.removeFieldValue(refField); opEntity.addFieldValue(refField, 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.updateEntityField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "create"); } } RelationshipService(CMSClientService service); }
RelationshipService { public <T extends ICMSEntity, K extends ICMSEntity> void createRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); String fromType = fromEntity.get_type(); MetaClass fromMetaClass = getMetadata(fromType); ensureFromEntity(fromEntity, refField, context); MetaField field = fromMetaClass.getField(refField); if (!(field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relation = (MetaRelationship) field; ensureToEntity(fromEntity, refField, toEntity, context, fromType); if (relation.getRelationType().equals(RelationTypeEnum.Reference)) { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.removeFieldValue(refField); opEntity.addFieldValue(refField, 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.updateEntityField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "create"); } } 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 createRelationship(final T fromEntity, final String refField, final K toEntity, CMSClientContext context) { context = context != null ? context : new CMSClientContext(); String fromType = fromEntity.get_type(); MetaClass fromMetaClass = getMetadata(fromType); ensureFromEntity(fromEntity, refField, context); MetaField field = fromMetaClass.getField(refField); if (!(field instanceof MetaRelationship)) { throw new CMSEntityException(MessageFormat.format("Field {0} on class {1} is not relationship!", refField, fromType)); } MetaRelationship relation = (MetaRelationship) field; ensureToEntity(fromEntity, refField, toEntity, context, fromType); if (relation.getRelationType().equals(RelationTypeEnum.Reference)) { final T opEntity = createNewEntity(fromEntity); if (field.getCardinality() == CardinalityEnum.Many) { opEntity.removeFieldValue(refField); opEntity.addFieldValue(refField, 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.updateEntityField(opEntity, refField, fContext); return 0; } }, opEntity.get_type(), opEntity.get_id(), refField, toEntity.get_type(), toEntity.get_id(), "create"); } } 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);  }