BrightSide Workbench Full Report + Source Code
elephant-mail/src/main/java/org/turro/mail/message/MailMessageTemplate.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.mail.message;
20 
21 import java.io.StringWriter;
22 import org.turro.mail.provider.MailProvider;
23 import org.turro.mail.provider.MailRecipient;
24 import org.turro.marker.ElephantMarker;
25 import org.turro.plugin.contacts.IContact;
26 import org.turro.string.Strings;
27 
32 public class MailMessageTemplate extends MailMessage {
33 
34  private String root;
35  private boolean processLiveLinks = true;
36 
37  public void setRoot(String root) {
38  this.root = root;
39  }
40 
41  public void setProcessLiveLinks(boolean processLiveLinks) {
42  this.processLiveLinks = processLiveLinks;
43  }
44 
45  public void setMessage(ElephantMarker marker, IContact contact) {
46  setMessage(marker, null, contact);
47  }
48 
49  public void setMessage(ElephantMarker marker, String template, IContact contact) {
50  if(contact != null && contact.isWebUser()) {
51  setMessage(marker, template, MailRecipient.of(contact));
52  }
53  }
54 
55  public void setMessage(ElephantMarker marker, String email, String name) {
56  setMessage(marker, null, MailRecipient.of(email, name));
57  }
58 
59  public void setMessage(ElephantMarker marker, String template, String email, String name) {
60  setMessage(marker, template, MailRecipient.of(email, name));
61  }
62 
63  public void setMessage(ElephantMarker marker, String template, MailRecipient recipient) {
64  StringWriter sw = new StringWriter();
65  marker.put("message", this);
66  marker.process("content" + (root == null ? "" : root), getMailTemplate(template), sw);
67  String msg = sw.toString();
68  if(processLiveLinks && !recipient.isEmpty()) {
69  msg = MailUtils.processLinks(recipient.getMail(), msg);
70  }
71  msg = MailUtils.processUser(recipient, msg);
72  msg = MailUtils.processImgWidth(msg);
73  setMessage(msg);
74  }
75 
76  public String getMailTemplate(String template) {
77  return Strings.isBlank(template) ? "default-mail" : template;
78  }
79 
80  /* Factory */
81 
83  return new MailMessageTemplate(provider);
84  }
85 
87  super(provider);
88  }
89 
90 }
void setMessage(ElephantMarker marker, String template, MailRecipient recipient)
void setMessage(ElephantMarker marker, String template, String email, String name)
static MailRecipient of(String value)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)