@Override public List<BusinessObjectDefinitionEntity> getPercentageOfAllBusinessObjectDefinitions(double percentage) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class); // The criteria root is the business object definition. Root<BusinessObjectDefinitionEntity> businessObjectDefinitionEntityRoot = criteria.from(BusinessObjectDefinitionEntity.class); // Get the columns. Path<Integer> idColumn = businessObjectDefinitionEntityRoot.get(BusinessObjectDefinitionEntity_.id); criteria.select(idColumn); List<Integer> allBusinessObjectDefinitionIdsList = entityManager.createQuery(criteria).getResultList(); List<Integer> percentageOfBusinessObjectDefinitionIdsList = new ArrayList<>(); /* * Gets a percentage of all business object definition entities. * The percentage is randomly selected from all the business object definitions. * * For each business object id in the list of all business object definition ids, get a random double value between 0 and 1. * If that value is below the percentage double value, also a number between 0 and 1 (inclusive), * then add the business object id to the list of business object definition ids that will be used to return a random percentage * of business object definition entities retrieved from the database. */ allBusinessObjectDefinitionIdsList.forEach(id -> { if (ThreadLocalRandom.current().nextDouble() < percentage) { percentageOfBusinessObjectDefinitionIdsList.add(id); } }); return getAllBusinessObjectDefinitionsByIds(percentageOfBusinessObjectDefinitionIdsList); }
@Override public List<BusinessObjectDefinitionEntity> getPercentageOfAllBusinessObjectDefinitions(double percentage) { // Create the criteria builder and a tuple style criteria query. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class); // The criteria root is the business object definition. Root<BusinessObjectDefinitionEntity> businessObjectDefinitionEntityRoot = criteria.from(BusinessObjectDefinitionEntity.class); // Get the columns. Path<Integer> idColumn = businessObjectDefinitionEntityRoot.get(BusinessObjectDefinitionEntity_.id); criteria.select(idColumn); List<Integer> allBusinessObjectDefinitionIdsList = entityManager.createQuery(criteria).getResultList(); List<Integer> percentageOfBusinessObjectDefinitionIdsList = new ArrayList<>(); /* * Gets a percentage of all business object definition entities. * The percentage is randomly selected from all the business object definitions. * * For each business object id in the list of all business object definition ids, get a random double value between 0 and 1. * If that value is below the percentage double value, also a number between 0 and 1 (inclusive), * then add the business object id to the list of business object definition ids that will be used to return a random percentage * of business object definition entities retrieved from the database. */ allBusinessObjectDefinitionIdsList.forEach(id -> { if (ThreadLocalRandom.current().nextDouble() < percentage) { percentageOfBusinessObjectDefinitionIdsList.add(id); } }); return getAllBusinessObjectDefinitionsByIds(percentageOfBusinessObjectDefinitionIdsList); }