Search in sources :

Example 1 with Product

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

the class Exporter method exportProducts.

private void exportProducts(File baseDir, Consumer consumer) throws IOException {
    File productDir = new File(baseDir.getCanonicalPath(), "products");
    productDir.mkdir();
    Map<String, Product> products = new HashMap<>();
    for (Entitlement entitlement : consumer.getEntitlements()) {
        Pool pool = entitlement.getPool();
        for (Product providedProduct : productCurator.getPoolProvidedProductsCached(pool)) {
            products.put(providedProduct.getId(), providedProduct);
        }
        // Don't forget the 'main' product!
        Product product = pool.getProduct();
        products.put(product.getId(), product);
        // Also need to check for sub products
        Product derivedProduct = pool.getDerivedProduct();
        if (derivedProduct != null) {
            products.put(derivedProduct.getId(), derivedProduct);
        }
        for (Product derivedProvidedProduct : productCurator.getPoolDerivedProvidedProductsCached(pool)) {
            products.put(derivedProvidedProduct.getId(), derivedProvidedProduct);
        }
    }
    for (Product product : products.values()) {
        // Clear the owner and UUID so they can be re-generated/assigned on import
        // product.setUuid(null);
        // product.setOwner(null);
        String path = productDir.getCanonicalPath();
        String productId = product.getId();
        File file = new File(path, productId + ".json");
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            productExporter.export(mapper, writer, product);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
        // Real products have a numeric id.
        if (StringUtils.isNumeric(product.getId())) {
            Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
            ProductCertificate cert = productAdapter.getProductCertificate(owner, product.getId());
            // XXX: need to decide if the cert should always be in the export, or never.
            if (cert != null) {
                file = new File(productDir.getCanonicalPath(), product.getId() + ".pem");
                writer = new FileWriter(file);
                productCertExporter.export(writer, cert);
                writer.close();
            }
        }
    }
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ProductCertificate(org.candlepin.model.ProductCertificate) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) File(java.io.File)

Example 2 with Product

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

the class ResolverUtil method validateProductData.

public void validateProductData(ProductData dto, Owner owner, boolean allowNull) {
    if (dto != null) {
        if (dto.getUuid() != null) {
            // UUID is set. Verify that product exists and matches the ID provided, if any
            Product product = this.productCurator.find(dto.getUuid());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the UUID \"{0}\"", dto.getUuid()));
            }
            dto.setId(product.getId());
        } else if (dto.getId() != null) {
            Product product = this.ownerProductCurator.getProductById(owner, dto.getId());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the ID \"{0}\" for owner \"{1}\"", dto.getId(), owner.getKey()));
            }
        } else {
            throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
        }
    } else if (!allowNull) {
        throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
    }
}
Also used : ProvidedProduct(org.candlepin.model.ProvidedProduct) Product(org.candlepin.model.Product) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 3 with Product

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

the class ResolverUtil method resolvePool.

