001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *     Thomas Roger
016 */
017
018package org.nuxeo.search.ui.seam;
019
020import static org.jboss.seam.ScopeType.CONVERSATION;
021import static org.nuxeo.search.ui.localconfiguration.Constants.SEARCH_CONFIGURATION_FACET;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.annotations.Install;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.Scope;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.localconfiguration.LocalConfigurationService;
035import org.nuxeo.ecm.platform.contentview.jsf.ContentViewHeader;
036import org.nuxeo.ecm.platform.contentview.jsf.ContentViewService;
037import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
038import org.nuxeo.ecm.webapp.action.ActionContextProvider;
039import org.nuxeo.runtime.api.Framework;
040import org.nuxeo.search.ui.SearchUIService;
041import org.nuxeo.search.ui.localconfiguration.SearchConfiguration;
042
043@Name("searchUIConfigurationActions")
044@Scope(CONVERSATION)
045@Install(precedence = Install.FRAMEWORK)
046public class SearchUIConfigurationActions implements Serializable {
047
048    private static final long serialVersionUID = 1L;
049
050    @In(create = true)
051    protected transient NavigationContext navigationContext;
052
053    @In(create = true, required = false)
054    protected transient CoreSession documentManager;
055
056    @In(create = true, required = false)
057    protected transient ActionContextProvider actionContextProvider;
058
059    @In(create = true)
060    protected transient ContentViewService contentViewService;
061
062    public List<ContentViewHeader> getSelectedContentViewHeaders() {
063        DocumentModel currentDoc = navigationContext.getCurrentDocument();
064        return getSelectedContentViewHeaders(currentDoc);
065    }
066
067    public List<ContentViewHeader> getSelectedContentViewHeaders(DocumentModel document) {
068        if (!document.hasFacet(SEARCH_CONFIGURATION_FACET)) {
069            return Collections.emptyList();
070        }
071
072        SearchUIService searchUIService = Framework.getService(SearchUIService.class);
073        List<ContentViewHeader> contentViewHeaders = searchUIService.getContentViewHeaders(actionContextProvider.createActionContext());
074
075        List<String> allowedContentViewNames = getAllowedContentViewNames(document);
076        if (allowedContentViewNames.isEmpty()) {
077            LocalConfigurationService localConfigurationService = Framework.getService(LocalConfigurationService.class);
078            SearchConfiguration configuration = localConfigurationService.getConfiguration(SearchConfiguration.class,
079                    SEARCH_CONFIGURATION_FACET, document);
080            if (configuration == null) {
081                return contentViewHeaders;
082            }
083            allowedContentViewNames = configuration.getAllowedContentViewNames();
084        }
085
086        if (allowedContentViewNames.isEmpty()) {
087            return contentViewHeaders;
088        }
089
090        List<ContentViewHeader> selectedContentViewHeaders = new ArrayList<>();
091        for (ContentViewHeader contentViewHeader : contentViewHeaders) {
092            if (allowedContentViewNames.contains(contentViewHeader.getName())) {
093                selectedContentViewHeaders.add(contentViewHeader);
094            }
095        }
096
097        return selectedContentViewHeaders;
098    }
099
100    public List<ContentViewHeader> getNotSelectedContentViewHeaders() {
101        DocumentModel currentDoc = navigationContext.getCurrentDocument();
102        return getNotSelectedContentViewHeaders(currentDoc);
103    }
104
105    public List<ContentViewHeader> getNotSelectedContentViewHeaders(DocumentModel document) {
106        if (!document.hasFacet(SEARCH_CONFIGURATION_FACET)) {
107            return Collections.emptyList();
108        }
109
110        List<ContentViewHeader> notSelectedContentViewHeaders = new ArrayList<>();
111        List<ContentViewHeader> selectedContentViewHeaders = getSelectedContentViewHeaders(document);
112        SearchUIService searchUIService = Framework.getService(SearchUIService.class);
113        List<ContentViewHeader> contentViewHeaders = searchUIService.getContentViewHeaders(actionContextProvider.createActionContext());
114        for (ContentViewHeader contentViewHeader : contentViewHeaders) {
115            if (!selectedContentViewHeaders.contains(contentViewHeader)) {
116                notSelectedContentViewHeaders.add(contentViewHeader);
117            }
118        }
119
120        return notSelectedContentViewHeaders;
121    }
122
123    protected List<String> getAllowedContentViewNames(DocumentModel doc) {
124        SearchConfiguration adapter = doc.getAdapter(SearchConfiguration.class);
125        if (adapter == null) {
126            return Collections.emptyList();
127        }
128
129        return adapter.getAllowedContentViewNames();
130    }
131}