Search in sources :

Example 86 with Content

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

the class OwnerContentResource method updateContent.

@ApiOperation(notes = "Updates a Content", value = "updateContent")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{content_id}")
public ContentDTO updateContent(@PathParam("owner_key") String ownerKey, @PathParam("content_id") String contentId, @ApiParam(name = "content", required = true) ContentDTO content) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Content existing = this.fetchContent(owner, contentId);
    if (existing.isLocked()) {
        throw new ForbiddenException(i18n.tr("content \"{0}\" is locked", existing.getId()));
    }
    existing = this.contentManager.updateContent(content, owner, true);
    ownerManager.refreshOwnerForContentAccess(owner);
    return this.translator.translate(existing, ContentDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT)

Example 87 with Content

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

the class OwnerContentResource method createBatchContent.

@ApiOperation(notes = "Creates Contents in bulk", value = "createBatchContent")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/batch")
@Transactional
public Collection<ContentDTO> createBatchContent(@PathParam("owner_key") String ownerKey, @ApiParam(name = "contents", required = true) List<ContentDTO> contents) {
    Collection<ContentDTO> result = new LinkedList<>();
    Owner owner = this.getOwnerByKey(ownerKey);
    for (ContentDTO content : contents) {
        Content entity = this.createContentImpl(owner, content);
        result.add(this.translator.translate(entity, ContentDTO.class));
    }
    ownerManager.refreshOwnerForContentAccess(owner);
    return result;
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) LinkedList(java.util.LinkedList) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) Transactional(com.google.inject.persist.Transactional)

Example 88 with Content

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

the class DefaultContentAccessCertServiceAdapter method createX509Certificate.

public X509Certificate createX509Certificate(Consumer consumer, Owner owner, BigInteger serialNumber, KeyPair keyPair, Date startDate, Date endDate) throws GeneralSecurityException, IOException {
    // fake a product dto as a container for the org content
    org.candlepin.model.dto.Product container = new org.candlepin.model.dto.Product();
    org.candlepin.model.dto.Content dContent = new org.candlepin.model.dto.Content();
    List<org.candlepin.model.dto.Content> dtoContents = new ArrayList<>();
    dtoContents.add(dContent);
    Environment environment = this.environmentCurator.getConsumerEnvironment(consumer);
    dContent.setPath(getContentPrefix(owner, environment));
    container.setContent(dtoContents);
    Set<X509ExtensionWrapper> extensions = prepareV3Extensions();
    Set<X509ByteExtensionWrapper> byteExtensions = prepareV3ByteExtensions(container);
    X509Certificate x509Cert = this.pki.createX509Certificate(createDN(consumer, owner), extensions, byteExtensions, startDate, endDate, keyPair, serialNumber, null);
    return x509Cert;
}
Also used : ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) X509Certificate(java.security.cert.X509Certificate) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Environment(org.candlepin.model.Environment) X509ByteExtensionWrapper(org.candlepin.pki.X509ByteExtensionWrapper) X509ExtensionWrapper(org.candlepin.pki.X509ExtensionWrapper)

Example 89 with Content

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

the class ProductManager method applyProductChanges.

/**
 * Applies the changes from the given DTO to the specified entity
 *
 * @param entity
 *  The entity to modify
 *
 * @param update
 *  The DTO containing the modifications to apply
 *
 * @param content
 *  A mapping of Red Hat content ID to content entities to use for content resolution
 *
 * @throws IllegalArgumentException
 *  if entity, update or owner is null
 *
 * @return
 *  The updated product entity
 */
private Product applyProductChanges(Product entity, ProductData update, Map<String, Content> contentMap) {
    if (entity == null) {
        throw new IllegalArgumentException("entity is null");
    }
    if (update == null) {
        throw new IllegalArgumentException("update is null");
    }
    if (contentMap == null) {
        throw new IllegalArgumentException("contentMap is null");
    }
    if (update.getName() != null) {
        entity.setName(update.getName());
    }
    if (update.getMultiplier() != null) {
        entity.setMultiplier(update.getMultiplier());
    }
    if (update.getAttributes() != null) {
        entity.setAttributes(update.getAttributes());
    }
    if (update.getProductContent() != null) {
        Collection<ProductContent> productContent = new LinkedList<>();
        // Sort the existing ProductContent so we aren't iterating on it several times.
        // TODO: Remove this if/when product content is stored as a map on products.
        Map<String, ProductContent> existingLinks = new HashMap<>();
        for (ProductContent pc : entity.getProductContent()) {
            existingLinks.put(pc.getContent().getId(), pc);
        }
        // Actually process our list of content...
        for (ProductContentData pcd : update.getProductContent()) {
            if (pcd == null) {
                throw new IllegalStateException("Product data contains a null product-content mapping: " + update);
            }
            ContentData cdto = pcd.getContent();
            if (cdto == null || cdto.getId() == null) {
                // adding it to our link object. This is very bad.
                throw new IllegalStateException("Product data contains an incomplete product-content " + "mapping: " + update);
            }
            ProductContent existingLink = existingLinks.get(cdto.getId());
            Content content = contentMap.get(cdto.getId());
            if (content == null) {
                // Content doesn't exist yet -- it should have been created already
                throw new IllegalStateException("product references content which does not exist: " + cdto);
            }
            if (existingLink == null) {
                existingLink = new ProductContent(entity, content, pcd.isEnabled() != null ? pcd.isEnabled() : false);
            } else {
                existingLink.setContent(content);
                if (pcd.isEnabled() != null) {
                    existingLink.setEnabled(pcd.isEnabled());
                }
            }
            productContent.add(existingLink);
        }
        entity.setProductContent(productContent);
    }
    if (update.getDependentProductIds() != null) {
        entity.setDependentProductIds(update.getDependentProductIds());
    }
    if (update.isLocked() != null) {
        entity.setLocked(update.isLocked());
    }
    return entity;
}
Also used : ContentData(org.candlepin.model.dto.ContentData) ProductContentData(org.candlepin.model.dto.ProductContentData) HashMap(java.util.HashMap) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentData(org.candlepin.model.dto.ProductContentData) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList)

Example 90 with Content

use of org.candlepin.model.Content 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, ProductData 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<ProductContentData> productContent = dto.getProductContent();
    if (productContent != null) {
        Comparator comparator = new Comparator() {

            public int compare(Object lhs, Object rhs) {
                ProductContent existing = (ProductContent) lhs;
                ProductContentData update = (ProductContentData) rhs;
                if (existing != null && update != null) {
                    Content content = existing.getContent();
                    ContentData 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 : ContentData(org.candlepin.model.dto.ContentData) ProductContentData(org.candlepin.model.dto.ProductContentData) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) Collection(java.util.Collection) ProductContentData(org.candlepin.model.dto.ProductContentData) ProductContent(org.candlepin.model.ProductContent) Comparator(java.util.Comparator)

Aggregations

Content (org.candlepin.model.Content)97 Test (org.junit.Test)45 ProductContent (org.candlepin.model.ProductContent)41 Product (org.candlepin.model.Product)40 Owner (org.candlepin.model.Owner)39 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)25 HashMap (java.util.HashMap)18 EnvironmentContent (org.candlepin.model.EnvironmentContent)17 HashSet (java.util.HashSet)14 LinkedList (java.util.LinkedList)11 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)10 ArrayList (java.util.ArrayList)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Transactional (com.google.inject.persist.Transactional)8 Produces (javax.ws.rs.Produces)8 Parameters (junitparams.Parameters)8 ApiOperation (io.swagger.annotations.ApiOperation)7 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)7 Path (javax.ws.rs.Path)6 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)6