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.webengine.negotiation.engine;
016
017import org.nuxeo.ecm.webengine.model.Resource;
018import org.nuxeo.ecm.webengine.model.WebContext;
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        WebContext webContext = (WebContext) context;
030        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
031        final ApplicationType application = (ApplicationType) typeRegistry.lookup(TypeFamily.APPLICATION,
032                webContext.getModulePath(), webContext.getModule().getName());
033        if (application == null) {
034            return null;
035        }
036
037        Resource targetObject = webContext.getTargetObject();
038        if (targetObject == null) {
039            return null;
040        }
041        final String rootPath = webContext.getRoot().getPath();
042        final String viewId = targetObject.getPath().substring(rootPath.length());
043
044        final ViewDef view = application.getViewById(viewId);
045        if (view == null) {
046            return null;
047        }
048        final String engineName = view.getEngine();
049        if (engineName == null) {
050            return null;
051        }
052        if (Manager.getTypeRegistry().lookup(TypeFamily.ENGINE, engineName) != null) {
053            return engineName;
054        }
055        return null;
056    }
057}