BrightSide Workbench Full Report + Source Code
NewsletterTemplate.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.newsletter;
19 
20 import java.io.Closeable;
21 import java.io.File;
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;
39 
44 public class NewsletterTemplate implements Closeable {
45 
46  private final Newsletter newsletter;
47  private File template;
48 
49  public NewsletterTemplate(Newsletter newsletter) {
50  this.newsletter = newsletter;
51  newsletter.resetSections();
52  }
53 
55  try {
56  if(pool == null) pool = MailProviders.instance().getGeneric();
58  mmt.setProcessLiveLinks(true);
59  mmt.setSubject(getTitle());
60  mmt.addTo(contact.getEmail(), contact.getName());
62  em.put("newsletter", newsletter);
63  for(NewsSection ns : newsletter.getNewsSections()) {
64  em.put("newsSection" + ns.getNewsOrder(), ns);
65  }
66  em.put("contact", contact);
67  em.put("email", contact.getEmail());
68  mmt.setRoot("/newsletter/tmp");
69  mmt.setMessage(em, getTemplate(contact), contact);
70  return mmt;
71  } catch (EmailException ex) {
72  Logger.getLogger(NewsletterTemplate.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
73  return null;
74  }
75  }
76 
77  public String getTemplate(IContact contact) {
78  try {
79  if(template == null) {
80  File rootTmp = new File(ElephantContext.getRealPath("/WEB-INF/elephant/templates-mail/content/newsletter/tmp"));
81  rootTmp.mkdirs();
82  template = File.createTempFile("news", "Template.html", rootTmp);
83  try (PrintWriter fw = new PrintWriter(template, ElephantContext.getEncoding())) {
84  newsletter.generateNewsletter(contact, fw);
85  }
86  }
87  return extractName(template);
88  } catch (IOException ex) {
89  Logger.getLogger(NewsletterTemplate.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  return null;
91  }
92  }
93 
94  public void releaseTemplate() {
95  if(template != null) {
96  template.delete();
97  }
98  }
99 
100  private String extractName(File file) {
101  String name = file.getName();
102  return name.substring(0, name.length() - "Template.html".length());
103  }
104 
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()));
109  }
110  if(title.contains("{datetime}")) {
111  title = title.replaceAll("\\{datetime\\}", DateFormats.format(new Date(), "SHORT", "SHORT", Application.getUsedLocale()));
112  }
113  if(title.contains("{month}")) {
114  title = title.replaceAll("\\{month\\}", new CheckDate().formatDate("MMMM"));
115  }
116  if(title.contains("{year}")) {
117  title = title.replaceAll("\\{year\\}", new CheckDate().formatDate("YYYY"));
118  }
119  if(title.contains("{day}")) {
120  title = title.replaceAll("\\{day\\}", new CheckDate().formatDate("dd"));
121  }
122  if(title.contains("{week}")) {
123  title = title.replaceAll("\\{week\\}", new CheckDate().formatDate("ww"));
124  }
125  if(title.contains("{site}")) {
126  title = title.replaceAll("\\{site\\}", ElephantContext.getSiteName());
127  }
128  return title;
129  }
130 
131  @Override
132  public void close() throws IOException {
133  releaseTemplate();
134  }
135 
136 }
Object put(Object key, Object value)
MailMessageTemplate getMailMessage(MailMessagePool pool, IContact contact)
void generateNewsletter(IContact contact, Writer writer)