001/*
002 * (C) Copyright 2006-2007 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 *
019 * $Id: MultiDirectoryDescriptor.java 24597 2007-09-05 16:04:04Z fguillaume $
020 */
021
022package org.nuxeo.ecm.directory.multi;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeList;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.ecm.directory.PermissionDescriptor;
028
029/**
030 * @author Florent Guillaume
031 */
032@XObject(value = "directory")
033public class MultiDirectoryDescriptor implements Cloneable {
034
035    @XNode("@name")
036    public String name;
037
038    @XNode("schema")
039    protected String schemaName;
040
041    @XNode("idField")
042    protected String idField;
043
044    @XNode("passwordField")
045    protected String passwordField;
046
047    @XNode("readOnly")
048    public Boolean readOnly;
049
050    @XNode("querySizeLimit")
051    public Integer querySizeLimit;
052
053    @XNode("@remove")
054    public boolean remove = false;
055
056    @XNodeList(value = "source", type = SourceDescriptor[].class, componentType = SourceDescriptor.class)
057    protected SourceDescriptor[] sources;
058
059    @XNodeList(value = "permissions/permission", type = PermissionDescriptor[].class, componentType = PermissionDescriptor.class)
060    public PermissionDescriptor[] permissions = null;
061
062    public void merge(MultiDirectoryDescriptor other) {
063        merge(other, false);
064    }
065
066    public void merge(MultiDirectoryDescriptor other, boolean overwrite) {
067        if (other.schemaName != null || overwrite) {
068            schemaName = other.schemaName;
069        }
070        if (other.idField != null || overwrite) {
071            idField = other.idField;
072        }
073        if (other.passwordField != null || overwrite) {
074            passwordField = other.passwordField;
075        }
076        if (other.readOnly != null || overwrite) {
077            readOnly = other.readOnly;
078        }
079        if (other.querySizeLimit != null || overwrite) {
080            querySizeLimit = other.querySizeLimit;
081        }
082        if (other.sources != null || overwrite) {
083            if (sources == null) {
084                sources = other.sources;
085            } else {
086                SourceDescriptor[] s = new SourceDescriptor[sources.length + other.sources.length];
087                System.arraycopy(sources, 0, s, 0, sources.length);
088                System.arraycopy(other.sources, 0, s, sources.length, other.sources.length);
089                sources = s;
090            }
091        }
092        if ((other.permissions != null && other.permissions.length != 0) || overwrite) {
093            permissions = other.permissions;
094        }
095    }
096
097    /**
098     * @since 5.6
099     */
100    public MultiDirectoryDescriptor clone() {
101        MultiDirectoryDescriptor clone = new MultiDirectoryDescriptor();
102        clone.name = name;
103        clone.schemaName = schemaName;
104        clone.idField = idField;
105        clone.passwordField = passwordField;
106        clone.readOnly = readOnly;
107        clone.querySizeLimit = querySizeLimit;
108        clone.remove = remove;
109        if (sources != null) {
110            clone.sources = new SourceDescriptor[sources.length];
111            for (int i = 0; i < sources.length; i++) {
112                clone.sources[i] = sources[i].clone();
113            }
114        }
115        if (permissions != null) {
116            clone.permissions = new PermissionDescriptor[permissions.length];
117            for (int i = 0; i < permissions.length; i++) {
118                clone.permissions[i] = permissions[i].clone();
119            }
120        }
121        return clone;
122    }
123
124}