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!
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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
Can you give me a detailed approach along with some example code snippet maybe?
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.
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!
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.
@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.
That was the comment for the lines of code given below that
Oh okay
ValueMap valueMap = childPage.getProperties();
that was a typo
//Adding to the array List
childList.add(hit);
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.
@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?
@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.
Views
Likes
Replies
Views
Like
Replies
Views
Likes
Replies
Views
Likes
Replies