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 *     Nuxeo - initial API and implementation
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.directory.sql;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.ecm.directory.BaseDirectoryDescriptor;
026import org.nuxeo.ecm.directory.InverseReference;
027import org.nuxeo.ecm.directory.Reference;
028
029@XObject(value = "directory")
030public class SQLDirectoryDescriptor extends BaseDirectoryDescriptor {
031
032    public static final int QUERY_SIZE_LIMIT_DEFAULT = 0;
033
034    @XNode("dataSource")
035    public String dataSourceName;
036
037    @XNode("querySizeLimit")
038    private Integer querySizeLimit;
039
040    @XNodeList(value = "references/tableReference", type = TableReference[].class, componentType = TableReference.class)
041    private TableReference[] tableReferences;
042
043    @XNodeList(value = "references/inverseReference", type = InverseReference[].class, componentType = InverseReference.class)
044    private InverseReference[] inverseReferences;
045
046    @XNodeList(value = "filters/staticFilter", type = SQLStaticFilter[].class, componentType = SQLStaticFilter.class)
047    private SQLStaticFilter[] staticFilters;
048
049    @XNode("nativeCase")
050    public Boolean nativeCase;
051
052    @XNode("computeMultiTenantId")
053    private boolean computeMultiTenantId = true;
054
055    public String getDataSourceName() {
056        return dataSourceName;
057    }
058
059    public void setDataSourceName(String dataSourceName) {
060        this.dataSourceName = dataSourceName;
061    }
062
063    public Reference[] getInverseReferences() {
064        return inverseReferences;
065    }
066
067    public Reference[] getTableReferences() {
068        return tableReferences;
069    }
070
071    public void setInverseReferences(InverseReference[] inverseReferences) {
072        this.inverseReferences = inverseReferences;
073    }
074
075    public void setTableReferences(TableReference[] tableReferences) {
076        this.tableReferences = tableReferences;
077    }
078
079    public int getQuerySizeLimit() {
080        return querySizeLimit == null ? QUERY_SIZE_LIMIT_DEFAULT : querySizeLimit.intValue();
081    }
082
083    public void setQuerySizeLimit(int querySizeLimit) {
084        this.querySizeLimit = Integer.valueOf(querySizeLimit);
085    }
086
087    public SQLStaticFilter[] getStaticFilters() {
088        if (staticFilters == null) {
089            return new SQLStaticFilter[0];
090        }
091        return staticFilters;
092    }
093
094    /**
095     * Returns {@code true} if a multi tenant id should be computed for this directory, if the directory has support for
096     * multi tenancy, {@code false} otherwise.
097     *
098     * @since 5.6
099     */
100    public boolean isComputeMultiTenantId() {
101        return computeMultiTenantId;
102    }
103
104    @Override
105    public void merge(BaseDirectoryDescriptor other) {
106        super.merge(other);
107        if (other instanceof SQLDirectoryDescriptor) {
108            merge((SQLDirectoryDescriptor) other);
109        }
110    }
111
112    protected void merge(SQLDirectoryDescriptor other) {
113        if (other.dataSourceName != null) {
114            dataSourceName = other.dataSourceName;
115        }
116        if (other.querySizeLimit != null) {
117            querySizeLimit = other.querySizeLimit;
118        }
119        if (other.inverseReferences != null && other.inverseReferences.length != 0) {
120            inverseReferences = other.inverseReferences;
121        }
122        if (other.tableReferences != null && other.tableReferences.length != 0) {
123            tableReferences = other.tableReferences;
124        }
125        if (other.staticFilters != null && other.staticFilters.length != 0) {
126            staticFilters = other.staticFilters;
127        }
128        if (other.nativeCase != null) {
129            nativeCase = other.nativeCase;
130        }
131        computeMultiTenantId = other.computeMultiTenantId;
132    }
133
134    @Override
135    public SQLDirectoryDescriptor clone() {
136        SQLDirectoryDescriptor clone = (SQLDirectoryDescriptor) super.clone();
137        // basic fields are already copied by super.clone()
138        if (tableReferences != null) {
139            clone.tableReferences = new TableReference[tableReferences.length];
140            for (int i = 0; i < tableReferences.length; i++) {
141                clone.tableReferences[i] = tableReferences[i].clone();
142            }
143        }
144        if (inverseReferences != null) {
145            clone.inverseReferences = new InverseReference[inverseReferences.length];
146            for (int i = 0; i < inverseReferences.length; i++) {
147                clone.inverseReferences[i] = inverseReferences[i].clone();
148            }
149        }
150        if (staticFilters != null) {
151            clone.staticFilters = new SQLStaticFilter[staticFilters.length];
152            for (int i = 0; i < staticFilters.length; i++) {
153                clone.staticFilters[i] = staticFilters[i].clone();
154            }
155        }
156        return clone;
157    }
158
159    @Override
160    public SQLDirectory newDirectory() {
161        return new SQLDirectory(this);
162    }
163
164}