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