session is null when trying to get it from event

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

 if (!(ctx instanceof DocumentEventContext)) {
            return;
        }  

is true so the rest of the code is not executed . Here is the entire code of the event listener:

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

        }
0 votes

0 answers

1467 views

ANSWER