001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.mail.web.actions;
021
022import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.MAIL_FOLDER_TYPE;
023import static org.nuxeo.ecm.platform.mail.web.utils.MailWebConstants.CURRENT_PAGE;
024
025import java.io.Serializable;
026
027import javax.mail.MessagingException;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.seam.ScopeType;
032import org.jboss.seam.annotations.In;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.core.Events;
036import org.jboss.seam.faces.FacesMessages;
037import org.jboss.seam.international.StatusMessage;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.platform.mail.utils.MailCoreHelper;
041import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
042import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
043
044/**
045 * Handles mail actions.
046 *
047 * @author Catalin Baican
048 */
049@Name("mailActions")
050@Scope(ScopeType.CONVERSATION)
051public class MailActionsBean implements Serializable {
052
053    private static final long serialVersionUID = 1L;
054
055    private static final Log log = LogFactory.getLog(MailActionsBean.class);
056
057    @In(create = true, required = false)
058    protected transient NavigationContext navigationContext;
059
060    @In(create = true, required = false)
061    protected transient CoreSession documentManager;
062
063    @In(create = true, required = false)
064    protected transient FacesMessages facesMessages;
065
066    @In(create = true)
067    protected transient ResourcesAccessor resourcesAccessor;
068
069    public String checkCurrentInbox() {
070        DocumentModel mailFolder = navigationContext.getCurrentDocument();
071
072        try {
073            MailCoreHelper.checkMail(mailFolder, documentManager);
074        } catch (MessagingException e) {
075            log.debug(e, e);
076            facesMessages.add(StatusMessage.Severity.ERROR,
077                    resourcesAccessor.getMessages().get("feedback.check.mail.error") + e.getMessage());
078
079            return CURRENT_PAGE;
080        }
081
082        facesMessages.add(StatusMessage.Severity.INFO,
083                resourcesAccessor.getMessages().get("feedback.check.mail.success"));
084        Events.instance().raiseEvent("documentChildrenChanged");
085
086        return CURRENT_PAGE;
087    }
088
089    public boolean isMailFolder() {
090        DocumentModel currentDocument = navigationContext.getCurrentDocument();
091        return MAIL_FOLDER_TYPE.equals(currentDocument.getType());
092    }
093
094}