Search in sources :

Example 41 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class PreEntitlementRulesCheckOp method preProcess.

/**
 * The java script portion of the rules can be run before the locks have been placed.
 * if there is a failure, we stop the chain here.
 * @param context
 */
@Override
public boolean preProcess(BindContext context) {
    /* Whether or not we run pre ent rules check, this first call
         * is used to fetch the pools and enrich some context fields.
         */
    Map<String, PoolQuantity> poolQuantityMap = context.getPoolQuantities();
    if (context.isQuantityRequested()) {
        log.debug("Running pre-entitlement rules.");
        // XXX preEntitlement is run twice for new entitlement creation
        results = enforcer.preEntitlement(context.getConsumer(), poolQuantityMap.values(), callerType);
        EntitlementRefusedException exception = checkResults();
        if (exception != null) {
            context.setException(exception, Thread.currentThread().getStackTrace());
            return false;
        }
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException)

Example 42 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class PreEntitlementRulesCheckOp method execute.

/**
 * The pool's quantity might have changed since we last fetched it,
 * so ensure that the pool still has enough quantity left.
 * @param context
 */
@Override
public boolean execute(BindContext context) {
    if (context.isQuantityRequested()) {
        for (PoolQuantity poolQuantity : context.getPoolQuantities().values()) {
            Pool pool = poolQuantity.getPool();
            enforcer.finishValidation(results.get(pool.getId()), pool, context.getPoolQuantities().get(pool.getId()).getQuantity());
        }
        EntitlementRefusedException exception = checkResults();
        if (exception != null) {
            context.setException(exception, Thread.currentThread().getStackTrace());
            return false;
        }
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) Pool(org.candlepin.model.Pool)

Example 43 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class BindContext method getPoolQuantities.

public Map<String, PoolQuantity> getPoolQuantities() {
    if (poolQuantities == null) {
        poolQuantities = new HashMap<>();
        for (Pool pool : poolCurator.listAllByIds(quantities.keySet())) {
            Integer quantity = quantities.get(pool.getId());
            if (quantity > 0) {
                quantityRequested = true;
            }
            poolQuantities.put(pool.getId(), new PoolQuantity(pool, quantity));
            quantities.remove(pool.getId());
        }
        if (!quantities.isEmpty()) {
            throw new IllegalArgumentException(i18n.tr("Subscription pool(s) {0} do not exist.", quantities.keySet()));
        }
    }
    return poolQuantities;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Pool(org.candlepin.model.Pool)

Example 44 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class BindContext method getEntitlementMap.

public Map<String, Entitlement> getEntitlementMap() {
    if (entitlementMap == null) {
        entitlementMap = new HashMap<>();
        for (PoolQuantity poolQuantity : poolQuantities.values()) {
            Pool pool = poolQuantity.getPool();
            Entitlement ent = new Entitlement(consumer, getOwner(), poolQuantity.getQuantity());
            // we manually generate ids as they are needed before the entitlement
            // can be persisted.
            ent.setId(Util.generateDbUUID());
            ent.setUpdatedOnStart(pool.getStartDate().after(new Date()));
            entitlementMap.put(pool.getId(), ent);
        }
    }
    return entitlementMap;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date)

Example 45 with PoolQuantity

use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.

the class StubEntitlementCertServiceAdapter method generateEntitlementCert.

@Override
public EntitlementCertificate generateEntitlementCert(Entitlement entitlement, Product product) throws GeneralSecurityException, IOException {
    Map<String, Entitlement> ents = new HashMap<>();
    Map<String, PoolQuantity> poolQuantityMap = new HashMap<>();
    Map<String, Product> productMap = new HashMap<>();
    Pool pool = entitlement.getPool();
    ents.put(pool.getId(), entitlement);
    poolQuantityMap.put(pool.getId(), new PoolQuantity(pool, entitlement.getQuantity()));
    productMap.put(pool.getId(), product);
    return generateEntitlementCerts(entitlement.getConsumer(), poolQuantityMap, ents, productMap, true).get(pool.getId());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement)

Aggregations

PoolQuantity (org.candlepin.model.PoolQuantity)75 Pool (org.candlepin.model.Pool)65 Test (org.junit.Test)52 Entitlement (org.candlepin.model.Entitlement)34 LinkedList (java.util.LinkedList)30 Product (org.candlepin.model.Product)28 HashMap (java.util.HashMap)27 ArrayList (java.util.ArrayList)21 Consumer (org.candlepin.model.Consumer)12 List (java.util.List)11 ConsumerType (org.candlepin.model.ConsumerType)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Date (java.util.Date)8 Set (java.util.Set)8 HashSet (java.util.HashSet)7 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)7 ValidationResult (org.candlepin.policy.ValidationResult)7 Subscription (org.candlepin.model.dto.Subscription)6 Matchers.anyLong (org.mockito.Matchers.anyLong)6 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)5