Carve is a Sling Model Object Relational Mapper that aims to simplify writing to data with Sling Models. Jetpack Carve aims to provide an ORM-like experience like Hibernate offers for SQL-based systems.
Converting resources into Sling Models is not new. With Carve, Sling Models can write the containing information back to the repository. The location is configurable on the CarveModel annotation and the structure inside this location can be determined with a PolicyProvider:
ChildResources are also supported, which will create a sub-node.
Add the following dependency to your pom.xml file:
<dependency>
<groupId>be.ida-mediafoundry.jetpack</groupId>
<artifactId>carve.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
Create a Sling Model, which is also a CarveModel:
@CarveModel(pathPolicyProvider = SimplePathPolicyProvider.class, location = "/etc/patches/completed")
@Model(adaptables = Resource.class)
public class PatchResult {
@CarveId
@Inject
private String id;
@Inject
@Named("patch-status")
private String status;
public PatchResult() {
}
public PatchResult(String id, String status) {
this.id = id;
this.status = status;
}
public String getId() {
return id;
}
public String getStatus() {
return status;
}
}
Use the ModelManager to persist or retrieve the Sling Models:
import be.ida_mediafoundry.jetpack.carve.manager.ModelManager;
...
@Reference
private ModelManager modelManager;
public PatchResult getResult(String id) throws ModelManagerException {
return modelManager.retrieve(PatchResult.class, id);
}
public void createResult(String id) throws ModelManagerException {
PatchResult patchResult = new PatchResult(id, "In Progress");
modelManager.persist(patchResult);
}