Expand my Community achievements bar.

SOLVED

Problems when getting the index or count on data-sly-list

Avatar

Level 6

Hi,

I'm using sightly with a Java class annotated with @Model.

And the class has a method that return a list with objects. Like List<CarouselPage> pages;

I can iterate over the list and get the object properties, but when I try ${itemList.count} or ${itemList.index} it throws an error:

Caused by: io.sightly.java.compiler.CompilerException: Compilation errors in apps/<mysite>/components/content/carousel/SightlyJava_carousel.java:
Line 79, column 4018 : Cannot invoke equals(boolean) on the primitive type long
    at io.sightly.java.compiler.SightlyCompileServiceImpl.compileJavaResource(SightlyCompileServiceImpl.java:187)
    at io.sightly.java.compiler.SightlyCompileServiceImpl.compileSource(SightlyCompileServiceImpl.java:132)

I thought it was a problem wiht List itself, then I tried returning the iterator instead of the List, but I got the same problem. 

But when I try with the iterator returned by currentPage.listChildren the index and cound works.

Did you guys already faced this problem?

Follow the code:

<ul data-sly-list="${currentPage.listChildren}"> <li>${itemList.index} - ${item.title}</li> </ul> <ol class="carousel-indicators" data-sly-list="${carousel.pages}"> <li data-target="#main-slider" data-slide-to="${itemList.index}" class=""></li> </ol>
1 Accepted Solution

Avatar

Correct answer by
Employee

With the attribute I mean data-slide-to="", instead of <li>${itemList.index}</li>

View solution in original post

12 Replies

Avatar

Employee

Does your CarouselPage has a 'long' variable?

Can you change that to a 'Long'?

If the issue remains can you then show the code of your CarouselPage?

Avatar

Level 6

Hi Feike,

It doesn't. It has only String values.

Follow the code:

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; @Model(adaptables=SlingHttpServletRequest.class) public class CarouselPage { private String title; private String description; private String fileImage; private String path; public CarouselPage(String title, String description, String fileImage, String path) { this.title = title; this.description = description; this.fileImage = fileImage; this.path = path; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getFileImage() { return fileImage; } public void setFileImage(String fileImage) { this.fileImage = fileImage; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } @Override public String toString() { return "CarouselPage [title=" + title + ", description=" + description + ", fileImage=" + fileImage + ", path=" + path + "]"; } }

Avatar

Employee

Can you also share the other class you have with the @Model.

I have had a similar case, and it had to do that a @Inject property was of wrong type.

Avatar

Employee

BTW: Your carouselPage doesn't use any of the Sling-model functionalities, except @Model

Avatar

Level 6

Follow the model class that is used on the sighlty:

 

import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.Model; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.commons.WCMUtils; import com.day.cq.wcm.foundation.List; @Model(adaptables=SlingHttpServletRequest.class) public class CarouselComponent { private static final Logger LOG = LoggerFactory.getLogger(CarouselComponent.class); @Inject protected SlingHttpServletRequest request; @Inject protected Page currentPage; protected Iterator<Page> pagesIterator; public java.util.List<CarouselPage> pages; @PostConstruct public void init() throws PathNotFoundException, RepositoryException { LOG.info("Init CarouselComponent"); List listComponent = new List(request); pagesIterator = listComponent.getPages(); pages = populatePages(); } /** * * @return * @throws PathNotFoundException * @throws RepositoryException */ public java.util.List<CarouselPage> populatePages() throws PathNotFoundException, RepositoryException { java.util.List<CarouselPage> pageList = new LinkedList<CarouselPage>(); if(pagesIterator != null) { while(pagesIterator.hasNext()) { LOG.info("Inside While"); Page page = pagesIterator.next(); Node jcrContent = WCMUtils.getNode(page.getContentResource()); Node imageNode = getImage(jcrContent); String fileImage = null; if(imageNode != null) { fileImage = imageNode.getProperty("fileReference").getValue().toString(); LOG.info("fileImage:" + fileImage); } CarouselPage carouselPage = new CarouselPage(page.getPageTitle(), page.getDescription(), fileImage, page.getPath()); pageList.add(carouselPage); } } LOG.info("pageList:" + pageList); return pageList; } protected Node getImage(Node node) { Node imageNode = null; try { LOG.info("Getting image from:" + node.getPath()); LOG.info("Getting the resolver"); ResourceResolver rs = request.getResourceResolver(); Resource r = rs.resolve(node.getPath() +"/image"); if(r != null) LOG.info("Resource:" + r); NodeIterator it = node.getNodes(); while(it.hasNext()) { LOG.info("Node:" + ((Node)it.next()).getName()); } imageNode = node.getNode("image"); } catch (PathNotFoundException e) { LOG.error("Error while retrieving node image", e); } catch (RepositoryException e) { LOG.error("Error while retrieving node image", e); } catch (Throwable e) { LOG.error("Error while retrieving node image", e); } return imageNode; } }

Avatar

Employee

Would also be able to share you sightly code snippet?

Let's find the issue :-)

Avatar

Employee

Thanks. Trying to reproducing now on AEM6+SP1

Avatar

Level 6

Here is the sightly snippet:

The second itemList.index that throws the error.

<section id="main-slider" class="no-margin" data-sly-use.carousel="com.mycompany.models.CarouselComponent"> <div class="carousel slide"> <ul data-sly-list="${currentPage.listChildren}"> <li>${itemList.index} - ${item.title}</li> </ul> <ol class="carousel-indicators" data-sly-list="${carousel.pages}"> <li data-target="#main-slider" data-slide-to="${itemList.index}" class=""></li> </ol> </div> </section><!--/#main-slider-->

Avatar

Employee

Found it....

We were looking at the wrong place, it was due to this:

data-slide-to="${itemList.index}"

Because the value is in attribute, Sightly tries to convert it to a boolean to check if the attribute need to be removed. That why the error:  equals(boolean) on the primitive type long

If you replace it with this it is ok:

data-slide-to="${ '{0}' @ format=[itemList.index]}"

Avatar

Level 6

Wow It worked smiley

Thanks for your help.

I didn't understand very well the problem, what did you mean by the value is in attribute?

Att.

Avatar

Correct answer by
Employee

With the attribute I mean data-slide-to="", instead of <li>${itemList.index}</li>

Avatar

Level 6

Ahh!! got it!!! smiley

Thanks you so much!!