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    @Override
147    public DBSRepositoryDescriptor clone() {
148        try {
149            DBSRepositoryDescriptor clone = (DBSRepositoryDescriptor) super.clone();
150            clone.fulltextDescriptor = new FulltextDescriptor(fulltextDescriptor);
151            return clone;
152        } catch (CloneNotSupportedException e) { // cannot happen
153            throw new RuntimeException(e);
154        }
155    }
156
157    public void merge(DBSRepositoryDescriptor other) {
158        if (other.name != null) {
159            name = other.name;
160        }
161        if (other.label != null) {
162            label = other.label;
163        }
164        if (other.isDefault != null) {
165            isDefault = other.isDefault;
166        }
167        if (other.idType != null) {
168            idType = other.idType;
169        }
170        fulltextDescriptor.merge(other.fulltextDescriptor);
171        if (other.cacheEnabled != null) {
172            cacheEnabled = other.cacheEnabled;
173        }
174        if (other.cacheTTL != null) {
175            cacheTTL = other.cacheTTL;
176        }
177        if (other.cacheMaxSize != null) {
178            cacheMaxSize = other.cacheMaxSize;
179        }
180        if (other.cacheConcurrencyLevel != null) {
181            cacheConcurrencyLevel = other.cacheConcurrencyLevel;
182        }
183        if (other.clusterNodeId != null) {
184            clusterNodeId = other.clusterNodeId;
185        }
186        if (other.clusteringEnabled != null) {
187            clusteringEnabled = other.clusteringEnabled;
188        }
189        if (other.clusterInvalidatorClass != null) {
190            clusterInvalidatorClass = other.clusterInvalidatorClass;
191        }
192    }
193
194    private static boolean defaultFalse(Boolean bool) {
195        return Boolean.TRUE.equals(bool);
196    }
197
198}