Highlighted

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

Avatar

Avatar

canh_nguyen

Avatar

canh_nguyen

canh_nguyen

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

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

06-06-2018

How are you trying to read items0, item1, etc

Highlighted

Avatar

Avatar

canh_nguyen

Avatar

canh_nguyen

canh_nguyen

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.

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

06-06-2018

Have you tried to code that?

Highlighted

Avatar

Avatar

canh_nguyen

Avatar

canh_nguyen

canh_nguyen

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.

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

06-06-2018

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

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
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

Highlighted

Avatar

Avatar

canh_nguyen

Avatar

canh_nguyen

canh_nguyen

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;

          }

     }

}

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

08-06-2018

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