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