19 package org.turro.zul.convocation;
21 import biweekly.ICalVersion;
22 import biweekly.ICalendar;
23 import biweekly.component.VEvent;
24 import biweekly.io.text.ICalWriter;
25 import biweekly.parameter.ParticipationStatus;
26 import biweekly.parameter.Role;
27 import biweekly.property.Classification;
28 import biweekly.property.Organizer;
29 import biweekly.property.Status;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import org.turro.string.Strings;
39 import org.apache.commons.mail.EmailException;
40 import org.turro.action.Contacts;
41 import org.turro.action.IAgreements;
42 import org.turro.action.Plugins;
43 import org.turro.action.queue.NotificationCategory;
44 import org.turro.action.queue.Notifications;
45 import org.turro.assistant.Assistant;
46 import org.turro.assistant.AssistantSet;
47 import org.turro.contacts.Attendee;
48 import org.turro.contacts.Connector;
49 import org.turro.contacts.Contact;
50 import org.turro.contacts.Convocation;
51 import org.turro.elephant.context.Application;
52 import org.turro.elephant.context.ElephantContext;
53 import org.turro.elephant.security.IUser;
54 import org.turro.log.SystemLogType;
55 import org.turro.log.SystemLogger;
56 import org.turro.mail.message.MailMessageTemplate;
57 import org.turro.mail.message.MailUtils;
58 import org.turro.mail.pool.MailMessagePool;
59 import org.turro.mail.provider.MailProviders;
60 import org.turro.mail.queue.GenericElephantNotification;
61 import org.turro.mail.queue.QueuedSender;
62 import org.turro.marker.ElephantMarker;
63 import org.turro.plugin.contacts.IContact;
64 import org.turro.www.convocation.ConvocationCtrl;
78 this.assistants =
null;
79 this.contact = contact;
84 this.assistants = assistants;
90 final List<File> tmpFiles =
new ArrayList<>();
93 String organizerEmail =
null;
100 if(contact !=
null) {
101 createMail(
new Assistant(contact, entity), tmpFiles, organizerEmail, pool);
108 createMail(assistant, tmpFiles, organizerEmail, pool);
113 for(File f : tmpFiles) {
121 private void createMail(
Assistant assistant,
final List<File> tmpFiles, String organizerEmail,
MailMessagePool pool) {
124 File tmp = generateCalendar(assistant.
contact, assistant.email);
128 if(checkWebDomain(organizerEmail)) {
131 mmt.
addTo(assistant.email, assistant.
name);
132 ElephantMarker em =
new ElephantMarker(Application.getApplication().getConstructor(),
true);
133 em.put(
"convocation", entity);
134 em.put(
"contact", assistant.
contact);
135 em.put(
"email", assistant.email);
136 em.put(
"notifier",
this);
138 mmt.
attach(tmp.getAbsolutePath(),
"invite.ics",
"invite.ics");
141 }
catch (EmailException ex) {
142 Logger.getLogger(
ConvocationSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
146 private File generateCalendar(IContact contact, String email) {
148 ICalendar cal =
new ICalendar();
149 cal.setVersion(ICalVersion.V2_0);
150 cal.setProductId(ElephantContext.getSiteName());
151 cal.setMethod(biweekly.property.Method.request());
152 VEvent
event =
new VEvent();
153 event.setUid(entity.
getId());
154 event.setStatus(Status.confirmed());
155 event.setClassification(Classification.public_());
156 event.setSummary(entity.
getName());
159 event.setDescription(MailUtils.processMacros(contact, entity.
getPlainText()));
164 if(organizer !=
null) {
165 Organizer o =
new Organizer(organizer.getName(), organizer.getConnectorMap().get(IUser.CONNECTOR_EMAIL).getValue());
166 event.setOrganizer(o);
168 if(contact !=
null && !Strings.isBlank(email)) {
169 biweekly.property.Attendee at =
new biweekly.property.Attendee(contact.getName(), email);
170 at.setRole(Role.ATTENDEE);
171 at.setParticipationStatus(ParticipationStatus.NEEDS_ACTION);
172 at.setRsvp(Boolean.TRUE);
173 event.addAttendee(at);
176 File tmp = File.createTempFile(
"attach_",
"_calendar.ics");
177 try (ICalWriter writer =
new ICalWriter(tmp, ICalVersion.V2_0)) {
182 }
catch (IOException ex) {
183 Logger.getLogger(
ConvocationSender.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
188 private boolean checkWebDomain(String organizerEmail) {
189 Pattern p = Pattern.compile(
".*[^a-zA-Z0-9-]([a-zA-Z0-9-]+\\.[a-zA-Z0-9-]+)[^a-zA-Z0-9-]*");
190 String domainServer =
null, domainMail =
null;
191 String server = ElephantContext.getServerBase(
"http");
192 Matcher m = p.matcher(server);
194 domainServer = m.group(1);
196 m = p.matcher(organizerEmail);
198 domainMail = m.group(1);
200 return !Strings.isBlank(domainServer) &&
201 !Strings.isBlank(domainMail) &&
202 domainServer.equals(domainMail);
212 assistants.
addContact(att.getIContact(), convocation);
static< T > T loadImplementation(Class< T > jclass)
static NotificationCategory getCategory(String id)
void addContact(IContact contact, Object relationEntity)
IConstructor getConstructor()
static Application getApplication()
static ISystemLogger getInstance()
void setMessage(ElephantMarker marker, IContact contact)
static MailMessageTemplate of(MailProvider provider)
void attach(String path, String description, String name)
Email setFrom(String email, String name)
Email addTo(String email, String name)
void setSubject(String subject)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailProvider getProvider()
MailMessagePool getGeneric()
static MailProviders instance()
static final String GENERIC_NOTIFICATION
static String createAttendantQrCodeURL(IConstructor constructor, IContact contact, Convocation convocation)
ConvocationSender(Convocation entity, IContact contact)
ConvocationSender(Convocation entity, AssistantSet assistants)
String createAttendantQrCode(IContact contact, Convocation convocation)
static AssistantSet getAsAssistants(Convocation convocation)
boolean canSendEmails(NotificationCategory nc)
void setContact(IContact contact)
static final String CONNECTOR_EMAIL
void doLog(SystemLogType type, Object entity, String comment, Serializable data)