Expand my Community achievements bar.

SOLVED

Iterating a map in sightly which have custom object as key

Avatar

Level 4

Hi All,

(Similar issue to mine: http://coderissues.com/questions/31437688/how-to-traverse-a-map-having-keys-of-object-type-using-sig... )

I am having tough time iterating a custom map in sightly, previously I have iterated through normal map<String, String> many type in sightly and it worked perfectly fine.

My custom map Map<Product, List<CartEntry>>, As I am using Product as key to my map I have overridden the equals and hashcode method for my product class as below. I am able to iterate the map in normal java class and able to output the required values. But when I trying to iterate it in sightly it is failing and saying error as invalid key.

<div data-sly-list.parentEntry="${shoppingCart.cartItems}" data-sly-unwrap>

         <div data-sly-list.childEntry="${shoppingCart.cartItems[parentEntry]}" data-sly-unwrap>

                 ${childEntry.productCode}

        </div>

</div>

    @Override
    public int hashCode()
    {
        return this.code.hashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Product other = (Product) obj;
        if (this.code != other.getCode())
        {
            return false;
        }

        return true;
    }

Not sure how sightly has implemented data-sly-list? So it is possible to iterate this kind of map in sightly?

Any hints would be greatly appreciated, 

Kind regards,

Shehjad

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi All,

I was able to make it work, I just need to use entrySet. Below is the code

 

div data-sly-list.cartItemsEntrySet="${shoppingCart.cartItems.entrySet}" data-sly-unwrap>

         <div data-sly-list.childEntry="${cartItemsEntrySet.value}" data-sly-unwrap>

                 ${cartItemsEntrySet.key.productCode}

        </div>

</div>

Thanks!

Shehjad

View solution in original post

11 Replies

Avatar

Correct answer by
Level 4

Hi All,

I was able to make it work, I just need to use entrySet. Below is the code

 

div data-sly-list.cartItemsEntrySet="${shoppingCart.cartItems.entrySet}" data-sly-unwrap>

         <div data-sly-list.childEntry="${cartItemsEntrySet.value}" data-sly-unwrap>

                 ${cartItemsEntrySet.key.productCode}

        </div>

</div>

Thanks!

Shehjad

Avatar

Employee

If you are using 6.1 then you can also use the <sly> element instead of data-sly-unwrap.

Avatar

Level 3

Hi Shehjad,

I tried with the code which you posted,but it dint worked for me.Do I need to add any other changes to make it work?

Regards,

swati

Avatar

Level 4

swathi07 wrote...

Hi Shehjad,

I tried with the code which you posted,but it dint worked for me.Do I need to add any other changes to make it work?

Regards,

swati

 

 

 

Hi Swathi,

Have you override the equals method and hashCode so that your custom object is iterable. Please check logs also if you getting any error.

Thanks,

Shehjad

Avatar

Level 3

Simple map( HashMap<String,String>) itself is not working in sightly.I tried to display keys and values but nothing displayed.And no errors,I checked the log file.

Avatar

Level 4

Simple map works, please can you share your code

Avatar

Level 4

Please try below example, please make sure your map object is available in your sightly file through whichever way you are using for example WCMUse class.

<div data-sly-list.key="${map}">

   Value : ${map[key]}

</div>

Avatar

Level 3

Below is the osgi  sightly class code::

package com.test.sightly;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUse;

public class TestSightly extends WCMUse {

    private static final Logger log = LoggerFactory.getLogger(TestSightly.class);
    private Map<String, String> test;
    
    
    @Override
    public void activate() throws Exception {
        test = new HashMap<String, String>();
        test.put("hai", "helo");
        test.put("sightly", "test");
        log.info("main map object------>"+test);
    }
    
    public Map<String, String> getTest(){
        return test;
    }

}

Html::

<div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div>
<div data-sly-resource = "${@path = 'parsy' , resourceType = 'foundation/components/parsys'}"
<div data-sly-use.locat="com.test.sightly.TestSightly">
<div>
<h1>${locat.test}</h1>
</div>

</div>

 

I tried in my colleagues machine, I am able to display the keys,but in my machine itz displaying nothing.Even no errors in the log file(I am even logging the map,it is not null).

 

Thanks,

Swati 

Avatar

Level 4

Please change your html code to below and check 

<div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div> <div data-sly-resource = "${@path = 'parsy' , resourceType = 'foundation/components/parsys'}" <div data-sly-use.local="com.test.sightly.TestSightly"> <div> <ul data-sly-list.key="${local.test}"> <li> Map key -- ${key} Map Value :: ${local.test[key]} </li> </ul> </div> </div>

Avatar

Level 3

It is my aem instance issue I guess.I am able to iterate simple and custom class map in my colleagues machine.I tried with your code as well but it dint worked in my machine.