Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

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

...

@Deprecarted

Few API members have been deprecated:

DeprecatedSuggestion

com.cognifide.slice.api.provider.ChildrenProvider

...

Below table describes methods of the interface.

MethodDescription
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

...


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.

...

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 LinkBuilderLinkBuilder 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();
    }
    ...
}

...

provider.ModelProvider

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

...

titleBest practice

...

*

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.