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.localconfiguration; 019 020import static org.nuxeo.search.ui.localconfiguration.Constants.SEARCH_CONFIGURATION_ALLOWED_CONTENT_VIEWS; 021 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.Collections; 025import java.util.List; 026 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentRef; 029import org.nuxeo.ecm.core.api.PropertyException; 030import org.nuxeo.ecm.core.api.localconfiguration.AbstractLocalConfiguration; 031 032/** 033 * Default implementation of {@link SearchConfiguration} 034 * 035 * @since 6.0 036 */ 037public class SearchConfigurationAdapter extends AbstractLocalConfiguration<SearchConfiguration> implements 038 SearchConfiguration { 039 040 protected List<String> allowedContentViews; 041 042 protected DocumentRef docRef; 043 044 protected boolean canMerge = true; 045 046 public SearchConfigurationAdapter(DocumentModel doc) { 047 docRef = doc.getRef(); 048 allowedContentViews = getList(doc, SEARCH_CONFIGURATION_ALLOWED_CONTENT_VIEWS); 049 } 050 051 protected List<String> getList(DocumentModel doc, String property) { 052 String[] content; 053 try { 054 content = (String[]) doc.getPropertyValue(property); 055 } catch (PropertyException e) { 056 return Collections.emptyList(); 057 } 058 if (content != null) { 059 return Collections.unmodifiableList(Arrays.asList(content)); 060 } 061 return Collections.emptyList(); 062 } 063 064 @Override 065 public DocumentRef getDocumentRef() { 066 return docRef; 067 } 068 069 @Override 070 public boolean canMerge() { 071 return canMerge; 072 } 073 074 @Override 075 public SearchConfiguration merge(SearchConfiguration other) { 076 if (other == null) { 077 return this; 078 } 079 080 // set the documentRef to the other UITypesConfiguration to continue 081 // merging, if needed 082 docRef = other.getDocumentRef(); 083 if (allowedContentViews.isEmpty() && !other.getAllowedContentViewNames().isEmpty()) { 084 this.allowedContentViews = Collections.unmodifiableList(other.getAllowedContentViewNames()); 085 canMerge = false; 086 } 087 088 return this; 089 } 090 091 @Override 092 public List<String> getAllowedContentViewNames() { 093 return allowedContentViews; 094 } 095 096 protected boolean isAllowedName(String name) { 097 return getAllowedContentViewNames().contains(name); 098 } 099 100 @Override 101 public List<String> filterAllowedContentViewNames(List<String> names) { 102 if (allowedContentViews.isEmpty()) { 103 return names; 104 } 105 106 List<String> filtered = new ArrayList<>(); 107 for (String name : names) { 108 if (isAllowedName(name)) { 109 filtered.add(name); 110 } 111 } 112 return filtered; 113 } 114}