001/*
002 * (C) Copyright 2017 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 java.util.Collections;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Handler used to test if a specific property is marked as deprecated/removed and to get its fallback.
027 *
028 * @since 9.2
029 */
030public class PropertyDeprecationHandler {
031
032    /**
033     * Deprecated/removed properties map, its mapping is:
034     * <p>
035     * schemaName -> propertyXPath -> fallbackXPath
036     * </p>
037     */
038    protected final Map<String, Map<String, String>> properties;
039
040    public PropertyDeprecationHandler(Map<String, Map<String, String>> properties) {
041        this.properties = properties;
042    }
043
044    /**
045     * @return true if the input property has deprecated/removed property
046     */
047    public boolean hasMarkedProperties(String schema) {
048        return properties.containsKey(schema);
049    }
050
051    /**
052     * Returned properties are a path to marked property.
053     *
054     * @return the deprecated/removed properties for input schema or an empty set if schema doesn't have marked
055     *         properties
056     */
057    public Set<String> getProperties(String schema) {
058        Map<String, String> schemaProperties = properties.get(schema);
059        if (schemaProperties == null) {
060            return Collections.emptySet();
061        }
062        return Collections.unmodifiableSet(schemaProperties.keySet());
063    }
064
065    /**
066     * @return true if the input property is marked as deprecated/removed
067     */
068    public boolean isMarked(String schema, String name) {
069        Map<String, String> schemaProperties = properties.get(schema);
070        return schemaProperties != null && schemaProperties.containsKey(name);
071    }
072
073    /**
074     * @return the fallback of input property, if it is marked as deprecated/removed and has a fallback
075     */
076    public String getFallback(String schema, String name) {
077        Map<String, String> schemaProperties = properties.get(schema);
078        if (schemaProperties != null) {
079            return schemaProperties.get(name);
080        }
081        return null;
082    }
083
084}