001/*
002 * (C) Copyright 2017 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 *     Funsho David
018 *
019 */
020
021package org.nuxeo.directory.mongodb;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeList;
025import org.nuxeo.common.xmap.annotation.XObject;
026import org.nuxeo.ecm.directory.BaseDirectoryDescriptor;
027import org.nuxeo.ecm.directory.InverseReference;
028import org.nuxeo.ecm.directory.Reference;
029
030/**
031 * @since 9.1
032 */
033@XObject("directory")
034public class MongoDBDirectoryDescriptor extends BaseDirectoryDescriptor {
035
036    @XNode("serverUrl")
037    public String serverUrl;
038
039    @XNode("databaseName")
040    public String databaseName;
041
042    @XNodeList(value = "references/reference", type = MongoDBReference[].class, componentType = MongoDBReference.class)
043    public MongoDBReference[] references;
044
045    @XNodeList(value = "references/inverseReference", type = InverseReference[].class, componentType = InverseReference.class)
046    public InverseReference[] inverseReferences;
047
048    public String getServerUrl() {
049        return serverUrl;
050    }
051
052    public void setServerUrl(String serverUrl) {
053        this.serverUrl = serverUrl;
054    }
055
056    public String getDatabaseName() {
057        return databaseName;
058    }
059
060    public void setDatabaseName(String databaseName) {
061        this.databaseName = databaseName;
062    }
063
064    public Reference[] getInverseReferences() {
065        return inverseReferences;
066    }
067
068    public Reference[] getMongoDBReferences() {
069        return references;
070    }
071
072    @Override
073    public void merge(BaseDirectoryDescriptor other) {
074        super.merge(other);
075        if (other instanceof MongoDBDirectoryDescriptor) {
076            merge((MongoDBDirectoryDescriptor) other);
077        }
078    }
079
080    protected void merge(MongoDBDirectoryDescriptor other) {
081        if (other.serverUrl != null) {
082            serverUrl = other.serverUrl;
083        }
084        if (other.databaseName != null) {
085            databaseName = other.databaseName;
086        }
087        if (other.inverseReferences != null && other.inverseReferences.length != 0) {
088            inverseReferences = other.inverseReferences;
089        }
090        if (other.references != null && other.references.length != 0) {
091            references = other.references;
092        }
093    }
094
095    @Override
096    public MongoDBDirectoryDescriptor clone() {
097        MongoDBDirectoryDescriptor clone = (MongoDBDirectoryDescriptor) super.clone();
098        if (references != null) {
099            clone.references = new MongoDBReference[references.length];
100            for (int i = 0; i < references.length; i++) {
101                clone.references[i] = references[i].clone();
102            }
103        }
104        if (inverseReferences != null) {
105            clone.inverseReferences = new InverseReference[inverseReferences.length];
106            for (int i = 0; i < inverseReferences.length; i++) {
107                clone.inverseReferences[i] = inverseReferences[i].clone();
108            }
109        }
110        return clone;
111    }
112
113    @Override
114    public MongoDBDirectory newDirectory() {
115        return new MongoDBDirectory(this);
116    }
117
118}