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?
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?
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();
}
}
© 2022 - 2024 — McMap. All rights reserved.