<commit_before>package it.smartcommunitylab.aac.apim;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import it.smartcommunitylab.aac.keymanager.model.AACService;
import it.smartcommunitylab.aac.manager.ClientDetailsManager;
import it.smartcommunitylab.aac.manager.ServiceManager;
import it.smartcommunitylab.aac.model.ClientAppBasic;
import it.smartcommunitylab.aac.model.ClientDetailsEntity;
import it.smartcommunitylab.aac.model.User;
import it.smartcommunitylab.aac.repository.ClientDetailsRepository;
import it.smartcommunitylab.aac.repository.UserRepository;

@Component
//@Transactional
public class APIMProviderService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ClientDetailsRepository clientDetailsRepository;
	@Autowired
	private ClientDetailsManager clientDetailsManager;	
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private ServiceManager serviceManager;
	@Value("${api.contextSpace}")
	private String apiProviderContext;

	public ClientAppBasic createClient(ClientAppBasic app, String userName) throws Exception {
		User user = userRepository.findByUsername(userName);
		
		if (user == null) {
			return null;
		}
		
		ClientAppBasic resApp = clientDetailsManager.createOrUpdate(app, user.getId());
		resApp.setRedirectUris(app.getRedirectUris());
		resApp.setGrantedTypes(app.getGrantedTypes());
		clientDetailsManager.update(resApp.getClientId(), resApp);
		
		return resApp;
	}	
	
	public ClientAppBasic updateClient(String clientId, ClientAppBasic app) throws Exception {
		ClientAppBasic resApp = clientDetailsManager.update(clientId, app);

		return resApp;
	}
	
	public void updateValidity(String clientId, Integer validity) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		
		entity.setAccessTokenValidity(validity);
//		entity.setRefreshTokenValidity(validity);

		clientDetailsRepository.save(entity);
	}	
	
	public void updateClientScope(String clientId, String scope) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		Set<String> oldScope = entity.getScope();
		oldScope.addAll(Splitter.on(",").splitToList(scope));
		entity.setScope(Joiner.on(",").join(oldScope));
		
		Set<String> serviceIds = serviceManager.findServiceIdsByScopes(entity.getScope());
		entity.setResourceIds(StringUtils.collectionToCommaDelimitedString(serviceIds));		
		
		ClientAppBasic resApp = clientDetailsManager.convertToClientApp(entity);
		clientDetailsManager.update(entity.getClientId(), resApp);
	}		
	
	public ClientAppBasic getClient(String consumerKey) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(consumerKey);
		ClientAppBasic resApp = clientDetailsManager.convertToClientApp(entity);
		
		return resApp;
	}	

	public void deleteClient(String clientId) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		clientDetailsRepository.delete(entity);
	}	
	
	public boolean createResource(AACService service, String userName, String tenant) throws Exception {
		// Do nothing: scopes / services should already exist
		return true;
	}
	
	public void deleteResource(String resourceName) throws Exception {
		// Do nothing: scopes / services should already exist
	}
		
}
<commit_msg>fix(APIMService) little fix for non existing client<commit_after>package it.smartcommunitylab.aac.apim;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import it.smartcommunitylab.aac.keymanager.model.AACService;
import it.smartcommunitylab.aac.manager.ClientDetailsManager;
import it.smartcommunitylab.aac.manager.ServiceManager;
import it.smartcommunitylab.aac.model.ClientAppBasic;
import it.smartcommunitylab.aac.model.ClientDetailsEntity;
import it.smartcommunitylab.aac.model.User;
import it.smartcommunitylab.aac.repository.ClientDetailsRepository;
import it.smartcommunitylab.aac.repository.UserRepository;

@Component
//@Transactional
public class APIMProviderService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ClientDetailsRepository clientDetailsRepository;
	@Autowired
	private ClientDetailsManager clientDetailsManager;	
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private ServiceManager serviceManager;
	@Value("${api.contextSpace}")
	private String apiProviderContext;

	public ClientAppBasic createClient(ClientAppBasic app, String userName) throws Exception {
		User user = userRepository.findByUsername(userName);
		
		if (user == null) {
			return null;
		}
		
		ClientAppBasic resApp = clientDetailsManager.createOrUpdate(app, user.getId());
		resApp.setRedirectUris(app.getRedirectUris());
		resApp.setGrantedTypes(app.getGrantedTypes());
		clientDetailsManager.update(resApp.getClientId(), resApp);
		
		return resApp;
	}	
	
	public ClientAppBasic updateClient(String clientId, ClientAppBasic app) throws Exception {
		ClientAppBasic resApp = clientDetailsManager.update(clientId, app);

		return resApp;
	}
	
	public void updateValidity(String clientId, Integer validity) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		
		entity.setAccessTokenValidity(validity);
//		entity.setRefreshTokenValidity(validity);

		clientDetailsRepository.save(entity);
	}	
	
	public void updateClientScope(String clientId, String scope) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		Set<String> oldScope = entity.getScope();
		oldScope.addAll(Splitter.on(",").splitToList(scope));
		entity.setScope(Joiner.on(",").join(oldScope));
		
		Set<String> serviceIds = serviceManager.findServiceIdsByScopes(entity.getScope());
		entity.setResourceIds(StringUtils.collectionToCommaDelimitedString(serviceIds));		
		
		ClientAppBasic resApp = clientDetailsManager.convertToClientApp(entity);
		clientDetailsManager.update(entity.getClientId(), resApp);
	}		
	
	public ClientAppBasic getClient(String consumerKey) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(consumerKey);
		if(entity == null) return null;
		ClientAppBasic resApp = clientDetailsManager.convertToClientApp(entity);
		
		return resApp;
	}	

	public void deleteClient(String clientId) throws Exception {
		ClientDetailsEntity entity = clientDetailsRepository.findByClientId(clientId);
		clientDetailsRepository.delete(entity);
	}	
	
	public boolean createResource(AACService service, String userName, String tenant) throws Exception {
		// Do nothing: scopes / services should already exist
		return true;
	}
	
	public void deleteResource(String resourceName) throws Exception {
		// Do nothing: scopes / services should already exist
	}
		
}

{'fraction_non_alphanumeric': 0.08772165502418054, 'fraction_numerical': 0.0005373455131649651, 'mean_word_length': 6.385912698412699, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4370557', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2058, 'n_words': 412}