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 *     Salem Aouana
018 */
019
020package org.nuxeo.elasticsearch.config;
021
022import static java.util.Objects.requireNonNullElse;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XObject;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.elasticsearch.api.ESHintQueryBuilder;
028import org.nuxeo.runtime.model.Descriptor;
029
030/**
031 * This descriptor allows to add a Elasticsearch Hint.
032 *
033 * @since 11.1
034 */
035@XObject("hint")
036public class ESHintQueryBuilderDescriptor implements Descriptor {
037
038    @XNode("@name")
039    protected String name;
040
041    @XNode("@class")
042    protected Class<? extends ESHintQueryBuilder> klass;
043
044    @XNode("@remove")
045    public boolean remove;
046
047    @Override
048    public String getId() {
049        return name;
050    }
051
052    @Override
053    public ESHintQueryBuilderDescriptor merge(Descriptor descriptor) {
054        ESHintQueryBuilderDescriptor other = ESHintQueryBuilderDescriptor.class.cast(descriptor);
055        ESHintQueryBuilderDescriptor merged = new ESHintQueryBuilderDescriptor();
056        merged.name = requireNonNullElse(other.name, name);
057        merged.klass = requireNonNullElse(other.klass, klass);
058        merged.remove = other.remove;
059        return merged;
060    }
061
062    @Override
063    public boolean doesRemove() {
064        return remove;
065    }
066
067    public String getName() {
068        return name;
069    }
070
071    public Class<? extends ESHintQueryBuilder> getKlass() {
072        return klass;
073    }
074
075    public ESHintQueryBuilder newInstance() {
076        try {
077            return getKlass().getDeclaredConstructor().newInstance();
078        } catch (ReflectiveOperationException e) {
079            throw new NuxeoException(e);
080        }
081    }
082}