cancel
Showing results for 
Search instead for 
Did you mean: 

sending email for expired documents

ITShine_
Star Contributor
Star Contributor

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);

28 REPLIES 28

Greg_Drayon
Star Contributor
Star Contributor

Hi! Quick question

ITShine_
Star Contributor
Star Contributor

Hi [Greg Drayon](https

Greg_Drayon
Star Contributor
Star Contributor

OK, then you can add some logs at the beginning of your

ITShine_
Star Contributor
Star Contributor

[Greg Drayon](https

Greg_Drayon
Star Contributor
Star Contributor

To add logs, as I said, add the category in your log4j.xml, with a DEBUG level.

ITShine_
Star Contributor
Star Contributor

Thank you [Greg Drayon](https

Greg_Drayon
Star Contributor
Star Contributor
  1. 'notifContract' is an event triggered by the scheduler, every day at 1 am.

ITShine_
Star Contributor
Star Contributor

okay thank you [Greg Drayon](https

Greg_Drayon
Star Contributor
Star Contributor

I would check the cron expression, but this is all I can think about. Maybe add a "year" value, such as "*"?

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.