001/* 002 * (C) Copyright 2006-2014 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.listener.action; 021 022import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.ATTACHMENTS_KEY; 023import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PARENT_PATH_KEY; 024import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.SUBJECT_KEY; 025 026import java.util.Collections; 027import java.util.List; 028import java.util.regex.Pattern; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.common.utils.IdUtils; 033import org.nuxeo.ecm.automation.AutomationService; 034import org.nuxeo.ecm.automation.OperationContext; 035import org.nuxeo.ecm.automation.OperationException; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.NuxeoException; 039import org.nuxeo.ecm.core.api.PathRef; 040import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 041import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 042import org.nuxeo.ecm.platform.mail.action.ExecutionContext; 043import org.nuxeo.ecm.platform.mail.listener.action.AbstractMailAction; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * Creates a MailMessage document for every new email found in the INBOX. The creation is handled by an AutomationChain 048 * 049 * @since 6.0 050 * @author tiry 051 */ 052public class CreateDocumentsFromAutomationChainAction extends AbstractMailAction { 053 054 private static final Log log = LogFactory.getLog(CreateDocumentsFromAutomationChainAction.class); 055 056 protected String chainName; 057 058 public CreateDocumentsFromAutomationChainAction(String chainName) { 059 super(); 060 this.chainName = chainName; 061 } 062 063 public Pattern stupidRegexp = Pattern.compile("^[- .,;?!:/\\\\'\"]*$"); 064 065 protected String getChainName() { 066 if (chainName == null) { 067 return Framework.getProperty("org.nuxeo.mail.automation.chain", "CreateMailDocumentFromAutomation"); 068 } 069 return chainName; 070 } 071 072 protected String generateMailName(String subject) { 073 PathSegmentService pss = Framework.getLocalService(PathSegmentService.class); 074 return pss.generatePathSegment(subject + System.currentTimeMillis() % 10000); 075 } 076 077 @Override 078 public boolean execute(ExecutionContext context) { 079 CoreSession session = getCoreSession(context); 080 if (session == null) { 081 log.error("Could not open CoreSession"); 082 return false; 083 } 084 085 AutomationService as = Framework.getService(AutomationService.class); 086 087 OperationContext automationCtx = new OperationContext(session); 088 automationCtx.putAll(context); 089 090 ExecutionContext initialContext = context.getInitialContext(); 091 String parentPath = (String) initialContext.get(PARENT_PATH_KEY); 092 DocumentModel mailFolder = session.getDocument(new PathRef(parentPath)); 093 automationCtx.put("mailFolder", mailFolder); 094 automationCtx.put("executionContext", initialContext); 095 String subject = (String) context.get(SUBJECT_KEY); 096 automationCtx.put("mailDocumentName", generateMailName(subject)); 097 098 @SuppressWarnings("unchecked") 099 List<FileBlob> attachments = (List<FileBlob>) context.get(ATTACHMENTS_KEY); 100 if (attachments == null) { 101 automationCtx.put(ATTACHMENTS_KEY, Collections.EMPTY_LIST); 102 } 103 104 try { 105 as.run(automationCtx, getChainName()); 106 } catch (OperationException e) { 107 throw new NuxeoException(e); 108 } 109 110 return true; 111 } 112 113}