use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class X509V3ExtensionUtil method createContent.
/*
* createContent
*
* productArchList is a list of arch strings parse from
* product attributes.
*/
public List<Content> createContent(Set<ProductContent> productContent, Product sku, String contentPrefix, Map<String, EnvironmentContent> promotedContent, Consumer consumer, Product product) {
List<Content> toReturn = new ArrayList<>();
boolean enableEnvironmentFiltering = config.getBoolean(ConfigProperties.ENV_CONTENT_FILTERING);
// Return only the contents that are arch appropriate
Set<ProductContent> archApproriateProductContent = filterContentByContentArch(productContent, consumer, product);
List<String> skuDisabled = sku.getSkuDisabledContentIds();
List<String> skuEnabled = sku.getSkuEnabledContentIds();
for (ProductContent pc : archApproriateProductContent) {
Content content = new Content();
if (enableEnvironmentFiltering && consumer.getEnvironmentId() != null && !promotedContent.containsKey(pc.getContent().getId())) {
log.debug("Skipping content not promoted to environment: {}", pc.getContent());
continue;
}
// Augment the content path with the prefix if it is passed in
String contentPath = this.createFullContentPath(contentPrefix, pc);
content.setId(pc.getContent().getId());
content.setType(pc.getContent().getType());
content.setName(pc.getContent().getName());
content.setLabel(pc.getContent().getLabel());
content.setVendor(pc.getContent().getVendor());
content.setPath(contentPath);
content.setGpgUrl(pc.getContent().getGpgUrl());
// Set content model's arches here, inheriting from the product if
// they are not set on the content.
List<String> archesList = new ArrayList<>();
Set<String> contentArches = Arch.parseArches(pc.getContent().getArches());
if (contentArches.isEmpty()) {
archesList.addAll(Arch.parseArches(product.getAttributeValue(Product.Attributes.ARCHITECTURE)));
} else {
archesList.addAll(Arch.parseArches(pc.getContent().getArches()));
}
content.setArches(archesList);
Boolean enabled = pc.isEnabled();
// sku level content enable override. if on both lists, active wins.
if (skuDisabled.contains(pc.getContent().getId())) {
enabled = false;
}
if (skuEnabled.contains(pc.getContent().getId())) {
enabled = true;
}
// Check if we should override the enabled flag due to setting on promoted content
if (enableEnvironmentFiltering && consumer.getEnvironmentId() != null) {
// we know content has been promoted at this point
Boolean enabledOverride = promotedContent.get(pc.getContent().getId()).getEnabled();
if (enabledOverride != null) {
log.debug("overriding enabled flag: {}", enabledOverride);
enabled = enabledOverride;
}
}
// only included if not the default value of true
if (!enabled) {
content.setEnabled(enabled);
}
// Include metadata expiry if specified on the content
if (pc.getContent().getMetadataExpire() != null) {
content.setMetadataExpire(pc.getContent().getMetadataExpire());
}
// Include required tags if specified on the content set
String requiredTags = pc.getContent().getRequiredTags();
if ((requiredTags != null) && !requiredTags.equals("")) {
StringTokenizer st = new StringTokenizer(requiredTags, ",");
List<String> tagList = new ArrayList<>();
while (st.hasMoreElements()) {
tagList.add((String) st.nextElement());
}
content.setRequiredTags(tagList);
}
toReturn.add(content);
}
return toReturn;
}
use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class DefaultEntitlementCertServiceAdapter method prepareV1Extensions.
public Set<X509ExtensionWrapper> prepareV1Extensions(Set<Product> products, Pool pool, Consumer consumer, Integer quantity, String contentPrefix, Map<String, EnvironmentContent> promotedContent) {
Set<X509ExtensionWrapper> result = new LinkedHashSet<>();
Set<String> entitledProductIds = entCurator.listEntitledProductIds(consumer, pool);
int contentCounter = 0;
boolean enableEnvironmentFiltering = config.getBoolean(ConfigProperties.ENV_CONTENT_FILTERING);
Product skuProd = pool.getProduct();
for (Product prod : Collections2.filter(products, X509Util.PROD_FILTER_PREDICATE)) {
log.debug("Adding X509 extensions for product: {}", prod);
result.addAll(extensionUtil.productExtensions(prod));
Set<ProductContent> filteredContent = extensionUtil.filterProductContent(prod, consumer, promotedContent, enableEnvironmentFiltering, entitledProductIds);
filteredContent = extensionUtil.filterContentByContentArch(filteredContent, consumer, prod);
// Keep track of the number of content sets that are being added.
contentCounter += filteredContent.size();
log.debug("Adding X509 extensions for content: {}", filteredContent);
result.addAll(extensionUtil.contentExtensions(filteredContent, contentPrefix, promotedContent, consumer, skuProd));
}
// informative error message to the user.
if (contentCounter > X509ExtensionUtil.V1_CONTENT_LIMIT) {
String cause = i18n.tr("Too many content sets for certificate {0}. A newer " + "client may be available to address this problem. " + "See knowledge database https://access.redhat.com/knowledge/node/129003 for more " + "information.", pool.getProductName());
throw new CertificateSizeException(cause);
}
result.addAll(extensionUtil.subscriptionExtensions(pool));
result.addAll(extensionUtil.entitlementExtensions(quantity));
result.addAll(extensionUtil.consumerExtensions(consumer));
if (log.isDebugEnabled()) {
for (X509ExtensionWrapper eWrapper : result) {
log.debug("Extension {} with value {}", eWrapper.getOid(), eWrapper.getValue());
}
}
return result;
}
use of org.candlepin.model.ProductContent 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.ProductContent 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;
}
use of org.candlepin.model.ProductContent 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;
}
Aggregations