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 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.NuxeoPrincipal;
040import org.nuxeo.ecm.core.api.security.SecurityConstants;
041import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
042import org.nuxeo.ecm.platform.ui.web.api.WebActions;
043import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceManagerActions;
044import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
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 NuxeoPrincipal 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.getService(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    @Override
188    @Factory(value = "isInsidePersonalWorkspace", scope = ScopeType.EVENT)
189    public boolean isShowingPersonalWorkspace() {
190        if (!initialized) {
191            initialize();
192        }
193        if (mainTabsActions.isOnMainTab(DOCUMENT_MANAGEMENT_ACTION)) {
194            DocumentModel currentDoc = navigationContext.getCurrentDocument();
195            showingPersonalWorkspace = getUserWorkspaceService().isUnderUserWorkspace(currentUser, null, currentDoc);
196        }
197        return showingPersonalWorkspace;
198    }
199
200    public void setShowingPersonalWorkspace(boolean showingPersonalWorkspace) {
201        this.showingPersonalWorkspace = showingPersonalWorkspace;
202    }
203
204    public boolean isInitialized() {
205        return initialized;
206    }
207
208    public void setInitialized(boolean initialized) {
209        this.initialized = initialized;
210    }
211
212}