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

SOLVED

get resource from session

shaheenas113483
Level 4
Level 4

i made a connection to repository using JCRUtils and got session. Next, I need the resource but, when I am trying to fetch resource using resourceResolver, I am getting null. Whereas, if I use session.getNode, i am getting the node (not null). I also tried to use AdaptManager to convert the node to resource, but I am getting null in this case also.

How do I get the resource from the node? or How do I fetch resource directly from session?

[I need to use valueMap, thus I need resource]

1 Accepted Solution
aemmarc
Correct answer by
Employee
Employee

final ResourceResolver resource_resolver = request.getResourceResolver();

Iterator<Resource> some_it = resource_resolver.findResources("select * from [dam:Asset] as asset", "JCR-SQL2");

some_it.forEachRemaining(object_from_iterator -> {

        Node jcr_node = object_from_iterator.adaptTo(Node.class);

        try {

              String someProperty = jcr_node.getProperty("some:Property").getString();

              jcr_node.getSession().getWorkspace().getObservationManager().setUserData("changedByWorkflowProcess");

              jcr_node.setProperty("some:Property", "some new value");

              logger.info("blah blah made some changes");

              jcr_node.getSession().save();

         }

        catch { ... }

        finally { ... }

}

View solution in original post

9 Replies
k_ra
Level 2
Level 2

Hi, Are you trying to get ValueMap to fetch properties?  if so you can use session.getNode("path to the node you wanna access").getProperties();

k_ra
Level 2
Level 2

When you try to get resource this way (adapterManager.getAdapter(nodeObj, Resource.class)) are getting null ?

aemmarc
Correct answer by
Employee
Employee

final ResourceResolver resource_resolver = request.getResourceResolver();

Iterator<Resource> some_it = resource_resolver.findResources("select * from [dam:Asset] as asset", "JCR-SQL2");

some_it.forEachRemaining(object_from_iterator -> {

        Node jcr_node = object_from_iterator.adaptTo(Node.class);

        try {

              String someProperty = jcr_node.getProperty("some:Property").getString();

              jcr_node.getSession().getWorkspace().getObservationManager().setUserData("changedByWorkflowProcess");

              jcr_node.setProperty("some:Property", "some new value");

              logger.info("blah blah made some changes");

              jcr_node.getSession().save();

         }

        catch { ... }

        finally { ... }

}

View solution in original post

smacdonald2008
Level 10
Level 10

WHen using the JCR API - you can get props - as Marc as shown.

Arun_Patidar
Community Advisor
Community Advisor

You can get Resource from from jcr session like below:

Map<String, Object> paramMap = new HashMap<String, Object>();

paramMap.put("user.jcr.session", session);

resourceResolver = resourceFactory.getResourceResolver(paramMap);

Resource resource=resourceResolver.getResource("/apps/AEM64App");

where session is JCR Session object

VoloMyhal2
Level 1
Level 1
Shouldn't you close that resourceResolver after the use?
Arun_Patidar
Community Advisor
Community Advisor

You can adapt resource to ValueMap

Example :

Map<String, Object> paramMap = new HashMap<String, Object>();

paramMap.put("user.jcr.session", session);

resourceResolver = resourceFactory.getResourceResolver(paramMap);

Resource resource=resourceResolver.getResource("/content/AEM64App/en/jcr:content/par/mycomponent");

ValueMap prop = resource.adaptTo(ValueMap.class);

String lastModified = prop.get("jcr:lastModified", String.class);