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.engine;
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.types.TypeFamily;
024import org.nuxeo.theme.types.TypeRegistry;
025
026public final class ViewId implements Scheme {
027
028    public String getOutcome(final Object context) {
029        final String viewId = ((FacesContext) context).getViewRoot().getViewId();
030        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
031
032        final String applicationPath = ((FacesContext) context).getExternalContext().getRequestContextPath();
033
034        final ApplicationType application = (ApplicationType) typeRegistry.lookup(TypeFamily.APPLICATION,
035                applicationPath);
036        if (application == null) {
037            return null;
038        }
039
040        final ViewDef view = application.getViewById(viewId);
041        if (view == null) {
042            return null;
043        }
044
045        final String engineName = view.getEngine();
046        if (engineName == null) {
047            return null;
048        }
049
050        if (Manager.getTypeRegistry().lookup(TypeFamily.ENGINE, engineName) != null) {
051            return engineName;
052        }
053        return null;
054    }
055}