001/* 002 * (C) Copyright 2006-2010 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 * Vilogia - Mail dedupe 017 * 018 */ 019 020package org.nuxeo.ecm.platform.mail.listener.action; 021 022import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PARENT_PATH_KEY; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.common.utils.Path; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModelList; 029import org.nuxeo.ecm.core.query.sql.NXQL; 030import org.nuxeo.ecm.platform.mail.action.ExecutionContext; 031import org.nuxeo.ecm.platform.mail.utils.MailCoreConstants; 032 033/** 034 * This class checks whether a mail is a duplicate of a previously stored mail document. The mail is considered a 035 * duplicate if the sender, sending_date, title, text and containing folder are the same. This should fit for most uses. 036 * 037 * @author <a href="mailto:christophe.capon@vilogia.fr">Christophe Capon</a> 038 * @author <a href="mailto:ldoguin@nuxeo.com">Laurent Doguin</a> 039 */ 040public class CheckMailUnicity extends AbstractMailAction { 041 042 private static final Log log = LogFactory.getLog(CheckMailUnicity.class); 043 044 public static final String MAIL_SEARCH_QUERY = "SELECT * FROM MailMessage " 045 + "WHERE mail:messageId = %s AND ecm:path STARTSWITH %s AND ecm:isProxy = 0 "; 046 047 @Override 048 public boolean execute(ExecutionContext context) { 049 050 CoreSession session = getCoreSession(context); 051 if (session == null) { 052 log.error("Could not open CoreSession"); 053 return false; 054 } 055 056 ExecutionContext initialContext = context.getInitialContext(); 057 Path parentPath = new Path((String) initialContext.get(PARENT_PATH_KEY)); 058 059 // Get the fields used for dedupe 060 String messageId = (String) context.get(MailCoreConstants.MESSAGE_ID_KEY); 061 062 // Build the query 063 // We intentionally include the deleted documents in the search 064 // If they have been deleted we do not reinject them in the 065 // mail folder 066 StringBuilder query = new StringBuilder(); 067 query.append(String.format(MAIL_SEARCH_QUERY, NXQL.escapeString(messageId), 068 NXQL.escapeString(parentPath.toString()))); 069 070 DocumentModelList duplicatedMail = session.query(query.toString()); 071 072 if (!duplicatedMail.isEmpty()) { 073 // Current Mail is a duplicate 074 return false; 075 } 076 return true; 077 } 078 079}