package au.gov.amsa.detection.behaviour;

import java.util.concurrent.TimeUnit;

import au.gov.amsa.detection.ArbitraryId;
import au.gov.amsa.detection.model.Contact;
import au.gov.amsa.detection.model.Contact.Behaviour;
import au.gov.amsa.detection.model.Contact.Events.Create;
import au.gov.amsa.detection.model.Contact.Events.Send;
import au.gov.amsa.detection.model.DetectionMessage;
import au.gov.amsa.detection.model.DetectionRule;
import au.gov.amsa.detection.model.MessageRecipient;
import scala.concurrent.duration.Duration;

public class ContactBehaviour implements Behaviour {

    private final Contact self;
    private final ContactSender sender;

    public ContactBehaviour(Contact self, ContactSender sender) {
        this.self = self;
        this.sender = sender;
    }

    @Override
    public void onEntryCreated(Create event) {
        self.setId(ArbitraryId.next());
        self.setEmail(event.getEmail());
        self.setEmailSubjectPrefix(event.getEmailSubjectPrefix());
        self.setRetryIntervalMs(event.getRetryIntervalMs());
        MessageRecipient r = MessageRecipient.create(ArbitraryId.next());
        r.setStartTime(event.getStartTime());
        r.setEndTime(event.getEndTime());
        r.relateAcrossR16(DetectionRule.find(event.getDetectionRuleID()).get());
        r.setState(MessageRecipient.State.CREATED);
        r.relateAcrossR14(self);
        r.persist();
    }

    @Override
    public void onEntrySent(Send event) {
        DetectionMessage m = DetectionMessage.find(event.getDetectionMessageID()).get();
        try {
            sender.send(self.getEmail(), self.getEmailSubjectPrefix() + m.getSubject(),
                    m.getBody());
        } catch (RuntimeException e) {
            self.signal(event, Duration.create(self.getRetryIntervalMs(), TimeUnit.MILLISECONDS));
        }
    }

}

@@ -74,16 +74,74 @@
 eUnit;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import a
@@ -640,16 +640,104 @@
 iour %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(ContactBehaviour.class);%0A%0A
     priv
@@ -1872,24 +1872,173 @@
 eption e) %7B%0A
+            log.warn(e.getMessage(), e);%0A            // if something goes wrong (say mail host not available) we try%0A            // again in a while%0A
             

add log