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