use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class CrnkClient method allocateRepository.
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T, I extends Serializable> RegistryEntry allocateRepository(Class<T> resourceClass) {
ResourceInformationProvider resourceInformationProvider = moduleRegistry.getResourceInformationBuilder();
ResourceInformation resourceInformation = resourceInformationProvider.build(resourceClass);
final ResourceRepositoryStub<T, I> repositoryStub = new ResourceRepositoryStubImpl<>(this, resourceClass, resourceInformation, urlBuilder);
// create interface for it!
RepositoryInstanceBuilder repositoryInstanceBuilder = new RepositoryInstanceBuilder(null, null) {
@Override
public Object buildRepository() {
return repositoryStub;
}
};
ResourceRepositoryInformation repositoryInformation = new ResourceRepositoryInformationImpl(resourceInformation.getResourceType(), resourceInformation, RepositoryMethodAccess.ALL);
ResourceEntry resourceEntry = new DirectResponseResourceEntry(repositoryInstanceBuilder, repositoryInformation);
Map<ResourceField, ResponseRelationshipEntry> relationshipEntries = new HashMap<>();
RegistryEntry registryEntry = new RegistryEntry(resourceEntry, relationshipEntries);
registryEntry.initialize(moduleRegistry);
resourceRegistry.addEntry(resourceClass, registryEntry);
allocateRepositoryRelations(registryEntry);
return registryEntry;
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class ClientResourceUpsert method setLinks.
protected void setLinks(Resource dataBody, Object instance, ResourceInformation resourceInformation) {
ResourceField linksField = resourceInformation.getLinksField();
if (dataBody.getLinks() != null && linksField != null) {
JsonNode linksNode = dataBody.getLinks();
Class<?> linksClass = linksField.getType();
ObjectReader linksMapper = objectMapper.readerFor(linksClass);
try {
Object links = linksMapper.readValue(linksNode);
linksField.getAccessor().setValue(instance, links);
} catch (IOException e) {
throw new ResponseBodyException("failed to parse links information", e);
}
}
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class ClientResourceUpsert method setRelationsField.
@Override
protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
Relationship relationship = property.getValue();
if (!relationship.getData().isPresent()) {
ObjectNode links = relationship.getLinks();
if (links != null) {
// create proxy to lazy load relations
String fieldName = property.getKey();
ResourceInformation resourceInformation = registryEntry.getResourceInformation();
ResourceField field = resourceInformation.findRelationshipFieldByName(fieldName);
Class elementType = field.getElementType();
Class collectionClass = field.getType();
JsonNode relatedNode = links.get("related");
if (relatedNode != null) {
String url = null;
if (relatedNode.has(SerializerUtil.HREF)) {
JsonNode hrefNode = relatedNode.get(SerializerUtil.HREF);
if (hrefNode != null) {
url = hrefNode.asText().trim();
}
} else {
url = relatedNode.asText().trim();
}
Object proxy = proxyFactory.createCollectionProxy(elementType, collectionClass, url);
field.getAccessor().setValue(newResource, proxy);
}
}
} else {
// set elements
super.setRelationsField(newResource, registryEntry, property, queryAdapter, parameterProvider);
}
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class JpaResourceInformationProvider method build.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public ResourceInformation build(final Class<?> resourceClass) {
String resourceType = getResourceType(resourceClass);
MetaDataObject meta = metaProvider.discoverMeta(resourceClass).asDataObject();
DefaultResourceInstanceBuilder instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
List<ResourceField> fields = getResourceFields(resourceClass);
Class<?> superclass = resourceClass.getSuperclass();
String superResourceType = superclass != Object.class && superclass.getAnnotation(MappedSuperclass.class) == null ? context.getResourceType(superclass) : null;
TypeParser typeParser = context.getTypeParser();
ResourceInformation info = new ResourceInformation(typeParser, resourceClass, resourceType, superResourceType, instanceBuilder, fields, new OffsetLimitPagingBehavior());
info.setValidator(new JpaOptimisticLockingValidator(meta));
info.setIdStringMapper(new JpaIdMapper(meta));
return info;
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class PagingSpecUrlBuilder method build.
public String build(QueryAdapter queryAdapter) {
JsonApiUrlBuilder urlBuilder = new JsonApiUrlBuilder(resourceRegistry);
Object relationshipSourceId = requestSpec.getId();
ResourceField relationshipField = requestSpec.getRelationshipField();
ResourceInformation rootInfo;
if (relationshipField == null) {
rootInfo = queryAdapter.getResourceInformation();
} else {
rootInfo = relationshipField.getParentResourceInformation();
}
return urlBuilder.buildUrl(rootInfo, relationshipSourceId, queryAdapter, relationshipField != null ? relationshipField.getJsonName() : null);
}
Aggregations