Search in sources :

Example 1 with ResourceException

use of io.crnk.core.exception.ResourceException in project crnk-framework by crnk-project.

the class ResourceUpsert method setRelationField.

protected void setRelationField(Object newResource, RegistryEntry registryEntry, String relationshipName, Relationship relationship, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    if (relationship.getData().isPresent()) {
        ResourceIdentifier relationshipId = (ResourceIdentifier) relationship.getData().get();
        ResourceField field = registryEntry.getResourceInformation().findRelationshipFieldByName(relationshipName);
        if (field == null) {
            throw new ResourceException(String.format("Invalid relationship name: %s", relationshipName));
        }
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipId = filter.modifyOneRelationship(newResource, field, relationshipId);
        }
        Object relationObject;
        if (relationshipId == null) {
            relationObject = null;
            field.getAccessor().setValue(newResource, relationObject);
        } else {
            RegistryEntry entry = resourceRegistry.getEntry(relationshipId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(relationshipId, idFieldType);
            if (field.hasIdField()) {
                field.getIdAccessor().setValue(newResource, typedRelationshipId);
            }
            if (decideSetRelationObjectField(entry, typedRelationshipId, field)) {
                relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                field.getAccessor().setValue(newResource, relationObject);
            }
        }
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceException(io.crnk.core.exception.ResourceException) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 2 with ResourceException

use of io.crnk.core.exception.ResourceException in project crnk-framework by crnk-project.

the class ResourceUpsert method setRelations.

protected void setRelations(Object newResource, RegistryEntry registryEntry, Resource resource, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, boolean ignoreMissing) {
    if (resource.getRelationships() != null) {
        for (Map.Entry<String, Relationship> entry : resource.getRelationships().entrySet()) {
            String relationshipName = entry.getKey();
            Relationship relationship = entry.getValue();
            if (relationship != null) {
                ResourceInformation resourceInformation = registryEntry.getResourceInformation();
                ResourceField field = resourceInformation.findRelationshipFieldByName(relationshipName);
                if (field == null && ignoreMissing) {
                    continue;
                }
                if (field == null) {
                    throw new ResourceException(String.format("Invalid relationship name: %s for %s", entry.getKey(), resourceInformation.getResourceType()));
                }
                if (field.isCollection()) {
                    // noinspection unchecked
                    setRelationsField(newResource, registryEntry, entry, queryAdapter, parameterProvider);
                } else {
                    // noinspection unchecked
                    setRelationField(newResource, registryEntry, relationshipName, relationship, queryAdapter, parameterProvider);
                }
            }
        }
    }
}
Also used : Relationship(io.crnk.core.engine.document.Relationship) ResourceException(io.crnk.core.exception.ResourceException)

Example 3 with ResourceException

use of io.crnk.core.exception.ResourceException in project crnk-framework by crnk-project.

the class ResourceUpsert method setAttribute.

private void setAttribute(ResourceInformation resourceInformation, Object instance, String attributeName, JsonNode valueNode) {
    ResourceField field = resourceInformation.findAttributeFieldByName(attributeName);
    if (canModifyField(resourceInformation, attributeName, field)) {
        try {
            if (field != null) {
                Type valueType = field.getGenericType();
                Object value;
                if (valueNode != null) {
                    JavaType jacksonValueType = objectMapper.getTypeFactory().constructType(valueType);
                    ObjectReader reader = objectMapper.reader().forType(jacksonValueType);
                    value = reader.readValue(valueNode);
                } else {
                    value = null;
                }
                for (ResourceModificationFilter filter : modificationFilters) {
                    value = filter.modifyAttribute(instance, field, attributeName, value);
                }
                field.getAccessor().setValue(instance, value);
            } else if (resourceInformation.getAnyFieldAccessor() != null) {
                AnyResourceFieldAccessor anyFieldAccessor = resourceInformation.getAnyFieldAccessor();
                Object value = objectMapper.reader().forType(Object.class).readValue(valueNode);
                for (ResourceModificationFilter filter : modificationFilters) {
                    value = filter.modifyAttribute(instance, field, attributeName, value);
                }
                anyFieldAccessor.setValue(instance, attributeName, value);
            }
        } catch (IOException e) {
            throw new ResourceException(String.format("Exception while setting %s.%s=%s due to %s", instance, attributeName, valueNode, e.getMessage()), e);
        }
    }
}
Also used : ResourceRelationshipModificationType(io.crnk.core.engine.filter.ResourceRelationshipModificationType) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) ResourceException(io.crnk.core.exception.ResourceException) IOException(java.io.IOException)

Example 4 with ResourceException

use of io.crnk.core.exception.ResourceException 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;
}
Also used : ResourceException(io.crnk.core.exception.ResourceException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 5 with ResourceException

use of io.crnk.core.exception.ResourceException in project crnk-framework by crnk-project.

the class ResourceInformation method initAny.

private void initAny() {
    final Method jsonAnyGetter = ClassUtils.findMethodWith(resourceClass, JsonAnyGetter.class);
    final Method jsonAnySetter = ClassUtils.findMethodWith(resourceClass, JsonAnySetter.class);
    if (absentAnySetter(jsonAnyGetter, jsonAnySetter)) {
        throw new InvalidResourceException(String.format("A resource %s has to have both methods annotated with @JsonAnySetter and @JsonAnyGetter", resourceClass.getCanonicalName()));
    }
    if (jsonAnyGetter != null) {
        anyFieldAccessor = new AnyResourceFieldAccessor() {

            @Override
            public Object getValue(Object resource, String name) {
                try {
                    return jsonAnyGetter.invoke(resource, name);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new ResourceException(String.format("Exception while reading %s.%s due to %s", resource, name, e.getMessage()), e);
                }
            }

            @Override
            public void setValue(Object resource, String name, Object fieldValue) {
                try {
                    jsonAnySetter.invoke(resource, name, fieldValue);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new ResourceException(String.format("Exception while writting %s.%s=%s due to %s", resource, name, fieldValue, e.getMessage()), e);
                }
            }
        };
    }
}
Also used : InvalidResourceException(io.crnk.core.exception.InvalidResourceException) InvalidResourceException(io.crnk.core.exception.InvalidResourceException) ResourceException(io.crnk.core.exception.ResourceException) Method(java.lang.reflect.Method)

Aggregations

ResourceException (io.crnk.core.exception.ResourceException)5 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)2 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 Relationship (io.crnk.core.engine.document.Relationship)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 ResourceRelationshipModificationType (io.crnk.core.engine.filter.ResourceRelationshipModificationType)1 InvalidResourceException (io.crnk.core.exception.InvalidResourceException)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1