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