I need to create a node in AEM using java services. I created a workflow, in which a process triggers a java service inside content/dam/Test
.
Do I need to create a node using java services or simply create a node programatically?
You can programmatically modify nodes and properties located within the Adobe CQ repository, which is part of the Adobe Marketing Cloud. To access the CQ repository, you use the Java Content Repository (JCR) API.
Go to Solution. Node n = session. getNode("/content/project/home"); n. setProperty("Name","AEM"); Node n = session.
The JCR is the base level of the AEM technology stack and is responsible for underlying content persistence, storage, search, access control and much more!
There are various APIs which can be used to create a node :
1. Using Node API
Node node = resource.adaptTo(Node.class);
node.addNode(nodeName, NodePrimaryType);
Using JcrUtil
There are 2 APIs for JCRUtil :
You can go through any of them to create a new node.
The workflow session can be adapted to a JCR session, from there you have access to read/write via the JCR API.
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.Constants;
import javax.jcr.Node;
import javax.jcr.Session;
@Component
@Service
@Properties({@Property(name = Constants.SERVICE_DESCRIPTION, value = "Some Service")})
public class AddTheNodeWorkflow implements WorkflowProcess {
@Override
public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
try {
final String payloadPath = getPayloadPath(workItem);
final Session session = workflowSession.adaptTo(Session.class);
// get the node for the workflow payload
final Node payloadNode = session.getNode(payloadPath);
// add the node
final Node somenode = payloadNode.addNode("somenode");
somenode.setProperty("myproperty", "my property value");
} catch (Exception e) {
e.printStackTrace();
}
}
private String getPayloadPath(WorkItem workItem) {
return workItem.getWorkflowData().getPayload().toString();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With