Search in sources :

Example 1 with DBMailLight

use of org.olat.core.util.mail.model.DBMailLight in project OpenOLAT by OpenOLAT.

the class MailBoxExtension method deleteUserData.

@Override
public void deleteUserData(Identity identity, String newDeletedUserName, File archivePath) {
    // set as deleted all recipients
    log.info("Delete intern messages");
    Collection<DBMailLight> inbox = new HashSet<DBMailLight>(mailManager.getInbox(identity, null, Boolean.FALSE, null, 0, 0));
    for (DBMailLight inMail : inbox) {
        mailManager.delete(inMail, identity, true);
    }
    Collection<DBMailLight> outbox = new HashSet<DBMailLight>(mailManager.getOutbox(identity, 0, 0, false));
    for (DBMailLight outMail : outbox) {
        mailManager.delete(outMail, identity, true);
    }
    log.info("Delete " + inbox.size() + " messages in INBOX and " + outbox.size() + " in OUTBOX");
}
Also used : DBMailLight(org.olat.core.util.mail.model.DBMailLight) HashSet(java.util.HashSet)

Example 2 with DBMailLight

use of org.olat.core.util.mail.model.DBMailLight in project OpenOLAT by OpenOLAT.

the class MailListController method event.

@Override
protected void event(UserRequest ureq, Controller source, Event event) {
    if (source == tableCtr) {
        if (event.getCommand().equals(Table.COMMANDLINK_ROWACTION_CLICKED)) {
            TableEvent te = (TableEvent) event;
            String actionid = te.getActionId();
            int rowid = te.getRowId();
            DBMailLight mail = (DBMailLight) tableCtr.getTableDataModel().getObject(rowid);
            if (CMD_READ.equals(actionid)) {
                if (outbox && StringHelper.containsNonWhitespace(mail.getMetaId()) && !mail.getMetaId().equals(metaId)) {
                    selectMetaMail(ureq, mail.getMetaId());
                } else {
                    selectMail(ureq, mail.getKey());
                }
            } else if (CMD_PROFILE.equals(actionid)) {
                DBMailRecipient from = mail.getFrom();
                if (from != null && from.getRecipient() != null) {
                    contextResolver.open(ureq, getWindowControl(), "[Identity:" + from.getRecipient().getKey() + "]");
                }
            } else if (CMD_MARK_TOGGLE.equals(actionid)) {
                mail = mailManager.toggleMarked(mail, getIdentity());
                replaceInModel(mail);
            } else if (CMD_READ_TOGGLE.equals(actionid)) {
                mail = mailManager.toggleRead(mail, getIdentity());
                replaceInModel(mail);
            }
        } else if (event.getCommand().equals(Table.COMMAND_MULTISELECT)) {
            // Multiselect events
            TableMultiSelectEvent tmse = (TableMultiSelectEvent) event;
            BitSet selectedMails = tmse.getSelection();
            if (selectedMails.isEmpty()) {
                tableVC.setDirty(true);
                showWarning("mail.action.emtpy");
                return;
            }
            String actionid = tmse.getAction();
            if (CMD_DELETE.equals(actionid)) {
                String title = translate("mail.confirm.delete.title");
                int selected = selectedMails.cardinality();
                String text;
                if (selected == 1) {
                    text = translate("mail.confirm.delete.single.text");
                } else {
                    text = translate("mail.confirm.delete.multi.text", selected + "");
                }
                deleteConfirmationBox = activateYesNoDialog(ureq, title, text, deleteConfirmationBox);
                deleteConfirmationBox.setUserObject(selectedMails);
            } else if (CMD_SEND_REAL_MAIL.equals(actionid)) {
                for (int i = selectedMails.nextSetBit(0); i >= 0; i = selectedMails.nextSetBit(i + 1)) {
                    DBMailLight mail = (DBMailLight) tableCtr.getTableDataModel().getObject(i);
                    MailerResult result = forwardToMyRealMail(mail);
                    if (result.getReturnCode() != MailerResult.OK) {
                        MailHelper.printErrorsAndWarnings(result, getWindowControl(), ureq.getUserSession().getRoles().isOLATAdmin(), getLocale());
                    } else {
                        showInfo("mail.action.send.real.success", mail.getSubject());
                    }
                }
                reloadModel();
            } else if (CMD_MARK_MARKED.equals(actionid) || CMD_MARK_UNMARKED.equals(actionid)) {
                for (int i = selectedMails.nextSetBit(0); i >= 0; i = selectedMails.nextSetBit(i + 1)) {
                    DBMailLight mail = (DBMailLight) tableCtr.getTableDataModel().getObject(i);
                    mailManager.setMarked(mail, CMD_MARK_MARKED.equals(actionid), getIdentity());
                }
                reloadModel();
            } else if (CMD_MARK_READ.equals(actionid) || CMD_MARK_UNREAD.equals(actionid)) {
                for (int i = selectedMails.nextSetBit(0); i >= 0; i = selectedMails.nextSetBit(i + 1)) {
                    DBMailLight mail = (DBMailLight) tableCtr.getTableDataModel().getObject(i);
                    mailManager.setRead(mail, CMD_MARK_READ.equals(actionid), getIdentity());
                }
                reloadModel();
            }
        } else if (TableController.EVENT_FILTER_SELECTED == event) {
            MailDataModel dataModel = (MailDataModel) tableCtr.getTableDataModel();
            MailContextShortName filter = (MailContextShortName) tableCtr.getActiveFilter();
            dataModel.filter(filter);
            tableCtr.setTableDataModel(dataModel);
        } else if (TableController.EVENT_NOFILTER_SELECTED == event) {
            MailDataModel dataModel = (MailDataModel) tableCtr.getTableDataModel();
            dataModel.filter(null);
            tableCtr.setTableDataModel(dataModel);
        }
    } else if (source == mailCtr) {
        backFromMail();
    } else if (source == metaMailCtr) {
        removeAsListenerAndDispose(metaMailCtr);
        metaMailCtr = null;
        mainVC.put(MAIN_CMP, tableVC);
    } else if (source == deleteConfirmationBox) {
        if (DialogBoxUIFactory.isYesEvent(event)) {
            BitSet deleteMails = (BitSet) deleteConfirmationBox.getUserObject();
            for (int i = deleteMails.nextSetBit(0); i >= 0; i = deleteMails.nextSetBit(i + 1)) {
                DBMailLight mail = (DBMailLight) tableCtr.getTableDataModel().getObject(i);
                // reload the message
                mail = mailManager.getMessageByKey(mail.getKey());
                boolean deleteMetaMail = outbox && !StringHelper.containsNonWhitespace(metaId);
                mailManager.delete(mail, getIdentity(), deleteMetaMail);
            // Do not remove from model to prevent concurrent modification
            // exception, instead just reload model afterwards
            }
            reloadModel();
        }
    } else {
        super.event(ureq, source, event);
    }
}
Also used : TableEvent(org.olat.core.gui.components.table.TableEvent) MailerResult(org.olat.core.util.mail.MailerResult) TableMultiSelectEvent(org.olat.core.gui.components.table.TableMultiSelectEvent) BitSet(java.util.BitSet) DBMailLight(org.olat.core.util.mail.model.DBMailLight) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient)

