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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations