Search in sources :

Example 41 with ProductDTO

use of org.candlepin.dto.api.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 42 with ProductDTO

use of org.candlepin.dto.api.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 43 with ProductDTO

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

the class ProductManager method isChangedBy.

/**
 * Determines whether or not this entity would be changed if the given DTO were applied to this
 * object.
 *
 * @param dto
 *  The product DTO to check for changes
 *
 * @throws IllegalArgumentException
 *  if dto is null
 *
 * @return
 *  true if this product would be changed by the given DTO; false otherwise
 */
public static boolean isChangedBy(Product entity, ProductDTO dto) {
    // Check simple properties first
    if (dto.getId() != null && !dto.getId().equals(entity.getId())) {
        return true;
    }
    if (dto.getName() != null && !dto.getName().equals(entity.getName())) {
        return true;
    }
    if (dto.getMultiplier() != null && !dto.getMultiplier().equals(entity.getMultiplier())) {
        return true;
    }
    if (dto.isLocked() != null && !dto.isLocked().equals(entity.isLocked())) {
        return true;
    }
    Collection<String> dependentProductIds = dto.getDependentProductIds();
    if (dependentProductIds != null && !Util.collectionsAreEqual(entity.getDependentProductIds(), dependentProductIds)) {
        return true;
    }
    // Impl note:
    // Depending on how strict we are regarding case-sensitivity and value-representation,
    // this may get us in to trouble. We may need to iterate through the attributes, performing
    // case-insensitive key/value comparison and similiarities (i.e. management_enabled: 1 is
    // functionally identical to Management_Enabled: true, but it will be detected as a change
    // in attributes.
    Map<String, String> attributes = dto.getAttributes();
    if (attributes != null && !attributes.equals(entity.getAttributes())) {
        return true;
    }
    Collection<ProductContentDTO> productContent = dto.getProductContent();
    if (productContent != null) {
        Comparator comparator = new Comparator() {

            public int compare(Object lhs, Object rhs) {
                ProductContent existing = (ProductContent) lhs;
                ProductContentDTO update = (ProductContentDTO) rhs;
                if (existing != null && update != null) {
                    Content content = existing.getContent();
                    ContentDTO cdto = update.getContent();
                    if (content != null && cdto != null) {
                        if (cdto.getUuid() != null ? cdto.getUuid().equals(content.getUuid()) : (cdto.getId() != null && cdto.getId().equals(content.getId()))) {
                            return (update.isEnabled() != null && !update.isEnabled().equals(existing.isEnabled())) || ContentManager.isChangedBy(content, cdto) ? 1 : 0;
                        }
                    }
                }
                return 1;
            }
        };
        if (!Util.collectionsAreEqual((Collection) entity.getProductContent(), (Collection) productContent, comparator)) {
            return true;
        }
    }
    return false;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) Collection(java.util.Collection) ProductContent(org.candlepin.model.ProductContent) Comparator(java.util.Comparator)

Example 44 with ProductDTO

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

the class ProductTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ProductDTO populate(ModelTranslator modelTranslator, Product source, ProductDTO destination) {
    destination = super.populate(modelTranslator, source, destination);
    destination.setUuid(source.getUuid());
    destination.setId(source.getId());
    destination.setName(source.getName());
    destination.setMultiplier(source.getMultiplier());
    destination.setHref(source.getHref());
    destination.setLocked(source.isLocked());
    destination.setAttributes(source.getAttributes());
    destination.setDependentProductIds(source.getDependentProductIds());
    if (modelTranslator != null) {
        Collection<ProductContent> productContent = source.getProductContent();
        destination.setProductContent(Collections.<ProductContentDTO>emptyList());
        if (productContent != null) {
            ObjectTranslator<Content, ContentDTO> contentTranslator = modelTranslator.findTranslatorByClass(Content.class, ContentDTO.class);
            for (ProductContent pc : productContent) {
                if (pc != null) {
                    ContentDTO dto = contentTranslator.translate(modelTranslator, pc.getContent());
                    if (dto != null) {
                        destination.addContent(dto, pc.isEnabled());
                    }
                }
            }
        }
    } else {
        destination.setProductContent(Collections.<ProductContentDTO>emptyList());
    }
    return destination;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContent(org.candlepin.model.ProductContent)

Example 45 with ProductDTO

use of org.candlepin.dto.api.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)

Aggregations

ProductDTO (org.candlepin.dto.api.v1.ProductDTO)29 Product (org.candlepin.model.Product)28 Test (org.junit.Test)27 Owner (org.candlepin.model.Owner)25 Content (org.candlepin.model.Content)15 ProductContent (org.candlepin.model.ProductContent)13 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)12 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)11 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)7 Parameters (junitparams.Parameters)6 Transactional (com.google.inject.persist.Transactional)5 Reader (java.io.Reader)5 LinkedList (java.util.LinkedList)5 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)5 StringReader (java.io.StringReader)4 HashSet (java.util.HashSet)4 Subscription (org.candlepin.model.dto.Subscription)4 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3