use of org.olat.core.util.mail.model.DBMailAttachment in project OpenOLAT by OpenOLAT.
the class MailManagerImpl method deleteMail.
private void deleteMail(DBMailLight mail, Identity identity, boolean forceRemoveRecipient) {
boolean delete = true;
List<DBMailRecipient> updates = new ArrayList<DBMailRecipient>();
if (mail.getFrom() != null && mail.getFrom().getRecipient() != null) {
if (identity.equalsByPersistableKey(mail.getFrom().getRecipient())) {
DBMailRecipient from = mail.getFrom();
from.setDeleted(Boolean.TRUE);
if (forceRemoveRecipient) {
from.setRecipient(null);
}
updates.add(from);
}
if (mail.getFrom().getDeleted() != null) {
delete &= mail.getFrom().getDeleted().booleanValue();
}
}
for (DBMailRecipient recipient : mail.getRecipients()) {
if (recipient == null)
continue;
if (recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
recipient.setDeleted(Boolean.TRUE);
if (forceRemoveRecipient) {
recipient.setRecipient(null);
}
updates.add(recipient);
}
if (recipient.getDeleted() != null) {
delete &= recipient.getDeleted().booleanValue();
}
}
if (delete) {
Set<String> paths = new HashSet<String>();
// all marked as deleted -> delete the mail
List<DBMailAttachment> attachments = getAttachments(mail);
for (DBMailAttachment attachment : attachments) {
// reload from the hibernate session
mail = attachment.getMail();
dbInstance.deleteObject(attachment);
if (StringHelper.containsNonWhitespace(attachment.getPath())) {
paths.add(attachment.getPath());
}
}
dbInstance.deleteObject(mail);
// try to remove orphans file
for (String path : paths) {
int count = countAttachment(path);
if (count == 0) {
VFSItem item = mailModule.getRootForAttachments().resolve(path);
if (item instanceof VFSLeaf) {
((VFSLeaf) item).delete();
}
}
}
} else {
for (DBMailRecipient update : updates) {
dbInstance.updateObject(update);
}
}
}
use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.
the class MailManagerImpl method deleteMail.
private void deleteMail(DBMailLight mail, Identity identity, boolean forceRemoveRecipient) {
boolean delete = true;
List<DBMailRecipient> updates = new ArrayList<DBMailRecipient>();
if (mail.getFrom() != null && mail.getFrom().getRecipient() != null) {
if (identity.equalsByPersistableKey(mail.getFrom().getRecipient())) {
DBMailRecipient from = mail.getFrom();
from.setDeleted(Boolean.TRUE);
if (forceRemoveRecipient) {
from.setRecipient(null);
}
updates.add(from);
}
if (mail.getFrom().getDeleted() != null) {
delete &= mail.getFrom().getDeleted().booleanValue();
}
}
for (DBMailRecipient recipient : mail.getRecipients()) {
if (recipient == null)
continue;
if (recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
recipient.setDeleted(Boolean.TRUE);
if (forceRemoveRecipient) {
recipient.setRecipient(null);
}
updates.add(recipient);
}
if (recipient.getDeleted() != null) {
delete &= recipient.getDeleted().booleanValue();
}
}
if (delete) {
Set<String> paths = new HashSet<String>();
// all marked as deleted -> delete the mail
List<DBMailAttachment> attachments = getAttachments(mail);
for (DBMailAttachment attachment : attachments) {
// reload from the hibernate session
mail = attachment.getMail();
dbInstance.deleteObject(attachment);
if (StringHelper.containsNonWhitespace(attachment.getPath())) {
paths.add(attachment.getPath());
}
}
dbInstance.deleteObject(mail);
// try to remove orphans file
for (String path : paths) {
int count = countAttachment(path);
if (count == 0) {
VFSItem item = mailModule.getRootForAttachments().resolve(path);
if (item instanceof VFSLeaf) {
((VFSLeaf) item).delete();
}
}
}
} else {
for (DBMailRecipient update : updates) {
dbInstance.updateObject(update);
}
}
}
use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.
the class MailManagerImpl method forwardToRealInbox.
@Override
public MailerResult forwardToRealInbox(Identity identity, DBMail mail, MailerResult result) {
if (result == null) {
result = new MailerResult();
}
List<DBMailAttachment> attachments = getAttachments(mail);
Address to = createAddress(identity, result, true);
MimeMessage message = createForwardMimeMessage(to, to, mail.getSubject(), mail.getBody(), attachments, result);
if (message != null) {
sendMessage(message, result);
}
return result;
}
use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.
the class MailManagerImpl method createForwardMimeMessage.
private MimeMessage createForwardMimeMessage(Address from, Address to, String subject, String body, List<DBMailAttachment> attachments, MailerResult result) {
try {
MimeMessage msg = createMessage(subject, from);
if (to != null) {
msg.addRecipient(RecipientType.TO, to);
}
if (attachments != null && !attachments.isEmpty()) {
// with attachment use multipart message
Multipart multipart = new MimeMultipart("mixed");
// 1) add body part
if (StringHelper.isHtml(body)) {
Multipart alternativePart = createMultipartAlternative(body);
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alternativePart);
multipart.addBodyPart(wrap);
} else {
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multipart.addBodyPart(messageBodyPart);
}
// 2) add attachments
for (DBMailAttachment attachment : attachments) {
// abort if attachment does not exist
if (attachment == null || attachment.getSize() <= 0) {
result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
log.error("Tried to send mail wit attachment that does not exist::" + (attachment == null ? null : attachment.getName()), null);
return msg;
}
BodyPart messageBodyPart = new MimeBodyPart();
VFSLeaf data = getAttachmentDatas(attachment);
DataSource source = new VFSDataSource(attachment.getName(), attachment.getMimetype(), data);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
msg.setContent(multipart);
} else {
// without attachment everything is easy, just set as text
if (StringHelper.isHtml(body)) {
msg.setContent(createMultipartAlternative(body));
} else {
msg.setText(body, "utf-8");
}
}
msg.setSentDate(new Date());
msg.saveChanges();
return msg;
} catch (MessagingException | UnsupportedEncodingException e) {
log.error("", e);
return null;
}
}
use of org.olat.core.util.mail.model.DBMailAttachment in project OpenOLAT by OpenOLAT.
the class MailManagerImpl method createForwardMimeMessage.
private MimeMessage createForwardMimeMessage(Address from, Address to, String subject, String body, List<DBMailAttachment> attachments, MailerResult result) {
try {
MimeMessage msg = createMessage(subject, from);
if (to != null) {
msg.addRecipient(RecipientType.TO, to);
}
if (attachments != null && !attachments.isEmpty()) {
// with attachment use multipart message
Multipart multipart = new MimeMultipart("mixed");
// 1) add body part
if (StringHelper.isHtml(body)) {
Multipart alternativePart = createMultipartAlternative(body);
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alternativePart);
multipart.addBodyPart(wrap);
} else {
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multipart.addBodyPart(messageBodyPart);
}
// 2) add attachments
for (DBMailAttachment attachment : attachments) {
// abort if attachment does not exist
if (attachment == null || attachment.getSize() <= 0) {
result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
log.error("Tried to send mail wit attachment that does not exist::" + (attachment == null ? null : attachment.getName()), null);
return msg;
}
BodyPart messageBodyPart = new MimeBodyPart();
VFSLeaf data = getAttachmentDatas(attachment);
DataSource source = new VFSDataSource(attachment.getName(), attachment.getMimetype(), data);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
msg.setContent(multipart);
} else {
// without attachment everything is easy, just set as text
if (StringHelper.isHtml(body)) {
msg.setContent(createMultipartAlternative(body));
} else {
msg.setText(body, "utf-8");
}
}
msg.setSentDate(new Date());
msg.saveChanges();
return msg;
} catch (MessagingException | UnsupportedEncodingException e) {
log.error("", e);
return null;
}
}
Aggregations