Fetch child pages and sort them as per publish date and time | Community
Skip to main content
Level 4
October 12, 2022
Solved

Fetch child pages and sort them as per publish date and time

  • October 12, 2022
  • 2 replies
  • 2908 views

I have a scenario wherein I've to fetch all the child pages of a parent page (the path will be fetched from dialog) and then sort them as per most recent published date, and when date is time the same should be sorted as per published time...and ultimately just fetch the first 4 elements/pages after sorting.

I've developed a sling model where I'm getting the parent path and then trying to iterate and get the child pages and then store them in an arrayList. I will need some help with the sorting and then ultimately getting the first 4 pages. If someone could help me with the sling model (it'll be great you can provide me with the code snippet).

 

Attached is the piece of code that I've written.

 

dynamicArticles denotes the path of the parent page

childrenList is the ArrayList.

 

 

@arunpatidar @heenamadan @ksh_ingole7 @santoshsai @b_sravan @sachin_arora_ @pulkit_jain_ @milind_bachani 

 

Thanks!

 

 

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 Saravanan_Dharmaraj

i would create a simple bean class just for sorting to make use of java collection with objects. 

public class ChildResult{
    private String path="";
    private String publishedTime="";

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public void setPublishedTime(String publishedTime) {
        this.publishedTime = publishedTime;
    }

    public String getPublishedTime() {
        return publishedTime;
    }
}



//in your Init() List<ChildResult> childList = new ArrayList<>(); while(listChildPages.next()){ //while looping child pages and its value Map, create bean and set path and time in epoch format. if(valueMap.containsKey("publishedDate")){ ChildResult hit = new ChildResult(); GregorianCalendar calendar = new GregorianCalendar(); Date startDate = valueMap.get("publishedDate", Date.class); calendar.setTime(startDate); long startEpoch = calendar.getTimeInMillis(); hit.setPublishedTime(startEpoch); hit.setPath(childPage.getPath()); childList(hit); } } //After reading all the child, just sort with below line. Collections.sort(childList, Comparator.comparing(ChildResult::getPublishedTime));
Collections.reverse(childList);  // To get the latest

 

Then loop thru the childList for top 4 and you have the path of the child resource in the bean you can return in the model by other methods.

 

2 replies

Shubham_borole
Community Advisor
Community Advisor
October 12, 2022

Hi @arindam6600 

 

You could try using a query. For eg: 

path=/content/project/en/home
type=cq:Page
p.limit=4
orderby=@jcr:content/cq:lastReplicated

I suggest to find a query based implementation and try that out.

Thanks

Level 4
October 12, 2022

Can you give me a detailed approach along with some example code snippet maybe?

Shubham_borole
Community Advisor
Community Advisor
October 17, 2022

Sorry for the delay. The link in my above message has some information about queries and implementation in java. 

Getting all the child pages and looping through all and them getting top four may be performance intensive hence using a query builder api could be better. 

Saravanan_Dharmaraj
Community Advisor
Saravanan_DharmarajCommunity AdvisorAccepted solution
Community Advisor
October 12, 2022

i would create a simple bean class just for sorting to make use of java collection with objects. 

public class ChildResult{
    private String path="";
    private String publishedTime="";

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public void setPublishedTime(String publishedTime) {
        this.publishedTime = publishedTime;
    }

    public String getPublishedTime() {
        return publishedTime;
    }
}



//in your Init() List<ChildResult> childList = new ArrayList<>(); while(listChildPages.next()){ //while looping child pages and its value Map, create bean and set path and time in epoch format. if(valueMap.containsKey("publishedDate")){ ChildResult hit = new ChildResult(); GregorianCalendar calendar = new GregorianCalendar(); Date startDate = valueMap.get("publishedDate", Date.class); calendar.setTime(startDate); long startEpoch = calendar.getTimeInMillis(); hit.setPublishedTime(startEpoch); hit.setPath(childPage.getPath()); childList(hit); } } //After reading all the child, just sort with below line. Collections.sort(childList, Comparator.comparing(ChildResult::getPublishedTime));
Collections.reverse(childList);  // To get the latest

 

Then loop thru the childList for top 4 and you have the path of the child resource in the bean you can return in the model by other methods.

 

Level 4
October 12, 2022

@saravanan_dharmaraj Thanks for the detailed explanation. Need one more help. It's dumb to ask still if you could help me out: 
Can you give me the piece of code to write over here? Like what exactly valueMap will consist of when instantiated?

//while looping child pages and its value Map, create bean and set path and time in epoch format.

 

Saravanan_Dharmaraj
Community Advisor
Community Advisor
October 12, 2022

That was the comment for the lines of code given below that 🙂 . The whole "If "statement