Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37477 amit 1
package com.spice.profitmandi.common.mail;
2
 
3
import com.spice.profitmandi.common.util.Utils;
4
 
5
/**
6
 * One mail to send, described in full.
7
 *
8
 * This exists to replace the twenty-odd overloads that grew across
9
 * MailOutboxService, EmailService and Utils — every combination of
10
 * to/cc/bcc/html/attachments/sender needed its own signature, and adding a
11
 * field meant adding another row of them. A caller now names only the parts it
12
 * cares about:
13
 *
14
 * <pre>
15
 *   mailQueue.queue(MailRequest.to("partner@example.com")
16
 *           .subject("Monthly target")
17
 *           .html(body)
18
 *           .from(MailSenderType.GOOGLE)
19
 *           .source("monthlyTargetForPartner"));
20
 * </pre>
21
 *
22
 * Defaults are the common case: plain text, sent through the relay. Nothing
23
 * here decides how the mail is delivered — that belongs to {@link MailQueue}.
24
 */
25
public final class MailRequest {
26
 
27
    private String[] to;
28
    private String[] cc;
29
    private String[] bcc;
30
    private String subject;
31
    private String body;
32
    private boolean html;
33
    private String source;
34
    private MailSenderType sender = MailSenderType.RELAY;
35
    private Utils.Attachment[] attachments;
36
 
37
    private MailRequest(String[] to) {
38
        this.to = to;
39
    }
40
 
41
    public static MailRequest to(String... recipients) {
42
        return new MailRequest(recipients);
43
    }
44
 
45
    public MailRequest cc(String... value) {
46
        this.cc = value;
47
        return this;
48
    }
49
 
50
    public MailRequest bcc(String... value) {
51
        this.bcc = value;
52
        return this;
53
    }
54
 
55
    public MailRequest subject(String value) {
56
        this.subject = value;
57
        return this;
58
    }
59
 
60
    /** Plain-text body. */
61
    public MailRequest body(String value) {
62
        this.body = value;
63
        this.html = false;
64
        return this;
65
    }
66
 
67
    /** HTML body. Equivalent to body(value) with the html flag set. */
68
    public MailRequest html(String value) {
69
        this.body = value;
70
        this.html = true;
71
        return this;
72
    }
73
 
74
    public MailRequest from(MailSenderType value) {
75
        this.sender = value;
76
        return this;
77
    }
78
 
79
    /**
80
     * Where this mail came from — a job or feature name. Stored on the outbox row
81
     * so a failed send can be traced back to whatever asked for it.
82
     */
83
    public MailRequest source(String value) {
84
        this.source = value;
85
        return this;
86
    }
87
 
88
    public MailRequest attach(Utils.Attachment... value) {
89
        this.attachments = value;
90
        return this;
91
    }
92
 
93
    public String[] getTo() {
94
        return to;
95
    }
96
 
97
    public String[] getCc() {
98
        return cc;
99
    }
100
 
101
    public String[] getBcc() {
102
        return bcc;
103
    }
104
 
105
    public String getSubject() {
106
        return subject;
107
    }
108
 
109
    public String getBody() {
110
        return body;
111
    }
112
 
113
    public boolean isHtml() {
114
        return html;
115
    }
116
 
117
    public String getSource() {
118
        return source;
119
    }
120
 
121
    public MailSenderType getSender() {
122
        return sender;
123
    }
124
 
125
    public Utils.Attachment[] getAttachments() {
126
        return attachments;
127
    }
128
 
129
    public boolean hasAttachments() {
130
        return attachments != null && attachments.length > 0;
131
    }
132
}