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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.webapp.helpers;
023
024import static org.jboss.seam.ScopeType.SESSION;
025
026import java.io.Serializable;
027import java.security.Principal;
028
029import javax.faces.context.FacesContext;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.annotations.Begin;
034import org.jboss.seam.annotations.In;
035import org.jboss.seam.annotations.Install;
036import org.jboss.seam.annotations.Name;
037import org.jboss.seam.annotations.Scope;
038import org.jboss.seam.contexts.Context;
039import org.jboss.seam.core.Events;
040import org.nuxeo.ecm.core.api.CoreSession;
041import org.nuxeo.ecm.core.api.DocumentModel;
042import org.nuxeo.ecm.core.api.DocumentModelList;
043import org.nuxeo.ecm.core.api.LifeCycleConstants;
044import org.nuxeo.ecm.core.api.NuxeoException;
045import org.nuxeo.ecm.core.api.repository.RepositoryManager;
046import org.nuxeo.ecm.core.api.security.SecurityConstants;
047import org.nuxeo.ecm.core.query.sql.NXQL;
048import org.nuxeo.ecm.core.schema.FacetNames;
049import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
050import org.nuxeo.ecm.platform.ui.web.api.WebActions;
051import org.nuxeo.ecm.platform.ui.web.rest.RestHelper;
052import org.nuxeo.ecm.platform.util.RepositoryLocation;
053import org.nuxeo.ecm.webapp.dashboard.DashboardNavigationHelper;
054
055@Name("startupHelper")
056@Scope(SESSION)
057@Install(precedence = Install.FRAMEWORK)
058public class StartupHelper implements Serializable {
059
060    public static final String SERVERS_VIEW = "view_servers";
061
062    public static final String DOMAINS_VIEW = "view_domains";
063
064    protected static final String DOMAIN_TYPE = "Domain";
065
066    protected static final String DOCUMENT_MANAGEMENT_TAB = WebActions.MAIN_TABS_CATEGORY + ":"
067            + WebActions.DOCUMENTS_MAIN_TAB_ID;
068
069    private static final long serialVersionUID = 3248972387619873245L;
070
071    private static final Log log = LogFactory.getLog(StartupHelper.class);
072
073    @In(create = true)
074    protected transient RepositoryManager repositoryManager;
075
076    @In(create = true)
077    protected transient NavigationContext navigationContext;
078
079    @In(create = true)
080    protected transient WebActions webActions;
081
082    @In
083    protected transient Context sessionContext;
084
085    @In(create = true)
086    protected DashboardNavigationHelper dashboardNavigationHelper;
087
088    @In(create = true)
089    ConversationIdGenerator conversationIdGenerator;
090
091    @In(create = true, required = false)
092    protected transient CoreSession documentManager;
093
094    @In(create = true)
095    protected transient RestHelper restHelper;
096
097    /**
098     * Initializes the context with the principal id, and try to connect to the default server if any. If several
099     * servers are available, let the user choose.
100     *
101     * @return the view_id of the contextually computed startup page
102     */
103    public String initServerAndFindStartupPage() {
104
105        setupCurrentUser();
106
107        // we try to select the server to go to the next screen
108        if (navigationContext.getCurrentServerLocation() == null) {
109            // update location
110            RepositoryLocation repLoc = new RepositoryLocation(repositoryManager.getDefaultRepositoryName());
111            navigationContext.setCurrentServerLocation(repLoc);
112        }
113
114        if (documentManager == null) {
115            documentManager = navigationContext.getOrCreateDocumentManager();
116        }
117
118        if (Events.exists()) {
119            Events.instance().raiseEvent(EventNames.USER_SESSION_STARTED, documentManager);
120        }
121
122        // select home page
123
124        DocumentModel rootDocument = documentManager.getRootDocument();
125        if (!documentManager.hasPermission(rootDocument.getRef(), SecurityConstants.READ_CHILDREN)) {
126            // user cannot see the root but maybe she can see contained
127            // documents thus forwarding her to her dashboard
128            return dashboardNavigationHelper.navigateToDashboard();
129        }
130
131        webActions.setCurrentTabIds(DOCUMENT_MANAGEMENT_TAB);
132        // if more than one repo : display the server selection screen
133        if (repositoryManager.getRepositoryNames().size() > 1) {
134            return SERVERS_VIEW;
135        }
136
137        // the Repository Location is initialized, skip the first screen
138        return DOMAINS_VIEW;
139    }
140
141    /**
142     * Initializes the context with the principal id, and tries to connect to the default server if any then: - if the
143     * server has several domains, redirect to the list of domains - if the server has only one domain, select it and
144     * redirect to viewId - if the server is empty, create a new domain with title 'domainTitle' and redirect to it on
145     * viewId.
146     * <p>
147     * If several servers are available, let the user choose.
148     *
149     * @return the view id of the contextually computed startup page
150     */
151    @Begin(id = "#{conversationIdGenerator.nextMainConversationId}", join = true)
152    public String initDomainAndFindStartupPage(String domainTitle, String viewId) {
153
154        try {
155            // delegate server initialized to the default helper
156            String result = initServerAndFindStartupPage();
157
158            // more than one repo
159            if (SERVERS_VIEW.equals(result)) {
160                return result;
161            }
162
163            String query = "SELECT * FROM Domain WHERE " + NXQL.ECM_MIXINTYPE + " <> '"
164                    + FacetNames.HIDDEN_IN_NAVIGATION + "' AND " + NXQL.ECM_LIFECYCLESTATE + " <> '"
165                    + LifeCycleConstants.DELETED_STATE + "'" + " AND ecm:isCheckedInVersion = 0 "
166                    + " AND ecm:isProxy = 0 ";
167            DocumentModelList domains = documentManager.query(query);
168            if (domains.size() == 1) {
169                // select and go to the unique domain
170                webActions.setCurrentTabIds(DOCUMENT_MANAGEMENT_TAB);
171                return navigationContext.navigateToDocument(domains.get(0), viewId);
172            }
173
174            // zero or several domains: let the user decide what to do if he has
175            // right on the Root document
176            DocumentModel rootDocument = documentManager.getRootDocument();
177            if (documentManager.hasPermission(rootDocument.getRef(), SecurityConstants.READ_CHILDREN)) {
178                webActions.setCurrentTabIds(DOCUMENT_MANAGEMENT_TAB);
179                navigationContext.navigateToDocument(rootDocument);
180                return DOMAINS_VIEW;
181            }
182
183            return result;
184        } catch (NuxeoException e) {
185            // avoid pages.xml contribution to catch exceptions silently
186            // hiding the cause of the problem to developers
187            // TODO: remove this catch clause if we find a way not to make it
188            // fail silently
189            log.error("error while initializing the Seam context with a CoreSession instance: " + e.getMessage(), e);
190            return null;
191        }
192    }
193
194    public void setupCurrentUser() {
195        Principal currentUser = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
196        sessionContext.set("currentUser", currentUser);
197    }
198
199}