Persisting Sling Model / OCM

Avatar

Avatar

jamiec4451712

Avatar

jamiec4451712

jamiec4451712

02-11-2018

I'm having a really hard time understanding what options there are out there for writing Java objects back to the JCR in some organized fashion. I came across Jackrabbit OCM (Object Content Mapping), but I can't for the life of me find any information on how it can be installed into AEM. I checked the depfinder and it's not already bundled, so I tried to find the JAR online, but there's no Maven repo for it (and it's not in the Adobe Public repo). I'm now trying to compile from source but once I hit that wall I thought there must be another way.

I've used Sling Models before, but I can't find a decent way to just map those models back to JCR nodes with normal operations (add, update, delete). I also ran into neba, but it appears that it works the same way as Sling Models and there is an issue that was opened and then closed asking for a persistence feature.

Overall, I've been a bit frustrated with the availability of APIs for AEM to work with custom, complex data types with the JCR. It's a waste of time and energy to write custom implementations for simple models, and not every case is a good fit for OOTB APIs like the PageManager.

I have also seen examples of people making their Sling Model adaptable to a ModifiableValueMap or something, but this doesn't work very well for nested objects.

Let's look at a real use case. I have some abstract data in a JSON object:

   "name":"John",

   "age":30,

   "cars":{

      "car1": { 

            "make":"Ford",

            "model":"Fiesta",

      },

      "car2": { 

            "make":"Honda",

            "model":"Accord",

      }

   }

}

And some POJO:

public class Person {

    public String path;

    public String name;

    public int age;

    public List<Car> cars;

}

public class Car {

    public String path;

    public String name;

    public String make;

    public String model;

}

I want to map this data to a POJO using GSON, then write that Java class to the JCR using some simple method. The JCR tree would look something like:

└── people

    └── John

        └── cars

            ├── car1

            └── car2

. I then want to be able to reverse that method, mapping the JCR nodes back into the POJO. Like Sling Models, PostConstruct methods and other features would be great. It seems like the Jackrabbit OCM package has these features, but there's no documentation on using it in AEM (and no clear path to bundling the JAR manually).

In the Jackrabbit OCM package, this would probably look something like this:

// this would come from GSON

Person person = new Person();

// Would build setters for this stuff

person.path = String.format("./people/%s", person.name);

// Create

ocm.insert(person);

ocm.save();

// Retrieve

person = (person) ocm.getObject("./people/John");

// Delete

ocm.remove(person);

ocm.save();

View Entire Topic

Avatar

Avatar

jamiec4451712

Avatar

jamiec4451712

jamiec4451712

14-11-2018

Hey Brendan,

Thanks for the in depth reply. I’ve added the dependencies and modified the maven-bundle-plugin, but I’m still struggling to get OCM installed in AEM.

I see the pom for jackrabbit-ocm downloading when I install, yet my autocomplete is not working in IntelliJ and the ocm reference is null when I debug. The project does compile.

The OCM package also does not show up in the AEM depfinder, which leads me to believe this is why the actual reference to the OCM reference is null. Are you able to find this in the depfinder on a working instance?

Thanks!