Slice Persistence - 4.4

Purpose

Slice persistence is an OSGi module that allows to save Slice models back into Sling repository.

Features

  • Saves simple field types as JCR properties (StringintCalendar, etc.)
  • Supports nested models - will be persisted as subresources
  • Allows for customizing the property name with @JcrProperty
  • Supports collections and arrays of properties
  • Supports collections and arrays of nested models with Slice @Children

Prerequisites

  • AEM 6

Installation

Add dependency to your project:

<dependency>
    <groupId>com.cognifide.slice</groupId>
    <artifactId>slice-persistence-api</artifactId>
    <version>4.4.0</version>
    <scope>provided</scope>
</dependency>

Usage

Considering following model:

@SliceResource
public class SampleModel {

    @JcrProperty
    private int pageLimit;

    @JcrProperty
    private PaginationType type; // PaginationType is an enum

    @JcrProperty
    private String[] tags;

    @JcrProperty
    @Children(SubModel.class)
    private List<SubModel> subModels;

    private String notAJcrProperty; // won't be serialized

    //...
}

Model can be persisted as follows:

@Inject
private ModelPersister modelPersister;

//...

SampleModel model = resource.adaptTo(SampleModel.class); // inject SampleModel with Slice
model.setPageLimit(123);
modelPersister.persist(model, resource);
resource.getResourceResolver().commit();