001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *     Vilogia - Mail dedupe
019 *
020 */
021
022package org.nuxeo.ecm.platform.mail.listener.action;
023
024import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PARENT_PATH_KEY;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.common.utils.Path;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.query.sql.NXQL;
032import org.nuxeo.ecm.platform.mail.action.ExecutionContext;
033import org.nuxeo.ecm.platform.mail.utils.MailCoreConstants;
034
035/**
036 * This class checks whether a mail is a duplicate of a previously stored mail document. The mail is considered a
037 * duplicate if the sender, sending_date, title, text and containing folder are the same. This should fit for most uses.
038 *
039 * @author <a href="mailto:christophe.capon@vilogia.fr">Christophe Capon</a>
040 * @author <a href="mailto:ldoguin@nuxeo.com">Laurent Doguin</a>
041 */
042public class CheckMailUnicity extends AbstractMailAction {
043
044    private static final Log log = LogFactory.getLog(CheckMailUnicity.class);
045
046    public static final String MAIL_SEARCH_QUERY = "SELECT * FROM MailMessage "
047            + "WHERE mail:messageId = %s AND ecm:path STARTSWITH %s AND ecm:isProxy = 0 ";
048
049    @Override
050    public boolean execute(ExecutionContext context) {
051
052        CoreSession session = getCoreSession(context);
053        if (session == null) {
054            log.error("Could not open CoreSession");
055            return false;
056        }
057
058        ExecutionContext initialContext = context.getInitialContext();
059        Path parentPath = new Path((String) initialContext.get(PARENT_PATH_KEY));
060
061        // Get the fields used for dedupe
062        String messageId = (String) context.get(MailCoreConstants.MESSAGE_ID_KEY);
063
064        // Build the query
065        // We intentionally include the deleted documents in the search
066        // If they have been deleted we do not reinject them in the
067        // mail folder
068        StringBuilder query = new StringBuilder();
069        query.append(String.format(MAIL_SEARCH_QUERY, NXQL.escapeString(messageId),
070                NXQL.escapeString(parentPath.toString())));
071
072        DocumentModelList duplicatedMail = session.query(query.toString());
073
074        if (!duplicatedMail.isEmpty()) {
075            // Current Mail is a duplicate
076            return false;
077        }
078        return true;
079    }
080
081}