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 * Alexandre Russel 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.mail.action; 021 022import java.util.Map; 023 024import javax.mail.Message; 025import javax.mail.MessagingException; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.ecm.core.api.CoreInstance; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 033import org.nuxeo.ecm.core.api.security.ACL; 034import org.nuxeo.ecm.core.api.security.ACP; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * @author Alexandre Russel 039 */ 040public class StoreMessageAction implements MessageAction { 041 042 public static final String MAIL_MESSAGE = "MailMessage"; 043 044 private static final Log log = LogFactory.getLog(StoreMessageAction.class); 045 046 protected final String parentPath; 047 048 public StoreMessageAction(String parentPath) { 049 this.parentPath = parentPath; 050 } 051 052 @SuppressWarnings("unchecked") 053 public boolean execute(ExecutionContext context) throws MessagingException { 054 PathSegmentService pss = Framework.getService(PathSegmentService.class); 055 Message message = context.getMessage(); 056 String title = message.getSubject(); 057 if (log.isDebugEnabled()) { 058 log.debug("Storing message: " + message.getSubject()); 059 } 060 Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader()); 061 try (CoreSession session = CoreInstance.openCoreSessionSystem(null)) { 062 DocumentModel doc = session.createDocumentModel(getMailDocumentType()); 063 doc.setProperty("dublincore", "title", title + System.currentTimeMillis()); 064 doc.setPathInfo(parentPath, pss.generatePathSegment(doc)); 065 doc.setProperty("dublincore", "title", title); 066 doc = session.createDocument(doc); 067 Map<String, Map<String, Object>> schemas = (Map<String, Map<String, Object>>) context.get("transformed"); 068 for (Map.Entry<String, Map<String, Object>> entry : schemas.entrySet()) { 069 doc.setProperties(entry.getKey(), entry.getValue()); 070 } 071 doc = session.saveDocument(doc); 072 ACL acl = (ACL) context.get("acl"); 073 if (acl != null) { 074 ACP acp = doc.getACP(); 075 acp.addACL(acl); 076 doc.setACP(acp, true); 077 } 078 session.save(); 079 context.put("document", doc); 080 } 081 return true; 082 } 083 084 protected String getMailDocumentType() { 085 return MAIL_MESSAGE; 086 } 087 088 public void reset(ExecutionContext context) { 089 // do nothing 090 } 091 092}