OAuthServiceImpl.java

/*
 * Copyright 2026 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.server.service.impl;

import java.util.Collections;
import java.util.List;

import org.genesys.blocks.oauth.service.AbstractClientServiceImpl;
import org.genesys.server.model.OAuthRole;
import org.genesys.server.model.genesys.OAuthClient;
import org.genesys.server.model.genesys.QOAuthClient;
import org.genesys.server.service.OAuthClientService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;

/**
 * The Class OAuthServiceImpl.
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class OAuthServiceImpl extends AbstractClientServiceImpl<OAuthRole, OAuthClient> implements OAuthClientService {

	@Override
	public List<OAuthClient> autocompleteClients(final String term, int limit) {
		if (StringUtils.isBlank(term) || term.length() < 1)
			return Collections.emptyList();

		log.debug("Autocomplete for={}", term);

		Predicate predicate = QOAuthClient.oAuthClient.title.startsWithIgnoreCase(term)
			// clientId
			.or(QOAuthClient.oAuthClient.clientId.startsWithIgnoreCase(term))
			// description contains
			.or(QOAuthClient.oAuthClient.description.contains(term));

		return oauthClientRepository.findAll(predicate, PageRequest.of(0, Math.min(100, limit), Sort.by("title"))).getContent();
	}

}