Search in sources :

Example 36 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class ProductDTOTranslator method populate.

/**
 * {@inheritDoc}
 *
 * <p>Note: the 'locked' field is always set to false and the 'href' field is not translated,
 * since these fields are not currently being exported.</p>
 */
@Override
public ProductData populate(ModelTranslator modelTranslator, ProductDTO source, ProductData dest) {
    if (source == null) {
        throw new IllegalArgumentException("source is null");
    }
    if (dest == null) {
        throw new IllegalArgumentException("dest is null");
    }
    dest.setCreated(source.getCreated());
    dest.setUpdated(source.getUpdated());
    dest.setUuid(source.getUuid());
    dest.setId(source.getId());
    dest.setName(source.getName());
    dest.setMultiplier(source.getMultiplier());
    dest.setAttributes(source.getAttributes());
    dest.setDependentProductIds(source.getDependentProductIds());
    // We manually set this to false since it is not included in the exported data.
    dest.setLocked(false);
    Collection<ProductDTO.ProductContentDTO> productContentDTOs = source.getProductContent();
    dest.setProductContent(Collections.emptyList());
    if (modelTranslator != null && productContentDTOs != null) {
        ObjectTranslator<ContentDTO, ContentData> contentDTOTranslator = modelTranslator.findTranslatorByClass(ContentDTO.class, ContentData.class);
        for (ProductDTO.ProductContentDTO pcdto : productContentDTOs) {
            if (pcdto != null && pcdto.getContent() != null) {
                ContentData contentData = contentDTOTranslator.translate(modelTranslator, pcdto.getContent());
                dest.addContent(contentData, pcdto.isEnabled());
            }
        }
    }
    return dest;
}
Also used : ContentDTO(org.candlepin.dto.manifest.v1.ContentDTO) ContentData(org.candlepin.model.dto.ContentData) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO)

Example 37 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class EntitlementImporter method importObject.

public Subscription importObject(ObjectMapper mapper, Reader reader, Owner owner, Map<String, ProductDTO> productsById, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
    EntitlementDTO entitlementDTO = mapper.readValue(reader, EntitlementDTO.class);
    Entitlement entitlement = new Entitlement();
    populateEntity(entitlement, entitlementDTO);
    Subscription subscription = new Subscription();
    log.debug("Building subscription for owner: {}", owner);
    log.debug("Using pool from entitlement: {}", entitlement.getPool());
    // Now that we no longer store Subscriptions in the on-site database, we need to
    // manually give the subscription a downstream ID. Note that this may later be
    // overwritten by reconciliation code if it determines this Subscription
    // should replace and existing one.
    subscription.setId(Util.generateDbUUID());
    subscription.setUpstreamPoolId(entitlement.getPool().getId());
    subscription.setUpstreamEntitlementId(entitlement.getId());
    subscription.setUpstreamConsumerId(consumerUuid);
    subscription.setOwner(owner);
    subscription.setStartDate(entitlement.getStartDate());
    subscription.setEndDate(entitlement.getEndDate());
    subscription.setAccountNumber(entitlement.getPool().getAccountNumber());
    subscription.setContractNumber(entitlement.getPool().getContractNumber());
    subscription.setOrderNumber(entitlement.getPool().getOrderNumber());
    subscription.setQuantity(entitlement.getQuantity().longValue());
    for (Branding b : entitlement.getPool().getBranding()) {
        subscription.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
    }
    String cdnLabel = meta.getCdnLabel();
    if (!StringUtils.isBlank(cdnLabel)) {
        Cdn cdn = cdnCurator.lookupByLabel(cdnLabel);
        if (cdn != null) {
            subscription.setCdn(cdn);
        }
    }
    ProductDTO productDTO = this.findProduct(productsById, entitlement.getPool().getProductId());
    subscription.setProduct(this.translator.translate(productDTO, ProductData.class));
    // Add any sub product data to the subscription.
    if (entitlement.getPool().getDerivedProductId() != null) {
        productDTO = this.findProduct(productsById, entitlement.getPool().getDerivedProductId());
        subscription.setDerivedProduct(this.translator.translate(productDTO, ProductData.class));
    }
    associateProvidedProducts(productsById, entitlement, subscription);
    Set<EntitlementCertificate> certs = entitlement.getCertificates();
    // subscriptions have one cert
    int entcnt = 0;
    for (EntitlementCertificate cert : certs) {
        ++entcnt;
        CertificateSerial cs = new CertificateSerial();
        cs.setCollected(cert.getSerial().isCollected());
        cs.setExpiration(cert.getSerial().getExpiration());
        cs.setUpdated(cert.getSerial().getUpdated());
        cs.setCreated(cert.getSerial().getCreated());
        SubscriptionsCertificate sc = new SubscriptionsCertificate();
        sc.setKey(cert.getKey());
        sc.setCertAsBytes(cert.getCertAsBytes());
        sc.setSerial(cs);
        subscription.setCertificate(sc);
    }
    if (entcnt > 1) {
        log.error("More than one entitlement cert found for subscription");
    }
    return subscription;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) EntitlementCertificate(org.candlepin.model.EntitlementCertificate) CertificateSerial(org.candlepin.model.CertificateSerial) Branding(org.candlepin.model.Branding) Cdn(org.candlepin.model.Cdn) EntitlementDTO(org.candlepin.dto.manifest.v1.EntitlementDTO) SubscriptionsCertificate(org.candlepin.model.SubscriptionsCertificate) Entitlement(org.candlepin.model.Entitlement) Subscription(org.candlepin.model.dto.Subscription) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO)

