18 package org.turro.newsletter;
20 import java.io.Closeable;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.Date;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.amic.util.date.CheckDate;
28 import org.amic.util.date.DateFormats;
29 import org.apache.commons.mail.EmailException;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.mail.message.MailMessageTemplate;
33 import org.turro.mail.pool.MailMessagePool;
34 import org.turro.mail.provider.MailProviders;
35 import org.turro.marker.ElephantMarker;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.publication.entity.NewsSection;
38 import org.turro.publication.entity.Newsletter;
47 private File
template;
50 this.newsletter = newsletter;
62 em.
put(
"newsletter", newsletter);
64 em.
put(
"newsSection" + ns.getNewsOrder(), ns);
66 em.
put(
"contact", contact);
71 }
catch (EmailException ex) {
79 if(
template ==
null) {
82 template = File.createTempFile(
"news",
"Template.html", rootTmp);
87 return extractName(
template);
88 }
catch (IOException ex) {
95 if(
template !=
null) {
100 private String extractName(File file) {
101 String name = file.getName();
102 return name.substring(0, name.length() -
"Template.html".length());
105 private String getTitle() {
106 String title = newsletter.
getTitle();
107 if(title.contains(
"{date}")) {
108 title = title.replaceAll(
"\\{date\\}", DateFormats.format(
new Date(),
"SHORT", Application.getUsedLocale()));
110 if(title.contains(
"{datetime}")) {
111 title = title.replaceAll(
"\\{datetime\\}", DateFormats.format(
new Date(),
"SHORT",
"SHORT", Application.getUsedLocale()));
113 if(title.contains(
"{month}")) {
114 title = title.replaceAll(
"\\{month\\}",
new CheckDate().formatDate(
"MMMM"));
116 if(title.contains(
"{year}")) {
117 title = title.replaceAll(
"\\{year\\}",
new CheckDate().formatDate(
"YYYY"));
119 if(title.contains(
"{day}")) {
120 title = title.replaceAll(
"\\{day\\}",
new CheckDate().formatDate(
"dd"));
122 if(title.contains(
"{week}")) {
123 title = title.replaceAll(
"\\{week\\}",
new CheckDate().formatDate(
"ww"));
125 if(title.contains(
"{site}")) {
126 title = title.replaceAll(
"\\{site\\}", ElephantContext.getSiteName());
132 public void close() throws IOException {
IConstructor getConstructor()
static Application getApplication()
static String getRealPath(String path)
static String logMsg(String msg)
static String getEncoding()
void setProcessLiveLinks(boolean processLiveLinks)
void setMessage(ElephantMarker marker, IContact contact)
static MailMessageTemplate of(MailProvider provider)
void setRoot(String root)
Email addTo(String email, String name)
void setSubject(String subject)
MailProvider getProvider()
MailMessagePool getGeneric()
static MailProviders instance()
Object put(Object key, Object value)
NewsletterTemplate(Newsletter newsletter)
String getTemplate(IContact contact)
MailMessageTemplate getMailMessage(MailMessagePool pool, IContact contact)
Set< NewsSection > getNewsSections()
void generateNewsletter(IContact contact, Writer writer)