Sling Model - Setting Node Property @PostConstruct

Avatar

Avatar

sud_varun

Avatar

sud_varun

sud_varun

15-09-2016

Hello,

AEM: 6.2

I have a use case where on inject properties of a component, my sling model executes a @PostConstruct method to configure a state of content by setting a node property value. For example:

@PostConstruct
    protected void setProperty() {
            try {
            ModifiableValueMap map = this.resource.adaptTo(ModifiableValueMap.class);
(*)         map.put("customProperty", "initalStateValue");      
            resource.getResourceResolver().commit();
        } catch (PersistenceException e) {
            LOGGER.error(e);
        }
    }

Now, when i publish a page which uses this AEM component, the anonymous user on the published site gets an error message:

Post-construct method has thrown an exception for model class 

On further analysis: I have found that since "anonymous" user does not have permissions to write on /content nodes, the statement marked above with an (*) sets map object to null.

My Question is: Is there a better implementation pattern to achieve the scenario of configuring the state of content node such that the state of the content can be set through node property only for authors?

Replies

Avatar

Avatar

ParitMittal

Avatar

ParitMittal

ParitMittal

16-09-2016

Hi , 

This forum is dedicated for queries related to Adobe DTM. Hence we re-posting your query to AEM Forum

Thanks & Regards

Parit Mittal

Avatar

Avatar

PuzanovsP

MVP

Avatar

PuzanovsP

MVP

PuzanovsP
MVP

16-09-2016

Dear Varun,

Usually, when you do content authoring in AEM, you do set all of the properties such as your "customProperty" via the dialog's[0]. Which automatically send your data to SlingPostServlet[1] that handles saving data to the database.

You normally, should not write data when you generate SlingModel.

In case if you do need to do it, due to your unique and awesome! requirements.

Kindly refer to the following guide[2] on how to allow 'anonymous' to write into your database.

[0] http://www.aemcq5tutorials.com/tutorials/create-dialogs-aem/

[1] https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-po...

[2] http://stackoverflow.com/questions/31350548/resourceresolverfactory-getserviceresourceresolver-throw... (look at accepted answer)

 

Kindest Regards,

Peter

Avatar

Avatar

sud_varun

Avatar

sud_varun

sud_varun

16-09-2016

The "customProperty" is not part of the dialog and hence not added via authoring interface.

Question: "You normally, should not write data when you generate SlingModel."

What if I require to set/unset a property (such as "customProperty") whenever the page is loaded? In such circumstance, without apply business logic of updating properties via SlingModel, is there any other scheme or design I could follow?

Avatar

Avatar

PuzanovsP

MVP

Avatar

PuzanovsP

MVP

PuzanovsP
MVP

16-09-2016

Dear Varun,

Correct, normally as per SlingModel design goals:

  • Entirely annotation driven. "Pure" POJOs.
  • Use standard annotations where possible.
  • Pluggable
  • OOTB, support resource properties (via ValueMap), SlingBindings, OSGi services, request attributes
  • Adapt multiple objects - minimal required Resource and SlingHttpServletRequest
  • Client doesn't know/care that these objects are different than any other adapter factory
  • Support both classes and interfaces.
  • Work with existing Sling infrastructure (i.e. not require changes to other bundles).

However, it fit's well with your 'customProperty' approach then go for it.

You can add an if check to make sure that you are adding custom property only for Author and get run mode(whether it's Publisher or Author) via the SlingSettingsService[0].

[0] https://sling.apache.org/documentation/bundles/sling-settings-org-apache-sling-settings.html

Regards,

Peter