use of org.candlepin.policy.js.entitlement.EntitlementRulesTranslator in project candlepin by candlepin.
the class EntitlerJob method toExecute.
@Override
public void toExecute(JobExecutionContext ctx) throws JobExecutionException {
try {
JobDataMap map = ctx.getMergedJobDataMap();
String uuid = (String) map.get(JobStatus.TARGET_ID);
PoolIdAndQuantity[] poolQuantities = (PoolIdAndQuantity[]) map.get("pool_and_quantities");
Map<String, Integer> poolMap = new HashMap<>();
for (PoolIdAndQuantity poolIdAndQuantity : poolQuantities) {
poolMap.put(poolIdAndQuantity.getPoolId(), poolIdAndQuantity.getQuantity());
}
List<Entitlement> ents = entitler.bindByPoolQuantities(uuid, poolMap);
entitler.sendEvents(ents);
PoolIdAndQuantity[] consumed = new PoolIdAndQuantity[ents.size()];
for (int i = 0; i < ents.size(); i++) {
consumed[i] = new PoolIdAndQuantity(ents.get(i).getPool().getId(), ents.get(i).getQuantity());
}
ctx.setResult(Arrays.asList(consumed));
poolCurator.clear();
} catch (EntitlementRefusedException e) {
log.error("EntitlerJob encountered a problem, translating errors", e);
Map<String, ValidationResult> validationResults = e.getResults();
EntitlementRulesTranslator translator = new EntitlementRulesTranslator(i18n);
List<PoolIdAndErrors> poolErrors = new ArrayList<>();
for (Pool pool : poolCurator.listAllByIds(validationResults.keySet())) {
List<String> errorMessages = new ArrayList<>();
for (ValidationError error : validationResults.get(pool.getId()).getErrors()) {
errorMessages.add(translator.poolErrorToMessage(pool, error));
}
poolErrors.add(new PoolIdAndErrors(pool.getId(), errorMessages));
}
ctx.setResult(poolErrors);
}// so that the job will be properly cleaned up on failure.
catch (Exception e) {
log.error("EntitlerJob encountered a problem.", e);
throw new JobExecutionException(e.getMessage(), e, false);
}
}
use of org.candlepin.policy.js.entitlement.EntitlementRulesTranslator in project candlepin by candlepin.
the class EntitlerTest method init.
@Before
public void init() {
when(consumer.getOwnerId()).thenReturn("admin");
when(ownerCurator.findOwnerById(eq("admin"))).thenReturn(owner);
i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.READ_PROPERTIES | I18nFactory.FALLBACK);
translator = new EntitlementRulesTranslator(i18n);
entitler = new Entitler(pm, cc, i18n, ef, sink, translator, entitlementCurator, config, ownerProductCurator, ownerCurator, poolCurator, productCurator, productManager, productAdapter, contentManager);
}
Aggregations