package com.collabnet.ctf.events.custom.events2;

import com.collabnet.ce.soap60.webservices.tracker.ArtifactSoapDO;
import com.collabnet.ctf.events.Asynchronous;
import com.collabnet.ctf.events.EventContext;
import com.collabnet.ctf.events.EventListener;
import com.collabnet.ctf.events.SoapVersion;
import com.collabnet.ce.soap60.webservices.cemain.ProjectSoapDO;

@EventListener(version = SoapVersion.SOAP_60)
public class AsynchronousListenerSample {

    @Asynchronous(topic = {"object.item.Artifact.update"}, rank=50, user="system")
    public void sendEmail(final EventContext context) throws Exception {
        context.logInfo("Category: " + context.getEventCategory());
        context.logInfo("Topic: " + context.getEventTopic());
        context.logInfo("Event Time: " + context.getEventTime());
        context.logInfo("Operation: " + context.getEventOperation());
        context.logInfo("ProjectId: " + context.getProjectId());
        context.logInfo("Comment: " + context.getComment());
        context.logInfo("UserName: " + context.getUsername());
        context.logInfo("One Time Token: " + context.getOneTimeToken());

        String projectId = context.getProjectId();

        ArtifactSoapDO originalDO = (ArtifactSoapDO) context.getOriginalData();
        ArtifactSoapDO updatedDO = (ArtifactSoapDO) context.getUpdatedData();
        String artifactId = updatedDO.getId();
        context.logInfo("Send Email:" + artifactId);
        String sender = "John";
        String senderAddress = "foo@domain.com";
        String recipient = "David";
        String toAddress = "bar@domain.com";
        String subject = artifactId + ": " + updatedDO.getTitle();
        String body = updatedDO.getDescription();
        context.sendHtmlEmail(sender, senderAddress, recipient, toAddress, subject, body, null);
    }
}