Example 3 with DBMailLight

use of org.olat.core.util.mail.model.DBMailLight in project OpenOLAT by OpenOLAT.

the class MailListController method reloadModel.

private void reloadModel() {
    List<DBMailLight> mails;
    if (outbox) {
        if (StringHelper.containsNonWhitespace(metaId)) {
            mails = mailManager.getEmailsByMetaId(metaId);
        } else {
            mails = mailManager.getOutbox(getIdentity(), 0, -1, true);
        }
        // strip meta emails
        Set<String> metaIds = new HashSet<>();
        for (Iterator<DBMailLight> it = mails.iterator(); it.hasNext(); ) {
            DBMailLight mail = it.next();
            if (StringHelper.containsNonWhitespace(mail.getMetaId())) {
                if (metaIds.contains(mail.getMetaId())) {
                    it.remove();
                } else {
                    metaIds.add(mail.getMetaId());
                }
            }
        }
    } else {
        mails = mailManager.getInbox(getIdentity(), null, Boolean.TRUE, null, 0, -1);
    }
    // extract contexts
    Map<String, String> bpToContexts = new HashMap<>();
    for (DBMailLight mail : mails) {
        String businessPath = mail.getContext().getBusinessPath();
        if (StringHelper.containsNonWhitespace(businessPath) && !bpToContexts.containsKey(businessPath)) {
            String contextName = contextResolver.getName(businessPath, getLocale());
            if (StringHelper.containsNonWhitespace(contextName)) {
                bpToContexts.put(businessPath, contextName);
            }
        }
    }
    if (!bpToContexts.isEmpty()) {
        List<ShortName> filters = new ArrayList<>();
        Map<String, MailContextShortName> uniqueNames = new HashMap<>();
        ShortName allContextFilter = new MailContextShortName("-");
        filters.add(allContextFilter);
        for (Map.Entry<String, String> entry : bpToContexts.entrySet()) {
            String businessPath = entry.getKey();
            String contextName = entry.getValue();
            if (!uniqueNames.containsKey(contextName)) {
                MailContextShortName cxt = new MailContextShortName(contextName, new HashSet<String>());
                filters.add(cxt);
                uniqueNames.put(contextName, cxt);
            }
            uniqueNames.get(contextName).getBusinessPaths().add(businessPath);
        }
        tableCtr.setFilters(filters, allContextFilter);
    }
    Formatter formatter = Formatter.getInstance(getLocale());
    MailDataModel dataModel = new MailDataModel(mails, bpToContexts, getIdentity(), getTranslator(), formatter, outbox);
    tableCtr.setTableDataModel(dataModel);
}
Also used : HashMap(java.util.HashMap) Formatter(org.olat.core.util.Formatter) ArrayList(java.util.ArrayList) ShortName(org.olat.core.gui.ShortName) DBMailLight(org.olat.core.util.mail.model.DBMailLight) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 4 with DBMailLight

