use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class ProductDataTest method testAddProductContentByEntity.
@Test
public void testAddProductContentByEntity() {
ProductData dto = new ProductData();
Content[] contentEntities = new Content[] { new Content("c1", "content-1", "test_type", "test_label-1", "test_vendor-1"), new Content("c2", "content-2", "test_type", "test_label-2", "test_vendor-2") };
ProductContent pcentity1 = new ProductContent(null, contentEntities[0], true);
ProductContent pcentity2 = new ProductContent(null, contentEntities[1], false);
ProductContent pcentity3 = new ProductContent(null, contentEntities[1], true);
ContentData[] content = new ContentData[] { contentEntities[0].toDTO(), contentEntities[1].toDTO() };
ProductContentData pcdata1 = new ProductContentData(content[0], true);
ProductContentData pcdata2 = new ProductContentData(content[1], false);
ProductContentData pcdata3 = new ProductContentData(content[1], true);
assertNull(dto.getProductContent());
boolean output = dto.addProductContent(pcentity1);
Collection<ProductContentData> output2 = dto.getProductContent();
assertTrue(output);
assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1), output2));
output = dto.addProductContent(pcentity1);
output2 = dto.getProductContent();
assertFalse(output);
assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1), output2));
output = dto.addProductContent(pcentity2);
output2 = dto.getProductContent();
assertTrue(output);
assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1, pcdata2), output2));
output = dto.addProductContent(pcentity3);
output2 = dto.getProductContent();
assertTrue(output);
assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1, pcdata3), output2));
}
use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class ProductContentDataTest method testPopulateWithEntityEnabled.
@Test
public void testPopulateWithEntityEnabled() {
ProductContentData base = new ProductContentData();
ProductContent source = new ProductContent();
source.setEnabled(true);
base.populate(source);
assertNull(base.getContent());
assertTrue(base.isEnabled());
}
use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class ProductContentDataTest method testPopulateWithEntityContent.
@Test
public void testPopulateWithEntityContent() {
Content contentEntity = new Content("id1", "name1", "type1", "label1", "vendor1");
ContentData contentDTO = contentEntity.toDTO();
ProductContentData base = new ProductContentData();
ProductContent source = new ProductContent();
source.setContent(contentEntity);
base.populate(source);
assertEquals(contentDTO, base.getContent());
// This will always be set after a populate
assertFalse(base.isEnabled());
}
use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class ProductContentData method populate.
/**
* Populates this DTO with data from the given source entity.
*
* @param source
* The source entity from which to copy data
*
* @throws IllegalArgumentException
* if source is null
*
* @return
* a reference to this DTO
*/
public ProductContentData populate(ProductContent source) {
if (source == null) {
throw new IllegalArgumentException("source is null");
}
Content content = source.getContent();
this.content = content != null ? (this.content != null ? this.content.populate(content) : content.toDTO()) : null;
this.enabled = source.isEnabled();
return this;
}
use of org.candlepin.model.ProductContent in project candlepin by candlepin.
the class OwnerProductResource method addBatchContent.
@ApiOperation(notes = "Adds one or more Content entities to a Product", value = "addBatchContent")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{product_id}/batch_content")
@Transactional
public ProductDTO addBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, @ApiParam(name = "contentMap", required = true) Map<String, Boolean> contentMap) {
Owner owner = this.getOwnerByKey(ownerKey);
Product product = this.fetchProduct(owner, productId);
Collection<ProductContent> productContent = new LinkedList<>();
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 (Entry<String, Boolean> entry : contentMap.entrySet()) {
Content content = this.fetchContent(owner, entry.getKey());
boolean enabled = entry.getValue() != null ? entry.getValue() : ProductContent.DEFAULT_ENABLED_STATE;
ContentDTO cdto = this.translator.translate(content, ContentDTO.class);
changed |= pdto.addContent(cdto, enabled);
}
if (changed) {
product = this.productManager.updateProduct(pdto, owner, true);
}
return this.translator.translate(product, ProductDTO.class);
}
Aggregations