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 *     Funsho David
018 *     Kevin Leturc
019 */
020package org.nuxeo.ecm.core.versioning;
021
022import java.util.HashSet;
023import java.util.Set;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeList;
029import org.nuxeo.common.xmap.annotation.XObject;
030
031/**
032 * @since 9.1
033 */
034@XObject("filter")
035public class VersioningFilterDescriptor {
036
037    private static final Log log = LogFactory.getLog(VersioningFilterDescriptor.class);
038
039    @XNode("@id")
040    protected String id;
041
042    @XNode("@class")
043    protected Class<VersioningPolicyFilter> className;
044
045    @XNodeList(value = "type", componentType = String.class, type = HashSet.class)
046    protected Set<String> types = new HashSet<>();
047
048    @XNodeList(value = "facet", componentType = String.class, type = HashSet.class)
049    protected Set<String> facets = new HashSet<>();
050
051    @XNodeList(value = "schema", componentType = String.class, type = HashSet.class)
052    protected Set<String> schemas = new HashSet<>();
053
054    @XNode("condition")
055    protected String condition;
056
057    public String getId() {
058        return id;
059    }
060
061    public VersioningPolicyFilter newInstance() {
062        VersioningPolicyFilter filter = null;
063        if (className != null) {
064            try {
065                filter = className.getDeclaredConstructor().newInstance();
066            } catch (ReflectiveOperationException e) {
067                log.error("Class " + className + " could not be instantiated", e);
068            }
069        } else {
070            return new StandardVersioningPolicyFilter(types, facets, schemas, condition);
071        }
072        return filter;
073    }
074
075    @Override
076    public String toString() {
077        return getClass().getSimpleName() + '(' + id + ')';
078    }
079
080}