001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.jsf.negotiation.perspective;
016
017import javax.faces.context.FacesContext;
018
019import org.nuxeo.theme.ApplicationType;
020import org.nuxeo.theme.Manager;
021import org.nuxeo.theme.ViewDef;
022import org.nuxeo.theme.negotiation.Scheme;
023import org.nuxeo.theme.perspectives.PerspectiveManager;
024import org.nuxeo.theme.types.TypeFamily;
025import org.nuxeo.theme.types.TypeRegistry;
026
027public final class ViewId implements Scheme {
028
029    public String getOutcome(final Object context) {
030        final String viewId = ((FacesContext) context).getViewRoot().getViewId();
031        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
032
033        final String applicationPath = ((FacesContext) context).getExternalContext().getRequestContextPath();
034
035        final ApplicationType application = (ApplicationType) typeRegistry.lookup(TypeFamily.APPLICATION,
036                applicationPath);
037        if (application == null) {
038            return null;
039        }
040
041        final ViewDef view = application.getViewById(viewId);
042        if (view == null) {
043            return null;
044        }
045
046        final String perspectiveName = view.getPerspective();
047        if (PerspectiveManager.hasPerspective(perspectiveName)) {
048            return perspectiveName;
049        }
050        return null;
051    }
052}