Expand my Community achievements bar.

SOLVED

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

Avatar

Level 5

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.

 

arindam6600_0-1665576516129.png

 

@arunpatidar @HeenaMadan @ksh_ingole7 @SantoshSai @B_Sravan @Sachin_Arora_ @Pulkit_Jain_ @milind_bachani 

 

Thanks!

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

 

View solution in original post

14 Replies

Avatar

Community Advisor

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

Avatar

Level 5

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

Avatar

Community Advisor

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. 

Avatar

Level 5

Hi @Shubham_borole yes you were right. I implemented Query builder logic and could do the required sorting. This method is also more efficient. Thanks!

Avatar

Correct answer by
Community Advisor

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.

 

Avatar

Level 5

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

 

Avatar

Community Advisor

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

 

 

Avatar

Level 5

Oh okay got it. Thanks. Just one last query do I need to instantiate the valueMap? If yes, how shall I instantiate it?

Avatar

Community Advisor

that was a typo

//Adding to the array List

childList.add(hit);

Avatar

Level 5

Hi @Saravanan_Dharmaraj,

 

Firstly I am really thankful to you for helping me out. 
Secondly, I've written the code as per your suggestion and now I want to fetch the path of the child pages that are stored in the Array List. What to do? 
I tried printing the arrayList now but it gives some values which seems to be something else. Please help.

arindam6600_0-1666012268953.png

 

Avatar

Level 5

@Saravanan_Dharmaraj Another issue is in bean class you have specified publishedTime as String but in init startEpoch is long type and thereafter hit.setPublishedTime(startEpoch) this throws error since type is contradicting. Which one shall be proper -- String or Long?

arindam6600_0-1666014229267.png

 

Avatar

Level 5

@Saravanan_Dharmaraj Ok so i managed to get the path but I'm unable to understand why there are too many duplications. Like one path is appearing 2-3 times. What can be the issue?

It will come like:

a

a

b

a

c

b

a

d

c

b

a

e

d

c

b

a

f

e

d

c

b

 

whereas the right one should be : 

a

f

e

d

c

b

Please suggest why the duplications are happening.