001/*
002 * (C) Copyright 2014-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 *     Maxime Hilaire
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.directory.core;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.lang.StringUtils;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeList;
030import org.nuxeo.common.xmap.annotation.XNodeMap;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.nuxeo.ecm.core.api.repository.RepositoryManager;
033import org.nuxeo.ecm.directory.BaseDirectoryDescriptor;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Descriptor for a {@link CoreDirectory}.
038 *
039 * @since 8.2
040 */
041@XObject(value = "directory")
042public class CoreDirectoryDescriptor extends BaseDirectoryDescriptor {
043
044    protected static final Log log = LogFactory.getLog(CoreDirectoryDescriptor.class);
045
046    public static final String DEFAULT_CREATE_PATH = "/";
047
048    public static final boolean DEFAULT_CAN_CREATE_ROOT_FOLDER = true;
049
050    @XObject(value = "acl")
051    public static class ACLDescriptor implements Cloneable {
052
053        @XNode("@userOrGroupName")
054        public String userOrGroupName;
055
056        @XNode("@privilege")
057        public String privilege;
058
059        @XNode("@granted")
060        public boolean granted = false;
061
062    }
063
064    @XNode("docType")
065    protected String docType;
066
067    @XNode("querySizeLimit")
068    public Integer querySizeLimit;
069
070    @XNode("repositoryName")
071    protected String repositoryName;
072
073    @XNode("createPath")
074    protected String createPath;
075
076    public String getCreatePath() {
077        return createPath == null ? DEFAULT_CREATE_PATH : createPath;
078    }
079
080    @XNode("canCreateRootFolder")
081    protected Boolean canCreateRootFolder;
082
083    public boolean canCreateRootFolder() {
084        return canCreateRootFolder == null ? DEFAULT_CAN_CREATE_ROOT_FOLDER : canCreateRootFolder.booleanValue();
085    }
086
087    @XNodeMap(value = "fieldMapping", key = "@name", type = HashMap.class, componentType = String.class)
088    public Map<String, String> fieldMapping = new HashMap<String, String>();
089
090    @XNodeList(value = "acl", type = ACLDescriptor[].class, componentType = ACLDescriptor.class)
091    protected ACLDescriptor[] acls;
092
093    @Override
094    public CoreDirectoryDescriptor clone() {
095        CoreDirectoryDescriptor clone = (CoreDirectoryDescriptor) super.clone();
096        // basic fields are already copied by super.clone()
097        clone.fieldMapping = fieldMapping;
098        if (acls != null) {
099            clone.acls = acls.clone();
100        }
101        return clone;
102    }
103
104    public String getRepositoryName() {
105        if (StringUtils.isBlank(repositoryName)) {
106            repositoryName = Framework.getService(RepositoryManager.class).getDefaultRepositoryName();
107        }
108        return repositoryName;
109    }
110
111    @Override
112    public void merge(BaseDirectoryDescriptor other) {
113        super.merge(other);
114        if (other instanceof CoreDirectoryDescriptor) {
115            merge((CoreDirectoryDescriptor) other);
116        }
117    }
118
119    protected void merge(CoreDirectoryDescriptor other) {
120        if (other.docType != null) {
121            docType = other.docType;
122        }
123        if (other.querySizeLimit != null) {
124            querySizeLimit = other.querySizeLimit;
125        }
126        if (other.repositoryName != null) {
127            repositoryName = other.repositoryName;
128        }
129        if (other.createPath != null) {
130            createPath = other.createPath;
131        }
132        if (other.docType != null) {
133            docType = other.docType;
134        }
135        if (other.fieldMapping != null) {
136            fieldMapping = other.fieldMapping;
137        }
138        if (other.canCreateRootFolder != null) {
139            canCreateRootFolder = other.canCreateRootFolder;
140        }
141        if (other.acls != null) {
142            ACLDescriptor[] otherAcls = new ACLDescriptor[acls.length + other.acls.length];
143            System.arraycopy(acls, 0, otherAcls, 0, acls.length);
144            System.arraycopy(other.acls, 0, otherAcls, acls.length, other.acls.length);
145            acls = otherAcls;
146        }
147    }
148
149    @Override
150    public CoreDirectory newDirectory() {
151        return new CoreDirectory(this);
152    }
153
154}