Is it possible to extend an adaptable? | Community
Skip to main content
Level 4
February 16, 2016
Solved

Is it possible to extend an adaptable?

  • February 16, 2016
  • 3 replies
  • 1807 views

I have an Article model that is more or less a com.day.cq.wcm.api.Page with a number of specific properties: 

@Model(adaptables=Resource.class) public class Article { @Inject public String author; @Inject public String[] contacts; @Inject public Date dispDate; //... }

This works fine by itself, but I'd also like the capabilities of a Page (onTime, offTime, etc). However, Page is an interface, so using `extends Page` won't quite work. Is this possible some other way, or am I barking up the wrong tree?

For further clarification, the bigger goal of this is to use this moddel from a number of components that call another UseClass with an ID parameter. That UseClass queries on the passed ID, gets the associated resource, and then adaptTo(Article.class), and associates that with a member object.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kunal_Gaba_

There are couple of ways you can achieve this - 

1. You can write custom injectors for injecting the page properties in your model. Examples are here - http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/

2. Instead of using the annotation based sling model you can use the old school approach for creating the model using the adaptor factory pattern. Check slide #5 in this deck - http://www.slideshare.net/AEMHub2014/sling-models-by-justin-edelson-final

3 replies

smacdonald2008
Level 10
February 16, 2016

After talking to a few people on this - this is not a typical use case.  I would not expect there to be any community articles on this use case. There are plenty of articles on Sling Models - but not extending the Java APIs.

BenSt10Author
Level 4
February 16, 2016

Model interfaces are extendable, but you have to access the data members with methods like getPropertyName(), however, as all of the data members wont directly to data members, I'm just going to go ahead and store the page as a data member on the Article object itself.

Kunal_Gaba_
Kunal_Gaba_Accepted solution
February 16, 2016

There are couple of ways you can achieve this - 

1. You can write custom injectors for injecting the page properties in your model. Examples are here - http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/

2. Instead of using the annotation based sling model you can use the old school approach for creating the model using the adaptor factory pattern. Check slide #5 in this deck - http://www.slideshare.net/AEMHub2014/sling-models-by-justin-edelson-final