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.Directory;
028import org.nuxeo.ecm.directory.BaseDirectoryDescriptor;
029
030/**
031 * @author Florent Guillaume
032 */
033@XObject(value = "directory")
034public class MultiDirectoryDescriptor extends BaseDirectoryDescriptor {
035
036    @XNode("querySizeLimit")
037    public Integer querySizeLimit;
038
039    @XNodeList(value = "source", type = SourceDescriptor[].class, componentType = SourceDescriptor.class)
040    protected SourceDescriptor[] sources;
041
042    @Override
043    public void merge(BaseDirectoryDescriptor other) {
044        super.merge(other);
045        if (other instanceof MultiDirectoryDescriptor) {
046            merge((MultiDirectoryDescriptor) other);
047        }
048    }
049
050    protected void merge(MultiDirectoryDescriptor other) {
051        if (other.querySizeLimit != null) {
052            querySizeLimit = other.querySizeLimit;
053        }
054        if (other.sources != null) {
055            if (sources == null) {
056                sources = other.sources;
057            } else {
058                SourceDescriptor[] s = new SourceDescriptor[sources.length + other.sources.length];
059                System.arraycopy(sources, 0, s, 0, sources.length);
060                System.arraycopy(other.sources, 0, s, sources.length, other.sources.length);
061                sources = s;
062            }
063        }
064    }
065
066    /**
067     * @since 5.6
068     */
069    @Override
070    public MultiDirectoryDescriptor clone() {
071        MultiDirectoryDescriptor clone = (MultiDirectoryDescriptor) super.clone();
072        // basic fields are already copied by super.clone()
073        if (sources != null) {
074            clone.sources = new SourceDescriptor[sources.length];
075            for (int i = 0; i < sources.length; i++) {
076                clone.sources[i] = sources[i].clone();
077            }
078        }
079        return clone;
080    }
081
082    @Override
083    public Directory newDirectory() {
084        return new MultiDirectory(this);
085    }
086
087}