use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourceUpsert method getRequestBody.
protected Resource getRequestBody(Document requestDocument, JsonPath path, HttpMethod method) {
String resourceType = path.getResourceType();
assertRequestDocument(requestDocument, method, resourceType);
if (!requestDocument.getData().isPresent() || requestDocument.getData().get() == null) {
throw new RequestBodyException(method, resourceType, "No data field in the body.");
}
if (requestDocument.getData().get() instanceof Collection) {
throw new RequestBodyException(method, resourceType, "Multiple data in body");
}
Resource resourceBody = (Resource) requestDocument.getData().get();
RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
if (bodyRegistryEntry == null) {
throw new RepositoryNotFoundException(resourceBody.getType());
}
return resourceBody;
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class PathBuilder method build.
/**
* Parses path provided by the application. The path provided cannot contain neither hostname nor protocol. It
* can start or end with slash e.g. <i>/tasks/1/</i> or <i>tasks/1</i>.
*
* @param path Path to be parsed
* @return doubly-linked list which represents path given at the input
*/
public JsonPath build(String path) {
String[] strings = splitPath(path);
if (strings.length == 0 || (strings.length == 1 && "".equals(strings[0]))) {
return null;
}
JsonPath previousJsonPath = null, currentJsonPath = null;
PathIds pathIds;
boolean relationshipMark;
String elementName;
String actionName;
int currentElementIdx = 0;
while (currentElementIdx < strings.length) {
elementName = null;
pathIds = null;
actionName = null;
relationshipMark = false;
if (RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
relationshipMark = true;
currentElementIdx++;
}
RegistryEntry entry = null;
if (currentElementIdx < strings.length && !RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
elementName = strings[currentElementIdx];
// support "/" in resource type to group repositories
StringBuilder potentialResourceType = new StringBuilder();
for (int i = 0; currentElementIdx + i < strings.length; i++) {
if (potentialResourceType.length() > 0) {
potentialResourceType.append("/");
}
potentialResourceType.append(strings[currentElementIdx + i]);
entry = resourceRegistry.getEntry(potentialResourceType.toString());
if (entry != null) {
currentElementIdx += i;
elementName = potentialResourceType.toString();
break;
}
}
currentElementIdx++;
}
if (currentElementIdx < strings.length && entry != null && entry.getRepositoryInformation().getActions().containsKey(strings[currentElementIdx])) {
// repository action
actionName = strings[currentElementIdx];
currentElementIdx++;
} else if (currentElementIdx < strings.length && !RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
// ids
pathIds = createPathIds(strings[currentElementIdx]);
currentElementIdx++;
if (currentElementIdx < strings.length && entry != null && entry.getRepositoryInformation().getActions().containsKey(strings[currentElementIdx])) {
// resource action
actionName = strings[currentElementIdx];
currentElementIdx++;
}
}
if (previousJsonPath != null) {
currentJsonPath = getNonResourcePath(previousJsonPath, elementName, relationshipMark);
if (pathIds != null) {
throw new ResourceException("RelationshipsPath and FieldPath cannot contain ids");
}
} else if (entry != null && !relationshipMark) {
currentJsonPath = new ResourcePath(elementName);
} else {
return null;
}
if (pathIds != null) {
currentJsonPath.setIds(pathIds);
}
if (actionName != null) {
ActionPath actionPath = new ActionPath(actionName);
actionPath.setParentResource(currentJsonPath);
currentJsonPath = actionPath;
}
if (previousJsonPath != null) {
currentJsonPath.setParentResource(previousJsonPath);
}
previousJsonPath = currentJsonPath;
}
return currentJsonPath;
}
use of io.crnk.core.engine.registry.RegistryEntry 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.registry.RegistryEntry in project crnk-framework by crnk-project.
the class IncludeLookupSetter method setupRelationId.
private void setupRelationId(Resource sourceResource, ResourceField relationshipField, Object targetEntityId) {
// set the relation
String relationshipName = relationshipField.getJsonName();
Map<String, Relationship> relationships = sourceResource.getRelationships();
Relationship relationship = relationships.get(relationshipName);
String oppositeType = relationshipField.getOppositeResourceType();
RegistryEntry entry = Objects.requireNonNull(resourceRegistry.getEntry(oppositeType));
ResourceInformation targetResourceInformation = entry.getResourceInformation();
if (targetEntityId instanceof Iterable) {
List<ResourceIdentifier> targetIds = new ArrayList<>();
for (Object targetElementId : (Iterable<?>) targetEntityId) {
targetIds.add(util.idToResourceId(targetResourceInformation, targetElementId));
}
relationship.setData(Nullable.of((Object) targetIds));
} else {
ResourceIdentifier targetResourceId = util.idToResourceId(targetResourceInformation, targetEntityId);
relationship.setData(Nullable.of((Object) targetResourceId));
}
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class IncludeLookupSetter method lookupRelatedResourceWithRelationship.
private Set<Resource> lookupRelatedResourceWithRelationship(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
ResourceInformation resourceInformation = relationshipField.getParentResourceInformation();
RegistryEntry registyEntry = resourceRegistry.getEntry(resourceInformation.getResourceType());
List<Serializable> resourceIds = getIds(sourceResources, resourceInformation);
boolean isMany = Iterable.class.isAssignableFrom(relationshipField.getType());
Set<Resource> loadedTargets = new HashSet<>();
@SuppressWarnings("rawtypes") RelationshipRepositoryAdapter relationshipRepository = registyEntry.getRelationshipRepository(relationshipField, parameterProvider);
if (relationshipRepository != null) {
Map<Object, JsonApiResponse> responseMap;
if (isMany) {
responseMap = relationshipRepository.findBulkManyTargets(resourceIds, relationshipField, queryAdapter);
} else {
responseMap = relationshipRepository.findBulkOneTargets(resourceIds, relationshipField, queryAdapter);
}
for (Resource sourceResource : sourceResources) {
Serializable sourceId = resourceInformation.parseIdString(sourceResource.getId());
JsonApiResponse targetResponse = responseMap.get(sourceId);
if (targetResponse != null && targetResponse.getEntity() != null) {
Object targetEntity = targetResponse.getEntity();
List<Resource> targets = setupRelation(sourceResource, relationshipField, targetEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
loadedTargets.addAll(targets);
} else {
Nullable<Object> emptyData = (Nullable) Nullable.of(Iterable.class.isAssignableFrom(relationshipField.getType()) ? Collections.emptyList() : null);
Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
relationship.setData(emptyData);
}
}
} else {
throw new RepositoryNotFoundException("no relationship repository found for " + resourceInformation.getResourceType() + "." + relationshipField.getUnderlyingName());
}
return loadedTargets;
}
Aggregations