Expand my Community achievements bar.

SOLVED

Is it possible to extend an adaptable?

Avatar

Level 5

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.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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/...

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

View solution in original post

3 Replies

Avatar

Level 10

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.

Avatar

Level 5

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.

Avatar

Correct answer by
Employee Advisor

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/...

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