<commit_before>package eu.bcvsolutions.idm.core.scheduler.task.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import eu.bcvsolutions.idm.core.api.domain.ContractState;
import eu.bcvsolutions.idm.core.api.domain.CoreResultCode;
import eu.bcvsolutions.idm.core.api.dto.AbstractIdmAutomaticRoleDto;
import eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto;
import eu.bcvsolutions.idm.core.api.exception.ResultCodeException;
import eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleAttributeService;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService;

/**
 * Process all contracts that passed and not passed given automatic role
 * 
 * @author <NAME> <<EMAIL>>
 *
 */

@Service
@Description("Add or remove automatic role by IdmAutomaticRoleAttribute.")
public class ProcessAutomaticRoleByAttributeTaskExecutor extends AbstractAutomaticRoleTaskExecutor {

	private static final int PAGE_SIZE = 100;
	
	private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory
			.getLogger(ProcessAutomaticRoleByAttributeTaskExecutor.class);
	
	@Autowired private IdmAutomaticRoleAttributeService automaticRoleAttributeService;
	@Autowired private IdmIdentityContractService identityContractService;
	
	@Override
	public void init(Map<String, Object> properties) {
		this.setAutomaticRoleId(getParameterConverter().toUuid(properties, PARAMETER_ROLE_TREE_NODE));
		super.init(properties);
	}
	
	@Override
	public Boolean process() {
		UUID automaticRoleId = getAutomaticRoleId();
		IdmAutomaticRoleAttributeDto automaticRolAttributeDto = automaticRoleAttributeService.get(automaticRoleId);
		if (automaticRoleId == null || automaticRolAttributeDto == null) {
			throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_TASK_EMPTY);
		}
		Set<AbstractIdmAutomaticRoleDto> setWithAutomaticRole = Sets.newHashSet(automaticRolAttributeDto);
		//
		List<String> failedEntitiesAdd = new ArrayList<>();
		List<String> failedEntitiesRemove = new ArrayList<>();
		//
		// by contract
		Page<UUID> newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, new PageRequest(0, PAGE_SIZE));
    	Page<UUID> newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, new PageRequest(0, PAGE_SIZE));
		//
		counter = 0L;
		count = Long.valueOf(newNotPassedContracts.getTotalElements() + newNotPassedContracts.getTotalElements());
		//
		// assign new passed roles
		boolean canContinue = true;
    	//
    	// process contracts
    	canContinue = true;
    	while (canContinue) {
    		for(UUID contractId : newPassedContracts) {
    			IdmIdentityContractDto contract = identityContractService.get(contractId);
    			//
    			// check for contract validity
    			if (contract.getState() == ContractState.DISABLED || !contract.isValidNowOrInFuture()) {
    				continue;
    			}
    			//
    			try {
    				automaticRoleAttributeService.addAutomaticRoles(contract, setWithAutomaticRole);
    				counter++;
    			} catch (Exception ex) {
    				LOG.error(
    						MessageFormat.format("Error while add new automatic role id [{0}] to contract id [{1}] and identity id [{2}]",
    								automaticRoleId, contractId, contract.getIdentity()), ex);
    				failedEntitiesAdd.add(contractId.toString());
    			} finally {
    				canContinue = updateState();
    				if (!canContinue) {
    					break;
    				}
				}
    		}
    		if (newPassedContracts.hasNext()) {
    			newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, newPassedContracts.nextPageable());
    		} else {
    			break;
    		}
    	}
    	//
    	while (canContinue) {
    		for(UUID contractId : newNotPassedContracts) {
    			try { 
    				automaticRoleAttributeService.removeAutomaticRoles(contractId, setWithAutomaticRole);
    				counter++;
    			} catch (Exception ex) {
    				LOG.error(
    						MessageFormat.format("Error while remove automatic role id [{0}] from contract id [{1}].",
    								automaticRoleId, contractId), ex);
    				failedEntitiesRemove.add(contractId.toString());
    			} finally {
    				canContinue = updateState();
    				if (!canContinue) {
    					break;
    				}
				}
    		}
    		if (newNotPassedContracts.hasNext()) {
    			newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, newNotPassedContracts.nextPageable());
    		} else {
    			break;
    		}<commit_msg>#1034 review<commit_after>package eu.bcvsolutions.idm.core.scheduler.task.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import eu.bcvsolutions.idm.core.api.domain.ContractState;
