View Javadoc

1   /**
2    * 2009, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.utils.mail;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import javax.mail.MessagingException;
12  import javax.mail.internet.AddressException;
13  
14  import pt.digitalis.dif.utils.ObjectFormatter;
15  import pt.digitalis.utils.pools.AbstractAction;
16  
17  /**
18   * A send mail action object
19   * 
20   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
21   * @created Feb 18, 2009
22   */
23  public class MailAction extends AbstractAction {
24  
25      /** The Serial version id for Serializable */
26      static private final long serialVersionUID = 429211122232L;
27  
28      /** The mail address BCC field */
29      private String addressBCC;
30  
31      /** The mail address CC field */
32      private String addressCC;
33  
34      /** The mail address FROM field */
35      private String addressFrom;
36  
37      /** The mail address TO field */
38      private String addressTo;
39  
40      /** The mail body content */
41      private String body;
42  
43      /** The Mail sender Instance */
44      private transient MailSender mailSenderInstance;
45  
46      /** The mail subject */
47      private String subject;
48  
49      /** The mail MIME type */
50      private MailType type;
51  
52      /**
53       * @throws MessagingException
54       * @throws AddressException
55       * @see pt.digitalis.utils.pools.AbstractAction#doExecute()
56       */
57      @Override
58      protected AbstractAction doExecute() throws AddressException, MessagingException
59      {
60          this.getMailSenderInstance().sendEmail(getType(), getAddressFrom(), getAddressTo(), getAddressCC(),
61                  getAddressBCC(), getSubject(), getBody(), null, null);
62  
63          return this;
64      }
65  
66      /**
67       * Inspector for the 'adressBCC' attribute.
68       * 
69       * @return the adressBCC value
70       */
71      public String getAddressBCC()
72      {
73          return addressBCC;
74      }
75  
76      /**
77       * Inspector for the 'adressCC' attribute.
78       * 
79       * @return the adressCC value
80       */
81      public String getAddressCC()
82      {
83          return addressCC;
84      }
85  
86      /**
87       * Inspector for the 'adressFrom' attribute.
88       * 
89       * @return the adressFrom value
90       */
91      public String getAddressFrom()
92      {
93          return addressFrom;
94      }
95  
96      /**
97       * Inspector for the 'adressTo' attribute.
98       * 
99       * @return the adressTo value
100      */
101     public String getAddressTo()
102     {
103         return addressTo;
104     }
105 
106     /**
107      * Inspector for the 'body' attribute.
108      * 
109      * @return the body value
110      */
111     public String getBody()
112     {
113         return body;
114     }
115 
116     /**
117      * Inspector for the 'mailSenderInstance' attribute.
118      * 
119      * @return the mailSenderInstance value
120      */
121     public MailSender getMailSenderInstance()
122     {
123         return mailSenderInstance;
124     }
125 
126     /**
127      * @see pt.digitalis.utils.pools.AbstractAction#getProperties()
128      */
129     public Map<String, Object> getProperties()
130     {
131         Map<String, Object> props = new HashMap<String, Object>();
132         props.put("From", getAddressFrom());
133         props.put("To", getAddressTo());
134         props.put("CC", getAddressCC());
135         props.put("BCC", getAddressBCC());
136         props.put("Subject", getSubject());
137         props.put("Type", getType().toString());
138         props.put("Body", getBody());
139 
140         return props;
141     }
142 
143     /**
144      * Inspector for the 'subject' attribute.
145      * 
146      * @return the subject value
147      */
148     public String getSubject()
149     {
150         return subject;
151     }
152 
153     /**
154      * Inspector for the 'type' attribute.
155      * 
156      * @return the type value
157      */
158     public MailType getType()
159     {
160         return type;
161     }
162 
163     /**
164      * Modifier for the 'adressBCC' attribute.
165      * 
166      * @param adressBCC
167      *            the new adressBCC value to set
168      */
169     public void setAddressBCC(String adressBCC)
170     {
171         this.addressBCC = adressBCC;
172     }
173 
174     /**
175      * Modifier for the 'adressCC' attribute.
176      * 
177      * @param adressCC
178      *            the new adressCC value to set
179      */
180     public void setAddressCC(String adressCC)
181     {
182         this.addressCC = adressCC;
183     }
184 
185     /**
186      * Modifier for the 'adressFrom' attribute.
187      * 
188      * @param adressFrom
189      *            the new adressFrom value to set
190      */
191     public void setAddressFrom(String adressFrom)
192     {
193         this.addressFrom = adressFrom;
194     }
195 
196     /**
197      * Modifier for the 'adressTo' attribute.
198      * 
199      * @param adressTo
200      *            the new adressTo value to set
201      */
202     public void setAddressTo(String adressTo)
203     {
204         this.addressTo = adressTo;
205     }
206 
207     /**
208      * Modifier for the 'body' attribute.
209      * 
210      * @param body
211      *            the new body value to set
212      */
213     public void setBody(String body)
214     {
215         this.body = body;
216     }
217 
218     /**
219      * Modifier for the 'mailSenderInstance' attribute.
220      * 
221      * @param mailSenderInstance
222      *            the new mailSenderInstance value to set
223      */
224     public void setMailSenderInstance(MailSender mailSenderInstance)
225     {
226         this.mailSenderInstance = mailSenderInstance;
227     }
228 
229     /**
230      * Modifier for the 'subject' attribute.
231      * 
232      * @param subject
233      *            the new subject value to set
234      */
235     public void setSubject(String subject)
236     {
237         this.subject = subject;
238     }
239 
240     /**
241      * Modifier for the 'type' attribute.
242      * 
243      * @param type
244      *            the new type value to set
245      */
246     public void setType(MailType type)
247     {
248         this.type = type;
249     }
250 
251     /**
252      * @see java.lang.Object#toString()
253      */
254     @Override
255     public String toString()
256     {
257         ObjectFormatter formatter = new ObjectFormatter();
258         formatter.addItem("ID", getId());
259         formatter.addItem("Status", getStatus());
260         formatter.addItem("Reason", getReason());
261         formatter.addItem("Reason Exception", getReasonException());
262 
263         formatter.addItemIfNotNull("Mail From", addressFrom);
264         formatter.addItemIfNotNull("Mail To", addressTo);
265         formatter.addItemIfNotNull("Mail CC", addressCC);
266         formatter.addItemIfNotNull("Mail BCC", addressBCC);
267         formatter.addItemIfNotNull("Mail Body", body);
268         formatter.addItemIfNotNull("Mail Subject", subject);
269         formatter.addItemIfNotNull("Mail Type", type);
270         formatter.addItem("Enrollment Time", getEnrollmentTime());
271         formatter.addItemIfNotNull("Execution Start Time", getExecutionStartTime());
272         formatter.addItemIfNotNull("Execution End Time", getExecutionEndTime());
273         formatter.addItemIfNotNull("Expiration Time", getExpirationTime());
274         formatter.addItemIfNotNull("Waiting Threads", getWaitingThreads());
275 
276         return formatter.getFormatedObject();
277     }
278 }