...
Method | Description |
---|---|
<T> T get(final Class<T> type, final String path); | Creates an object of class type and maps it from a resource from under specified path |
<T> T get(final Class<T> type, final Resource resource); | Creates an object of class type and maps it from the resource |
Object get(final String className, final 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 |
<T> List<T> getList(final Class<T> type, final Iterator<String> paths); | Creates a list of objects of class type and maps them accordingly from resources from under paths defined by paths interator. |
<T> List<T> getList(final Class<T> type, final String[] paths); | Creates a list of objects of class type and maps them accordingly from resources from under paths defined by paths array. |
ModelProvider
can be injected to your models, so that you can read a model from an arbitrary resource or path, e.g.:
Code Block |
---|
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);
}
...
} |
ChildrenProvider
The com.cognifide.slice.api.provider.ChildrenProvider
interface allows you to get a list of child resources (paths) from under specified path.
Below table describes methods of the interface.
Method | Description |
---|---|
List<String> getChildren(final String path); | Returns paths to child resources of a resource from under specified path. If a resource under specified path doesn't exist, empty list is returned. The method is not recursive - it reads only direct children. |
List<Resource> getChildResources(final String path); | Returns child resources of a resource from under specified path. If a resource under specified path doesn't exist, empty list is returned. The method is not recursive - it reads only direct children |
Link, LinkBuilder
Thanks to com.cognifide.slice.api.link.Link
and com.cognifide.slice.api.link.LinkBuilder
interfaces you can easily manipulate on Sling links, e.g. setting selectors, extension, query string, path, etc.
They should be used whenever there is a need to modify an existing link (e.g. adding or removing some selectors) or create a new one and then manipulate it.
New link can only be created using LinkBuilder
. LinkBuilder
can be obtained from LinkBuilderFactory which is injectable. An example usage of it is shown in the listings below:
Code Block |
---|
import com.cognifide.slice.api.link.Link;
import com.cognifide.slice.api.link.LinkBuilder;
import com.cognifide.slice.api.link.LinkBuilderFactory;
import com.cognifide.slice.api.qualifier.CurrentResourcePath;
import com.google.inject.Inject;
public class EventModel {
private final LinkBuilderFactory linkBuilderFactory;
private final String currentPath;
@Inject
public EventModel(LinkBuilderFactory linkBuilderFactory, @CurrentResourcePath String currentPath) {
this.linkBuilderFactory = linkBuilderFactory;
this.currentPath = currentPath;
}
public Link getPrintLink() {
LinkBuilder linkBuilder = linkBuilderFactory.getLinkBuilder();
linkBuilder.setPath(currentPath);
linkBuilder.setSelectorString("print");
linkBuilder.setExtension("pdf");
return linkBuilder.toLink();
}
...
} |
Code Block |
---|
import com.cognifide.slice.api.link.Link;
import com.cognifide.slice.api.link.LinkBuilder;
import com.cognifide.slice.api.link.LinkBuilderFactory;
import com.cognifide.slice.cq.qualifier.RequestedLink;
import com.google.inject.Inject;
public class EventModel {
private final LinkBuilderFactory linkBuilderFactory;
private final Link requestedLink;
@Inject
public EventModel(LinkBuilderFactory linkBuilderFactory, @RequestedLink Link requestedLink) {
this.linkBuilderFactory = linkBuilderFactory;
this.requestedLink = requestedLink;
}
public String getPrintLink() {
LinkBuilder linkBuilder = linkBuilderFactory.getLinkBuilder(requestedLink);
linkBuilder.addSelector("print").setExtension("pdf"); // it will retain query string and suffix
return linkBuilder.toString();
}
...
} |
Info | ||
---|---|---|
| ||
There is no need to create |
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.