[
https://issues.apache.org/jira/browse/SLING-3516?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13993732#comment-13993732
]
Igor Sechyn commented on SLING-3516:
------------------------------------
Hi Konrad,
Thanks for your feedback.
{quote}I would rather go for a separate injector, which also supports some kind
of filtering expression (a regexp) against the child node names, to only adapt
some of the children (and leave out others).{quote}
I had the same idea. Although in this case i do not want to filter child nodes
based on their names, but on the values, which they may contain, similar to the
Filter annotation used for the injection of the OSGI services. I ended up with
the following approach:
- defined a new annotation PropertyFilter similar to DefaultValue annotation,
since Filter annotation is hard wired with the OSGI injector
- extended the ValueMapInjector to return null, if the property filter value is
not equal the value from the node for the current field
The filtering happens as follows:
- i actually do consider the name, as i pointed out in the description. If I am
adapting the {{dealer}} node, I use the name of the field "addresses" to get
child resource {{addresses}}
- then the injector is iterating over the child nodes of the {{addresses}} node
and tries to adapt it to the actual type of the collection, in this case
{{AddressModel}}
- if the {{AddressModel}} class defines a mandatory property {{active}} with
{{PropertyFilter}} annotation set to true, all nodes with this property set to
false will not be included in the resulting list, because the adapter factory
will return null for this node. The only catch here is, that the property must
be mandatory.
I decided to extend the existing Resource child injector to keep the analogy to
the OSGI injector, which also handles the injection of a single service, as
well as the list.
Can you think of any scenarios, where the name of the item node is important?
Usually the wrapping node (in this case {{addresses}}) already defines the
semantics of the child nodes. But I definitely agree with you, that some sort
of filtering/validation is needed.
Cheers, Igor.
> Sling Models Resource Child List Injector
> -----------------------------------------
>
> Key: SLING-3516
> URL: https://issues.apache.org/jira/browse/SLING-3516
> Project: Sling
> Issue Type: New Feature
> Components: Extensions
> Affects Versions: Sling Models Implementation 1.0.2
> Reporter: Igor Sechyn
> Priority: Minor
> Attachments: ChildResourceInjectorPatch.txt
>
>
> Hi All,
> We have decided to use the sling models to process some hierarchical data in
> our project and are facing the following issue right now.
> Our content structure looks as follows:
> {code}
> +- dealer
> |
> +- addresses
> |
> + -address1
> |
> +- address2
> {code}
> Dealer and address nodes have attributes, that can be injected through the
> ValueMapInjector, so everything works fine here. Our goal however is also to
> inject the List of AddressModel objects into the DealerModel object,
> something like this:
> {code}
> @Inject
> List<AddressModel> addresses
> {code}
> The desired behavior would be:
> - the injector resolves addresses as a child resource of the adaptable object
> - iterates over the children of this resource (in this case „addresses")
> - adapts each child to the actual type argument of the parameterized list type
> - returns the list of adapted model objects
> I have tried to play around with @Named but could not get it to work. The
> only way of making it work, was to write a custom injector, which worked out
> pretty fine.
> But I was still wondering, if there might be an OOTB functionality, which can
> be used for this use case
> Cheers, Igor.
--
This message was sent by Atlassian JIRA
(v6.2#6252)