Example 38 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class OwnerProductResource method removeBatchContent.

@ApiOperation(notes = "Adds one or more Content entities to a Product", value = "addBatchContent")
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{product_id}/batch_content")
@Transactional
public ProductDTO removeBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, @ApiParam(name = "content", required = true) List<String> contentIds) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Product product = this.fetchProduct(owner, productId);
    if (product.isLocked()) {
        throw new ForbiddenException(i18n.tr("product \"{0}\" is locked", product.getId()));
    }
    this.productCurator.lock(product);
    ProductDTO pdto = this.translator.translate(product, ProductDTO.class);
    // Impl note:
    // This is a wholely inefficient way of doing this. When we return to using ID-based linking
    // and we're not linking the universe with our model, we can just attach the IDs directly
    // without needing all this DTO conversion back and forth.
    // Alternatively, we can shut off Hibernate's auto-commit junk and get in the habit of
    // calling commit methods as necessary so we don't have to work with DTOs internally.
    boolean changed = false;
    for (String contentId : contentIds) {
        changed |= pdto.removeContent(contentId);
    }
    if (changed) {
        product = this.productManager.updateProduct(pdto, owner, true);
    }
    return this.translator.translate(product, ProductDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) Transactional(com.google.inject.persist.Transactional)

Example 39 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class OwnerProductResourceTest method testCreateProductWithContent.

@Test
public void testCreateProductWithContent() {
    Owner owner = this.createOwner("Example-Corporation");
    Content content = this.createContent("content-1", "content-1", owner);
    ProductDTO pdto = this.buildTestProductDTO();
    ContentDTO cdto = this.modelTranslator.translate(content, ContentDTO.class);
    pdto.addContent(cdto, true);
    assertNull(this.ownerProductCurator.getProductById(owner.getKey(), pdto.getId()));
    ProductDTO result = this.ownerProductResource.createProduct(owner.getKey(), pdto);
    Product entity = this.ownerProductCurator.getProductById(owner, pdto.getId());
    ProductDTO expected = this.modelTranslator.translate(entity, ProductDTO.class);
    assertNotNull(result);
    assertNotNull(entity);
    assertEquals(expected, result);
    assertNotNull(result.getProductContent());
    assertEquals(1, result.getProductContent().size());
    assertEquals(cdto, result.getProductContent().iterator().next().getContent());
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Example 40 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class OwnerProductResourceTest method testCreateProductResource.

@Test
public void testCreateProductResource() {
    Owner owner = this.createOwner("Example-Corporation");
    ProductDTO pdto = this.buildTestProductDTO();
    assertNull(this.ownerProductCurator.getProductById(owner.getKey(), pdto.getId()));
    ProductDTO result = this.ownerProductResource.createProduct(owner.getKey(), pdto);
    Product entity = this.ownerProductCurator.getProductById(owner, pdto.getId());
    ProductDTO expected = this.modelTranslator.translate(entity, ProductDTO.class);
    assertNotNull(result);
    assertNotNull(entity);
    assertEquals(expected, result);
}
Also used : Owner(org.candlepin.model.Owner) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Aggregations

ProductDTO (org.candlepin.dto.api.v1.ProductDTO)29 Product (org.candlepin.model.Product)27 Test (org.junit.Test)27 Owner (org.candlepin.model.Owner)25 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)12 Content (org.candlepin.model.Content)10 ProductContent (org.candlepin.model.ProductContent)9 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)7 Parameters (junitparams.Parameters)6 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)6 Transactional (com.google.inject.persist.Transactional)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 Subscription (org.candlepin.model.dto.Subscription)4 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)3