Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

question about value maps

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

24-04-2021

As aem forums always deletes my post for no reason, Ill try putting the question into the response part. Please read the below reply for the question.

The problem is I cant find a way to combine two value maps. doing .putAll() fails (not supported), and so does constructing a new one.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

25-04-2021

Seems that ValueMaps are immutable, even if you use .put() you will get run time errors.

Instead I change everywhere which used ValueMap to use HashMap<String,Object> instead, and copy each ValueMap into this. Then I can use the full power of maps.

 

	public void copyProps(ValueMap from, HashMap<String,Object> to) {
		// ignore jrc: and sling: object properties.
		from.forEach((key, value) ->  {
			if (!key.startsWith("jcr:") && !key.startsWith("sling:"))  {
				to.put(key, value);
			}
		});
	}

 

 

 

I use the above code to convert ValueMaps into HashMaps, and to also combine several page components VAlueMaps into a single HashMap so I can send the page back as json (using Gson on the resultant master hashmap).

This relies on not using the same field names on each components properties. 

Answers (3)

Answers (3)

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

24-04-2021

Below is the code so far. If I try to put it in a code block AEM deletes the post.

 

                predicate.put("path", path);
                predicate.put("type", "cq:unstructured");
                predicate.put("group.1_property", "sling:resourceType");
                predicate.put("group.1_property.value", "mysite/components/gamemain");
                predicate.put("group.2_property", "sling:resourceType");
                predicate.put("group.2_property.value", "mysite/components/gameterms");
                predicate.put("group.p.or", "true");
                
                Query query = _builder.createQuery(PredicateGroup.create(predicate), session);
                
                // should only be 2 components!
                query.setStart(0);
                query.setHitsPerPage(20);
                
                SearchResult searchResult = query.getResult();
                
                ValueMap allProps = null;
                
                for(Hit hit : searchResult.getHits()) {
                        ValueMap props = hit.getProperties();
                        if (allProps == null) {
                                allProps  = props;
                        }  else {
                                allProps.putAll(props);  // Fails with null pointer exception
                        }
                }

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

24-04-2021

Here is the current (non working) code:

 

		predicate.put("path", path);
		predicate.put("type", "cq:unstructured");
		predicate.put("group.1_property", "sling:resourceType");
		predicate.put("group.1_property.value", "mysite/components/gamemain");
		predicate.put("group.2_property", "sling:resourceType");
		predicate.put("group.2_property.value", "mysite/components/gameterms");
		predicate.put("group.p.or", "true");
		
		Query query = _builder.createQuery(PredicateGroup.create(predicate), session);
		
		// should only be 2 components!
		query.setStart(0);
		query.setHitsPerPage(20);
		
		SearchResult searchResult = query.getResult();
		
		ValueMap allProps = null;
		
		for(Hit hit : searchResult.getHits()) {
			ValueMap props = hit.getProperties();
			if (allProps == null) {
				allProps  = props;
			}  else {
				allProps.putAll(props);  // Fails with null pointer exception
			}
		}
 

Avatar

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 4
TB3dock
Level 4

Likes

23 likes

Total Posts

172 posts

Correct Reply

4 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
TB3dock
Level 4

24-04-2021

In regards to a page which shows thumbnails and summary of recent played games.  The content comes from individual game info pages which have to be looked up by ID.

We need to send a page to the client to show the played games as json so it can be rendered in react.

First i search for the required game info pages using QueryBuilder.

Then, for each page found, I use QueryBuilder again to search for the required components the page.

This returns a ValueMap for the components properties.

Now I need to combine each components ValueMap into one ValueMap for the page, add a few additional ones (like the URL of the info page), so I can pass it back, and eventually convert it to JSON to the client.

I cant see a way to instantiate a new value map, in order to manually copy each item into it.

i tried pulAll but this gives a null pointer exception.

In the next reply, ill try pasting the code so far (trying to not let AEM delete this post)