001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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 *     btatar
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.userworkspace.web.ejb;
021
022import static org.jboss.seam.ScopeType.SESSION;
023
024import java.security.Principal;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.jboss.seam.ScopeType;
029import org.jboss.seam.annotations.Destroy;
030import org.jboss.seam.annotations.Factory;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Install;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.annotations.Startup;
036import org.jboss.seam.core.Events;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.core.api.security.SecurityConstants;
040import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
041import org.nuxeo.ecm.platform.ui.web.api.WebActions;
042import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceManagerActions;
043import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
044import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
045import org.nuxeo.ecm.webapp.action.MainTabsActions;
046import org.nuxeo.ecm.webapp.dashboard.DashboardNavigationHelper;
047import org.nuxeo.ecm.webapp.helpers.EventNames;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * Personal user workspace manager actions bean.
052 *
053 * @author btatar
054 */
055@Name("userWorkspaceManagerActions")
056@Scope(SESSION)
057@Install(precedence = Install.FRAMEWORK)
058@Startup
059public class UserWorkspaceManagerActionsBean implements UserWorkspaceManagerActions {
060
061    public static final String DOCUMENT_VIEW = "view_documents";
062
063    public static final String DOCUMENT_MANAGEMENT_ACTION = "documents";
064
065    private static final long serialVersionUID = 1828552026739219850L;
066
067    private static final Log log = LogFactory.getLog(UserWorkspaceManagerActions.class);
068
069    protected static final String DOCUMENT_MANAGEMENT_TAB = WebActions.MAIN_TABS_CATEGORY + ":"
070            + WebActions.DOCUMENTS_MAIN_TAB_ID;
071
072    protected boolean showingPersonalWorkspace;
073
074    protected boolean initialized;
075
076    protected DocumentModel lastAccessedDocument;
077
078    // Rux INA-252: very likely cause of passivation error
079    protected transient UserWorkspaceService userWorkspaceService;
080
081    // Rux INA-252: another cause of passivation error
082    @In(required = true)
083    protected transient NavigationContext navigationContext;
084
085    @In(create = true)
086    protected transient Principal currentUser;
087
088    @In(required = false, create = true)
089    protected transient CoreSession documentManager;
090
091    @In(create = true)
092    protected transient DashboardNavigationHelper dashboardNavigationHelper;
093
094    @In(create = true)
095    protected transient MainTabsActions mainTabsActions;
096
097    @In(create = true)
098    protected transient WebActions webActions;
099
100    public void initialize() {
101        log.debug("Initializing user workspace manager actions bean");
102        showingPersonalWorkspace = false;
103        initialized = true;
104    }
105
106    @Destroy
107    public void destroy() {
108        userWorkspaceService = null;
109        log.debug("Removing user workspace actions bean");
110    }
111
112    private UserWorkspaceService getUserWorkspaceService() {
113        if (userWorkspaceService != null) {
114            return userWorkspaceService;
115        }
116        userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
117        return userWorkspaceService;
118    }
119
120    public DocumentModel getCurrentUserPersonalWorkspace() {
121        if (!initialized) {
122            initialize();
123        }
124        // protection in case we have not yet chosen a repository. if not
125        // repository, then there is no documentManager(session)
126        if (documentManager == null) {
127            return null;// this is ok because it eventually will be
128            // dealt with by setCurrentDocument, which will deal with
129            // the lack of a documentManager
130        }
131        return getUserWorkspaceService().getCurrentUserPersonalWorkspace(documentManager,
132                navigationContext.getCurrentDocument());
133    }
134
135    public String navigateToCurrentUserPersonalWorkspace() {
136        if (!initialized) {
137            initialize();
138        }
139        String returnView = DOCUMENT_VIEW;
140
141        // force return to Documents tab
142        webActions.setCurrentTabId(WebActions.MAIN_TABS_CATEGORY, DOCUMENT_MANAGEMENT_ACTION);
143
144        // Rux INA-221: separated links for going to workspaces
145        DocumentModel currentUserPersonalWorkspace = getCurrentUserPersonalWorkspace();
146        DocumentModel currentDocument = navigationContext.getCurrentDocument();
147        if (!isShowingPersonalWorkspace() && currentDocument != null && currentDocument.getPath().segment(0) != null) {
148            lastAccessedDocument = mainTabsActions.getDocumentFor(DOCUMENT_MANAGEMENT_ACTION,
149                    navigationContext.getCurrentDocument());
150        }
151        navigationContext.setCurrentDocument(currentUserPersonalWorkspace);
152        showingPersonalWorkspace = true;
153
154        Events.instance().raiseEvent(EventNames.GO_PERSONAL_WORKSPACE);
155
156        return returnView;
157    }
158
159    // Rux INA-221: create a new method for the 2 separated links
160    public String navigateToOverallWorkspace() {
161        if (!initialized) {
162            initialize();
163        }
164        String returnView = DOCUMENT_VIEW;
165
166        // force return to Documents tab
167        webActions.setCurrentTabIds(DOCUMENT_MANAGEMENT_TAB);
168
169        if (lastAccessedDocument != null) {
170            navigationContext.setCurrentDocument(lastAccessedDocument);
171        } else if (navigationContext.getCurrentDomain() != null) {
172            navigationContext.setCurrentDocument(navigationContext.getCurrentDomain());
173        } else if (documentManager.hasPermission(documentManager.getRootDocument().getRef(),
174                SecurityConstants.READ_CHILDREN)) {
175            navigationContext.setCurrentDocument(documentManager.getRootDocument());
176        } else {
177            navigationContext.setCurrentDocument(null);
178            returnView = dashboardNavigationHelper.navigateToDashboard();
179        }
180        showingPersonalWorkspace = false;
181
182        Events.instance().raiseEvent(EventNames.GO_HOME);
183
184        return returnView;
185    }
186
187    @Factory(value = "isInsidePersonalWorkspace", scope = ScopeType.EVENT)
188    public boolean isShowingPersonalWorkspace() {
189        if (!initialized) {
190            initialize();
191        }
192
193        // NXP-9813 : navigating to a tab different than Documents should not change
194        // the value for showingPersonalWorkspace
195        if (mainTabsActions.isOnMainTab(DOCUMENT_MANAGEMENT_ACTION)) {
196            DocumentModel currentDoc = navigationContext.getCurrentDocument();
197
198            if (currentDoc == null || currentDoc.getPath().segmentCount() < 2) {
199                return false;
200            }
201
202            String rootChild = currentDoc.getPath().segment(1);
203            String wsName = currentDoc.getPath().segment(2);
204            showingPersonalWorkspace = rootChild != null
205                    && rootChild.startsWith(UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT) && wsName != null
206                    && wsName.equals(currentUser.getName());
207        }
208        return showingPersonalWorkspace;
209    }
210
211    public void setShowingPersonalWorkspace(boolean showingPersonalWorkspace) {
212        this.showingPersonalWorkspace = showingPersonalWorkspace;
213    }
214
215    public boolean isInitialized() {
216        return initialized;
217    }
218
219    public void setInitialized(boolean initialized) {
220        this.initialized = initialized;
221    }
222
223}