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 */
019package org.nuxeo.ecm.core.scroll;
020
021import static org.apache.commons.lang3.StringUtils.isEmpty;
022
023import java.time.Duration;
024import java.util.Objects;
025
026import org.nuxeo.ecm.core.api.repository.RepositoryManager;
027import org.nuxeo.ecm.core.api.scroll.ScrollRequest;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Request to scroll documents.
032 *
033 * @since 11.1
034 */
035public class DocumentScrollRequest implements ScrollRequest {
036
037    protected static final String SCROLL_TYPE = "document";
038
039    protected final String name;
040
041    protected final String query;
042
043    protected final String repository;
044
045    protected final Duration timeout;
046
047    protected final int size;
048
049    protected final String username;
050
051
052    protected DocumentScrollRequest(Builder builder) {
053        this.name = builder.getName();
054        this.query = builder.getQuery();
055        this.timeout = builder.getTimeout();
056        this.size = builder.getSize();
057        this.username = builder.getUsername();
058        this.repository = builder.getRepository();
059    }
060
061    @Override
062    public String getType() {
063        return SCROLL_TYPE;
064    }
065
066    @Override
067    public String getName() {
068        return name;
069    }
070
071    @Override
072    public int getSize() {
073        return size;
074    }
075
076    public String getQuery() {
077        return query;
078    }
079
080    public Duration getTimeout() {
081        return timeout;
082    }
083
084    public String getUsername() {
085        return username;
086    }
087
088    public String getRepository() {
089        return repository;
090    }
091
092    @Override
093    public String toString() {
094        return "DocumentScrollRequest{" + "name='" + name + '\'' + ", query='" + query + '\'' + ", repository='"
095                + repository + '\'' + ", timeout=" + timeout + ", size=" + size + ", username='" + username + '\''
096                + '}';
097    }
098
099    /**
100     * Creates a builder using an NXQL query.
101     */
102    public static Builder builder(String nxqlQuery) {
103        return new Builder(nxqlQuery);
104    }
105
106    public static class Builder {
107
108        protected final String query;
109
110        protected String name;
111
112        protected String username;
113
114        protected String repository;
115
116        protected Duration timeout;
117
118        protected int size;
119
120        public static final String UNKNOWN = "unknown";
121
122        public static final int DEFAULT_SCROLL_SIZE = 50;
123
124        public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(120);
125
126        protected Builder(String nxqlQuery) {
127            this.query = Objects.requireNonNull(nxqlQuery, "NXQL query cannot be null");
128        }
129
130        /**
131         * Uses a registered scroll implementation, {@code null} for default implementation.
132         */
133        public Builder name(String name) {
134            this.name = name;
135            return this;
136        }
137
138        /**
139         * Maximum duration between iteration on Scroll.
140         */
141        public Builder timeout(Duration timeout) {
142            this.timeout = Objects.requireNonNull(timeout, "Timeout cannot be null");
143            return this;
144        }
145
146        /**
147         * The number of item to fetch.
148         */
149        public Builder size(int size) {
150            if (size <= 0) {
151                throw new IllegalArgumentException("size must be > 0");
152            }
153            this.size = size;
154            return this;
155        }
156
157        /**
158         * The repository to execute the NXQL request.
159         */
160        public Builder repository(String repository) {
161            this.repository = repository;
162            return this;
163        }
164
165        /**
166         * The user executing the NXQL request.
167         */
168        public Builder username(String username) {
169            this.username = username;
170            return this;
171        }
172
173        public String getName() {
174            return name;
175        }
176
177        public String getQuery() {
178            return query;
179        }
180
181        public Duration getTimeout() {
182            return timeout == null ? DEFAULT_TIMEOUT : timeout;
183        }
184
185        public int getSize() {
186            return size == 0 ? DEFAULT_SCROLL_SIZE : size;
187        }
188
189        public String getUsername() {
190            return username == null ? UNKNOWN : username;
191        }
192
193        public String getRepository() {
194            if (isEmpty(repository)) {
195                RepositoryManager repoManager = Framework.getService(RepositoryManager.class);
196                if (repoManager == null) {
197                    return UNKNOWN;
198                }
199                return repoManager.getDefaultRepositoryName();
200            }
201            return repository;
202        }
203
204        public DocumentScrollRequest build() {
205            return new DocumentScrollRequest(this);
206        }
207    }
208
209}