[AEM 6.4] InheritanceValueMap having stuck to retrieve child nodes (multifield items)

Avatar

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile
canh_nguyen
Level 2

06-06-2018

When I develop content page inheritance, I didn't get values of multifield (save by nodes).

The InheritanceValueMap only get component data (title field), doesn't get child nodes data (items node).

Screen Shot 2018-06-06 at 3.14.22 PM.png

Screen Shot 2018-06-06 at 3.13.59 PM.png

Any suggestions or ideas on this?

Thanks,

Canh Nguyen,

Replies

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

06-06-2018

How are you trying to read items0, item1, etc

Avatar

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile
canh_nguyen
Level 2

06-06-2018

smacdonald2008

If I don't get inheritance properties, I use @ChildResource(name = "items").
When I get inherit properties, I think 'items' properties like JSON array and include in 'listoflinks' node.
I think I need to custom multifield save by JSON array.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

06-06-2018

Have you tried to code that?

Avatar

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile
canh_nguyen
Level 2

06-06-2018

Yep, I try to code that.
I have a problem when I get inheritance properties of multifield.
InheritanceValueMap doesn't get parent multifield property.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

06-06-2018

Can you post the code you tried to community can see it.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

06-06-2018

The inheritanceValueMap is just looking up the hierachy, it is not taking child nodes of ancestors into consideration.

Jörg

Avatar

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile

Avatar
Boost 1
Level 2
canh_nguyen
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Applaud 5
View profile
canh_nguyen
Level 2

08-06-2018

smacdonald2008

My approach is I get component resource by resourceType after I get InheritanceValueMap of the component. I have a problem when I get InheritanceValueMap, I don't get child node properties.

I think if multifield save by JSON array, I can get the inheritance of multifield properties.

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, adapters = { FooterModel.class,

ComponentExporter.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = FooterModelImpl.RESOURCE_TYPE)

@Exporters({

@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION) })

public class FooterModelImpl implements FooterModel {

     protected static final String RESOURCE_TYPE = "myproject/components/structure/footer/v1/footer";

     protected static final String COLUMN_RESOURCE_TYPE = "myproject/components/content/column/v1/column";

     protected static final String LIST_OF_LINKS_RESOURCE_TYPE = "myproject/components/content/listOfLinks/v1/listOfLinks";

     @Self

     private SlingHttpServletRequest request;

     @SlingObject

     private ResourceResolver resourceResolver;

     @SlingObject

     private Resource resource;

     private String hello;

     private Map<String, Object> map;

     @PostConstruct

     private void initModel() {

          hello = "Hello Footer";

          map = new HashMap<>();

          Iterator<Resource> columnResourceIterator = getListResourceByResourceType(resource.getPath(),

          COLUMN_RESOURCE_TYPE, resourceResolver);

          if (columnResourceIterator != null) {

               while (columnResourceIterator.hasNext()) {

                    Resource columnItemResource = columnResourceIterator.next();

                    if (columnItemResource != null) {

                         map.put(columnItemResource.getName(), getListOfLinksContent(columnItemResource));

                    }

               }

          }

     }

     private Map<String, Object> getListOfLinksContent(Resource columnResource) {

          Map<String, Object> map = new HashMap<>();

          Iterator<Resource> listOfLinksResources = getListOfLinksResources(columnResource.getPath());

          List<ListOfLinks> list = new ArrayList<>();

          while (listOfLinksResources.hasNext()) {

               Resource element = listOfLinksResources.next();

               ListOfLinks listOfLinks = getListOfLinksFromResource(element);

               list.add(listOfLinks);

          }

          map.put("listoflinks", list);

          return map;

     }

     private Iterator<Resource> getListOfLinksResources(String path) {

          return getListResourceByResourceType(path, LIST_OF_LINKS_RESOURCE_TYPE, resourceResolver);

     }

     private ListOfLinks getListOfLinksFromResource(Resource resource) {

          ListOfLinks listOfLinks = new ListOfLinks();

          InheritanceValueMap inheritanceMap = new HierarchyNodeInheritanceValueMap(resource);

          listOfLinks.setTitle(inheritanceMap.getInherited("title", StringUtils.EMPTY));

          listOfLinks.setNumberOrder(inheritanceMap.getInherited("order", StringUtils.EMPTY));

          listOfLinks.setLinks(getListLinkFromIteratorResource(getIteratorResourceFromResource(resource)));

          return listOfLinks;

     }

     private Iterator<Resource> getIteratorResourceFromResource(Resource resource) {

          // Save data to JSON array

          Resource items = resource.getResourceResolver().getResource(resource.getPath() + "/items");

          return items != null ? items.listChildren() : null;

     }

     private List<Link> getListLinkFromIteratorResource(Iterator<Resource> iterator) {

          List<Link> links = new ArrayList<>();

          while (iterator.hasNext()) {

               Resource linkResource = iterator.next();

               Link link = getLinkFromResource(linkResource);

               links.add(link);

          }

          return links;

     }

     private Link getLinkFromResource(Resource resource) {

          InheritanceValueMap inheritanceMap = new HierarchyNodeInheritanceValueMap(resource);

          return new Link(inheritanceMap.getInherited("label", StringUtils.EMPTY),

               inheritanceMap.getInherited("url", StringUtils.EMPTY));

     }

     private static Iterator<Resource> getListResourceByResourceType(String path, String resourceType,

          ResourceResolver resolver) {

          if (resolver == null) {

               return null;

          }

          String sql = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE([" + path + "]) "

               + "AND s.[sling:resourceType] = '" + resourceType + "'";

          return resolver.findResources(sql, Query.JCR_SQL2);

     }

     public String getHello() {

          return hello;

     }

     public Map<String, Object> getItems() {

          return map;

     }

     class ListOfLinks {

          private String title;

          private String numberOrder;

          private List<Link> links;

          public String getTitle() {

               return title;

          }

          public void setTitle(String title) {

               this.title = title;

          }

          public String getNumberOrder() {

               return numberOrder;

          }

          public void setNumberOrder(String numberOrder) {

               this.numberOrder = numberOrder;

          }

          public List<Link> getLinks() {

               return links;

          }

          public void setLinks(List<Link> links) {

               this.links = links;

          }

     }

     class Link {

          private String label;

          private String url;

          public Link(String label, String url) {

               this.label = label;

               this.url = url;

          }

          public String getLabel() {

               return label;

          }

          public void setLabel(String label) {

               this.label = label;

          }

          public String getUrl() {

               return url;

          }

          public void setUrl(String url) {

               this.url = url;

          }

     }

}

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

08-06-2018

Look at Joergs response. If you want to read child nodes - look at using the JCR API to read them.