001/*
002 * (C) Copyright 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.dbs;
020
021import java.util.ArrayList;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeList;
028import org.nuxeo.ecm.core.storage.FulltextDescriptor;
029import org.nuxeo.ecm.core.storage.FulltextDescriptor.FulltextIndexDescriptor;
030
031/**
032 * DBS Repository Descriptor.
033 *
034 * @since 7.10-HF04, 8.1
035 */
036public class DBSRepositoryDescriptor implements Cloneable {
037
038    public DBSRepositoryDescriptor() {
039    }
040
041    @XNode("@name")
042    public String name;
043
044    @XNode("@label")
045    public String label;
046
047    @XNode("@isDefault")
048    protected Boolean isDefault;
049
050    public Boolean isDefault() {
051        return isDefault;
052    }
053
054    @XNode("idType")
055    public String idType; // "varchar", "uuid", "sequence"
056
057    protected FulltextDescriptor fulltextDescriptor = new FulltextDescriptor();
058
059    public FulltextDescriptor getFulltextDescriptor() {
060        return fulltextDescriptor;
061    }
062
063    @XNode("fulltext@fieldSizeLimit")
064    public void setFulltextFieldSizeLimit(int fieldSizeLimit) {
065        fulltextDescriptor.setFulltextFieldSizeLimit(fieldSizeLimit);
066    }
067
068    @XNode("fulltext@disabled")
069    public void setFulltextDisabled(boolean disabled) {
070        fulltextDescriptor.setFulltextDisabled(disabled);
071    }
072
073    @XNode("fulltext@searchDisabled")
074    public void setFulltextSearchDisabled(boolean disabled) {
075        fulltextDescriptor.setFulltextSearchDisabled(disabled);
076    }
077
078    @XNode("fulltext@parser")
079    public void setFulltextParser(String fulltextParser) {
080        fulltextDescriptor.setFulltextParser(fulltextParser);
081    }
082
083    @XNodeList(value = "fulltext/index", type = ArrayList.class, componentType = FulltextIndexDescriptor.class)
084    public void setFulltextIndexes(List<FulltextIndexDescriptor> fulltextIndexes) {
085        fulltextDescriptor.setFulltextIndexes(fulltextIndexes);
086    }
087
088    @XNodeList(value = "fulltext/excludedTypes/type", type = HashSet.class, componentType = String.class)
089    public void setFulltextExcludedTypes(Set<String> fulltextExcludedTypes) {
090        fulltextDescriptor.setFulltextExcludedTypes(fulltextExcludedTypes);
091    }
092
093    @XNodeList(value = "fulltext/includedTypes/type", type = HashSet.class, componentType = String.class)
094    public void setFulltextIncludedTypes(Set<String> fulltextIncludedTypes) {
095        fulltextDescriptor.setFulltextIncludedTypes(fulltextIncludedTypes);
096    }
097
098    /** @since 8.10 */
099    @XNode("cache@enabled")
100    private Boolean cacheEnabled;
101
102    /** @since 8.10 */
103    public boolean isCacheEnabled() {
104        return defaultFalse(cacheEnabled);
105    }
106
107    /** @since 8.10 */
108    protected void setCacheEnabled(boolean enabled) {
109        cacheEnabled = Boolean.valueOf(enabled);
110    }
111
112    /** @since 8.10 */
113    @XNode("cache@ttl")
114    public Long cacheTTL;
115
116    /** @since 8.10 */
117    @XNode("cache@maxSize")
118    public Long cacheMaxSize;
119
120    /** @since 8.10 */
121    @XNode("cache@concurrencyLevel")
122    public Integer cacheConcurrencyLevel;
123
124    /** @since 8.10 */
125    @XNode("clustering@id")
126    public String clusterNodeId;
127
128    /** @since 8.10 */
129    @XNode("clustering@enabled")
130    private Boolean clusteringEnabled;
131
132    /** @since 8.10 */
133    @XNode("clustering/invalidatorClass")
134    public Class<? extends DBSClusterInvalidator> clusterInvalidatorClass;
135
136    /** @since 8.10 */
137    public boolean isClusteringEnabled() {
138        return defaultFalse(clusteringEnabled);
139    }
140
141    /** @since 8.10 */
142    protected void setClusteringEnabled(boolean enabled) {
143        clusteringEnabled = Boolean.valueOf(enabled);
144    }
145
146    /** @since 9.1 */
147    @XNode("changeTokenEnabled")
148    private Boolean changeTokenEnabled;
149
150    /** @since 9.1 */
151    public boolean isChangeTokenEnabled() {
152        return defaultFalse(changeTokenEnabled);
153    }
154
155    /** @since 9.1 */
156    public void setChangeTokenEnabled(boolean enabled) {
157        this.changeTokenEnabled = Boolean.valueOf(enabled);
158    }
159
160    @Override
161    public DBSRepositoryDescriptor clone() {
162        try {
163            DBSRepositoryDescriptor clone = (DBSRepositoryDescriptor) super.clone();
164            clone.fulltextDescriptor = new FulltextDescriptor(fulltextDescriptor);
165            return clone;
166        } catch (CloneNotSupportedException e) { // cannot happen
167            throw new RuntimeException(e);
168        }
169    }
170
171    public void merge(DBSRepositoryDescriptor other) {
172        if (other.name != null) {
173            name = other.name;
174        }
175        if (other.label != null) {
176            label = other.label;
177        }
178        if (other.isDefault != null) {
179            isDefault = other.isDefault;
180        }
181        if (other.idType != null) {
182            idType = other.idType;
183        }
184        fulltextDescriptor.merge(other.fulltextDescriptor);
185        if (other.cacheEnabled != null) {
186            cacheEnabled = other.cacheEnabled;
187        }
188        if (other.cacheTTL != null) {
189            cacheTTL = other.cacheTTL;
190        }
191        if (other.cacheMaxSize != null) {
192            cacheMaxSize = other.cacheMaxSize;
193        }
194        if (other.cacheConcurrencyLevel != null) {
195            cacheConcurrencyLevel = other.cacheConcurrencyLevel;
196        }
197        if (other.clusterNodeId != null) {
198            clusterNodeId = other.clusterNodeId;
199        }
200        if (other.clusteringEnabled != null) {
201            clusteringEnabled = other.clusteringEnabled;
202        }
203        if (other.clusterInvalidatorClass != null) {
204            clusterInvalidatorClass = other.clusterInvalidatorClass;
205        }
206        if (other.changeTokenEnabled != null) {
207            changeTokenEnabled = other.changeTokenEnabled;
208        }
209    }
210
211    private static boolean defaultFalse(Boolean bool) {
212        return Boolean.TRUE.equals(bool);
213    }
214
215}