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