Hi, the workflow model you will create on page publish will get trigger every time you publish the page,
So to differentiate the first page publish, you can use an identifier. I have summarized the steps below.
1. Create a custom workflow model that gets triggered on page publish.
2. Create a workflow process class as you have created above
(
service = WorkflowProcess.class,
property = {
"process.label=New Page Publish Email Notification"
}
)
public class NewPagePublishEmailWorkflowProcess implements WorkflowProcess {
private MessageGatewayService messageGatewayService;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
String pagePath = workItem.getWorkflowData().getPayload().toString();
ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
Page page = resolver.getResource(pagePath).adaptTo(Page.class);
// Check if the page is being published for the first time
if(!page.getProperties().containsKey("mailTriggered")){
sendEmail();
page.getProperties().put("mailTriggered", false);
resolver.commit();
}
}
private void sendEmail() {
try {
Email email = new SimpleEmail();
email.addTo("business@example.com");
email.setSubject("A new page has been published");
email.setMsg("A new page has been published on the website.");
MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
messageGateway.send(email);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Configure the workflow model with above Workflow process class.