use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.
the class EntitlementImporter method importObject.
public Subscription importObject(ObjectMapper mapper, Reader reader, Owner owner, Map<String, ProductDTO> productsById, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
EntitlementDTO entitlementDTO = mapper.readValue(reader, EntitlementDTO.class);
Entitlement entitlement = new Entitlement();
populateEntity(entitlement, entitlementDTO);
Subscription subscription = new Subscription();
log.debug("Building subscription for owner: {}", owner);
log.debug("Using pool from entitlement: {}", entitlement.getPool());
// Now that we no longer store Subscriptions in the on-site database, we need to
// manually give the subscription a downstream ID. Note that this may later be
// overwritten by reconciliation code if it determines this Subscription
// should replace and existing one.
subscription.setId(Util.generateDbUUID());
subscription.setUpstreamPoolId(entitlement.getPool().getId());
subscription.setUpstreamEntitlementId(entitlement.getId());
subscription.setUpstreamConsumerId(consumerUuid);
subscription.setOwner(owner);
subscription.setStartDate(entitlement.getStartDate());
subscription.setEndDate(entitlement.getEndDate());
subscription.setAccountNumber(entitlement.getPool().getAccountNumber());
subscription.setContractNumber(entitlement.getPool().getContractNumber());
subscription.setOrderNumber(entitlement.getPool().getOrderNumber());
subscription.setQuantity(entitlement.getQuantity().longValue());
for (Branding b : entitlement.getPool().getBranding()) {
subscription.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
}
String cdnLabel = meta.getCdnLabel();
if (!StringUtils.isBlank(cdnLabel)) {
Cdn cdn = cdnCurator.lookupByLabel(cdnLabel);
if (cdn != null) {
subscription.setCdn(cdn);
}
}
ProductDTO productDTO = this.findProduct(productsById, entitlement.getPool().getProductId());
subscription.setProduct(this.translator.translate(productDTO, ProductData.class));
// Add any sub product data to the subscription.
if (entitlement.getPool().getDerivedProductId() != null) {
productDTO = this.findProduct(productsById, entitlement.getPool().getDerivedProductId());
subscription.setDerivedProduct(this.translator.translate(productDTO, ProductData.class));
}
associateProvidedProducts(productsById, entitlement, subscription);
Set<EntitlementCertificate> certs = entitlement.getCertificates();
// subscriptions have one cert
int entcnt = 0;
for (EntitlementCertificate cert : certs) {
++entcnt;
CertificateSerial cs = new CertificateSerial();
cs.setCollected(cert.getSerial().isCollected());
cs.setExpiration(cert.getSerial().getExpiration());
cs.setUpdated(cert.getSerial().getUpdated());
cs.setCreated(cert.getSerial().getCreated());
SubscriptionsCertificate sc = new SubscriptionsCertificate();
sc.setKey(cert.getKey());
sc.setCertAsBytes(cert.getCertAsBytes());
sc.setSerial(cs);
subscription.setCertificate(sc);
}
if (entcnt > 1) {
log.error("More than one entitlement cert found for subscription");
}
return subscription;
}
use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.
the class OwnerProductResource method removeBatchContent.
@ApiOperation(notes = "Adds one or more Content entities to a Product", value = "addBatchContent")
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{product_id}/batch_content")
@Transactional
public ProductDTO removeBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, @ApiParam(name = "content", required = true) List<String> contentIds) {
Owner owner = this.getOwnerByKey(ownerKey);
Product product = this.fetchProduct(owner, productId);
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 (String contentId : contentIds) {
changed |= pdto.removeContent(contentId);
}
if (changed) {
product = this.productManager.updateProduct(pdto, owner, true);
}
return this.translator.translate(product, ProductDTO.class);
}
use of org.candlepin.dto.api.v1.ProductDTO 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.dto.api.v1.ProductDTO 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.dto.api.v1.ProductDTO 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());
}
Aggregations