BrightSide Workbench Full Report + Source Code
JsonMail.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.json;
20 
21 import java.util.Map;
22 import org.turro.action.Contacts;
23 import org.turro.action.IMailSender;
24 import org.turro.action.MailSenders;
25 import org.turro.json.Jsons;
26 import org.turro.string.Strings;
27 import org.turro.util.Nulls;
28 
33 public class JsonMail {
34 
35  public void pool() {
36  send(MailSenders.getPool());
37  }
38 
39  public void queue() {
40  send(MailSenders.getQueue());
41  }
42 
43  /* Compose */
44 
45  private void send(IMailSender sender) {
46  if(Nulls.any(structure.getSubject(), structure.getMessage())) return;
47  if(!Strings.isBlank(structure.getCategory())) {
48  sender.setCategory(structure.getCategory());
49  }
50  if(structure.isAdministrators()) sender.addAdministrators();
51  for(String entityPath : structure.getEntityPaths()) {
52  sender.addByEntity(entityPath, null);
53  }
54  for(String contactId : structure.getContactIds()) {
55  sender.addContact(Contacts.getContactById(contactId));
56  }
57  for(Map.Entry<String, String> user : structure.getUsers().entrySet()) {
58  sender.addUser(user.getKey(), user.getValue());
59  }
60  sender.send(structure.getSubject(), structure.getMessage());
61  }
62 
63  /* Factory */
64 
65  public static JsonMail from(Jsons structure) {
66  return from(JsonMailStructure.fromJson(structure.asJsonValue()));
67  }
68 
69  public static JsonMail from(JsonMailStructure structure) {
70  return new JsonMail(structure);
71  }
72 
73  private final JsonMailStructure structure;
74 
75  private JsonMail(JsonMailStructure structure) {
76  this.structure = structure;
77  }
78 
79 }
static IMailSender getPool()
static IMailSender getQueue()
static JsonMailStructure fromJson(JsonValue value)
static JsonMail from(Jsons structure)
Definition: JsonMail.java:65
static JsonMail from(JsonMailStructure structure)
Definition: JsonMail.java:69
T addByEntity(Object entity, Object data)
T addUser(String name, String email)
T addContact(IContact contact)
IMailSender setCategory(String idCategory)
void send(String subject, String message)