Search in sources :

Example 6 with ProductContentDTO

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

the class TestUtil method createProduct.

public static Product createProduct(ProductDTO dto) {
    Product product = null;
    if (dto != null) {
        product = new Product(dto.getId(), dto.getName());
        product.setUuid(dto.getUuid());
        product.setMultiplier(dto.getMultiplier());
        product.setAttributes(dto.getAttributes());
        if (dto.getProductContent() != null) {
            for (ProductContentDTO pcd : dto.getProductContent()) {
                if (pcd != null) {
                    Content content = createContent((ContentDTO) pcd.getContent());
                    if (content != null) {
                        product.addContent(content, pcd.isEnabled() != null ? pcd.isEnabled() : true);
                    }
                }
            }
        }
        product.setDependentProductIds(dto.getDependentProductIds());
        product.setLocked(dto.isLocked() != null ? dto.isLocked() : false);
    }
    return product;
}
Also used : Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)

Example 7 with ProductContentDTO

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

the class ProductTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(Product source, ProductDTO dto, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getUuid(), dto.getUuid());
        assertEquals(source.getId(), dto.getId());
        assertEquals(source.getName(), dto.getName());
        assertEquals(source.getMultiplier(), dto.getMultiplier());
        assertEquals(source.getAttributes(), dto.getAttributes());
        assertEquals(source.getDependentProductIds(), dto.getDependentProductIds());
        assertEquals(source.isLocked(), dto.isLocked());
        assertEquals(source.getHref(), dto.getHref());
        assertNotNull(dto.getProductContent());
        if (childrenGenerated) {
            for (ProductContent pc : source.getProductContent()) {
                for (ProductContentDTO pcdto : dto.getProductContent()) {
                    Content content = pc.getContent();
                    ContentDTO cdto = pcdto.getContent();
                    assertNotNull(cdto);
                    assertNotNull(cdto.getUuid());
                    if (cdto.getUuid().equals(content.getUuid())) {
                        assertEquals(pc.isEnabled(), pcdto.isEnabled());
                        // Pass the content off to the ContentTranslatorTest to verify it
                        this.contentTranslatorTest.verifyOutput(content, cdto, childrenGenerated);
                    }
                }
            }
        } else {
            assertTrue(dto.getProductContent().isEmpty());
        }
    } else {
        assertNull(dto);
    }
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent)

Example 8 with ProductContentDTO

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

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

Aggregations

ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)7 Content (org.candlepin.model.Content)6 ProductContent (org.candlepin.model.ProductContent)5 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)4 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)2 ProductContentDTO (org.candlepin.dto.manifest.v1.ProductDTO.ProductContentDTO)2 Product (org.candlepin.model.Product)2 ContentData (org.candlepin.model.dto.ContentData)2 ProductContentData (org.candlepin.model.dto.ProductContentData)2 Transactional (com.google.inject.persist.Transactional)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)1 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)1 OwnerContent (org.candlepin.model.OwnerContent)1