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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.webengine.negotiation.engine;
023
024import org.nuxeo.ecm.webengine.model.Resource;
025import org.nuxeo.ecm.webengine.model.WebContext;
026import org.nuxeo.theme.ApplicationType;
027import org.nuxeo.theme.Manager;
028import org.nuxeo.theme.ViewDef;
029import org.nuxeo.theme.negotiation.Scheme;
030import org.nuxeo.theme.types.TypeFamily;
031import org.nuxeo.theme.types.TypeRegistry;
032
033public final class ViewId implements Scheme {
034
035    public String getOutcome(final Object context) {
036        WebContext webContext = (WebContext) context;
037        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
038        final ApplicationType application = (ApplicationType) typeRegistry.lookup(TypeFamily.APPLICATION,
039                webContext.getModulePath(), webContext.getModule().getName());
040        if (application == null) {
041            return null;
042        }
043
044        Resource targetObject = webContext.getTargetObject();
045        if (targetObject == null) {
046            return null;
047        }
048        final String rootPath = webContext.getRoot().getPath();
049        final String viewId = targetObject.getPath().substring(rootPath.length());
050
051        final ViewDef view = application.getViewById(viewId);
052        if (view == null) {
053            return null;
054        }
055        final String engineName = view.getEngine();
056        if (engineName == null) {
057            return null;
058        }
059        if (Manager.getTypeRegistry().lookup(TypeFamily.ENGINE, engineName) != null) {
060            return engineName;
061        }
062        return null;
063    }
064}