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    @SuppressWarnings("unchecked")
055    public boolean execute(ExecutionContext context) throws MessagingException {
056        PathSegmentService pss = Framework.getService(PathSegmentService.class);
057        Message message = context.getMessage();
058        String title = message.getSubject();
059        if (log.isDebugEnabled()) {
060            log.debug("Storing message: " + message.getSubject());
061        }
062        Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader());
063        try (CoreSession session = CoreInstance.openCoreSessionSystem(null)) {
064            DocumentModel doc = session.createDocumentModel(getMailDocumentType());
065            doc.setProperty("dublincore", "title", title + System.currentTimeMillis());
066            doc.setPathInfo(parentPath, pss.generatePathSegment(doc));
067            doc.setProperty("dublincore", "title", title);
068            doc = session.createDocument(doc);
069            Map<String, Map<String, Object>> schemas = (Map<String, Map<String, Object>>) context.get("transformed");
070            for (Map.Entry<String, Map<String, Object>> entry : schemas.entrySet()) {
071                doc.setProperties(entry.getKey(), entry.getValue());
072            }
073            doc = session.saveDocument(doc);
074            ACL acl = (ACL) context.get("acl");
075            if (acl != null) {
076                ACP acp = doc.getACP();
077                acp.addACL(acl);
078                doc.setACP(acp, true);
079            }
080            session.save();
081            context.put("document", doc);
082        }
083        return true;
084    }
085
086    protected String getMailDocumentType() {
087        return MAIL_MESSAGE;
088    }
089
090    public void reset(ExecutionContext context) {
091        // do nothing
092    }
093
094}