Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.mail;

import com.spice.profitmandi.common.util.Utils;

/**
 * One mail to send, described in full.
 *
 * This exists to replace the twenty-odd overloads that grew across
 * MailOutboxService, EmailService and Utils — every combination of
 * to/cc/bcc/html/attachments/sender needed its own signature, and adding a
 * field meant adding another row of them. A caller now names only the parts it
 * cares about:
 *
 * <pre>
 *   mailQueue.queue(MailRequest.to("partner@example.com")
 *           .subject("Monthly target")
 *           .html(body)
 *           .from(MailSenderType.GOOGLE)
 *           .source("monthlyTargetForPartner"));
 * </pre>
 *
 * Defaults are the common case: plain text, sent through the relay. Nothing
 * here decides how the mail is delivered — that belongs to {@link MailQueue}.
 */
public final class MailRequest {

    private String[] to;
    private String[] cc;
    private String[] bcc;
    private String subject;
    private String body;
    private boolean html;
    private String source;
    private MailSenderType sender = MailSenderType.RELAY;
    private Utils.Attachment[] attachments;

    private MailRequest(String[] to) {
        this.to = to;
    }

    public static MailRequest to(String... recipients) {
        return new MailRequest(recipients);
    }

    public MailRequest cc(String... value) {
        this.cc = value;
        return this;
    }

    public MailRequest bcc(String... value) {
        this.bcc = value;
        return this;
    }

    public MailRequest subject(String value) {
        this.subject = value;
        return this;
    }

    /** Plain-text body. */
    public MailRequest body(String value) {
        this.body = value;
        this.html = false;
        return this;
    }

    /** HTML body. Equivalent to body(value) with the html flag set. */
    public MailRequest html(String value) {
        this.body = value;
        this.html = true;
        return this;
    }

    public MailRequest from(MailSenderType value) {
        this.sender = value;
        return this;
    }

    /**
     * Where this mail came from — a job or feature name. Stored on the outbox row
     * so a failed send can be traced back to whatever asked for it.
     */
    public MailRequest source(String value) {
        this.source = value;
        return this;
    }

    public MailRequest attach(Utils.Attachment... value) {
        this.attachments = value;
        return this;
    }

    public String[] getTo() {
        return to;
    }

    public String[] getCc() {
        return cc;
    }

    public String[] getBcc() {
        return bcc;
    }

    public String getSubject() {
        return subject;
    }

    public String getBody() {
        return body;
    }

    public boolean isHtml() {
        return html;
    }

    public String getSource() {
        return source;
    }

    public MailSenderType getSender() {
        return sender;
    }

    public Utils.Attachment[] getAttachments() {
        return attachments;
    }

    public boolean hasAttachments() {
        return attachments != null && attachments.length > 0;
    }
}