use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class JpaResourceInformationProviderTest method testManyToManyRelation.
@Test
public void testManyToManyRelation() {
ResourceInformation info = builder.build(ManyToManyTestEntity.class);
ResourceField field = info.findRelationshipFieldByName("opposites");
Assert.assertEquals(ResourceFieldType.RELATIONSHIP, field.getResourceFieldType());
Assert.assertEquals("manyToManyOpposite", field.getOppositeResourceType());
Assert.assertEquals(SerializeType.LAZY, field.getSerializeType());
// TODO consider bidirectiona assignment Assert.assertEquals("tests", field.getOppositeName());
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class JpaResourceInformationProviderTest method testOneToOneRelation.
@Test
public void testOneToOneRelation() {
ResourceInformation info = builder.build(OneToOneTestEntity.class);
ResourceField field = info.findRelationshipFieldByName("oneRelatedValue");
Assert.assertEquals(ResourceFieldType.RELATIONSHIP, field.getResourceFieldType());
Assert.assertEquals("related", field.getOppositeResourceType());
Assert.assertEquals(SerializeType.LAZY, field.getSerializeType());
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class BraveUtil method getComponentName.
public static String getComponentName(RepositoryRequestSpec request) {
ResourceField relationshipField = request.getRelationshipField();
StringBuilder pathBuilder = new StringBuilder();
String method = request.getMethod().toString();
pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME);
pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
pathBuilder.append(method);
pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
pathBuilder.append("/");
if (relationshipField == null) {
pathBuilder.append(request.getQueryAdapter().getResourceInformation().getResourceType());
} else {
pathBuilder.append(relationshipField.getParentResourceInformation().getResourceType());
}
Iterable<Object> ids = request.getIds();
if (ids != null) {
pathBuilder.append("/");
pathBuilder.append(StringUtils.join(",", ids));
}
if (relationshipField != null) {
pathBuilder.append("/");
pathBuilder.append(relationshipField.getJsonName());
}
return pathBuilder.toString();
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class PathBuilder method getNonResourcePath.
private JsonPath getNonResourcePath(JsonPath previousJsonPath, String elementName, boolean relationshipMark) {
String previousElementName = previousJsonPath.getElementName();
RegistryEntry previousEntry = resourceRegistry.getEntry(previousElementName);
ResourceInformation resourceInformation = previousEntry.getResourceInformation();
List<ResourceField> resourceFields = resourceInformation.getRelationshipFields();
for (ResourceField field : resourceFields) {
if (field.getJsonName().equals(elementName)) {
if (relationshipMark) {
return new RelationshipsPath(elementName);
} else {
return new FieldPath(elementName);
}
}
}
// TODO: Throw different exception? element name can be null..
throw new ResourceFieldNotFoundException(elementName);
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class RelationshipsResourceGet method handle.
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getResourceType();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
String elementName = jsonPath.getElementName();
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
verifyFieldNotNull(relationshipField, elementName);
boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());
RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
JsonApiResponse entities;
if (isCollection) {
entities = relationshipRepositoryForClass.findManyTargets(castedResourceId, relationshipField, queryAdapter);
} else {
entities = relationshipRepositoryForClass.findOneTarget(castedResourceId, relationshipField, queryAdapter);
}
Document responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
// return explicit { data : null } if values found
if (!responseDocument.getData().isPresent()) {
responseDocument.setData(Nullable.nullValue());
}
return new Response(responseDocument, 200);
}
Aggregations