Search in sources :

Example 1 with EntitlementRulesTranslator

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);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) PoolIdAndErrors(org.candlepin.model.dto.PoolIdAndErrors) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) SchedulerException(org.quartz.SchedulerException) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) ArrayList(java.util.ArrayList) List(java.util.List) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) Entitlement(org.candlepin.model.Entitlement) EntitlementRulesTranslator(org.candlepin.policy.js.entitlement.EntitlementRulesTranslator) HashMap(java.util.HashMap) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap)

Example 2 with EntitlementRulesTranslator

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);
}
Also used : EntitlementRulesTranslator(org.candlepin.policy.js.entitlement.EntitlementRulesTranslator) Before(org.junit.Before)

Aggregations

EntitlementRulesTranslator (org.candlepin.policy.js.entitlement.EntitlementRulesTranslator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entitlement (org.candlepin.model.Entitlement)1 Pool (org.candlepin.model.Pool)1 PoolIdAndErrors (org.candlepin.model.dto.PoolIdAndErrors)1 PoolIdAndQuantity (org.candlepin.model.dto.PoolIdAndQuantity)1 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)1 ValidationError (org.candlepin.policy.ValidationError)1 Before (org.junit.Before)1 JobDataMap (org.quartz.JobDataMap)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerException (org.quartz.SchedulerException)1