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

ITShine_
Star Contributor
Star Contributor

[Greg Drayon](https

Greg_Drayon
Star Contributor
Star Contributor

ITShine_
Star Contributor
Star Contributor

I changed the components order in MANIFEST.MF and the event fired ; now i'm getting

Greg_Drayon
Star Contributor
Star Contributor

As the line 28 in ContractNotifListener.class is an empty line here, I guess the code isn't up to date.

ITShine_
Star Contributor
Star Contributor

it's about this line DocumentModelList contracts = coreSession.query(query); coreSession is null

Greg_Drayon
Star Contributor
Star Contributor

Someone had the same issue [here](https

ITShine_
Star Contributor
Star Contributor

i've tried this code ```

Greg_Drayon
Star Contributor
Star Contributor

I don't know the ctx class, but you can still get a session using an unrestrictedSessionRunner. An example is in the page you followed. Maybe the example from the blog is not (anymore?) functional.

ITShine_
Star Contributor
Star Contributor

It worked using

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.