001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.user.center.dashboard.jsf;
018
019import static org.jboss.seam.ScopeType.CONVERSATION;
020
021import java.io.Serializable;
022import java.security.Principal;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027import javax.faces.context.FacesContext;
028import javax.servlet.http.HttpServletRequest;
029
030import org.jboss.seam.ScopeType;
031import org.jboss.seam.annotations.Factory;
032import org.jboss.seam.annotations.In;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Observer;
035import org.jboss.seam.annotations.Scope;
036import org.nuxeo.common.utils.UserAgentMatcher;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.core.api.IdRef;
040import org.nuxeo.ecm.core.api.NuxeoPrincipal;
041import org.nuxeo.ecm.core.security.SecurityService;
042import org.nuxeo.ecm.platform.contentview.jsf.ContentView;
043import org.nuxeo.ecm.platform.contentview.seam.ContentViewActions;
044import org.nuxeo.ecm.platform.task.TaskEventNames;
045import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
046import org.nuxeo.ecm.webapp.helpers.EventNames;
047
048/**
049 * Handles JSF dashboard actions.
050 *
051 * @since 5.4.2
052 */
053@Name("jsfDashboardActions")
054@Scope(CONVERSATION)
055public class JSFDashboardActions implements Serializable {
056
057    private static final long serialVersionUID = 1L;
058
059    public static final String CONTENT_VIEW_OBSERVER_WORKFLOW_EVENT = "workflowEvent";
060
061    public static final String USER_DOMAINS_CONTENT_VIEW = "user_domains";
062
063    @In(create = true)
064    protected ContentViewActions contentViewActions;
065
066    @In(create = true)
067    protected NavigationContext navigationContext;
068
069    @In(create = true, required = false)
070    protected CoreSession documentManager;
071
072    protected DocumentModel selectedDomain;
073
074    protected List<DocumentModel> availableDomains;
075
076    @Factory(value = "userDomains", scope = ScopeType.EVENT)
077    @SuppressWarnings("unchecked")
078    public List<DocumentModel> getUserDomains() {
079        if (documentManager == null) {
080            return new ArrayList<DocumentModel>();
081        }
082        if (availableDomains == null) {
083            ContentView cv = contentViewActions.getContentView(USER_DOMAINS_CONTENT_VIEW);
084            availableDomains = (List<DocumentModel>) cv.getPageProvider().getCurrentPage();
085        }
086        return availableDomains;
087    }
088
089    public DocumentModel getSelectedDomain() {
090        List<DocumentModel> domains = getUserDomains();
091        if (selectedDomain == null) {
092            // initialize to current domain, or take first domain found
093            DocumentModel currentDomain = navigationContext.getCurrentDomain();
094            if (currentDomain != null) {
095                selectedDomain = currentDomain;
096            } else {
097                if (domains != null && !domains.isEmpty()) {
098                    selectedDomain = domains.get(0);
099                }
100            }
101        } else if (domains != null && !domains.isEmpty() && !domains.contains(selectedDomain)) {
102            // reset old domain: it's not available anymore
103            selectedDomain = domains.get(0);
104        }
105        return selectedDomain;
106    }
107
108    public String getSelectedDomainId() {
109        DocumentModel selectedDomain = getSelectedDomain();
110        if (selectedDomain != null) {
111            return selectedDomain.getId();
112        }
113        return null;
114    }
115
116    public void setSelectedDomainId(String selectedDomainId) {
117        selectedDomain = documentManager.getDocument(new IdRef(selectedDomainId));
118    }
119
120    public String getSelectedDomainPath() {
121        DocumentModel domain = getSelectedDomain();
122        if (domain == null) {
123            return "/";
124        }
125        return domain.getPathAsString() + "/";
126    }
127
128    public String getSelectedDomainTemplatesPath() {
129        return getSelectedDomainPath() + "templates";
130    }
131
132    /**
133     * Refreshes and resets content views that have declared the event "workflowEvent" as a refresh or reset event, on
134     * every kind of workflow/task event.
135     */
136    @Observer(value = { TaskEventNames.WORKFLOW_ENDED, TaskEventNames.WORKFLOW_NEW_STARTED,
137            TaskEventNames.WORKFLOW_TASK_STOP, TaskEventNames.WORKFLOW_TASK_REJECTED,
138            TaskEventNames.WORKFLOW_USER_ASSIGNMENT_CHANGED, TaskEventNames.WORKFLOW_TASK_COMPLETED,
139            TaskEventNames.WORKFLOW_TASK_REMOVED, TaskEventNames.WORK_ITEMS_LIST_LOADED,
140            TaskEventNames.WORKFLOW_TASKS_COMPUTED, TaskEventNames.WORKFLOW_ABANDONED,
141            TaskEventNames.WORKFLOW_CANCELED, EventNames.DOCUMENT_PUBLICATION_REJECTED,
142            EventNames.DOCUMENT_PUBLICATION_APPROVED, EventNames.DOCUMENT_PUBLISHED }, create = false)
143    public void onWorkflowEvent() {
144        contentViewActions.refreshOnSeamEvent(CONTENT_VIEW_OBSERVER_WORKFLOW_EVENT);
145        contentViewActions.resetPageProviderOnSeamEvent(CONTENT_VIEW_OBSERVER_WORKFLOW_EVENT);
146    }
147
148    @Factory(value = "isMSIE6or7", scope = ScopeType.SESSION)
149    public boolean isMSIE6or7() {
150        FacesContext context = FacesContext.getCurrentInstance();
151        if (context != null) {
152            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
153            String ua = request.getHeader("User-Agent");
154            return UserAgentMatcher.isMSIE6or7(ua);
155        } else {
156            return false;
157        }
158
159    }
160
161    public String[] getCurrentUserPrincipalsToCheck() {
162        return SecurityService.getPrincipalsToCheck(documentManager.getPrincipal());
163    }
164}