001/*
002 * (C) Copyright 2014-2018 Nuxeo (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.lang3.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        @Override
064        public ACLDescriptor clone() {
065            ACLDescriptor clone;
066            try {
067                clone = (ACLDescriptor) super.clone();
068            } catch (CloneNotSupportedException e) {
069                throw new AssertionError(e);
070            }
071            // basic fields are already copied by super.clone()
072            return clone;
073        }
074    }
075
076    @XNode("docType")
077    protected String docType;
078
079    @XNode("querySizeLimit")
080    public Integer querySizeLimit;
081
082    @XNode("repositoryName")
083    protected String repositoryName;
084
085    @XNode("createPath")
086    protected String createPath;
087
088    public String getCreatePath() {
089        return createPath == null ? DEFAULT_CREATE_PATH : createPath;
090    }
091
092    @XNode("canCreateRootFolder")
093    protected Boolean canCreateRootFolder;
094
095    public boolean canCreateRootFolder() {
096        return canCreateRootFolder == null ? DEFAULT_CAN_CREATE_ROOT_FOLDER : canCreateRootFolder.booleanValue();
097    }
098
099    @XNodeMap(value = "fieldMapping", key = "@name", type = HashMap.class, componentType = String.class)
100    public Map<String, String> fieldMapping = new HashMap<String, String>();
101
102    @XNodeList(value = "acl", type = ACLDescriptor[].class, componentType = ACLDescriptor.class)
103    protected ACLDescriptor[] acls;
104
105    @Override
106    public CoreDirectoryDescriptor clone() {
107        CoreDirectoryDescriptor clone = (CoreDirectoryDescriptor) super.clone();
108        // basic fields are already copied by super.clone()
109        clone.fieldMapping = fieldMapping;
110        if (acls != null) {
111            clone.acls = acls.clone();
112        }
113        return clone;
114    }
115
116    public String getRepositoryName() {
117        if (StringUtils.isBlank(repositoryName)) {
118            repositoryName = Framework.getService(RepositoryManager.class).getDefaultRepositoryName();
119        }
120        return repositoryName;
121    }
122
123    @Override
124    public void merge(BaseDirectoryDescriptor other) {
125        super.merge(other);
126        if (other instanceof CoreDirectoryDescriptor) {
127            merge((CoreDirectoryDescriptor) other);
128        }
129    }
130
131    protected void merge(CoreDirectoryDescriptor other) {
132        if (other.docType != null) {
133            docType = other.docType;
134        }
135        if (other.querySizeLimit != null) {
136            querySizeLimit = other.querySizeLimit;
137        }
138        if (other.repositoryName != null) {
139            repositoryName = other.repositoryName;
140        }
141        if (other.createPath != null) {
142            createPath = other.createPath;
143        }
144        if (other.docType != null) {
145            docType = other.docType;
146        }
147        if (other.fieldMapping != null) {
148            fieldMapping = other.fieldMapping;
149        }
150        if (other.canCreateRootFolder != null) {
151            canCreateRootFolder = other.canCreateRootFolder;
152        }
153        if (other.acls != null) {
154            ACLDescriptor[] otherAcls = new ACLDescriptor[acls.length + other.acls.length];
155            System.arraycopy(acls, 0, otherAcls, 0, acls.length);
156            System.arraycopy(other.acls, 0, otherAcls, acls.length, other.acls.length);
157            acls = otherAcls;
158        }
159    }
160
161    @Override
162    public CoreDirectory newDirectory() {
163        return new CoreDirectory(this);
164    }
165
166}