Persist your data using Slice 4.3
20 May 2016
Krystian Panek
Frink_Cognifide_2016_HeaderImages_0117

Calling all Slice users!

I'm very happy to announce that the latest version of Slice framework has just been released. Version 4.3 comes with completely new features and stability fixes.  Read all about it here.


Slice Persistence

Now you can save your data into a repository using Slice. As in Java Persistence API, Slice allows two-way data processing. Use your existing Slice models and save them back into the Sling repository.

Features

  • Saves simple field types (primitives & wrappers) as JCR properties (for instance: String, Integer, Calendar, InputStream)
  • 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

Usage

Consider the following model:

@SliceResourcepublic
 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
      //...
}

How do you save it into a repository just as it is? Use the new OSGi service, ModelPersister:

@Injectprivate
 ModelPersister modelPersister;

@Injectprivate
 Resource resource;

void updatePageLimit() {
    SampleModel model = resource.adaptTo(SampleModel.class);
    model.setPageLimit(123);
    modelPersister.persist(model, resource);
    resource.getResourceResolver().commit();
}

Extending mapper made easy

Extending the OOTB mapper is now much easier. Thanks to Guice multibindings you can easily register your own Field Processor which can map a field in a custom way. The code snippet below demonstrates how you can register your own field processor, MyCustomFieldProcessor, using multibinding:

public class CustomProcessorModule implements Module {

   @Override
   protected void configure() {
      Multibinder<FieldProcessor> multibinder = Multibinder.newSetBinder(binder(), FieldProcessor.class);
      multibinder.addBinding().to(MyCustomFieldProcessor.class);
   }
}


@PreMapping & @PostMapping annotations

Now, if you are annotating a model with @SliceResource you can annotate one method with @PreMapping and another with @PostMapping annotation. These will be called before or after mapping is done respectively.

Also note that in exceptional cases, you can still have both annotations on a single method:

@SliceResourcepublic 
class MyComponentModel {

   @JcrProperty
  private String property;

   @Inject
  private ModelProvider modelProvider;

   @PreMapping
  public void preMapping() {
    // modelProvider is already injected; property is not mapped yet
  }

   @PostMapping
  public void postMapping() {
    // modelProvider and property are already set
  }
}


Improvement to @Children annotation

Now, using @Children, you can annotate not only java.lang.List, but also java.lang.Set, java.lang.SortedSet or java.lang.Collection.

@SliceResource
public class MyComponentModel {

    @Chldren(ChildModel.class)
    @JcrProperty  
    private Set<ChildModel> childModels;
    // ...
}



Bug fixing

Slice 4.3 also features fixes for some of the minor bugs. You can find a full list of them in the release notes.


What's next?

In the near future, we are planning to create an add on for JAX-RS resources. Also our existing add ons for AEM will achieve support for e.g targeting and versioning. 

Maybe you have interesting ideas for new add ons for Slice?  If so, we would like to encourage you to collaborate on Slice project on GitHub

Finally, stay tuned! Slice 5.0 is coming.


Useful links

Get in touch

Name
Name
*
Company
Company
*
Email
Email
*
I'd like a demo of
*
Notes
Notes