sending email for expired documents
I've followed this article in Nuxeo Blog for sending amail when some documents are expired, Here is what i've done
contract-scheduler-contrib.xml :
<?xml version="1.0"?>
<component name="org.nuxeo.sample.scheduler.notifContract">
<extension
target="org.nuxeo.ecm.core.scheduler.SchedulerService"
point="schedule">
<schedule id="notifContractScehdulerId">
<username>Administrator</username>
<eventId>notifContract</eventId>
<eventCategory>default</eventCategory>
<cronExpression>0 * * * * ?</cronExpression>
</schedule>
</extension>
</component>
contract-listener-contrib.xml
<?xml version="1.0"?>
<component name="org.nuxeo.sample.listener.contrib.notifContract">
<extension target="org.nuxeo.ecm.core.event.EventServiceComponent"
point="listener">
<listener name="notifContractListener" async="true" postCommit="false" priority="120"
class="org.nuxeo.sample.restAPI.ContractNotifListener" >
<event>notifContract</event>
</listener>
</extension>
</component>
contract-notif-contrib.xml :
<?xml version="1.0"?>
<component
name="org.nuxeo.sample.contract.notifcontrib">
<extension
target="org.nuxeo.ecm.platform.ec.notification.service.NotificationService"
point="notifications">
<notification name="Contract Expired" channel="email" enabled="true" availableIn="Workspace"
autoSubscribed="false" template="contractExpired" subject="Contract expired" label="label.nuxeo.notifications.contractExpired">
<event name="contractExpired"/>
</notification>
</extension>
<extension
target="org.nuxeo.ecm.platform.ec.notification.service.NotificationService"
point="templates">
<template name="contractExpired" src="templates/contractExpired.ftl" />
</extension>
</component>
ContractNotifListener.java
package org.nuxeo.sample.restAPI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.event.Event;
import org.nuxeo.ecm.core.event.EventListener;
import org.nuxeo.ecm.core.event.EventService;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
import org.nuxeo.runtime.api.Framework;
public class ContractNotifListener implements EventListener {
private static final String QUERY_CONTRACTS = "Select * From Document WHERE ecm:isCheckedInVersion = 0 AND ecm:currentLifeCycleState != 'deleted' AND dc:expired = DATE '%s'";
public void handleEvent(Event event) throws ClientException {
CoreSession coreSession = event.getContext().getCoreSession();
Calendar expirationDate = Calendar.getInstance();
expirationDate.add(Calendar.DATE, 1);
Date now = expirationDate.getTime();
String date = new SimpleDateFormat("yyyy-MM-dd").format(now);
String query = String.format(QUERY_CONTRACTS, date);
DocumentModelList contracts = coreSession.query(query);
EventService eventService;
try {
eventService = Framework.getService(EventService.class);
for (DocumentModel contract : contracts) {
DocumentEventContext ctx = new DocumentEventContext(
coreSession, coreSession.getPrincipal(), contract);
Event contractExpiredEvent = ctx.newEvent("contractExpired");
eventService.fireEvent(contractExpiredEvent);
}
} catch (Exception e) {
throw new RuntimeException("could not get the EventService", e);
}
}
}
### MANIEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 1
Bundle-Name: Nuxeo Sample project
Bundle-SymbolicName: org.nuxeo.project.sample;singleton:=true
Bundle-Version: 1.0.0
Bundle-Vendor: Nuxeo
Provide-Package: org.nuxeo.project.sample
Require-Bundle: org.nuxeo.runtime,
org.nuxeo.ecm.core.api,
org.nuxeo.ecm.core,
org.nuxeo.ecm.webapp.core
Nuxeo-Component: OSGI-INF/contract-scheduler-contrib.xml,
OSGI-INF/contract-listener-contrib.xml,
OSGI-INF/contract-notif-contrib.xml,
OSGI-INF/core-types-contrib.xml,
OSGI-INF/lifecycle-contrib.xml,
OSGI-INF/ui-types-contrib.xml,
OSGI-INF/login-contrib.xml,
OSGI-INF/theme-contrib.xml,
OSGI-INF/my-directories-contrib.xml,
OSGI-INF/layouts-contrib.xml,
OSGI-INF/actions-contrib.xml,
OSGI-INF/contentviews-contrib.xml,
OSGI-INF/versioningrules-contrib.xml,
OSGI-INF/workflow-contrib.xml,
OSGI-INF/directory-layout-contrib.xml
However, i can't get the email , the code is not executed and i have nothing in my log file, can you tell what i'm missing ? Any help would appreciated.
Update :
I've tried sending alert using documentCreated event that i send when clicking in a button, so i end up noticing that i have a problem with the Scheduler or with the new event 'contractExpired' . I want to mention that in those lines
Event contractExpiredEvent = ctx.newEvent("contractExpired");
eventService.fireEvent(event);
we should have eventService.fireEvent(contractExpiredEvent);
method, and activate the logs for the
category in your log4j.xml file. Also, change your cron expression to something like
so the event will be triggered every minute. Maybe you have already changed it, but if it is still at 5:20pm, it's going to be a long wait.
To add logs, as I said, add the category in your log4j.xml, with a DEBUG level. Then, in your ContractNotifListener, you should add some logs lines. You will find an example here, lines 112-114:
`0 * * * * ?
` but still not workingThank you for your help
DocumentModelList contracts = coreSession.query(query);
coreSession is nulli've tried this code ``` EventContext ctx = event.getContext();
It worked using