import eu.bcvsolutions.idm.core.api.domain.CoreResultCode;
import eu.bcvsolutions.idm.core.api.dto.AbstractIdmAutomaticRoleDto;
import eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto;
import eu.bcvsolutions.idm.core.api.exception.ResultCodeException;
import eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleAttributeService;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService;

/**
 * Process all contracts that passed and not passed given automatic role
 * 
 * @author <NAME> <<EMAIL>>
 *
 */

@Service
@Description("Add or remove automatic role by IdmAutomaticRoleAttribute.")
public class ProcessAutomaticRoleByAttributeTaskExecutor extends AbstractAutomaticRoleTaskExecutor {

	private static final int PAGE_SIZE = 100;
	
	private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory
			.getLogger(ProcessAutomaticRoleByAttributeTaskExecutor.class);
	
	@Autowired private IdmAutomaticRoleAttributeService automaticRoleAttributeService;
	@Autowired private IdmIdentityContractService identityContractService;
	
	@Override
	public void init(Map<String, Object> properties) {
		this.setAutomaticRoleId(getParameterConverter().toUuid(properties, PARAMETER_ROLE_TREE_NODE));
		super.init(properties);
	}
	
	@Override
	public Boolean process() {
		UUID automaticRoleId = getAutomaticRoleId();
		IdmAutomaticRoleAttributeDto automaticRolAttributeDto = automaticRoleAttributeService.get(automaticRoleId);
		if (automaticRoleId == null || automaticRolAttributeDto == null) {
			throw new ResultCodeException(CoreResultCode.AUTOMATIC_ROLE_TASK_EMPTY);
		}
		Set<AbstractIdmAutomaticRoleDto> setWithAutomaticRole = Sets.newHashSet(automaticRolAttributeDto);
		//
		List<String> failedEntitiesAdd = new ArrayList<>();
		List<String> failedEntitiesRemove = new ArrayList<>();
		//
		// by contract
		Page<UUID> newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, new PageRequest(0, PAGE_SIZE));
    	Page<UUID> newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, new PageRequest(0, PAGE_SIZE));
		//
		counter = 0L;
		count = Long.valueOf(newNotPassedContracts.getTotalElements() + newNotPassedContracts.getTotalElements());
		//
		// assign new passed roles
		boolean canContinue = true;
    	//
    	// process contracts
    	canContinue = true;
    	while (canContinue) {
    		for(UUID contractId : newPassedContracts) {
    			IdmIdentityContractDto contract = identityContractService.get(contractId);
    			//
    			// check for contract validity
    			if (contract.getState() == ContractState.DISABLED || !contract.isValidNowOrInFuture()) {
    				continue;
    			}
    			//
    			try {
    				automaticRoleAttributeService.addAutomaticRoles(contract, setWithAutomaticRole);
    				counter++;
    			} catch (Exception ex) {
    				LOG.error("Error while add new automatic role id [{}] to contract id [{}] and identity id [{}]", 
    						automaticRoleId, contractId, contract.getIdentity(), ex);
    				failedEntitiesAdd.add(contractId.toString());
    			} finally {
    				canContinue = updateState();
    				if (!canContinue) {
    					break;
    				}
				}
    		}
    		if (newPassedContracts.hasNext()) {
    			newPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, true, newPassedContracts.nextPageable());
    		} else {
    			break;
    		}
    	}
    	//
    	while (canContinue) {
    		for(UUID contractId : newNotPassedContracts) {
    			try { 
    				automaticRoleAttributeService.removeAutomaticRoles(contractId, setWithAutomaticRole);
    				counter++;
    			} catch (Exception ex) {
    				LOG.error("Error while remove automatic role id [{}] from contract id [{}].",
    								automaticRoleId, contractId, ex);
    				failedEntitiesRemove.add(contractId.toString());
    			} finally {
    				canContinue = updateState();
    				if (!canContinue) {
    					break;
    				}
				}
    		}
    		if (newNotPassedContracts.hasNext()) {
    			newNotPassedContracts = automaticRoleAttributeService.getContractsForAutomaticRole(automaticRoleId, false, newNotPassedContracts.nextPageable());
    		} else {
    			break;
    		}
{'fraction_non_alphanumeric': 0.09771309771309772, 'fraction_numerical': 0.002673002673002673, 'mean_word_length': 4.167263427109974, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '383318', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3074, 'n_words': 526}