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