001/*
002 * (C) Copyright 2006-2016 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage;
020
021import java.util.ArrayList;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025
026import org.apache.commons.lang.ObjectUtils;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeList;
029import org.nuxeo.common.xmap.annotation.XObject;
030
031/**
032 * Structure holding fulltext descriptor info for generic fulltext indexing.
033 * <p>
034 * Not directly a XObject, but used by various RepositoryDescriptors.
035 *
036 * @since 7.10-HF04, 8.1
037 */
038public class FulltextDescriptor {
039
040    @XObject(value = "index")
041    public static class FulltextIndexDescriptor {
042
043        @XNode("@name")
044        public String name;
045
046        /** string or blob */
047        @XNode("fieldType")
048        public String fieldType;
049
050        @XNodeList(value = "field", type = HashSet.class, componentType = String.class)
051        public Set<String> fields = new HashSet<>(0);
052
053        @XNodeList(value = "excludeField", type = HashSet.class, componentType = String.class)
054        public Set<String> excludeFields = new HashSet<>(0);
055
056        public FulltextIndexDescriptor() {
057        }
058
059        /** Copy constructor. */
060        public FulltextIndexDescriptor(FulltextIndexDescriptor other) {
061            name = other.name;
062            fieldType = other.fieldType;
063            fields = new HashSet<>(other.fields);
064            excludeFields = new HashSet<>(other.excludeFields);
065        }
066
067        public static List<FulltextIndexDescriptor> copyList(List<FulltextIndexDescriptor> other) {
068            List<FulltextIndexDescriptor> copy = new ArrayList<>(other.size());
069            for (FulltextIndexDescriptor fid : other) {
070                copy.add(new FulltextIndexDescriptor(fid));
071            }
072            return copy;
073        }
074
075        public void merge(FulltextIndexDescriptor other) {
076            if (other.name != null) {
077                name = other.name;
078            }
079            if (other.fieldType != null) {
080                fieldType = other.fieldType;
081            }
082            fields.addAll(other.fields);
083            excludeFields.addAll(other.excludeFields);
084        }
085    }
086
087    /** False if the boolean is null or FALSE, true otherwise. */
088    private static boolean defaultFalse(Boolean bool) {
089        return Boolean.TRUE.equals(bool);
090    }
091
092    private Boolean fulltextDisabled;
093
094    public boolean getFulltextDisabled() {
095        return defaultFalse(fulltextDisabled);
096    }
097
098    public void setFulltextDisabled(boolean disabled) {
099        fulltextDisabled = Boolean.valueOf(disabled);
100    }
101
102    private Boolean fulltextSearchDisabled;
103
104    public boolean getFulltextSearchDisabled() {
105        if (getFulltextDisabled()) {
106            return true;
107        }
108        return defaultFalse(fulltextSearchDisabled);
109    }
110
111    public void setFulltextSearchDisabled(boolean disabled) {
112        fulltextSearchDisabled = Boolean.valueOf(disabled);
113    }
114
115    private String fulltextParser;
116
117    public String getFulltextParser() {
118        return fulltextParser;
119    }
120
121    public void setFulltextParser(String fulltextParser) {
122        this.fulltextParser = fulltextParser;
123    }
124
125    private List<FulltextIndexDescriptor> fulltextIndexes = new ArrayList<>(0);
126
127    public List<FulltextIndexDescriptor> getFulltextIndexes() {
128        return fulltextIndexes;
129    }
130
131    public void setFulltextIndexes(List<FulltextIndexDescriptor> fulltextIndexes) {
132        this.fulltextIndexes = fulltextIndexes;
133    }
134
135    private Set<String> fulltextExcludedTypes = new HashSet<>(0);
136
137    public Set<String> getFulltextExcludedTypes() {
138        return fulltextExcludedTypes;
139    }
140
141    public void setFulltextExcludedTypes(Set<String> fulltextExcludedTypes) {
142        this.fulltextExcludedTypes = fulltextExcludedTypes;
143    }
144
145    private Set<String> fulltextIncludedTypes = new HashSet<>(0);
146
147    public Set<String> getFulltextIncludedTypes() {
148        return fulltextIncludedTypes;
149    }
150
151    public void setFulltextIncludedTypes(Set<String> fulltextIncludedTypes) {
152        this.fulltextIncludedTypes = fulltextIncludedTypes;
153    }
154
155    public FulltextDescriptor() {
156    }
157
158    /** Copy constructor. */
159    public FulltextDescriptor(FulltextDescriptor other) {
160        fulltextDisabled = other.fulltextDisabled;
161        fulltextSearchDisabled = other.fulltextSearchDisabled;
162        fulltextParser = other.fulltextParser;
163        fulltextIndexes = FulltextIndexDescriptor.copyList(other.fulltextIndexes);
164        fulltextExcludedTypes = new HashSet<>(other.fulltextExcludedTypes);
165        fulltextIncludedTypes = new HashSet<>(other.fulltextIncludedTypes);
166    }
167
168    public void merge(FulltextDescriptor other) {
169        if (other.fulltextDisabled != null) {
170            fulltextDisabled = other.fulltextDisabled;
171        }
172        if (other.fulltextSearchDisabled != null) {
173            fulltextSearchDisabled = other.fulltextSearchDisabled;
174        }
175        if (other.fulltextParser != null) {
176            fulltextParser = other.fulltextParser;
177        }
178        for (FulltextIndexDescriptor oi : other.fulltextIndexes) {
179            boolean append = true;
180            for (FulltextIndexDescriptor i : fulltextIndexes) {
181                if (ObjectUtils.equals(i.name, oi.name)) {
182                    i.merge(oi);
183                    append = false;
184                    break;
185                }
186            }
187            if (append) {
188                fulltextIndexes.add(oi);
189            }
190        }
191        fulltextExcludedTypes.addAll(other.fulltextExcludedTypes);
192        fulltextIncludedTypes.addAll(other.fulltextIncludedTypes);
193    }
194
195}