How to get all images in a folder programmatically?

247 views
Skip to first unread message

Eric Nguyen personal

unread,
Mar 2, 2015, 10:04:14 PM3/2/15
to hippo-c...@googlegroups.com
The idea is the site I'm building need to have galleries of photos e.g. a birthday party event would have a gallery.
This makes sense and is quite common in CMS world right?

Now I have a document type called Event.
For each event there is a link which points to a folder under Images.





How do I get all the images in such folder to display on my Event details page?

*Notes: I'm not using multiple image fields for my Event document type because I don't want my users to have to 10 times upload an image to achieve a gallery!

Eric Nguyen personal

unread,
Mar 3, 2015, 3:00:37 AM3/3/15
to hippo-c...@googlegroups.com
I've found the XPath expression and I can correctly get the images under an image folder:

/jcr:root/content/gallery/scc/gallery1//element(*, hippo:handle)


I'm trying the following function in my bean class but I'm getting ClassCastException:
public List<HippoGalleryImageSet> getImages() {
 log
.info("getImages");
 
List<HippoGalleryImageSet> images = new ArrayList<HippoGalleryImageSet>();
 
String expression = "/jcr:root/content/gallery/scc/gallery1//element(*, hippo:handle)";
 
try {
 
Session session = getNode().getSession();
 
QueryManager queryManager = session.getWorkspace()
 
.getQueryManager();
 
String language = Query.XPATH;
 
Query query = queryManager.createQuery(expression, language);
 
QueryResult result = query.execute();
 
NodeIterator nodes = result.getNodes();
 
if (nodes.hasNext()) {
 javax
.jcr.Node image = nodes.nextNode();
 log
.info("image is "+image);
 images
.add((HippoGalleryImageSet) image);
 
}
 
} catch (RepositoryException e) {
 log
 
.error(
 
"Failed to retrieve average rating for product document {}",
 getCanonicalHandleUUID
(), e);
 
}
 
return images;
}


How can I get the HippoGalleryImageSet objects from the nodes I found under the same folder which are essentially images!

Jeroen Hoffman

unread,
Mar 3, 2015, 3:55:26 AM3/3/15
to hippo-c...@googlegroups.com
Hi Eric,

IMO you shouldn't do queries like that. If you want a query, we provide HST
Query, see http://www.onehippo.org/library/concepts/search/hst-2-search.html

But in your case, the easiest way is get the linked folder and then iterate it's
contents, for example:

document.getLinkedBean("yournamspace:mirrorproperty",
HippoFolder.class).getDocuments(HippoGalleryImageSet.class);

There are other methods available too, the main idea is to use beans.

HTH
Jeroen




Eric Nguyen personal wrote on 03-03-2015 09:00:
> I've found the XPath expression and I can correctly get the images under an
> image folder:
>
> <https://lh6.googleusercontent.com/-nbO0xszcU1I/VPVqEnG-OWI/AAAAAAAADMI/WERWtlFuMGM/s1600/Screen%2BShot%2B2015-03-03%2Bat%2B3.58.57%2Bpm.png>
>
> |
> /jcr:root/content/gallery/scc/gallery1//element(*, hippo:handle)
> |
>
>
> I'm trying the following function in my bean class but I'm getting
> ClassCastException:
>
> |
> publicList<HippoGalleryImageSet>getImages(){
> log.info("getImages");
> List<HippoGalleryImageSet>images =newArrayList<HippoGalleryImageSet>();
> Stringexpression ="/jcr:root/content/gallery/scc/gallery1//element(*,
> For each event there is a link which points to a *folder* under *Images*.
>
>
> <https://lh4.googleusercontent.com/-BKeWZIotIKQ/VPUkLMRyBmI/AAAAAAAADLo/ANORDe2BHH4/s1600/Screen%2BShot%2B2015-03-03%2Bat%2B11.01.31%2Bam.png>
>
> <https://lh6.googleusercontent.com/-3zZcomnr9F4/VPUkbO_ojII/AAAAAAAADL4/jm4yezPaxzg/s1600/Screen%2BShot%2B2015-03-03%2Bat%2B10.58.16%2Bam.png>
>
>
>
>
> How do I get all the images in such folder to display on my Event details page?
>
> *Notes: I'm not using multiple image fields for my Event document type
> because I don't want my users to have to 10 times upload an image to achieve
> a gallery!
>
> --
> Hippo Community Group: The place for all discussions and announcements about
> Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google Groups "Hippo
> Community" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.

Eric Nguyen personal

unread,
Mar 3, 2015, 4:25:52 AM3/3/15
to hippo-c...@googlegroups.com
Thanks Jeoren, that looks like a good direction to go.

I've already achieved my goal using XPath by the following though :D
while (nodes.hasNext()) {
 javax
.jcr.Node node = nodes.nextNode();

 
HippoGalleryImageSet image = new HippoGalleryImageSet();
 image
.setNode(node);

 images
.add(image);
}

Reply all
Reply to author
Forward
0 new messages