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