Search in sources :

Example 6 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class SecurityModule method reconfigure.

/**
 * Applies the new configuration to this module.
 */
public void reconfigure(SecurityConfig config) {
    this.config = config;
    LOGGER.debug("reconfiguring with {} rules", config.getRules().size());
    Map<String, Map<String, ResourcePermission>> newPermissions = new HashMap<>();
    for (SecurityRule rule : config.getRules()) {
        String resourceType = rule.getResourceType();
        if (resourceType == null) {
            Class<?> resourceClass = rule.getResourceClass();
            if (resourceClass != null) {
                resourceType = toType(resourceClass);
            }
        }
        if (resourceType == null) {
            Collection<RegistryEntry> entries = context.getResourceRegistry().getResources();
            for (RegistryEntry entry : entries) {
                String entryResourceType = entry.getResourceInformation().getResourceType();
                configureRule(newPermissions, entryResourceType, rule.getRole(), rule.getPermission());
            }
        } else {
            configureRule(newPermissions, resourceType, rule.getRole(), rule.getPermission());
        }
    }
    this.permissions = newPermissions;
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 7 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class QuerySpecAdapter method getResourceType.

private String getResourceType(QuerySpec spec) {
    if (spec.getResourceType() != null) {
        return spec.getResourceType();
    }
    RegistryEntry entry = resourceRegistry.getEntry(spec.getResourceClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return resourceInformation.getResourceType();
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 8 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method setRelation.

@Override
public void setRelation(T source, J targetId, String fieldName) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = getSourceEntry().getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        field.getIdAccessor().setValue(source, targetId);
    } else {
        RegistryEntry targetEntry = getTargetEntry(field);
        D target = getTarget(targetEntry, targetId);
        field.getAccessor().setValue(source, target);
    }
    sourceAdapter.update(source, getSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 9 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method addRelations.

@Override
public void addRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = getSourceEntry().getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        Collection currentIds = (Collection) field.getIdAccessor().getValue(source);
        currentIds.addAll((Collection) targetIds);
    } else {
        RegistryEntry targetEntry = getTargetEntry(field);
        Iterable<D> targets = getTargets(targetEntry, targetIds);
        @SuppressWarnings("unchecked") Collection<D> currentTargets = getOrCreateCollection(source, fieldName);
        for (D target : targets) {
            currentTargets.add(target);
        }
    }
    sourceAdapter.update(source, getSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Collection(java.util.Collection) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 10 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = getTargetEntry(field);
    String oppositeName = getOppositeName(fieldName);
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
    idQuerySpec.includeRelation(Arrays.asList(oppositeName));
    ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
    JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
    List<D> results = (List<D>) response.getEntity();
    MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {

        @Override
        protected List<D> newList() {
            return new DefaultResourceList<>();
        }
    };
    Set<I> sourceIdSet = new HashSet<>();
    for (I sourceId : sourceIds) {
        sourceIdSet.add(sourceId);
    }
    for (D result : results) {
        handleTarget(bulkResult, result, sourceIdSet, oppositeName, sourceInformation);
    }
    return bulkResult;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceList(io.crnk.core.resource.list.ResourceList) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6