001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Martins
016 */
017
018package org.nuxeo.ecm.platform.userworkspace.web.ejb;
019
020import static org.jboss.seam.ScopeType.CONVERSATION;
021import static org.jboss.seam.ScopeType.EVENT;
022
023import java.io.Serializable;
024
025import org.jboss.seam.annotations.Factory;
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Observer;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.annotations.intercept.BypassInterceptors;
031import org.nuxeo.common.utils.Path;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
034import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
035import org.nuxeo.ecm.webapp.helpers.EventNames;
036
037/**
038 * Standalone class to check if navigation is in the user workspaces and if needed get the current personal workspace
039 * path
040 *
041 * @since 5.6
042 * @author Thierry Martins <tm@nuxeo.com>
043 */
044@Scope(CONVERSATION)
045@Name("userWorkspaceChecker")
046public class UserWorkspaceCheckerActionsBean implements Serializable {
047
048    private static final long serialVersionUID = 1L;
049
050    @In(create = true)
051    protected transient NavigationContext navigationContext;
052
053    @In(required = false, create = true)
054    protected transient CoreSession documentManager;
055
056    protected Boolean isUserWorkspace;
057
058    protected String currentPersonalWorkspacePath;
059
060    @Factory(value = "isUserWorkspace", scope = EVENT)
061    public Boolean computeIsUserWorkspace() {
062        if (isUserWorkspace == null && navigationContext.getCurrentDocument() != null) {
063            isUserWorkspace = navigationContext.getCurrentDocument().getPathAsString().contains(
064                    UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
065        }
066        return isUserWorkspace;
067    }
068
069    @Factory(value = "currentPersonalWorkspacePath", scope = EVENT)
070    public String getCurrentPersonalWorkspace() {
071        if (currentPersonalWorkspacePath == null && Boolean.TRUE.equals(isUserWorkspace)) {
072            // Do not compute path if not necessary
073            Path path = navigationContext.getCurrentDocument().getPath();
074            String lastSegment = "";
075            while (!path.isRoot() || !path.isEmpty()) {
076                if (UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT.equals(path.lastSegment())) {
077                    if (lastSegment.isEmpty()) {
078                        currentPersonalWorkspacePath = "";
079                    } else {
080                        currentPersonalWorkspacePath = path.append(lastSegment).toString();
081                    }
082                    return currentPersonalWorkspacePath;
083                } else {
084                    lastSegment = path.lastSegment();
085                }
086                path = path.removeLastSegments(1);
087            }
088        }
089        return currentPersonalWorkspacePath;
090    }
091
092    @Observer(value = { EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED, EventNames.LOCATION_SELECTION_CHANGED }, create = false)
093    @BypassInterceptors
094    public void reset() {
095        isUserWorkspace = null;
096        currentPersonalWorkspacePath = null;
097    }
098}