001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.action;
021
022import static org.jboss.seam.ScopeType.EVENT;
023import static org.jboss.seam.ScopeType.STATELESS;
024
025import java.io.Serializable;
026
027import javax.faces.context.FacesContext;
028
029import org.jboss.seam.annotations.Factory;
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.NuxeoPrincipal;
036import org.nuxeo.ecm.platform.actions.ActionContext;
037import org.nuxeo.ecm.platform.actions.jsf.JSFActionContext;
038import org.nuxeo.ecm.platform.actions.seam.SeamActionContext;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040import org.nuxeo.ecm.platform.ui.web.util.SeamContextHelper;
041
042@Name("actionContextProvider")
043@Scope(STATELESS)
044public class ActionContextProvider implements Serializable {
045
046    private static final long serialVersionUID = 675765759871L;
047
048    @In(create = true)
049    private transient NavigationContext navigationContext;
050
051    @In(create = true)
052    private transient NuxeoPrincipal currentNuxeoPrincipal;
053
054    @In(create = true, required = false)
055    private transient CoreSession documentManager;
056
057    // XXX AT: sometimes EVENT scope is not enough when changing the current
058    // document several times in the same request. See
059    // WebActionsBean#setCurrentTabAndNavigate hack for instance.
060    @Factory(value = "currentActionContext", scope = EVENT)
061    public ActionContext createActionContext() {
062        return createActionContext(navigationContext.getCurrentDocument());
063    }
064
065    /**
066     * Returns an action context computed from current core session and current user, and document given as parameter.
067     * <p>
068     * The action context uses the JSF context if available, or fallbacks on a Seam context only (useful for Seam
069     * remoting calls, for instance in contextual menu)
070     *
071     * @since 5.7.3
072     */
073    public ActionContext createActionContext(DocumentModel document) {
074        ActionContext ctx;
075        FacesContext faces = FacesContext.getCurrentInstance();
076        if (faces == null) {
077            ctx = new SeamActionContext();
078        } else {
079            ctx = new JSFActionContext(faces);
080        }
081        ctx.setCurrentDocument(document);
082        ctx.setDocumentManager(documentManager);
083        ctx.setCurrentPrincipal(currentNuxeoPrincipal);
084        ctx.putLocalVariable("SeamContext", new SeamContextHelper());
085        return ctx;
086    }
087
088}