Search code examples
javajcrmagnolia

Is my code to retrieve a content object and property from a JCR repository valid?


I am finishing up a book for O'Reilly (http://flyingsquirrelbook.com/).

In this book, I compare several examples of code to (1) retrieve a content object from a repository, and (2) retrieve a single property/attribute from that object. I do this to demonstrate the APIs of different CMS are different, yet somewhat similar in logic. I have examples from C# (Episerver), PHP (Concrete5), Python (Plone), and Magnolia (Java, using JCR).

I have validated the first three, but I have no experience with Java or JCR. I need someone to look at this code and tell me if this is a reasonably accepted way to do what I state above:

Session session = MgnlContext.getJCRSession("myWorkspace");
Node myPage = session.getNodeByIdentifier("123");
String title = myPage.getProperty("PageTitle").getValue().getString() 

Is that egregiously wrong, for any reason?


Solution

  • While you wouldn't get compiler complaining about the code, there's few things you can do to improve on example.
    getNodeByIdentifier() method takes node if in UUID format, so you should show same instead of using simple Int as Id.
    Similarly property names typically start with lowercase.
    And last call to getValue() is unnecessary. You can call getString() directly on property object.

    HTH,
    Jan