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.mode;
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 String applicationPath = webContext.getModulePath();
031        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
032        ApplicationType application = (ApplicationType) typeRegistry.lookup(TypeFamily.APPLICATION,
033                webContext.getModulePath(), webContext.getModule().getName());
034        if (application == null) {
035            return null;
036        }
037
038        Resource targetObject = webContext.getTargetObject();
039        if (targetObject == null) {
040            return null;
041        }
042        final String rootPath = webContext.getRoot().getPath();
043        final String viewId = targetObject.getPath().substring(rootPath.length());
044
045        final ViewDef view = application.getViewById(viewId);
046        if (view == null) {
047            return null;
048        }
049        final String mode = view.getMode();
050        if (mode != null) {
051            return mode;
052        }
053        return null;
054    }
055}