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 *     bdelbosc
018 */
019
020package org.nuxeo.ecm.core.scroll;
021
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeMap;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.scroll.Scroll;
031import org.nuxeo.runtime.model.Descriptor;
032
033/**
034 * @since 11.1
035 */
036@XObject("scroll")
037public class ScrollDescriptor implements Descriptor {
038
039    @XNode("@enabled")
040    protected boolean isEnabled = true;
041
042    @XNode("@type")
043    protected String type;
044
045    @XNode("@name")
046    protected String name;
047
048    @XNode("@default")
049    protected boolean isDefault;
050
051    @XNode("@class")
052    protected Class<? extends Scroll> scrollClass;
053
054    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
055    protected Map<String, String> options = new HashMap<>();
056
057    protected Map<String, String> optionsReadOnly;
058
059    @Override
060    public String getId() {
061        return getType() + ":" + getName();
062    }
063
064    public Scroll newScrollInstance() {
065        try {
066            return scrollClass.getDeclaredConstructor().newInstance();
067        } catch (ReflectiveOperationException e) {
068            throw new NuxeoException("Cannot create scroll class of type " + scrollClass.getName(), e);
069        }
070    }
071
072    public String getName() {
073        return name;
074    }
075
076    public String getType() {
077        return type;
078    }
079
080    public boolean isEnabled() {
081        return isEnabled;
082    }
083
084    public boolean isDefault() {
085        return isDefault;
086    }
087
088    public Map<String, String> getOptions() {
089        if (optionsReadOnly == null) {
090            optionsReadOnly = Collections.unmodifiableMap(options);
091        }
092        return optionsReadOnly;
093    }
094}