use of org.olat.core.util.mail.model.DBMailLight in project OpenOLAT by OpenOLAT.

the class MailDataModel method getValueAt.

@Override
public Object getValueAt(int row, int col) {
    DBMailLight mail = filteredMails == null ? mails.get(row) : filteredMails.get(row);
    Columns[] cols = Columns.values();
    if (col < cols.length) {
        switch(cols[col]) {
            case read:
                {
                    for (DBMailRecipient recipient : mail.getRecipients()) {
                        if (recipient != null && recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
                            return recipient.getRead();
                        }
                    }
                    return Boolean.FALSE;
                }
            case marked:
                {
                    for (DBMailRecipient recipient : mail.getRecipients()) {
                        if (recipient != null && recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
                            return recipient.getMarked();
                        }
                    }
                    return Boolean.FALSE;
                }
            case context:
                {
                    String businessPath = mail.getContext().getBusinessPath();
                    if (StringHelper.containsNonWhitespace(businessPath)) {
                        String contextName = bpToContexts.get(businessPath);
                        if (StringHelper.containsNonWhitespace(businessPath) && StringHelper.containsNonWhitespace(contextName)) {
                            return new ContextPair(contextName, businessPath);
                        }
                    }
                    return null;
                }
            case subject:
                return mail.getSubject();
            case receivedDate:
            case sendDate:
                {
                    return mail.getCreationDate();
                }
            case from:
                {
                    DBMailRecipient from = mail.getFrom();
                    if (from != null) {
                        if (from.getRecipient() != null) {
                            return from.getRecipient();
                        } else if (StringHelper.containsNonWhitespace(from.getGroup())) {
                            return from.getGroup();
                        } else {
                            return UserManager.getInstance().getUserDisplayEmail(from.getEmailAddress(), translator.getLocale());
                        }
                    }
                    return "-";
                }
            case recipients:
                {
                    if (StringHelper.containsNonWhitespace(mail.getMetaId())) {
                        return translator.translate("mail.from.miscellaneous");
                    }
                    StringBuilder sb = new StringBuilder();
                    Set<String> groupSet = new HashSet<>();
                    for (DBMailRecipient recipient : mail.getRecipients()) {
                        if (recipient != null && recipient.getGroup() != null) {
                            String group = recipient.getGroup();
                            if (!groupSet.contains(group)) {
                                if (sb.length() > 0)
                                    sb.append(", ");
                                sb.append(group);
                                groupSet.add(group);
                            }
                        }
                    }
                    return sb.toString();
                }
        }
    }
    return mail;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DBMailLight(org.olat.core.util.mail.model.DBMailLight) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient)

