001/*
002 * (C) Copyright 2006-2008 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id: EndAction.java 55400 2008-05-26 09:46:02Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.mail.listener.action;
021
022import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.IMAP;
023import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.IMAPS;
024import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.LEAVE_ON_SERVER_KEY;
025import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PROTOCOL_TYPE_KEY;
026
027import javax.mail.Flags.Flag;
028import javax.mail.Message;
029import javax.mail.MessagingException;
030
031import org.nuxeo.ecm.platform.mail.action.ExecutionContext;
032
033/**
034 * @author Catalin Baican
035 */
036public class EndAction extends AbstractMailAction {
037
038    @Override
039    public boolean execute(ExecutionContext context) {
040        try {
041            Message message = context.getMessage();
042            // erase marker: mail has been treated
043            // VDU it could be nice to have a field in schema 'protocol' that says
044            // messages stay in server or not. This only work with IMAP* protocols, as POP3*
045            // protocols does not support flags other than DELETED.
046            // message.setFlag(Flag.FLAGGED, false);
047            boolean leaveOnServer = (Boolean) context.getInitialContext().get(LEAVE_ON_SERVER_KEY);
048            String protocolType = (String) context.getInitialContext().get(PROTOCOL_TYPE_KEY);
049            // log.debug(PROTOCOL_TYPE_KEY + ": " + protocolType);
050            // log.debug(LEAVE_ON_SERVER_KEY + ": " + leaveOnServer);
051            if ((IMAP.equals(protocolType) || IMAPS.equals(protocolType)) && leaveOnServer) {
052                message.setFlag(Flag.SEEN, true);
053
054            } else {
055                message.setFlag(Flag.DELETED, true);
056            }
057            return true;
058        } catch (MessagingException e) {
059            return false;
060        }
061    }
062
063}