001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 *
017 * $Id: MultiDirectoryDescriptor.java 24597 2007-09-05 16:04:04Z fguillaume $
018 */
019
020package org.nuxeo.ecm.directory.multi;
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.PermissionDescriptor;
026
027/**
028 * @author Florent Guillaume
029 */
030@XObject(value = "directory")
031public class MultiDirectoryDescriptor implements Cloneable {
032
033    @XNode("@name")
034    public String name;
035
036    @XNode("schema")
037    protected String schemaName;
038
039    @XNode("idField")
040    protected String idField;
041
042    @XNode("passwordField")
043    protected String passwordField;
044
045    @XNode("readOnly")
046    public Boolean readOnly;
047
048    @XNode("querySizeLimit")
049    public Integer querySizeLimit;
050
051    @XNode("@remove")
052    public boolean remove = false;
053
054    @XNodeList(value = "source", type = SourceDescriptor[].class, componentType = SourceDescriptor.class)
055    protected SourceDescriptor[] sources;
056
057    @XNodeList(value = "permissions/permission", type = PermissionDescriptor[].class, componentType = PermissionDescriptor.class)
058    public PermissionDescriptor[] permissions = null;
059
060    public void merge(MultiDirectoryDescriptor other) {
061        merge(other, false);
062    }
063
064    public void merge(MultiDirectoryDescriptor other, boolean overwrite) {
065        if (other.schemaName != null || overwrite) {
066            schemaName = other.schemaName;
067        }
068        if (other.idField != null || overwrite) {
069            idField = other.idField;
070        }
071        if (other.passwordField != null || overwrite) {
072            passwordField = other.passwordField;
073        }
074        if (other.readOnly != null || overwrite) {
075            readOnly = other.readOnly;
076        }
077        if (other.querySizeLimit != null || overwrite) {
078            querySizeLimit = other.querySizeLimit;
079        }
080        if (other.sources != null || overwrite) {
081            if (sources == null) {
082                sources = other.sources;
083            } else {
084                SourceDescriptor[] s = new SourceDescriptor[sources.length + other.sources.length];
085                System.arraycopy(sources, 0, s, 0, sources.length);
086                System.arraycopy(other.sources, 0, s, sources.length, other.sources.length);
087                sources = s;
088            }
089        }
090        if ((other.permissions != null && other.permissions.length != 0) || overwrite) {
091            permissions = other.permissions;
092        }
093    }
094
095    /**
096     * @since 5.6
097     */
098    public MultiDirectoryDescriptor clone() {
099        MultiDirectoryDescriptor clone = new MultiDirectoryDescriptor();
100        clone.name = name;
101        clone.schemaName = schemaName;
102        clone.idField = idField;
103        clone.passwordField = passwordField;
104        clone.readOnly = readOnly;
105        clone.querySizeLimit = querySizeLimit;
106        clone.remove = remove;
107        if (sources != null) {
108            clone.sources = new SourceDescriptor[sources.length];
109            for (int i = 0; i < sources.length; i++) {
110                clone.sources[i] = sources[i].clone();
111            }
112        }
113        if (permissions != null) {
114            clone.permissions = new PermissionDescriptor[permissions.length];
115            for (int i = 0; i < permissions.length; i++) {
116                clone.permissions[i] = permissions[i].clone();
117            }
118        }
119        return clone;
120    }
121
122}