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.CloseableCoreSession;
032import org.nuxeo.ecm.core.api.CoreInstance;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
036import org.nuxeo.ecm.core.api.security.ACL;
037import org.nuxeo.ecm.core.api.security.ACP;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @author Alexandre Russel
042 */
043public class StoreMessageAction implements MessageAction {
044
045    public static final String MAIL_MESSAGE = "MailMessage";
046
047    private static final Log log = LogFactory.getLog(StoreMessageAction.class);
048
049    protected final String parentPath;
050
051    public StoreMessageAction(String parentPath) {
052        this.parentPath = parentPath;
053    }
054
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        try (CloseableCoreSession session = CoreInstance.openCoreSessionSystem(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        }
084        return true;
085    }
086
087    protected String getMailDocumentType() {
088        return MAIL_MESSAGE;
089    }
090
091    public void reset(ExecutionContext context) {
092        // do nothing
093    }
094
095}