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 org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.url.DocumentViewImpl;
028import org.nuxeo.ecm.platform.url.api.DocumentView;
029import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Computes the URL of a document.
034 * <p>
035 * It expects the document to be in the context under the key "document" and the baseUrl at baseUrl. It puts the URL at
036 * key "url".
037 *
038 * @author Alexandre Russel
039 */
040public class DocumentURLAction implements MessageAction {
041
042    private static final Log log = LogFactory.getLog(DocumentURLAction.class);
043
044    protected DocumentViewCodecManager documentViewCodecManager;
045
046    protected final String baseUrl;
047
048    public DocumentURLAction(String baseUrl) {
049        this.baseUrl = baseUrl;
050    }
051
052    public boolean execute(ExecutionContext context) {
053        DocumentModel documentModel = (DocumentModel) context.get("document");
054        if (log.isDebugEnabled()) {
055            log.debug("Document url computing for doc: " + documentModel);
056        }
057        documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class);
058        DocumentView docView = new DocumentViewImpl(documentModel);
059        String url = documentViewCodecManager.getUrlFromDocumentView(docView, true, baseUrl);
060        context.put("url", url);
061        return true;
062    }
063
064    public void reset(ExecutionContext context) {
065        // do nothing
066    }
067
068}