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, 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;
}
use of org.candlepin.model.Content 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.setMultiplier(source.getMultiplier());
destination.setId(source.getId());
destination.setName(source.getName());
destination.setAttributes(source.getAttributes());
destination.setDependentProductIds(source.getDependentProductIds());
if (modelTranslator != null) {
Collection<ProductContent> productContent = source.getProductContent();
destination.setProductContent(Collections.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.emptyList());
}
return destination;
}
use of org.candlepin.model.Content 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;
}
use of org.candlepin.model.Content in project candlepin by candlepin.
the class OwnerProductResourceTest method testCreateProductWithContent.
@Test
public void testCreateProductWithContent() {
Owner owner = this.createOwner("Example-Corporation");
Content content = this.createContent("content-1", "content-1", owner);
ProductDTO pdto = this.buildTestProductDTO();
ContentDTO cdto = this.modelTranslator.translate(content, ContentDTO.class);
pdto.addContent(cdto, true);
assertNull(this.ownerProductCurator.getProductById(owner.getKey(), pdto.getId()));
ProductDTO result = this.ownerProductResource.createProduct(owner.getKey(), pdto);
Product entity = this.ownerProductCurator.getProductById(owner, pdto.getId());
ProductDTO expected = this.modelTranslator.translate(entity, ProductDTO.class);
assertNotNull(result);
assertNotNull(entity);
assertEquals(expected, result);
assertNotNull(result.getProductContent());
assertEquals(1, result.getProductContent().size());
assertEquals(cdto, result.getProductContent().iterator().next().getContent());
}
use of org.candlepin.model.Content in project candlepin by candlepin.
the class ContentDataTest method testPopulateWithEntity.
@Test
@Parameters(method = "getValuesPopulationByEntity")
public void testPopulateWithEntity(String valueName, Object input, Object defaultValue) throws Exception {
Method accessor = null;
Method mutator = null;
try {
accessor = ContentData.class.getDeclaredMethod("get" + valueName, null);
} catch (NoSuchMethodException e) {
accessor = ContentData.class.getDeclaredMethod("is" + valueName, null);
}
try {
mutator = Content.class.getDeclaredMethod("set" + valueName, input.getClass());
} catch (NoSuchMethodException e) {
if (Collection.class.isAssignableFrom(input.getClass())) {
mutator = Content.class.getDeclaredMethod("set" + valueName, Collection.class);
} else if (Boolean.class.isAssignableFrom(input.getClass())) {
mutator = Content.class.getDeclaredMethod("set" + valueName, boolean.class);
} else {
throw e;
}
}
ContentData base = new ContentData();
Content source = new Content();
mutator.invoke(source, input);
base.populate(source);
// Verify only the specified field was set
for (Method method : ContentData.class.getDeclaredMethods()) {
if (method.getName().matches("^(get|is)\\w+")) {
Object output = method.invoke(base, null);
if (method.getName().equals(accessor.getName())) {
if (input instanceof Collection) {
assertTrue(output instanceof Collection);
assertTrue(Util.collectionsAreEqual((Collection) input, (Collection) output));
} else {
assertEquals(input, output);
}
} else {
for (Object[] values : this.getValuesPopulationByEntity()) {
if (method.getName().endsWith((String) values[0])) {
if (values[2] instanceof Collection) {
assertTrue(output instanceof Collection);
assertTrue(Util.collectionsAreEqual((Collection) values[2], (Collection) output));
} else {
assertEquals(values[2], output);
}
}
}
}
}
}
}
Aggregations