/*
 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Thierry Martins
 */

package org.nuxeo.ecm.platform.userworkspace.web.ejb;

import static org.jboss.seam.ScopeType.CONVERSATION;
import static org.jboss.seam.ScopeType.EVENT;

import java.io.Serializable;

import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;

/**
 * Standalone class to check if navigation is in the user workspaces
 *
 * @since 5.6
 * @author Thierry Martins <tm@nuxeo.com>
 */
@Scope(CONVERSATION)
@Name("userWorkspaceChecker")
public class UserWorkspaceCheckerActionsBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @In(create = true)
    protected transient NavigationContext navigationContext;

    @Factory(value = "isUserWorkspace", scope = EVENT)
    public Boolean computeIsUserWorkspace() {
        return navigationContext.getCurrentDocument().getPathAsString().contains(
                UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
    }
}

@@ -983,21 +983,255 @@
 tations.
-Scope
+Observer;%0Aimport org.jboss.seam.annotations.Scope;%0Aimport org.jboss.seam.annotations.intercept.BypassInterceptors;%0Aimport org.nuxeo.common.utils.Path;%0Aimport org.nuxeo.ecm.core.api.ClientException;%0Aimport org.nuxeo.ecm.core.api.CoreSession
 ;%0Aimport
@@ -1361,16 +1361,64 @@
 nstants;
+%0Aimport org.nuxeo.ecm.webapp.helpers.EventNames;
 %0A%0A/**%0A *
@@ -1484,16 +1484,73 @@
 kspaces%0A
+ * and if needed get the current personal workspace path%0A
  *%0A * @s
@@ -1865,16 +1865,203 @@
 ntext;%0A%0A
+    @In(required = false, create = true)%0A    protected transient CoreSession documentManager;%0A%0A    protected Boolean isUserWorkspace;%0A%0A    protected String currentPersonalWorkspacePath;%0A%0A
     @Fac
@@ -2165,14 +2165,68 @@
     
-return
+if (isUserWorkspace == null) %7B%0A            isUserWorkspace =
  nav
@@ -2305,16 +2305,20 @@
         
+    
 UserWork
@@ -2365,16 +2365,1443 @@
 _ROOT);%0A
+        %7D%0A        return isUserWorkspace;%0A    %7D%0A%0A    @Factory(value = %22currentPersonalWorkspacePath%22, scope = EVENT)%0A    public String getCurrentPersonalWorkspace() throws ClientException %7B%0A        if (currentPersonalWorkspacePath == null%0A                && Boolean.TRUE.equals(isUserWorkspace)) %7B%0A            // Do not compute path if not necessary%0A            Path path = navigationContext.getCurrentDocument().getPath();%0A            String lastSegment = %22%22;%0A            while (!path.isRoot() %7C%7C !path.isEmpty()) %7B%0A                if (UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT.equals(path.lastSegment())) %7B%0A                    if (lastSegment.isEmpty()) %7B%0A                        currentPersonalWorkspacePath = %22%22;%0A                    %7D else %7B%0A                        currentPersonalWorkspacePath = path.append(lastSegment).toString();%0A                    %7D%0A                    return currentPersonalWorkspacePath;%0A                %7D else %7B%0A                    lastSegment = path.lastSegment();%0A                %7D%0A                path = path.removeLastSegments(1);%0A            %7D%0A        %7D%0A        return currentPersonalWorkspacePath;%0A    %7D%0A%0A    @Observer(value = %7B EventNames.USER_ALL_DOCUMENT_TYPES_SELECTION_CHANGED,%0A            EventNames.LOCATION_SELECTION_CHANGED %7D, create = false)%0A    @BypassInterceptors%0A    public void reset() %7B%0A        isUserWorkspace = null;%0A        currentPersonalWorkspacePath = null;%0A
     %7D%0A%7D%0A

add a new factory to get current personal workspace path