<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic session is null when trying to get it from event in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/session-is-null-when-trying-to-get-it-from-event/m-p/321175#M8176</link>
    <description>&lt;P&gt;I'm trying to implement a scheduler associated with an event but not able to get the session from the event. In the following code  the result of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; if (!(ctx instanceof DocumentEventContext)) {
            return;
        }  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is true so the rest of the code is not executed .
Here is the entire code of the event listener:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;package org.nuxeo.sample.restAPI;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.jboss.seam.annotations.In;
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.EventContext;
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 {
    	 
    	EventContext ctx1 = event.getContext();
        if (!(ctx1 instanceof DocumentEventContext)) {
        	PrintWriter writer0;
    		try {
    			writer0 = new PrintWriter("C:\\share\\ctx1.txt", "UTF-8");
    			writer0.println("Not instanceof DocumentEventContext ");
    			writer0.close();
    		} catch (FileNotFoundException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (UnsupportedEncodingException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            return;
            
        }
        PrintWriter writer00;
        try {
			writer00 = new PrintWriter("C:\\share\\actx1.txt", "UTF-8");
			writer00.println(" instanceof DocumentEventContext ");
			writer00.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        CoreSession coreSession = ctx1.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);
        PrintWriter writer1;
		
        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);
        }
    	
        }
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 19 Jan 2016 14:51:54 GMT</pubDate>
    <dc:creator>ITShine_</dc:creator>
    <dc:date>2016-01-19T14:51:54Z</dc:date>
    <item>
      <title>session is null when trying to get it from event</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/session-is-null-when-trying-to-get-it-from-event/m-p/321175#M8176</link>
      <description>&lt;P&gt;I'm trying to implement a scheduler associated with an event but not able to get the session from the event. In the following code  the result of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; if (!(ctx instanceof DocumentEventContext)) {
            return;
        }  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is true so the rest of the code is not executed .
Here is the entire code of the event listener:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;package org.nuxeo.sample.restAPI;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.jboss.seam.annotations.In;
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.EventContext;
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 {
    	 
    	EventContext ctx1 = event.getContext();
        if (!(ctx1 instanceof DocumentEventContext)) {
        	PrintWriter writer0;
    		try {
    			writer0 = new PrintWriter("C:\\share\\ctx1.txt", "UTF-8");
    			writer0.println("Not instanceof DocumentEventContext ");
    			writer0.close();
    		} catch (FileNotFoundException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (UnsupportedEncodingException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            return;
            
        }
        PrintWriter writer00;
        try {
			writer00 = new PrintWriter("C:\\share\\actx1.txt", "UTF-8");
			writer00.println(" instanceof DocumentEventContext ");
			writer00.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        CoreSession coreSession = ctx1.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);
        PrintWriter writer1;
		
        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);
        }
    	
        }
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2016 14:51:54 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/session-is-null-when-trying-to-get-it-from-event/m-p/321175#M8176</guid>
      <dc:creator>ITShine_</dc:creator>
      <dc:date>2016-01-19T14:51:54Z</dc:date>
    </item>
  </channel>
</rss>

