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