use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.
the class QuantityRules method getSuggestedQuantity.
@SuppressWarnings("checkstyle:indentation")
public SuggestedQuantity getSuggestedQuantity(Pool p, Consumer c, Date date) {
JsonJsContext args = new JsonJsContext(mapper);
Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream().filter(ent -> ent.isValidOnDate(date)).map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
args.put("consumer", this.translator.translate(c, ConsumerDTO.class));
args.put("pool", this.translator.translate(p, PoolDTO.class));
args.put("validEntitlements", entStream.collect(Collectors.toSet()));
args.put("log", log, false);
args.put("guestIds", c.getGuestIds());
String json = jsRules.runJsFunction(String.class, "get_suggested_quantity", args);
SuggestedQuantity dto = mapper.toObject(json, SuggestedQuantity.class);
return dto;
}
use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.
the class QuantityRules method getSuggestedQuantities.
/**
* Calculates the suggested quantities for many pools in one call. This allows for
* performant list pools queries with large numbers of pools and a large amount of
* entitlements to serialize.
*
* Map returned will map each pool ID to the suggested quantities for it. Every pool
* provided should have it's ID present in the result.
*
* @param pools
* @param c
* @param date
* @return suggested quantities for all pools requested
*/
@SuppressWarnings("checkstyle:indentation")
public Map<String, SuggestedQuantity> getSuggestedQuantities(List<Pool> pools, Consumer c, Date date) {
JsonJsContext args = new JsonJsContext(mapper);
Stream<PoolDTO> poolStream = pools == null ? Stream.empty() : pools.stream().map(this.translator.getStreamMapper(Pool.class, PoolDTO.class));
Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream().filter(ent -> ent.isValidOnDate(date)).map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
args.put("pools", poolStream.collect(Collectors.toSet()));
args.put("consumer", this.translator.translate(c, ConsumerDTO.class));
args.put("validEntitlements", entStream.collect(Collectors.toSet()));
args.put("log", log, false);
args.put("guestIds", c.getGuestIds());
String json = jsRules.runJsFunction(String.class, "get_suggested_quantities", args);
Map<String, SuggestedQuantity> resultMap;
TypeReference<Map<String, SuggestedQuantity>> typeref = new TypeReference<Map<String, SuggestedQuantity>>() {
};
try {
resultMap = mapper.toObject(json, typeref);
} catch (Exception e) {
throw new RuleExecutionException(e);
}
return resultMap;
}
use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.
the class EntitlementRules method filterPools.
@Override
@SuppressWarnings("checkstyle:indentation")
public List<Pool> filterPools(Consumer consumer, List<Pool> pools, boolean showAll) {
JsonJsContext args = new JsonJsContext(objectMapper);
Map<String, ValidationResult> resultMap = new HashMap<>();
ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
if (!ctype.isType(ConsumerTypeEnum.SHARE)) {
Stream<PoolDTO> poolStream = pools == null ? Stream.empty() : pools.stream().map(this.translator.getStreamMapper(Pool.class, PoolDTO.class));
Stream<EntitlementDTO> entStream = consumer.getEntitlements() == null ? Stream.empty() : consumer.getEntitlements().stream().map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
args.put("hostConsumer", this.translator.translate(getHost(consumer, pools), ConsumerDTO.class));
args.put("consumerEntitlements", entStream.collect(Collectors.toSet()));
args.put("standalone", config.getBoolean(ConfigProperties.STANDALONE));
args.put("pools", poolStream.collect(Collectors.toSet()));
args.put("caller", CallerType.LIST_POOLS.getLabel());
args.put("log", log, false);
String json = jsRules.runJsFunction(String.class, "validate_pools_list", args);
TypeReference<Map<String, ValidationResult>> typeref = new TypeReference<Map<String, ValidationResult>>() {
};
try {
resultMap = objectMapper.toObject(json, typeref);
} catch (Exception e) {
throw new RuleExecutionException(e);
}
}
List<Pool> filteredPools = new LinkedList<>();
for (Pool pool : pools) {
ValidationResult result;
if (ctype.isType(ConsumerTypeEnum.SHARE)) {
result = new ValidationResult();
resultMap.put(pool.getId(), result);
validatePoolSharingEligibility(result, pool);
} else {
result = resultMap.get(pool.getId());
}
finishValidation(result, pool, 1);
if (result.isSuccessful() && (!result.hasWarnings() || showAll)) {
filteredPools.add(pool);
} else if (log.isDebugEnabled()) {
log.debug("Omitting pool due to failed rules: " + pool.getId());
if (result.hasErrors()) {
log.debug("\tErrors: " + result.getErrors());
}
if (result.hasWarnings()) {
log.debug("\tWarnings: " + result.getWarnings());
}
}
}
return filteredPools;
}
use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.
the class OverrideRules method canOverrideForConsumer.
public boolean canOverrideForConsumer(String name) {
JsonJsContext args = new JsonJsContext(mapper);
args.put("name", name);
args.put("log", log, false);
args.put("hosted", !config.getBoolean(ConfigProperties.STANDALONE));
return !jsRules.runJsFunction(Boolean.class, "get_allow_override", args);
}
use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.
the class ComplianceRules method isEntitlementCompliant.
public boolean isEntitlementCompliant(Consumer consumer, Entitlement ent, Date onDate) {
List<Entitlement> ents = entCurator.listByConsumerAndDate(consumer, onDate).list();
Stream<EntitlementDTO> entStream = ents == null ? Stream.empty() : ents.stream().map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
JsonJsContext args = new JsonJsContext(mapper);
args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
args.put("entitlement", this.translator.translate(ent, EntitlementDTO.class));
args.put("entitlements", entStream.collect(Collectors.toSet()));
args.put("log", log, false);
args.put("guestIds", consumer.getGuestIds());
return jsRules.runJsFunction(Boolean.class, "is_ent_compliant", args);
}
Aggregations