001/*
002 * (C) Copyright 2006-2012 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 *     Thierry Martins
018 */
019
020package org.nuxeo.ecm.platform.userworkspace.web.ejb;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023import static org.jboss.seam.ScopeType.EVENT;
024
025import java.io.Serializable;
026
027import org.jboss.seam.annotations.Factory;
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Observer;
031import org.jboss.seam.annotations.Scope;
032import org.jboss.seam.annotations.intercept.BypassInterceptors;
033import org.nuxeo.common.utils.Path;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
036import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
037import org.nuxeo.ecm.webapp.helpers.EventNames;
038
039/**
040 * Standalone class to check if navigation is in the user workspaces and if needed get the current personal workspace
041 * path
042 *
043 * @since 5.6
044 * @author Thierry Martins <tm@nuxeo.com>
045 */
046@Scope(CONVERSATION)
047@Name("userWorkspaceChecker")
048public class UserWorkspaceCheckerActionsBean implements Serializable {
049
050    private static final long serialVersionUID = 1L;
051
052    @In(create = true)
053    protected transient NavigationContext navigationContext;
054
055    @In(required = false, create = true)
056    protected transient CoreSession documentManager;
057
058    protected Boolean isUserWorkspace;
059
060    protected String currentPersonalWorkspacePath;
061
062    @Factory(value = "isUserWorkspace", scope = EVENT)
063    public Boolean computeIsUserWorkspace() {
064        if (isUserWorkspace == null && navigationContext.getCurrentDocument() != null) {
065            isUserWorkspace = navigationContext.getCurrentDocument().getPathAsString().contains(
066                    UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
067        }
068        return isUserWorkspace;
069    }
070
071    @Factory(value = "currentPersonalWorkspacePath", scope = EVENT)
072    public String getCurrentPersonalWorkspace() {
073        if (currentPersonalWorkspacePath == null && Boolean.TRUE.equals(isUserWorkspace)) {
074            // Do not compute path if not necessary
075            Path path = navigationContext.getCurrentDocument().getPath();
076            String lastSegment = "";
077            while (!path.isRoot() || !path.isEmpty()) {
078                if (UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT.equals(path.lastSegment())) {
079                    if (lastSegment.isEmpty()) {
080                        currentPersonalWorkspacePath = "";
081                    } else {
082                        currentPersonalWorkspacePath = path.append(lastSegment).toString();
083                    }
084                    return currentPersonalWorkspacePath;
085                } else {
086                    lastSegment = path.lastSegment();
087                }
088                path = path.removeLastSegments(1);
089            }
090        }
091        return currentPersonalWorkspacePath;
092    }
093
094    @Observer(value = { EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED, EventNames.LOCATION_SELECTION_CHANGED }, create = false)
095    @BypassInterceptors
096    public void reset() {
097        isUserWorkspace = null;
098        currentPersonalWorkspacePath = null;
099    }
100}