Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

This section describes key classes which are essential while using Slice. They constitute a base for a day-to-day development.

ModelProvider

The com.cognifide.slice.api.provider.ModelProvider interface is a basic interface of Slice. It allows you to fetch an injectable object(s) mapped from specified resource or path. It's very similar to Guice com.google.inject.Injector because it is used for fetching objects of different classes. However it's more Sling-related because it always takes resource (or path) as a parameter, so that an object of the specified class can be mapped. You should use ModelProvider whenever there is a need for injecting a model mapped from a given resource (path).

ModelProvider allows you to fetch either a single object of a specified class or a list of objects of a specified class. During execution of methods of this interface, the execution context is modified, meaning that the specified path is put on the top of the execution stack. This allows other objects to inject the specified resource (or path) and use it as a current resource. Read more about execution stack  here.

Below table describes methods of the interface.

MethodDescription
<T> T get(Class<T> type, String path);

Creates an object of class type and maps it from a resource from under specified path

<T> T get(Class<T> type, Resource resource);Creates an object of class type and maps it from the resource
<T> T get(com.google.inject.Key<T> key, String path) throws ClassNotFoundException;
Creates an object identified by key type and maps it from the resource under specified path
<T> T get(com.google.inject.Key<T> key, Resource resource);Creates an object identified by key type and maps it from the resource under specified path
<T> List<T> getChildModels(Class<T> type, String parentPath);
Creates a list of objects of class type and maps it from the resources under specified parent path
<T> List<T> getChildModels(Class<T> type, Resource parentResource);Creates a list of objects of class type and maps it from the resources under specified parent resource
Object get(String className, String path) throws ClassNotFoundException;Creates an object of a class defined by specified className String and maps it from a resource from under specified path
Object get(String className, Resource resource) throws ClassNotFoundException;Creates an object of a class defined by specified className String and maps it from the resource
<T> List<T> getList(Class<T> type, Iterator<String> paths);Creates a list of objects of class type and maps them accordingly from resources from under paths defined by paths iterator.
<T> List<T> getList(Class<T> type, String[] paths);

Creates a list of objects of class type and maps them accordingly from resources from under paths defined by paths array.

<T> List<T> getListFromResources(Class<T> type, Iterator<Resource> resources);Creates a list of objects of class type and maps it from the resource returned by iterator

ModelProvider can be injected to your models, so that you can read a model from an arbitrary resource or path, e.g.:

import com.cognifide.app.util.Currency;

import com.cognifide.slice.api.provider.ModelProvider;
import com.cognifide.slice.mapper.annotation.JcrProperty;
import com.cognifide.slice.mapper.annotation.SliceResource;
import com.google.inject.Inject;

@SliceResource
public class OrderModel {

    private final static String CONFIGURATION_PATH = "/content/app/configuration/jcr:content/currency";

    private final ModelProvider modelProvider;

    @JcrProperty
    private int value;

    @Inject
    public OrderModel(ModelProvider modelProvider) {
        this.modelProvider = modelProvider;
    }

    public int getValue() {
        Currency currency = modelProvider.get(Currency.class, CONFIGURATION_PATH);
        return formatToCurrency(value, currency);
    }
    ...
}

@Deprecarted

Few API members have been deprecated:

DeprecatedSuggestion

com.cognifide.slice.api.provider.ChildrenProvider


Use one of ModelProvider's methods:

  • <T> List<T> getChildModels(Class<T> type, String parentPath);
  • <T> List<T> getChildModels(Class<T> type, Resource parentResource);
  • <T> List<T> getList(Class<T> type, String[] paths);
  • <T> List<T> getList(Class<T> type, Iterator<String> paths);

com.cognifide.slice.api.provider.ModelProvider

  • getChildren(String path)
  • getChildResources(String path)
Use Resource.listChildren()
com.cognifide.slice.api.link.*

Whole package has been removed in Slice 4.0.0

Feel free to extract code from Slice 3.2.0 source code: https://github.com/Cognifide/Slice/tree/3.2.0

ClassToKeyMapper

The com.cognifide.slice.api.provider.ClassToKeyMapper interface allows you to obtain a com.google.inject.Key associated with a class specified by a String value. Thanks to this you can inject objects of classes specified by Strings rather than classes. In general, this interface is not widely used.

  • No labels