001/*
002 * (C) Copyright 2019 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 *     Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.core.schema;
020
021import org.apache.commons.lang3.builder.ToStringBuilder;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.runtime.model.Descriptor;
025
026/**
027 * Descriptor representing a Nuxeo Property.
028 * <p>
029 * It maps the xml below:
030 *
031 * <pre>
032 * {@code <property schema="SCHEMA" name="NAME" secured="true" deprecation="deprecated|removed" fallback="NAME" />}
033 * </pre>
034 *
035 * @since 11.1
036 */
037@XObject("property")
038public class PropertyDescriptor implements Descriptor {
039
040    public static final String DEPRECATED = "deprecated";
041
042    public static final String REMOVED = "removed";
043
044    @XNode("@schema")
045    protected String schema;
046
047    @XNode("@name")
048    protected String name;
049
050    @XNode("@secured")
051    public Boolean secured;
052
053    @XNode("@deprecation")
054    protected String deprecation;
055
056    @XNode("@fallback")
057    protected String fallback;
058
059    @XNode("@remove")
060    public boolean remove;
061
062    @Override
063    public String getId() {
064        return schema + ':' + name;
065    }
066
067    public String getSchema() {
068        return schema;
069    }
070
071    public String getName() {
072        return name;
073    }
074
075    public boolean isSecured() {
076        return Boolean.TRUE.equals(secured);
077    }
078
079    /**
080     * @return {@link #DEPRECATED deprecated}, {@link #REMOVED removed} or null
081     */
082    public String getDeprecation() {
083        return deprecation;
084    }
085
086    public boolean isDeprecated() {
087        return DEPRECATED.equalsIgnoreCase(deprecation);
088    }
089
090    public boolean isRemoved() {
091        return REMOVED.equalsIgnoreCase(deprecation);
092    }
093
094    public String getFallback() {
095        return fallback;
096    }
097
098    @Override
099    public Descriptor merge(Descriptor o) {
100        PropertyDescriptor other = (PropertyDescriptor) o;
101        PropertyDescriptor merged = new PropertyDescriptor();
102        merged.schema = schema;
103        merged.name = name;
104        merged.secured = other.secured != null ? other.secured : secured;
105        merged.deprecation = other.deprecation != null ? other.deprecation : deprecation;
106        merged.fallback = other.fallback != null ? other.fallback : fallback;
107        return merged;
108    }
109
110    @Override
111    public boolean doesRemove() {
112        return remove;
113    }
114
115    @Override
116    public String toString() {
117        return ToStringBuilder.reflectionToString(this);
118    }
119}