public Pool resolvePool(Pool pool) {
    // doesn't (i.e. during creation). We just need to make sure it's not null.
    if (pool == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(pool.getOwner());
    pool.setOwner(owner);
    // Ensure the specified product(s) exists for the given owner
    pool.setProduct(this.resolveProduct(owner, pool.getProduct()));
    if (pool.getDerivedProduct() != null) {
        pool.setDerivedProduct(this.resolveProduct(owner, pool.getDerivedProduct()));
    }
    HashSet<Product> presolved = new HashSet<>();
    pool.populateAllTransientProvidedProducts(productCurator);
    for (ProvidedProduct product : pool.getProvidedProductDtos()) {
        // TODO: Maybe add UUID resolution as well?
        presolved.add(resolveProduct(owner, product.getProductId()));
    }
    pool.setProvidedProducts(presolved);
    presolved.clear();
    for (ProvidedProduct product : pool.getDerivedProvidedProductDtos()) {
        presolved.add(this.resolveProduct(owner, product.getProductId()));
    }
    pool.setDerivedProvidedProducts(presolved);
    return pool;
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ProvidedProduct(org.candlepin.model.ProvidedProduct) Product(org.candlepin.model.Product) ProvidedProduct(org.candlepin.model.ProvidedProduct) HashSet(java.util.HashSet)

Example 4 with Product

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

the class DefaultContentAccessCertServiceAdapter method createContentAccessDataPayload.

private byte[] createContentAccessDataPayload(Owner owner, Environment environment) throws IOException {
    // fake a product dto as a container for the org content
    Set<Product> containerSet = new HashSet<>();
    CandlepinQuery<Content> ownerContent = ownerContentCurator.getContentByOwner(owner);
    Set<String> entitledProductIds = new HashSet<>();
    List<org.candlepin.model.dto.Product> productModels = new ArrayList<>();
    Map<String, EnvironmentContent> promotedContent = getPromotedContent(environment);
    String contentPrefix = getContentPrefix(owner, environment);
    Product container = new Product();
    Entitlement emptyEnt = new Entitlement();
    Pool emptyPool = new Pool();
    Product skuProduct = new Product();
    Consumer emptyConsumer = new Consumer();
    containerSet.add(container);
    container.setId("content_access");
    container.setName(" Content Access");
    for (Content c : ownerContent) {
        container.addContent(c, false);
    }
    emptyConsumer.setEnvironment(environment);
    emptyEnt.setPool(emptyPool);
    emptyEnt.setConsumer(emptyConsumer);
    emptyPool.setProduct(skuProduct);
    emptyPool.setStartDate(new Date());
    emptyPool.setEndDate(new Date());
    skuProduct.setName("Content Access");
    skuProduct.setId("content_access");
    entitledProductIds.add("content-access");
    org.candlepin.model.dto.Product productModel = v3extensionUtil.mapProduct(container, skuProduct, contentPrefix, promotedContent, emptyConsumer, emptyPool, entitledProductIds);
    productModels.add(productModel);
    return v3extensionUtil.createEntitlementDataPayload(productModels, emptyConsumer, emptyPool, null);
}
Also used : ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) EnvironmentContent(org.candlepin.model.EnvironmentContent) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet)

Example 5 with Product

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

the class HandleCertificatesOp method preProcess.

/**
 * create certificates without associating the entitlements and certs with each other.
 * also, dont persist the certs.
 * @param context
 */
@Override
public boolean preProcess(BindContext context) {
    if (!context.getConsumerType().isType(ConsumerTypeEnum.SHARE)) {
        List<String> poolIds = new LinkedList<>();
        Map<String, Product> products = new HashMap<>();
        Map<String, PoolQuantity> poolQuantities = context.getPoolQuantities();
        for (PoolQuantity poolQuantity : poolQuantities.values()) {
            Pool pool = poolQuantity.getPool();
            products.put(pool.getId(), pool.getProduct());
            poolIds.add(pool.getId());
        }
        certs = ecGenerator.generateEntitlementCertificates(context.getConsumer(), products, poolQuantities, context.getEntitlementMap(), false);
        modifyingEnts = this.eCurator.getDependentEntitlementIdsForPools(context.getConsumer(), poolIds);
    }
    return true;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) LinkedList(java.util.LinkedList)

Aggregations

Product (org.candlepin.model.Product)407 Test (org.junit.Test)281 Pool (org.candlepin.model.Pool)216 Owner (org.candlepin.model.Owner)153 Consumer (org.candlepin.model.Consumer)112 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)108 HashSet (java.util.HashSet)84 Date (java.util.Date)74 ArrayList (java.util.ArrayList)69 Entitlement (org.candlepin.model.Entitlement)67 LinkedList (java.util.LinkedList)66 HashMap (java.util.HashMap)65 Subscription (org.candlepin.model.dto.Subscription)47 Content (org.candlepin.model.Content)40 ValidationResult (org.candlepin.policy.ValidationResult)38 SourceSubscription (org.candlepin.model.SourceSubscription)36 Matchers.anyString (org.mockito.Matchers.anyString)31 List (java.util.List)29 PoolQuantity (org.candlepin.model.PoolQuantity)29 DateRange (org.candlepin.policy.js.compliance.DateRange)27