Example 5 with DBMailLight

use of org.olat.core.util.mail.model.DBMailLight in project OpenOLAT by OpenOLAT.

the class MailManagerTest method testSend_BCC.

@Test
public void testSend_BCC() {
    // send a mail to three ids
    Identity fromId = JunitTestHelper.createAndPersistIdentityAsUser("mail-7-" + UUID.randomUUID().toString());
    Identity toId_1 = JunitTestHelper.createAndPersistIdentityAsUser("mail-8-" + UUID.randomUUID().toString());
    Identity toId_2 = JunitTestHelper.createAndPersistIdentityAsUser("mail-9-" + UUID.randomUUID().toString());
    Identity toId_3 = JunitTestHelper.createAndPersistIdentityAsUser("mail-10-" + UUID.randomUUID().toString());
    ContactList ccs = new ContactList("unit-test-cc");
    ccs.add(toId_1);
    ccs.add(toId_2);
    ccs.add(toId_3);
    MailBundle bundle = new MailBundle();
    bundle.setFromId(fromId);
    bundle.setContactList(ccs);
    bundle.setContent("Hello ccList", "Content of ccList");
    MailerResult result = mailManager.sendMessage(bundle);
    Assert.assertNotNull(result);
    Assert.assertEquals(MailerResult.OK, result.getReturnCode());
    dbInstance.commitAndCloseSession();
    // retrieve the inbox of 1
    List<DBMailLight> incomingsMails = mailManager.getInbox(toId_1, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails);
    Assert.assertEquals(1, incomingsMails.size());
    DBMailLight incomingMail = incomingsMails.get(0);
    Assert.assertNotNull(incomingMail);
    Assert.assertEquals("Hello ccList", incomingMail.getSubject());
    // retrieve the inbox of 2
    List<DBMailLight> incomingsMails_2 = mailManager.getInbox(toId_2, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails_2);
    Assert.assertEquals(1, incomingsMails_2.size());
    Assert.assertEquals(incomingMail, incomingsMails_2.get(0));
    // retrieve the inbox of 3
    List<DBMailLight> incomingsMails_3 = mailManager.getInbox(toId_2, Boolean.TRUE, Boolean.TRUE, null, 0, -1);
    Assert.assertNotNull(incomingsMails_3);
    Assert.assertEquals(1, incomingsMails_3.size());
    Assert.assertEquals(incomingMail, incomingsMails_3.get(0));
}
Also used : MailerResult(org.olat.core.util.mail.MailerResult) ContactList(org.olat.core.util.mail.ContactList) Identity(org.olat.core.id.Identity) MailBundle(org.olat.core.util.mail.MailBundle) DBMailLight(org.olat.core.util.mail.model.DBMailLight) Test(org.junit.Test)

Aggregations

DBMailLight (org.olat.core.util.mail.model.DBMailLight)22 MailerResult (org.olat.core.util.mail.MailerResult)14 Test (org.junit.Test)12 Identity (org.olat.core.id.Identity)12 MailBundle (org.olat.core.util.mail.MailBundle)12 HashSet (java.util.HashSet)6 ContactList (org.olat.core.util.mail.ContactList)4 DBMailRecipient (org.olat.core.util.mail.model.DBMailRecipient)4 ArrayList (java.util.ArrayList)2 BitSet (java.util.BitSet)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Publisher (org.olat.core.commons.services.notifications.Publisher)2 SubscriptionInfo (org.olat.core.commons.services.notifications.SubscriptionInfo)2 SubscriptionListItem (org.olat.core.commons.services.notifications.model.SubscriptionListItem)2 TitleItem (org.olat.core.commons.services.notifications.model.TitleItem)2 ShortName (org.olat.core.gui.ShortName)2 TableEvent (org.olat.core.gui.components.table.TableEvent)2